Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Plugin / PluginManagerBaseTest.php
index a4dd6a76bd9f7dba9abef84655c6f3566f01b0cc..2bad780dbcabefebaa6e12e60068898bd987ef8f 100644 (file)
@@ -3,6 +3,8 @@
 namespace Drupal\Tests\Component\Plugin;
 
 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
+use Drupal\Component\Plugin\Mapper\MapperInterface;
+use Drupal\Component\Plugin\PluginManagerBase;
 use PHPUnit\Framework\TestCase;
 
 /**
@@ -90,4 +92,43 @@ class PluginManagerBaseTest extends TestCase {
     $this->assertEquals($configuration_array, $fallback_result['configuration']);
   }
 
+  /**
+   * @covers ::getInstance
+   */
+  public function testGetInstance() {
+    $options = [
+      'foo' => 'F00',
+      'bar' => 'bAr',
+    ];
+    $instance = new \stdClass();
+    $mapper = $this->prophesize(MapperInterface::class);
+    $mapper->getInstance($options)
+      ->shouldBeCalledTimes(1)
+      ->willReturn($instance);
+    $manager = new StubPluginManagerBaseWithMapper($mapper->reveal());
+    $this->assertEquals($instance, $manager->getInstance($options));
+  }
+
+  /**
+   * @covers ::getInstance
+   */
+  public function testGetInstanceWithoutMapperShouldThrowException() {
+    $options = [
+      'foo' => 'F00',
+      'bar' => 'bAr',
+    ];
+    /** @var \Drupal\Component\Plugin\PluginManagerBase $manager */
+    $manager = $this->getMockBuilder(PluginManagerBase::class)
+      ->getMockForAbstractClass();
+    // Set the expected exception thrown by ::getInstance.
+    if (method_exists($this, 'expectException')) {
+      $this->expectException(\BadMethodCallException::class);
+      $this->expectExceptionMessage(sprintf('%s does not support this method unless %s::$mapper is set.', get_class($manager), get_class($manager)));
+    }
+    else {
+      $this->setExpectedException(\BadMethodCallException::class, sprintf('%s does not support this method unless %s::$mapper is set.', get_class($manager), get_class($manager)));
+    }
+    $manager->getInstance($options);
+  }
+
 }