You are here

class ContextMapperTest in Page Manager 8

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

@coversDefaultClass \Drupal\page_manager\ContextMapper

@group PageManager

Hierarchy

Expanded class hierarchy of ContextMapperTest

File

tests/src/Unit/ContextMapperTest.php, line 26
Contains \Drupal\Tests\page_manager\Unit\ContextMapperTest.

Namespace

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

  /**
   * The typed data manager.
   *
   * @var \Drupal\Core\TypedData\TypedDataManager|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $typedDataManager;

  /**
   * @var \Drupal\Core\Entity\EntityRepositoryInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $entityRepository;

  /**
   * @var \Drupal\page_manager\ContextMapper
   */
  protected $staticContext;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->typedDataManager = $this
      ->prophesize(TypedDataManager::class);
    $this->entityRepository = $this
      ->prophesize(EntityRepositoryInterface::class);
    $this->staticContext = new ContextMapper($this->entityRepository
      ->reveal());
    $container = new ContainerBuilder();
    $container
      ->set('typed_data_manager', $this->typedDataManager
      ->reveal());
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::getContextValues
   */
  public function testGetContextValues() {
    $input = [];
    $actual = $this->staticContext
      ->getContextValues($input);
    $this
      ->assertEquals([], $actual);
  }

  /**
   * @covers ::getContextValues
   */
  public function testGetContextValuesContext() {
    $data_definition = DataDefinition::createFromDataType('integer');
    $typed_data = IntegerData::createInstance($data_definition);
    $this->typedDataManager
      ->createDataDefinition('integer')
      ->willReturn($data_definition);
    $this->typedDataManager
      ->getDefaultConstraints($data_definition)
      ->willReturn([]);
    $this->typedDataManager
      ->create($data_definition, 5)
      ->willReturn($typed_data);
    $input = [
      'foo' => [
        'label' => 'Foo',
        'type' => 'integer',
        'value' => 5,
      ],
    ];
    $expected = new Context(new ContextDefinition('integer', 'Foo'), 5);
    $actual = $this->staticContext
      ->getContextValues($input)['foo'];
    $this
      ->assertEquals($expected, $actual);
  }

  /**
   * @covers ::getContextValues
   */
  public function testGetContextValuesEntityContext() {
    $input = [
      'foo' => [
        'label' => 'Foo',
        'type' => 'entity:node',
        'value' => 'the_node_uuid',
      ],
    ];
    $expected = new EntityLazyLoadContext(new ContextDefinition('entity:node', 'Foo'), $this->entityRepository
      ->reveal(), 'the_node_uuid');
    $actual = $this->staticContext
      ->getContextValues($input)['foo'];
    $this
      ->assertEquals($expected, $actual);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContextMapperTest::$entityRepository protected property
ContextMapperTest::$staticContext protected property
ContextMapperTest::$typedDataManager protected property The typed data manager.
ContextMapperTest::setUp protected function Overrides UnitTestCase::setUp
ContextMapperTest::testGetContextValues public function @covers ::getContextValues
ContextMapperTest::testGetContextValuesContext public function @covers ::getContextValues
ContextMapperTest::testGetContextValuesEntityContext public function @covers ::getContextValues
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.