760c0a9151eb1dc10126d13d324be6077f03ad2e
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Plugin / DefaultFactoryTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Plugin;
4
5 use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
6 use Drupal\Component\Plugin\Exception\PluginException;
7 use Drupal\Component\Plugin\Factory\DefaultFactory;
8 use Drupal\Tests\Component\Plugin\Fixtures\vegetable\Broccoli;
9 use Drupal\Tests\Component\Plugin\Fixtures\vegetable\Corn;
10 use Drupal\Tests\Component\Plugin\Fixtures\vegetable\VegetableInterface;
11 use PHPUnit\Framework\TestCase;
12
13 /**
14  * @coversDefaultClass \Drupal\Component\Plugin\Factory\DefaultFactory
15  * @group Plugin
16  */
17 class DefaultFactoryTest extends TestCase {
18
19   /**
20    * Tests getPluginClass() with a valid array plugin definition.
21    *
22    * @covers ::getPluginClass
23    */
24   public function testGetPluginClassWithValidArrayPluginDefinition() {
25     $plugin_class = Corn::class;
26     $class = DefaultFactory::getPluginClass('corn', ['class' => $plugin_class]);
27
28     $this->assertEquals($plugin_class, $class);
29   }
30
31   /**
32    * Tests getPluginClass() with a valid object plugin definition.
33    *
34    * @covers ::getPluginClass
35    */
36   public function testGetPluginClassWithValidObjectPluginDefinition() {
37     $plugin_class = Corn::class;
38     $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
39     $plugin_definition->expects($this->atLeastOnce())
40       ->method('getClass')
41       ->willReturn($plugin_class);
42     $class = DefaultFactory::getPluginClass('corn', $plugin_definition);
43
44     $this->assertEquals($plugin_class, $class);
45   }
46
47   /**
48    * Tests getPluginClass() with a missing class definition.
49    *
50    * @covers ::getPluginClass
51    */
52   public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() {
53     if (method_exists($this, 'expectException')) {
54       $this->expectException(PluginException::class);
55       $this->expectExceptionMessage('The plugin (corn) did not specify an instance class.');
56     }
57     else {
58       $this->setExpectedException(PluginException::class, 'The plugin (corn) did not specify an instance class.');
59     }
60     DefaultFactory::getPluginClass('corn', []);
61   }
62
63   /**
64    * Tests getPluginClass() with a missing class definition.
65    *
66    * @covers ::getPluginClass
67    */
68   public function testGetPluginClassWithMissingClassWithObjectPluginDefinition() {
69     $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
70     if (method_exists($this, 'expectException')) {
71       $this->expectException(PluginException::class);
72       $this->expectExceptionMessage('The plugin (corn) did not specify an instance class.');
73     }
74     else {
75       $this->setExpectedException(PluginException::class, 'The plugin (corn) did not specify an instance class.');
76     }
77     DefaultFactory::getPluginClass('corn', $plugin_definition);
78   }
79
80   /**
81    * Tests getPluginClass() with a not existing class definition.
82    *
83    * @covers ::getPluginClass
84    */
85   public function testGetPluginClassWithNotExistingClassWithArrayPluginDefinition() {
86     if (method_exists($this, 'expectException')) {
87       $this->expectException(PluginException::class);
88       $this->expectExceptionMessage('Plugin (carrot) instance class "Drupal\Tests\Component\Plugin\Fixtures\vegetable\Carrot" does not exist.');
89     }
90     else {
91       $this->setExpectedException(PluginException::class, 'Plugin (carrot) instance class "Drupal\Tests\Component\Plugin\Fixtures\vegetable\Carrot" does not exist.');
92     }
93     DefaultFactory::getPluginClass('carrot', ['class' => 'Drupal\Tests\Component\Plugin\Fixtures\vegetable\Carrot']);
94   }
95
96   /**
97    * Tests getPluginClass() with a not existing class definition.
98    *
99    * @covers ::getPluginClass
100    */
101   public function testGetPluginClassWithNotExistingClassWithObjectPluginDefinition() {
102     $plugin_class = 'Drupal\Tests\Component\Plugin\Fixtures\vegetable\Carrot';
103     $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
104     $plugin_definition->expects($this->atLeastOnce())
105       ->method('getClass')
106       ->willReturn($plugin_class);
107     if (method_exists($this, 'expectException')) {
108       $this->expectException(PluginException::class);
109     }
110     else {
111       $this->setExpectedException(PluginException::class);
112     }
113     DefaultFactory::getPluginClass('carrot', $plugin_definition);
114   }
115
116   /**
117    * Tests getPluginClass() with a required interface.
118    *
119    * @covers ::getPluginClass
120    */
121   public function testGetPluginClassWithInterfaceWithArrayPluginDefinition() {
122     $plugin_class = Corn::class;
123     $class = DefaultFactory::getPluginClass('corn', ['class' => $plugin_class], VegetableInterface::class);
124
125     $this->assertEquals($plugin_class, $class);
126   }
127
128   /**
129    * Tests getPluginClass() with a required interface.
130    *
131    * @covers ::getPluginClass
132    */
133   public function testGetPluginClassWithInterfaceWithObjectPluginDefinition() {
134     $plugin_class = Corn::class;
135     $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
136     $plugin_definition->expects($this->atLeastOnce())
137       ->method('getClass')
138       ->willReturn($plugin_class);
139     $class = DefaultFactory::getPluginClass('corn', $plugin_definition, VegetableInterface::class);
140
141     $this->assertEquals($plugin_class, $class);
142   }
143
144   /**
145    * Tests getPluginClass() with a required interface but no implementation.
146    *
147    * @covers ::getPluginClass
148    */
149   public function testGetPluginClassWithInterfaceAndInvalidClassWithArrayPluginDefinition() {
150     if (method_exists($this, 'expectException')) {
151       $this->expectException(PluginException::class);
152       $this->expectExceptionMessage('Plugin "corn" (Drupal\Tests\Component\Plugin\Fixtures\vegetable\Broccoli) must implement interface Drupal\Tests\Component\Plugin\Fixtures\vegetable\VegetableInterface.');
153     }
154     else {
155       $this->setExpectedException(PluginException::class, 'Plugin "corn" (Drupal\Tests\Component\Plugin\Fixtures\vegetable\Broccoli) must implement interface Drupal\Tests\Component\Plugin\Fixtures\vegetable\VegetableInterface.');
156     }
157     DefaultFactory::getPluginClass('corn', ['class' => Broccoli::class], VegetableInterface::class);
158   }
159
160   /**
161    * Tests getPluginClass() with a required interface but no implementation.
162    *
163    * @covers ::getPluginClass
164    */
165   public function testGetPluginClassWithInterfaceAndInvalidClassWithObjectPluginDefinition() {
166     $plugin_class = Broccoli::class;
167     $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
168     $plugin_definition->expects($this->atLeastOnce())
169       ->method('getClass')
170       ->willReturn($plugin_class);
171     if (method_exists($this, 'expectException')) {
172       $this->expectException(PluginException::class);
173     }
174     else {
175       $this->setExpectedException(PluginException::class);
176     }
177     DefaultFactory::getPluginClass('corn', $plugin_definition, VegetableInterface::class);
178   }
179
180 }