You are here

class ContextTest in Zircon Profile 8

Same name in this branch
  1. 8 vendor/sebastian/recursion-context/tests/ContextTest.php \SebastianBergmann\RecursionContext\ContextTest
  2. 8 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest
  3. 8 core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest
Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest

@coversDefaultClass \Drupal\Component\Plugin\Context\Context @group Plugin

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
    • class \Drupal\Tests\Component\Plugin\Context\ContextTest

Expanded class hierarchy of ContextTest

File

core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php, line 17
Contains \Drupal\Tests\Component\Plugin\Context\ContextTest.

Namespace

Drupal\Tests\Component\Plugin\Context
View source
class ContextTest extends UnitTestCase {

  /**
   * Data provider for testGetContextValue.
   */
  public function providerGetContextValue() {
    return [
      [
        'context_value',
        'context_value',
        FALSE,
        'data_type',
      ],
      [
        NULL,
        NULL,
        FALSE,
        'data_type',
      ],
      [
        'will throw exception',
        NULL,
        TRUE,
        'data_type',
      ],
    ];
  }

  /**
   * @covers ::getContextValue
   * @dataProvider providerGetContextValue
   */
  public function testGetContextValue($expected, $context_value, $is_required, $data_type) {

    // Mock a Context object.
    $mock_context = $this
      ->getMockBuilder('Drupal\\Component\\Plugin\\Context\\Context')
      ->disableOriginalConstructor()
      ->setMethods(array(
      'getContextDefinition',
    ))
      ->getMock();

    // If the context value exists, getContextValue() behaves like a normal
    // getter.
    if ($context_value) {

      // Set visibility of contextValue.
      $ref_context_value = new \ReflectionProperty($mock_context, 'contextValue');
      $ref_context_value
        ->setAccessible(TRUE);

      // Set contextValue to a testable state.
      $ref_context_value
        ->setValue($mock_context, $context_value);

      // Exercise getContextValue().
      $this
        ->assertEquals($context_value, $mock_context
        ->getContextValue());
    }
    else {

      // Create a mock definition.
      $mock_definition = $this
        ->getMockBuilder('Drupal\\Component\\Plugin\\Context\\ContextDefinitionInterface')
        ->setMethods(array(
        'isRequired',
        'getDataType',
      ))
        ->getMockForAbstractClass();

      // Set expectation for isRequired().
      $mock_definition
        ->expects($this
        ->once())
        ->method('isRequired')
        ->willReturn($is_required);

      // Set expectation for getDataType().
      $mock_definition
        ->expects($this
        ->exactly($is_required ? 1 : 0))
        ->method('getDataType')
        ->willReturn($data_type);

      // Set expectation for getContextDefinition().
      $mock_context
        ->expects($this
        ->once())
        ->method('getContextDefinition')
        ->willReturn($mock_definition);

      // Set expectation for exception.
      if ($is_required) {
        $this
          ->setExpectedException('Drupal\\Component\\Plugin\\Exception\\ContextException', sprintf("The %s context is required and not present.", $data_type));
      }

      // Exercise getContextValue().
      $this
        ->assertEquals($context_value, $mock_context
        ->getContextValue());
    }
  }

  /**
   * @covers ::getContextValue
   */
  public function testDefaultValue() {
    $mock_definition = $this
      ->getMockBuilder('Drupal\\Component\\Plugin\\Context\\ContextDefinitionInterface')
      ->setMethods(array(
      'getDefaultValue',
    ))
      ->getMockForAbstractClass();
    $mock_definition
      ->expects($this
      ->once())
      ->method('getDefaultValue')
      ->willReturn('test');
    $context = new Context($mock_definition);
    $this
      ->assertEquals('test', $context
      ->getContextValue());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContextTest::providerGetContextValue public function Data provider for testGetContextValue.
ContextTest::testDefaultValue public function @covers ::getContextValue
ContextTest::testGetContextValue public function @covers ::getContextValue @dataProvider providerGetContextValue
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