You are here

class RouteGroupCacheContextTest in Group 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/RouteGroupCacheContextTest.php \Drupal\Tests\group\Unit\RouteGroupCacheContextTest

Tests the route.group cache context.

@coversDefaultClass \Drupal\group\Cache\Context\RouteGroupCacheContext @group group

Hierarchy

Expanded class hierarchy of RouteGroupCacheContextTest

File

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

Namespace

Drupal\Tests\group\Unit
View source
class RouteGroupCacheContextTest extends UnitTestCase {

  /**
   * The current route match object.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $currentRouteMatch;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->currentRouteMatch = $this
      ->prophesize(RouteMatchInterface::class);
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
  }

  /**
   * Tests getting the context value when there is no group on the route.
   *
   * @covers ::getContext
   */
  public function testGetContextNoGroup() {
    $this->currentRouteMatch
      ->getParameter('group')
      ->willReturn(NULL);
    $this->currentRouteMatch
      ->getRouteName()
      ->willReturn('foo');
    $cache_context = new RouteGroupCacheContext($this->currentRouteMatch
      ->reveal(), $this->entityTypeManager
      ->reveal());
    $this
      ->assertSame('group.none', $cache_context
      ->getContext());
  }

  /**
   * Tests getting the context value when there is a group on the route.
   *
   * @covers ::getContext
   */
  public function testGetContextWithGroup() {
    $group = $this
      ->prophesize(GroupInterface::class);
    $group
      ->id()
      ->willReturn(1);
    $this->currentRouteMatch
      ->getParameter('group')
      ->willReturn($group
      ->reveal());
    $cache_context = new RouteGroupCacheContext($this->currentRouteMatch
      ->reveal(), $this->entityTypeManager
      ->reveal());
    $this
      ->assertSame(1, $cache_context
      ->getContext());
  }

  /**
   * Tests getting the context value when on the group add form route.
   *
   * @covers ::getContext
   */
  public function testGetContextOnGroupAddForm() {
    $group = $this
      ->prophesize(GroupInterface::class);
    $group
      ->id()
      ->willReturn(NULL);
    $group
      ->bundle()
      ->willReturn('foo');
    $group_type = $this
      ->prophesize(GroupTypeInterface::class);
    $group_type
      ->id()
      ->willReturn('foo');
    $this->currentRouteMatch
      ->getParameter('group')
      ->willReturn(NULL);
    $this->currentRouteMatch
      ->getParameter('group_type')
      ->willReturn($group_type
      ->reveal());
    $this->currentRouteMatch
      ->getRouteName()
      ->willReturn('entity.group.add_form');
    $storage = $this
      ->prophesize(ContentEntityStorageInterface::class);
    $storage
      ->create([
      'type' => 'foo',
    ])
      ->willReturn($group
      ->reveal());
    $this->entityTypeManager
      ->getStorage('group')
      ->willReturn($storage
      ->reveal());
    $cache_context = new RouteGroupCacheContext($this->currentRouteMatch
      ->reveal(), $this->entityTypeManager
      ->reveal());
    $this
      ->assertSame('foo', $cache_context
      ->getContext());
  }

  /**
   * Tests getting the cacheable metadata for the cache context.
   *
   * @covers ::getCacheableMetadata
   */
  public function testGetCacheableMetadata() {
    $cache_context = new RouteGroupCacheContext($this->currentRouteMatch
      ->reveal(), $this->entityTypeManager
      ->reveal());
    $this
      ->assertEquals(new CacheableMetadata(), $cache_context
      ->getCacheableMetadata());
  }

}

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.
RouteGroupCacheContextTest::$currentRouteMatch protected property The current route match object.
RouteGroupCacheContextTest::$entityTypeManager protected property The entity type manager service.
RouteGroupCacheContextTest::setUp public function Overrides UnitTestCase::setUp
RouteGroupCacheContextTest::testGetCacheableMetadata public function Tests getting the cacheable metadata for the cache context.
RouteGroupCacheContextTest::testGetContextNoGroup public function Tests getting the context value when there is no group on the route.
RouteGroupCacheContextTest::testGetContextOnGroupAddForm public function Tests getting the context value when on the group add form route.
RouteGroupCacheContextTest::testGetContextWithGroup public function Tests getting the context value when there is a group on the route.
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.