function PluginManagerBaseTest::testCreateInstanceFallback

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php \Drupal\Tests\Component\Plugin\PluginManagerBaseTest::testCreateInstanceFallback()
  2. 10 core/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php \Drupal\Tests\Component\Plugin\PluginManagerBaseTest::testCreateInstanceFallback()
  3. 11.x core/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php \Drupal\Tests\Component\Plugin\PluginManagerBaseTest::testCreateInstanceFallback()

Tests createInstance() with a fallback method.

@covers ::createInstance

File

core/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php, line 71

Class

PluginManagerBaseTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Component%21Plugin%21PluginManagerBase.php/class/PluginManagerBase/8.9.x" title="Base class for plugin managers." class="local">\Drupal\Component\Plugin\PluginManagerBase</a> @group Plugin

Namespace

Drupal\Tests\Component\Plugin

Code

public function testCreateInstanceFallback() {
    // We use our special stub class which extends PluginManagerBase and also
    // implements FallbackPluginManagerInterface.
    $manager = new StubFallbackPluginManager();
    // Put our stubbed factory on the base object.
    $factory_ref = new \ReflectionProperty($manager, 'factory');
    $factory_ref->setAccessible(TRUE);
    // Set up the configuration array.
    $configuration_array = [
        'config' => 'something',
    ];
    // Test with fallback interface and valid plugin_id.
    $factory_ref->setValue($manager, $this->getMockFactoryInterface(1));
    $no_fallback_result = $manager->createInstance('valid', $configuration_array);
    $this->assertEquals('valid', $no_fallback_result['plugin_id']);
    $this->assertEquals($configuration_array, $no_fallback_result['configuration']);
    // Test with fallback interface and invalid plugin_id.
    $factory_ref->setValue($manager, $this->getMockFactoryInterface(2));
    $fallback_result = $manager->createInstance('invalid', $configuration_array);
    $this->assertEquals('invalid_fallback', $fallback_result['plugin_id']);
    $this->assertEquals($configuration_array, $fallback_result['configuration']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.