You are here

class EmitControllerTest in Radioactivity 8.3

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

@coversDefaultClass \Drupal\radioactivity\Controller\EmitController @group radioactivity

Hierarchy

Expanded class hierarchy of EmitControllerTest

File

tests/src/Unit/EmitControllerTest.php, line 20

Namespace

Drupal\Tests\radioactivity\Unit
View source
class EmitControllerTest extends UnitTestCase {

  /**
   * The request received by the controller.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The dependency injection container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $container;

  /**
   * The incident storage factory.
   *
   * @var \Drupal\radioactivity\StorageFactory
   */
  protected $incidentStorageFactory;

  /**
   * The default incident storage.
   *
   * @var \Drupal\radioactivity\DefaultIncidentStorage
   */
  protected $defaultIncidentStorage;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();

    // Initiate the Settings singleton used by this test.
    new Settings([
      'hash_salt' => 'liesjeleerdelotjelopen',
    ]);
    $this->request = $this
      ->prophesize(Request::class);
    $this->incidentStorageFactory = $this
      ->prophesize(StorageFactory::class);
    $this->defaultIncidentStorage = $this
      ->prophesize(IncidentStorageInterface::class);
    $this->incidentStorageFactory
      ->get('default')
      ->willReturn($this->defaultIncidentStorage
      ->reveal());
    $this->container = $this
      ->prophesize(ContainerInterface::class);
    \Drupal::setContainer($this->container
      ->reveal());
    $this->container
      ->get('radioactivity.storage')
      ->willReturn($this->incidentStorageFactory
      ->reveal());
  }

  /**
   * @covers ::emit
   */
  public function testEmitEmptyRequest() {
    $this->request
      ->getContent()
      ->willReturn(NULL);
    $controller = EmitController::create($this->container
      ->reveal());
    $response = $controller
      ->emit($this->request
      ->reveal());
    $this->defaultIncidentStorage
      ->addIncident()
      ->shouldNotBeCalled();
    $this
      ->assertEquals('Symfony\\Component\\HttpFoundation\\JsonResponse', get_class($response));
    $this
      ->assertEquals('{"status":"error","message":"Empty request."}', $response
      ->getContent());
  }

  /**
   * @covers ::emit
   */
  public function testEmitValidRequest() {
    $postData = Json::encode([
      [
        'fn' => 'field_name',
        'et' => 'entity_type',
        'id' => '99',
        'e' => 5.5,
        'h' => '5aa2ff01ac75da55751051a55021092768d079c5',
      ],
    ]);
    $this->request
      ->getContent()
      ->willReturn($postData);
    $sut = EmitController::create($this->container
      ->reveal());
    $response = $sut
      ->emit($this->request
      ->reveal());
    $this->defaultIncidentStorage
      ->addIncident(Argument::type(IncidentInterface::class))
      ->shouldBeCalledTimes(1);
    $this
      ->assertEquals('Symfony\\Component\\HttpFoundation\\JsonResponse', get_class($response));
    $this
      ->assertEquals('{"status":"ok","message":"1 incidents added."}', $response
      ->getContent());
  }

  /**
   * @covers ::emit
   */
  public function testEmitInvalidRequest() {
    $postData = Json::encode([
      [
        'fn' => 'field_name',
        'et' => 'entity_type',
        'id' => '99',
        'e' => 5.5,
        'h' => '',
      ],
    ]);
    $this->request
      ->getContent()
      ->willReturn($postData);
    $controller = EmitController::create($this->container
      ->reveal());
    $response = $controller
      ->emit($this->request
      ->reveal());
    $this->defaultIncidentStorage
      ->addIncident(Argument::type(IncidentInterface::class))
      ->shouldNotBeCalled(0);
    $this
      ->assertEquals('Symfony\\Component\\HttpFoundation\\JsonResponse', get_class($response));
    $this
      ->assertEquals('{"status":"error","message":"invalid incident (0)."}', $response
      ->getContent());
  }

  /**
   * @covers ::emit
   */
  public function testEmitMultipleRequest() {
    $postData = Json::encode([
      [
        'fn' => 'field_name',
        'et' => 'entity_type',
        'id' => '99',
        'e' => 5.5,
        'h' => '5aa2ff01ac75da55751051a55021092768d079c5',
      ],
      [
        'fn' => 'field_name',
        'et' => 'entity_type',
        'id' => '99',
        'e' => 3.3,
        'h' => '',
      ],
    ]);
    $this->request
      ->getContent()
      ->willReturn($postData);
    $controller = EmitController::create($this->container
      ->reveal());
    $response = $controller
      ->emit($this->request
      ->reveal());
    $this->defaultIncidentStorage
      ->addIncident(Argument::type(IncidentInterface::class))
      ->shouldBeCalledTimes(1);
    $this
      ->assertEquals('Symfony\\Component\\HttpFoundation\\JsonResponse', get_class($response));
    $this
      ->assertEquals('{"status":"error","message":"invalid incident (1)."}', $response
      ->getContent());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EmitControllerTest::$container protected property The dependency injection container.
EmitControllerTest::$defaultIncidentStorage protected property The default incident storage.
EmitControllerTest::$incidentStorageFactory protected property The incident storage factory.
EmitControllerTest::$request protected property The request received by the controller.
EmitControllerTest::setUp protected function Overrides UnitTestCase::setUp
EmitControllerTest::testEmitEmptyRequest public function @covers ::emit
EmitControllerTest::testEmitInvalidRequest public function @covers ::emit
EmitControllerTest::testEmitMultipleRequest public function @covers ::emit
EmitControllerTest::testEmitValidRequest public function @covers ::emit
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.
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.