You are here

class RouteParamContextTest in Page Manager 8

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

Tests the route param context.

@coversDefaultClass \Drupal\page_manager\EventSubscriber\RouteParamContext

@group PageManager

Hierarchy

Expanded class hierarchy of RouteParamContextTest

File

tests/src/Unit/RouteParamContextTest.php, line 28
Contains \Drupal\Tests\page_manager\Unit\RouteParamContextTest.

Namespace

Drupal\Tests\page_manager\Unit
View source
class RouteParamContextTest extends PageContextTestBase {

  /**
   * @covers ::onPageContext
   */
  public function testOnPageContext() {
    $collection = new RouteCollection();
    $route_provider = $this
      ->prophesize(RouteProviderInterface::class);
    $route_provider
      ->getRoutesByPattern('/test_route')
      ->willReturn($collection);
    $request = new Request();
    $request_stack = new RequestStack();
    $request_stack
      ->push($request);
    $data_definition = new DataDefinition([
      'type' => 'entity:user',
    ]);
    $typed_data = $this
      ->prophesize(TypedDataInterface::class);
    $this->typedDataManager
      ->getDefaultConstraints($data_definition)
      ->willReturn([]);
    $this->typedDataManager
      ->create($data_definition, 'banana')
      ->willReturn($typed_data
      ->reveal());
    $this->typedDataManager
      ->createDataDefinition('bar')
      ->will(function () use ($data_definition) {
      return $data_definition;
    });
    $this->page
      ->getPath()
      ->willReturn('/test_route');
    $this->page
      ->getParameter('foo')
      ->willReturn([
      'machine_name' => 'foo',
      'type' => 'integer',
      'label' => 'Foo',
    ]);
    $this->page
      ->hasParameter('foo')
      ->willReturn(TRUE);
    $this->page
      ->getParameter('bar')
      ->willReturn(NULL);
    $this->page
      ->hasParameter('bar')
      ->willReturn(FALSE);
    $this->page
      ->getParameter('baz')
      ->willReturn([
      'machine_name' => 'baz',
      'type' => 'integer',
      'label' => '',
    ]);
    $this->page
      ->hasParameter('baz')
      ->willReturn(TRUE);
    $this->page
      ->getParameter('page')
      ->willReturn([
      'machine_name' => 'page',
      'type' => 'entity:page',
      'label' => '',
    ]);
    $this->page
      ->hasParameter('page')
      ->willReturn(TRUE);
    $this->page
      ->addContext('foo', Argument::that(function ($context) {
      return $context instanceof Context && $context
        ->getContextDefinition()
        ->getLabel() == 'Foo';
    }))
      ->shouldBeCalled();
    $this->page
      ->addContext('baz', Argument::that(function ($context) {
      return $context instanceof Context && $context
        ->getContextDefinition()
        ->getLabel() == '{baz} from route';
    }))
      ->shouldBeCalled();
    $this->page
      ->addContext('page', Argument::that(function ($context) {
      return $context instanceof Context && $context
        ->getContextDefinition()
        ->getLabel() == '{page} from route';
    }))
      ->shouldBeCalled();
    $collection
      ->add('test_route', new Route('/test_route', [], [], [
      'parameters' => [
        'foo' => [
          'type' => 'bar',
        ],
        'baz' => [
          'type' => 'bop',
        ],
        'page' => [
          'type' => 'entity:page',
        ],
      ],
    ]));

    // Set up a request with one of the expected parameters as an attribute.
    $request->attributes
      ->add([
      'foo' => 'banana',
    ]);
    $route_param_context = new RouteParamContext($route_provider
      ->reveal(), $request_stack);
    $route_param_context
      ->onPageContext($this->event);
  }

  /**
   * @covers ::onPageContext
   */
  public function testOnPageContextEmpty() {
    $collection = new RouteCollection();
    $route_provider = $this
      ->prophesize(RouteProviderInterface::class);
    $route_provider
      ->getRoutesByPattern('/test_route')
      ->willReturn($collection);
    $request = new Request();
    $request_stack = new RequestStack();
    $request_stack
      ->push($request);
    $this->page
      ->getPath()
      ->willReturn('/test_route');
    $this->page
      ->addContext(Argument::cetera())
      ->shouldNotBeCalled();

    // Set up a request with one of the expected parameters as an attribute.
    $request->attributes
      ->add([
      'foo' => 'banana',
    ]);
    $route_param_context = new RouteParamContext($route_provider
      ->reveal(), $request_stack);
    $route_param_context
      ->onPageContext($this->event);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PageContextTestBase::$event protected property The event.
PageContextTestBase::$page protected property The page entity.
PageContextTestBase::$typedDataManager protected property The typed data manager.
PageContextTestBase::setUp protected function Overrides UnitTestCase::setUp
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.
RouteParamContextTest::testOnPageContext public function @covers ::onPageContext
RouteParamContextTest::testOnPageContextEmpty public function @covers ::onPageContext
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.