View source
<?php
namespace Drupal\Tests\radioactivity\Unit;
use Drupal\Core\State\StateInterface;
use Drupal\radioactivity\DefaultIncidentStorage;
use Drupal\radioactivity\IncidentInterface;
use Drupal\radioactivity\IncidentStorageInterface;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
class DefaultIncidentStorageTest extends UnitTestCase {
protected $state;
protected $sut;
protected $incident;
protected function setUp() : void {
parent::setUp();
$this->state = $this
->prophesize(StateInterface::class);
$this->sut = new DefaultIncidentStorage($this->state
->reveal());
$this->incident = $this
->prophesize(IncidentInterface::class);
}
public function testAddIncident() {
$this->sut
->addIncident($this->incident
->reveal());
$this->state
->set(IncidentStorageInterface::STORAGE_KEY, Argument::any())
->shouldBeCalled();
}
public function testGetIncidents() {
$content = $this->incident
->reveal();
$this->state
->get(IncidentStorageInterface::STORAGE_KEY, Argument::any())
->willReturn([
$content,
]);
$result = $this->sut
->getIncidents();
$this
->assertEquals($result[0], $content);
}
public function testGetIncidentsByType() {
$incident = $this->incident
->reveal();
$this->incident
->getEntityTypeId()
->willReturn('type1', 'type1', 'type2');
$this->incident
->getEntityId()
->willReturn('123', '234', '345');
$this->state
->get(IncidentStorageInterface::STORAGE_KEY, Argument::any())
->willReturn([
$incident,
$incident,
$incident,
]);
$result = $this->sut
->getIncidentsByType('type1');
$this
->assertEquals([
'type1',
], array_keys($result));
$this
->assertEquals([
'123',
'234',
], array_keys($result['type1']));
$this
->assertEquals($result['type1']['234'][0], $incident);
$this->incident
->getEntityTypeId()
->willReturn('type1', 'type1', 'type2');
$this->incident
->getEntityId()
->willReturn('123', '234', '345');
$result = $this->sut
->getIncidentsByType();
$this
->assertEquals([
'type1',
'type2',
], array_keys($result));
$this
->assertEquals([
'345',
], array_keys($result['type2']));
$this
->assertEquals($result['type2']['345'][0], $incident);
$result = $this->sut
->getIncidentsByType('unknown_entity');
$this
->assertArrayNotHasKey('unknown_entity', $result);
}
public function testClearIncidents() {
$this->sut
->clearIncidents();
$this->state
->set(IncidentStorageInterface::STORAGE_KEY, Argument::any())
->shouldBeCalled();
}
public function testInjectSettings() {
$page = [];
$this->sut
->injectSettings($page);
$this
->assertTrue(isset($page['#attached']['drupalSettings']['radioactivity']));
}
}