comparison core/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\Component\Plugin; 3 namespace Drupal\Tests\Component\Plugin;
4 4
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException; 5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Component\Plugin\Mapper\MapperInterface;
7 use Drupal\Component\Plugin\PluginManagerBase;
6 use PHPUnit\Framework\TestCase; 8 use PHPUnit\Framework\TestCase;
7 9
8 /** 10 /**
9 * @coversDefaultClass \Drupal\Component\Plugin\PluginManagerBase 11 * @coversDefaultClass \Drupal\Component\Plugin\PluginManagerBase
10 * @group Plugin 12 * @group Plugin
88 $fallback_result = $manager->createInstance('invalid', $configuration_array); 90 $fallback_result = $manager->createInstance('invalid', $configuration_array);
89 $this->assertEquals('invalid_fallback', $fallback_result['plugin_id']); 91 $this->assertEquals('invalid_fallback', $fallback_result['plugin_id']);
90 $this->assertEquals($configuration_array, $fallback_result['configuration']); 92 $this->assertEquals($configuration_array, $fallback_result['configuration']);
91 } 93 }
92 94
95 /**
96 * @covers ::getInstance
97 */
98 public function testGetInstance() {
99 $options = [
100 'foo' => 'F00',
101 'bar' => 'bAr',
102 ];
103 $instance = new \stdClass();
104 $mapper = $this->prophesize(MapperInterface::class);
105 $mapper->getInstance($options)
106 ->shouldBeCalledTimes(1)
107 ->willReturn($instance);
108 $manager = new StubPluginManagerBaseWithMapper($mapper->reveal());
109 $this->assertEquals($instance, $manager->getInstance($options));
110 }
111
112 /**
113 * @covers ::getInstance
114 */
115 public function testGetInstanceWithoutMapperShouldThrowException() {
116 $options = [
117 'foo' => 'F00',
118 'bar' => 'bAr',
119 ];
120 /** @var \Drupal\Component\Plugin\PluginManagerBase $manager */
121 $manager = $this->getMockBuilder(PluginManagerBase::class)
122 ->getMockForAbstractClass();
123 // Set the expected exception thrown by ::getInstance.
124 if (method_exists($this, 'expectException')) {
125 $this->expectException(\BadMethodCallException::class);
126 $this->expectExceptionMessage(sprintf('%s does not support this method unless %s::$mapper is set.', get_class($manager), get_class($manager)));
127 }
128 else {
129 $this->setExpectedException(\BadMethodCallException::class, sprintf('%s does not support this method unless %s::$mapper is set.', get_class($manager), get_class($manager)));
130 }
131 $manager->getInstance($options);
132 }
133
93 } 134 }