You are here

class Fast404EventSubscriberTest in Fast 404 8.2

Same name and namespace in other branches
  1. 8 tests/src/Unit/Fast404EventSubscriberTest.php \Drupal\Tests\fast404\Unit\Fast404EventSubscriberTest

Tests the fast404 event subscriber logic.

@coversDefaultClass \Drupal\fast404\EventSubscriber\Fast404EventSubscriber @group fast404

Hierarchy

Expanded class hierarchy of Fast404EventSubscriberTest

File

tests/src/Unit/Fast404EventSubscriberTest.php, line 18

Namespace

Drupal\Tests\fast404\Unit
View source
class Fast404EventSubscriberTest extends UnitTestCase {

  /**
   * The event.
   *
   * @var \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
   */
  protected $event;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $kernel = $this
      ->createMock('\\Symfony\\Component\\HttpKernel\\HttpKernelInterface');
    $request = new Request();
    $this->event = new GetResponseForExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, new NotFoundHttpException());
  }

  /**
   * Tests event handling for kernel requests.
   *
   * @covers ::onKernelRequest
   */

  // public function testOnKernelRequest() {}

  /**
   * Tests event handling for not found exceptions.
   *
   * @covers ::onNotFoundException
   */
  public function testOnNotFoundException() {
    $subscriber = $this
      ->getFast404EventSubscriber();
    $subscriber
      ->onNotFoundException($this->event);
    $e = $this->event
      ->getException();
    $this
      ->assertInstanceOf(NotFoundHttpException::class, $e);
  }

  /**
   * Creates a Fast404EventSubscriber object to test.
   *
   * @return \Drupal\fast404\EventSubscriber\Fast404EventSubscriber
   *   A mock Fast404EventSubscriber object to test.
   */
  protected function getFast404EventSubscriber() {
    $requestStackStub = $this
      ->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\RequestStack')
      ->disableOriginalConstructor()
      ->getMock();
    $subscriber = $this
      ->getMockBuilder('\\Drupal\\fast404\\EventSubscriber\\Fast404EventSubscriber')
      ->setConstructorArgs([
      $requestStackStub,
    ])
      ->getMock();
    return $subscriber;
  }

  /**
   * Tests event listener registration.
   *
   * @covers ::getSubscribedEvents
   */
  public function testGetSubscribedEvents() {
    $this
      ->assertEquals([
      'kernel.request' => [
        [
          'onKernelRequest',
          100,
        ],
      ],
      'kernel.exception' => [
        [
          'onNotFoundException',
          0,
        ],
      ],
    ], Fast404EventSubscriber::getSubscribedEvents());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Fast404EventSubscriberTest::$event protected property The event.
Fast404EventSubscriberTest::getFast404EventSubscriber protected function Creates a Fast404EventSubscriber object to test.
Fast404EventSubscriberTest::setUp protected function Overrides UnitTestCase::setUp
Fast404EventSubscriberTest::testGetSubscribedEvents public function Tests event listener registration.
Fast404EventSubscriberTest::testOnNotFoundException public function Tests event handling for not found exceptions.
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.