diff 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
line wrap: on
line diff
--- a/core/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php	Tue Jul 10 15:07:59 2018 +0100
+++ b/core/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php	Thu Feb 28 13:21:36 2019 +0000
@@ -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 @@
     $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);
+  }
+
 }