3 namespace Drupal\Tests\Component\Plugin\Context;
5 use Drupal\Component\Plugin\Context\Context;
6 use PHPUnit\Framework\TestCase;
9 * @coversDefaultClass \Drupal\Component\Plugin\Context\Context
12 class ContextTest extends TestCase {
15 * Data provider for testGetContextValue.
17 public function providerGetContextValue() {
19 ['context_value', 'context_value', FALSE, 'data_type'],
20 [NULL, NULL, FALSE, 'data_type'],
21 ['will throw exception', NULL, TRUE, 'data_type'],
26 * @covers ::getContextValue
27 * @dataProvider providerGetContextValue
29 public function testGetContextValue($expected, $context_value, $is_required, $data_type) {
30 // Mock a Context object.
31 $mock_context = $this->getMockBuilder('Drupal\Component\Plugin\Context\Context')
32 ->disableOriginalConstructor()
33 ->setMethods(['getContextDefinition'])
36 // If the context value exists, getContextValue() behaves like a normal
39 // Set visibility of contextValue.
40 $ref_context_value = new \ReflectionProperty($mock_context, 'contextValue');
41 $ref_context_value->setAccessible(TRUE);
42 // Set contextValue to a testable state.
43 $ref_context_value->setValue($mock_context, $context_value);
44 // Exercise getContextValue().
45 $this->assertEquals($context_value, $mock_context->getContextValue());
47 // If no context value exists, we have to cover either returning NULL or
48 // throwing an exception if the definition requires it.
50 // Create a mock definition.
51 $mock_definition = $this->getMockBuilder('Drupal\Component\Plugin\Context\ContextDefinitionInterface')
52 ->setMethods(['isRequired', 'getDataType'])
53 ->getMockForAbstractClass();
55 // Set expectation for isRequired().
56 $mock_definition->expects($this->once())
57 ->method('isRequired')
58 ->willReturn($is_required);
60 // Set expectation for getDataType().
61 $mock_definition->expects($this->exactly(
64 ->method('getDataType')
65 ->willReturn($data_type);
67 // Set expectation for getContextDefinition().
68 $mock_context->expects($this->once())
69 ->method('getContextDefinition')
70 ->willReturn($mock_definition);
72 // Set expectation for exception.
74 if (method_exists($this, 'expectException')) {
75 $this->expectException('Drupal\Component\Plugin\Exception\ContextException');
76 $this->expectExceptionMessage(sprintf("The %s context is required and not present.", $data_type));
79 $this->setExpectedException(
80 'Drupal\Component\Plugin\Exception\ContextException',
81 sprintf("The %s context is required and not present.", $data_type)
86 // Exercise getContextValue().
87 $this->assertEquals($context_value, $mock_context->getContextValue());
92 * @covers ::getContextValue
94 public function testDefaultValue() {
95 $mock_definition = $this->getMockBuilder('Drupal\Component\Plugin\Context\ContextDefinitionInterface')
96 ->setMethods(['getDefaultValue'])
97 ->getMockForAbstractClass();
99 $mock_definition->expects($this->once())
100 ->method('getDefaultValue')
101 ->willReturn('test');
103 $context = new Context($mock_definition);
104 $this->assertEquals('test', $context->getContextValue());