More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Plugin / Context / ContextTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Plugin\Context;
4
5 use Drupal\Component\Plugin\Context\Context;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Component\Plugin\Context\Context
10  * @group Plugin
11  */
12 class ContextTest extends TestCase {
13
14   /**
15    * Data provider for testGetContextValue.
16    */
17   public function providerGetContextValue() {
18     return [
19       ['context_value', 'context_value', FALSE, 'data_type'],
20       [NULL, NULL, FALSE, 'data_type'],
21       ['will throw exception', NULL, TRUE, 'data_type'],
22     ];
23   }
24
25   /**
26    * @covers ::getContextValue
27    * @dataProvider providerGetContextValue
28    */
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'])
34       ->getMock();
35
36     // If the context value exists, getContextValue() behaves like a normal
37     // getter.
38     if ($context_value) {
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());
46     }
47     // If no context value exists, we have to cover either returning NULL or
48     // throwing an exception if the definition requires it.
49     else {
50       // Create a mock definition.
51       $mock_definition = $this->getMockBuilder('Drupal\Component\Plugin\Context\ContextDefinitionInterface')
52         ->setMethods(['isRequired', 'getDataType'])
53         ->getMockForAbstractClass();
54
55       // Set expectation for isRequired().
56       $mock_definition->expects($this->once())
57         ->method('isRequired')
58         ->willReturn($is_required);
59
60       // Set expectation for getDataType().
61       $mock_definition->expects($this->exactly(
62             $is_required ? 1 : 0
63         ))
64         ->method('getDataType')
65         ->willReturn($data_type);
66
67       // Set expectation for getContextDefinition().
68       $mock_context->expects($this->once())
69         ->method('getContextDefinition')
70         ->willReturn($mock_definition);
71
72       // Set expectation for exception.
73       if ($is_required) {
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));
77         }
78         else {
79           $this->setExpectedException(
80             'Drupal\Component\Plugin\Exception\ContextException',
81             sprintf("The %s context is required and not present.", $data_type)
82           );
83         }
84       }
85
86       // Exercise getContextValue().
87       $this->assertEquals($context_value, $mock_context->getContextValue());
88     }
89   }
90
91   /**
92    * @covers ::getContextValue
93    */
94   public function testDefaultValue() {
95     $mock_definition = $this->getMockBuilder('Drupal\Component\Plugin\Context\ContextDefinitionInterface')
96       ->setMethods(['getDefaultValue'])
97       ->getMockForAbstractClass();
98
99     $mock_definition->expects($this->once())
100       ->method('getDefaultValue')
101       ->willReturn('test');
102
103     $context = new Context($mock_definition);
104     $this->assertEquals('test', $context->getContextValue());
105   }
106
107 }