You are here

class PageVariantTest in Page Manager 8

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

@coversDefaultClass \Drupal\page_manager\Entity\PageVariant

@group PageManager

Hierarchy

Expanded class hierarchy of PageVariantTest

File

tests/src/Unit/PageVariantTest.php, line 23
Contains \Drupal\Tests\page_manager\Unit\PageVariantTest.

Namespace

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

  /**
   * @var \Drupal\page_manager\Entity\PageVariant
   */
  protected $pageVariant;

  /**
   * @var \Drupal\page_manager\PageInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $page;

  /**
   * @var \Drupal\page_manager\ContextMapperInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $contextMapper;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->pageVariant = new PageVariant([
      'id' => 'the_page_variant',
      'page' => 'the_page',
    ], 'page_variant');
    $this->page = $this
      ->prophesize(PageInterface::class);
    $entity_storage = $this
      ->prophesize(EntityStorageInterface::class);
    $entity_storage
      ->load('the_page')
      ->willReturn($this->page
      ->reveal());
    $entity_type_manager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->getStorage('page')
      ->willReturn($entity_storage);
    $this->contextMapper = $this
      ->prophesize(ContextMapperInterface::class);
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.manager', $entity_type_manager
      ->reveal());
    $container
      ->set('page_manager.context_mapper', $this->contextMapper
      ->reveal());
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::getContexts
   * @dataProvider providerTestGetContexts
   */
  public function testGetContexts($static_contexts, $page_contexts, $expected) {
    $this->contextMapper
      ->getContextValues([])
      ->willReturn($static_contexts)
      ->shouldBeCalledTimes(1);
    $this->page
      ->getContexts()
      ->willReturn($page_contexts)
      ->shouldBeCalledTimes(1);
    $contexts = $this->pageVariant
      ->getContexts();
    $this
      ->assertSame($expected, $contexts);
    $contexts = $this->pageVariant
      ->getContexts();
    $this
      ->assertSame($expected, $contexts);
  }
  public function providerTestGetContexts() {
    $data = [];
    $data['empty'] = [
      [],
      [],
      [],
    ];
    $data['additive'] = [
      [
        'static' => 'static',
      ],
      [
        'page' => 'page',
      ],
      [
        'page' => 'page',
        'static' => 'static',
      ],
    ];
    $data['conflicting'] = [
      [
        'foo' => 'static',
      ],
      [
        'foo' => 'page',
      ],
      [
        'foo' => 'page',
      ],
    ];
    return $data;
  }

  /**
   * @covers ::getContexts
   * @covers ::removeStaticContext
   */
  public function testGetContextsAfterReset() {
    $this->contextMapper
      ->getContextValues([])
      ->willReturn([])
      ->shouldBeCalledTimes(2);
    $this->page
      ->getContexts()
      ->willReturn([])
      ->shouldBeCalledTimes(2);
    $expected = [];
    $contexts = $this->pageVariant
      ->getContexts();
    $this
      ->assertSame($expected, $contexts);
    $this->pageVariant
      ->removeStaticContext('anything');
    $contexts = $this->pageVariant
      ->getContexts();
    $this
      ->assertSame($expected, $contexts);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PageVariantTest::$contextMapper protected property
PageVariantTest::$page protected property
PageVariantTest::$pageVariant protected property
PageVariantTest::providerTestGetContexts public function
PageVariantTest::setUp protected function Overrides UnitTestCase::setUp
PageVariantTest::testGetContexts public function @covers ::getContexts @dataProvider providerTestGetContexts
PageVariantTest::testGetContextsAfterReset public function @covers ::getContexts @covers ::removeStaticContext
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.