You are here

class RestIncidentStorageTest in Radioactivity 8.3

Same name and namespace in other branches
  1. 4.0.x tests/src/Unit/RestIncidentStorageTest.php \Drupal\Tests\radioactivity\Unit\RestIncidentStorageTest

@coversDefaultClass \Drupal\radioactivity\RestIncidentStorage @group radioactivity

Hierarchy

Expanded class hierarchy of RestIncidentStorageTest

File

tests/src/Unit/RestIncidentStorageTest.php, line 15

Namespace

Drupal\Tests\radioactivity\Unit
View 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

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RestIncidentStorageTest::$sut private property The RestIncidentStorage under test.
RestIncidentStorageTest::getMultipleIncidentData private function Returns incident data as returned by RestProcessor::getData.
RestIncidentStorageTest::setUp public function Overrides UnitTestCase::setUp
RestIncidentStorageTest::testAddIncident public function @covers ::addIncident
RestIncidentStorageTest::testClearIncidents public function @covers ::clearIncidents
RestIncidentStorageTest::testGetIncidentsByType public function @covers ::getIncidentsByType
RestIncidentStorageTest::testGetInvalidIncident public function @covers ::getIncidents
RestIncidentStorageTest::testGetMultipleIncidents public function @covers ::getIncidents
RestIncidentStorageTest::testGetSingleIncident public function @covers ::getIncidents
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.