You are here

class ContextTypedDataTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTypedDataTest.php \Drupal\Tests\Core\Plugin\Context\ContextTypedDataTest

Tests that contexts work properly with the typed data manager.

@coversDefaultClass \Drupal\Core\Plugin\Context\Context @group Context

Hierarchy

Expanded class hierarchy of ContextTypedDataTest

File

core/tests/Drupal/Tests/Core/Plugin/Context/ContextTypedDataTest.php, line 23
Contains \Drupal\Tests\Core\Plugin\Context\ContextTypedDataTest.

Namespace

Drupal\Tests\Core\Plugin\Context
View source
class ContextTypedDataTest extends UnitTestCase {

  /**
   * The typed data object used during testing.
   *
   * @var \Drupal\Core\TypedData\Plugin\DataType\StringData
   */
  protected $typedData;

  /**
   * Tests that getting a context value does not throw fatal errors.
   *
   * This test ensures that the typed data manager is set correctly on the
   * Context class.
   *
   * @covers ::getContextValue
   */
  public function testGetContextValue() {

    // Prepare a container that holds the typed data manager mock.
    $typed_data_manager = $this
      ->getMockBuilder('Drupal\\Core\\TypedData\\TypedDataManager')
      ->disableOriginalConstructor()
      ->getMock();
    $typed_data_manager
      ->expects($this
      ->once())
      ->method('getCanonicalRepresentation')
      ->will($this
      ->returnCallback(array(
      $this,
      'getCanonicalRepresentation',
    )));
    $container = new ContainerBuilder();
    $container
      ->set('typed_data_manager', $typed_data_manager);
    \Drupal::setContainer($container);
    $definition = new ContextDefinition('any');
    $data_definition = DataDefinition::create('string');
    $this->typedData = new StringData($data_definition);
    $this->typedData
      ->setValue('example string');
    $context = new Context($definition, $this->typedData);
    $value = $context
      ->getContextValue();
    $this
      ->assertSame($value, $this->typedData
      ->getValue());
  }

  /**
   * Helper mock callback to return the typed data value.
   */
  public function getCanonicalRepresentation() {
    return $this->typedData
      ->getValue();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContextTypedDataTest::$typedData protected property The typed data object used during testing.
ContextTypedDataTest::getCanonicalRepresentation public function Helper mock callback to return the typed data value.
ContextTypedDataTest::testGetContextValue public function Tests that getting a context value does not throw fatal errors.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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 259