function ContextTest::testGetContextValue

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest::testGetContextValue()
  2. 8.9.x core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest::testGetContextValue()
  3. 10 core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest::testGetContextValue()

@covers ::getContextValue @dataProvider providerGetContextValue

File

core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php, line 31

Class

ContextTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Component%21Plugin%21Context%21Context.php/class/Context/11.x" title="A generic context class for wrapping data a plugin needs to operate." class="local">\Drupal\Component\Plugin\Context\Context</a> @group Plugin

Namespace

Drupal\Tests\Component\Plugin\Context

Code

public function testGetContextValue($expected, $context_value, $is_required, $data_type) : void {
    // Mock a Context object.
    $mock_context = $this->getMockBuilder('Drupal\\Component\\Plugin\\Context\\Context')
        ->disableOriginalConstructor()
        ->onlyMethods([
        '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');
        // 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->createMock('Drupal\\Component\\Plugin\\Context\\ContextDefinitionInterface');
        // 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->expectException('Drupal\\Component\\Plugin\\Exception\\ContextException');
            $this->expectExceptionMessage(sprintf("The %s context is required and not present.", $data_type));
        }
        // Exercise getContextValue().
        $this->assertEquals($context_value, $mock_context->getContextValue());
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.