You are here

DefaultIncidentStorageTest.php in Radioactivity 8.3

Same filename and directory in other branches
  1. 4.0.x tests/src/Unit/DefaultIncidentStorageTest.php

File

tests/src/Unit/DefaultIncidentStorageTest.php
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;

/**
 * @coversDefaultClass \Drupal\radioactivity\DefaultIncidentStorage
 * @group radioactivity
 */
class DefaultIncidentStorageTest extends UnitTestCase {

  /**
   * A mock state storage.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The system under test.
   *
   * @var \Drupal\radioactivity\DefaultIncidentStorage
   */
  protected $sut;

  /**
   * A mock radioactivity incident.
   *
   * @var \Drupal\radioactivity\IncidentInterface
   */
  protected $incident;

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * @covers ::addIncident
   */
  public function testAddIncident() {
    $this->sut
      ->addIncident($this->incident
      ->reveal());
    $this->state
      ->set(IncidentStorageInterface::STORAGE_KEY, Argument::any())
      ->shouldBeCalled();
  }

  /**
   * @covers ::getIncidents
   */
  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);
  }

  /**
   * @covers ::getIncidentsByType
   */
  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);
  }

  /**
   * @covers ::clearIncidents
   */
  public function testClearIncidents() {
    $this->sut
      ->clearIncidents();
    $this->state
      ->set(IncidentStorageInterface::STORAGE_KEY, Argument::any())
      ->shouldBeCalled();
  }

  /**
   * @covers ::injectSettings
   */
  public function testInjectSettings() {
    $page = [];
    $this->sut
      ->injectSettings($page);
    $this
      ->assertTrue(isset($page['#attached']['drupalSettings']['radioactivity']));
  }

}

Classes

Namesort descending Description
DefaultIncidentStorageTest @coversDefaultClass \Drupal\radioactivity\DefaultIncidentStorage @group radioactivity