You are here

class RouteNameResponseSubscriberTest in Page Manager 8

Same name and namespace in other branches
  1. 8.4 tests/src/Unit/RouteNameResponseSubscriberTest.php \Drupal\Tests\page_manager\Unit\RouteNameResponseSubscriberTest

@coversDefaultClass \Drupal\page_manager\EventSubscriber\RouteNameResponseSubscriber @group PageManager

Hierarchy

Expanded class hierarchy of RouteNameResponseSubscriberTest

File

tests/src/Unit/RouteNameResponseSubscriberTest.php, line 24
Contains \Drupal\Tests\page_manager\Unit\RouteNameResponseSubscriberTest.

Namespace

Drupal\Tests\page_manager\Unit
View source
class RouteNameResponseSubscriberTest extends UnitTestCase {

  /**
   * @covers ::onResponse
   */
  public function testOnResponseCacheable() {
    $response = new CacheableResponse('');
    $event = $this
      ->buildEvent($response);
    $route_name = 'the_route_name';
    $master_route_match = $this
      ->prophesize(RouteMatchInterface::class);
    $master_route_match
      ->getParameter('base_route_name')
      ->willReturn(NULL);
    $master_route_match
      ->getRouteName()
      ->willReturn($route_name);
    $current_route_match = $this
      ->prophesize(StackedRouteMatchInterface::class);
    $current_route_match
      ->getMasterRouteMatch()
      ->willReturn($master_route_match
      ->reveal());
    $subscriber = new RouteNameResponseSubscriber($current_route_match
      ->reveal());
    $subscriber
      ->onResponse($event);
    $expected = [
      "page_manager_route_name:{$route_name}",
    ];
    $this
      ->assertSame($expected, $response
      ->getCacheableMetadata()
      ->getCacheTags());
  }

  /**
   * @covers ::onResponse
   */
  public function testOnResponseUncacheable() {
    $response = new Response('');
    $event = $this
      ->buildEvent($response);
    $master_route_match = $this
      ->prophesize(RouteMatchInterface::class);
    $master_route_match
      ->getParameter()
      ->shouldNotBeCalled();
    $master_route_match
      ->getRouteName()
      ->shouldNotBeCalled();
    $current_route_match = $this
      ->prophesize(StackedRouteMatchInterface::class);
    $current_route_match
      ->getMasterRouteMatch()
      ->willReturn($master_route_match
      ->reveal());
    $subscriber = new RouteNameResponseSubscriber($current_route_match
      ->reveal());
    $subscriber
      ->onResponse($event);
  }

  /**
   * @covers ::onResponse
   */
  public function testOnResponseCacheableWithBaseRouteName() {
    $response = new CacheableResponse('');
    $event = $this
      ->buildEvent($response);
    $route_name = 'the_route_name';
    $master_route_match = $this
      ->prophesize(RouteMatchInterface::class);
    $master_route_match
      ->getParameter('base_route_name')
      ->willReturn($route_name);
    $master_route_match
      ->getRouteName()
      ->shouldNotBeCalled();
    $current_route_match = $this
      ->prophesize(StackedRouteMatchInterface::class);
    $current_route_match
      ->getMasterRouteMatch()
      ->willReturn($master_route_match
      ->reveal());
    $subscriber = new RouteNameResponseSubscriber($current_route_match
      ->reveal());
    $subscriber
      ->onResponse($event);
    $expected = [
      "page_manager_route_name:{$route_name}",
    ];
    $this
      ->assertSame($expected, $response
      ->getCacheableMetadata()
      ->getCacheTags());
  }

  /**
   * Builds an event to wrap a response.
   *
   * @param \Symfony\Component\HttpFoundation\Response $response
   *   The response to be sent as the event payload.
   *
   * @return \Symfony\Component\HttpKernel\Event\FilterResponseEvent
   *   An event suitable for a KernelEvents::RESPONSE subscriber to process.
   */
  protected function buildEvent(Response $response) {
    $kernel = $this
      ->prophesize(HttpKernelInterface::class);
    $request = Request::create('');
    return new FilterResponseEvent($kernel
      ->reveal(), $request, HttpKernelInterface::SUB_REQUEST, $response);
  }

}

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.
RouteNameResponseSubscriberTest::buildEvent protected function Builds an event to wrap a response.
RouteNameResponseSubscriberTest::testOnResponseCacheable public function @covers ::onResponse
RouteNameResponseSubscriberTest::testOnResponseCacheableWithBaseRouteName public function @covers ::onResponse
RouteNameResponseSubscriberTest::testOnResponseUncacheable public function @covers ::onResponse
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.
UnitTestCase::setUp protected function 340