class RestIncidentStorageTest in Radioactivity 8.3
Same name and namespace in other branches
- 4.0.x tests/src/Unit/RestIncidentStorageTest.php \Drupal\Tests\radioactivity\Unit\RestIncidentStorageTest
@coversDefaultClass \Drupal\radioactivity\RestIncidentStorage @group radioactivity
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\radioactivity\Unit\RestIncidentStorageTest
Expanded class hierarchy of RestIncidentStorageTest
File
- tests/
src/ Unit/ RestIncidentStorageTest.php, line 15
Namespace
Drupal\Tests\radioactivity\UnitView source
class RestIncidentStorageTest extends UnitTestCase {
/**
* The RestIncidentStorage under test.
*
* @var \Drupal\radioactivity\RestIncidentStorage
*/
private $sut;
/**
* {@inheritdoc}
*/
public function setUp() : void {
parent::setUp();
$this->sut = $this
->getMockBuilder(RestIncidentStorage::class)
->disableOriginalConstructor()
->setMethods([
'getDefaultEndpoint',
'getIncidentsFromStorage',
'clearIncidentStorage',
])
->getMock();
// Initiate the Settings singleton used by this test.
new Settings([
'hash_salt' => 'liesjeleerdelotjelopen',
]);
}
/**
* @covers ::addIncident
*/
public function testAddIncident() {
$incident = $this
->getMockBuilder(IncidentInterface::class)
->disableOriginalConstructor()
->getMock();
$this
->expectException("Exception");
$this
->expectExceptionMessage("The Radioactivity rest endpoint expects incidents to be added somewhere else.");
$this->sut
->addIncident($incident);
}
/**
* @covers ::getIncidents
*/
public function testGetSingleIncident() {
$incidentData = Json::decode('[[{"fn":"field_name","et":"entity_type","id":"99","e":10,"h":"fb05739ec66040df9e6c53330755753c02e73621"}]]');
$this->sut
->expects($this
->once())
->method('getIncidentsFromStorage')
->will($this
->returnValue($incidentData));
$result = $this->sut
->getIncidents();
$this
->assertCount(1, $result);
$this
->assertInstanceOf(IncidentInterface::class, $result[0]);
}
/**
* @covers ::getIncidents
*/
public function testGetInvalidIncident() {
$incidentData = Json::decode('[[{"fn":"field_name","et":"entity_type","id":"99","e":10,"h":"invalid-hash"}]]');
$this->sut
->expects($this
->once())
->method('getIncidentsFromStorage')
->will($this
->returnValue($incidentData));
$result = $this->sut
->getIncidents();
$this
->assertCount(0, $result);
}
/**
* @covers ::getIncidents
*/
public function testGetMultipleIncidents() {
$this->sut
->expects($this
->once())
->method('getIncidentsFromStorage')
->will($this
->returnValue($this
->getMultipleIncidentData()));
$result = $this->sut
->getIncidents();
$this
->assertCount(4, $result);
$this
->assertInstanceOf(IncidentInterface::class, $result[0]);
}
/**
* @covers ::getIncidentsByType
*/
public function testGetIncidentsByType() {
$this->sut
->expects($this
->any())
->method('getIncidentsFromStorage')
->will($this
->returnValue($this
->getMultipleIncidentData()));
$result = $this->sut
->getIncidentsByType();
$this
->assertEquals([
'entity_type',
'node',
], array_keys($result));
$this
->assertEquals([
99,
88,
], array_keys($result['entity_type']));
$this
->assertInstanceOf(IncidentInterface::class, $result['entity_type'][99][0]);
$this
->assertEquals([
123,
], array_keys($result['node']));
$this
->assertInstanceOf(IncidentInterface::class, $result['node'][123][0]);
$result = $this->sut
->getIncidentsByType('entity_type');
$this
->assertEquals([
'entity_type',
], array_keys($result));
$this
->assertEquals([
99,
88,
], array_keys($result['entity_type']));
$this
->assertInstanceOf(IncidentInterface::class, $result['entity_type'][99][0]);
$result = $this->sut
->getIncidentsByType('node');
$this
->assertEquals([
123,
], array_keys($result['node']));
$this
->assertInstanceOf(IncidentInterface::class, $result['node'][123][0]);
$result = $this->sut
->getIncidentsByType('unknown_entity');
$this
->assertArrayNotHasKey('unknown_entity', $result);
}
/**
* @covers ::clearIncidents
*/
public function testClearIncidents() {
$this->sut
->expects($this
->once())
->method('clearIncidentStorage');
$this->sut
->clearIncidents();
}
/**
* Returns incident data as returned by RestProcessor::getData.
*
* @return array
* Decoded rest incident storage data.
*/
private function getMultipleIncidentData() {
$jsonData = '[[{"fn":"field_name","et":"entity_type","id":"99","e":10,"h":"fb05739ec66040df9e6c53330755753c02e73621"}],' . PHP_EOL . '[{"fn":"field_name","et":"entity_type","id":"88","e":10,"h":"5aae235691b06918a2a8eb3833bfb22a67d9b183"}],' . PHP_EOL . '[{"fn":"field_name","et":"entity_type","id":"88","e":10,"h":"5aae235691b06918a2a8eb3833bfb22a67d9b183"}],' . PHP_EOL . '[{"fn":"field_other","et":"node","id":"123","e":10,"h":"dedd866aefddfd94049f6fd8cf1b8d02065343e6"}]]';
return Json::decode($jsonData);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
RestIncidentStorageTest:: |
private | property | The RestIncidentStorage under test. | |
RestIncidentStorageTest:: |
private | function | Returns incident data as returned by RestProcessor::getData. | |
RestIncidentStorageTest:: |
public | function |
Overrides UnitTestCase:: |
|
RestIncidentStorageTest:: |
public | function | @covers ::addIncident | |
RestIncidentStorageTest:: |
public | function | @covers ::clearIncidents | |
RestIncidentStorageTest:: |
public | function | @covers ::getIncidentsByType | |
RestIncidentStorageTest:: |
public | function | @covers ::getIncidents | |
RestIncidentStorageTest:: |
public | function | @covers ::getIncidents | |
RestIncidentStorageTest:: |
public | function | @covers ::getIncidents | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |