function DefaultLazyPluginCollectionTest::testSetInstanceConfigurationPluginChange

Same name and namespace in other branches
  1. 10 core/tests/Drupal/Tests/Core/Plugin/DefaultLazyPluginCollectionTest.php \Drupal\Tests\Core\Plugin\DefaultLazyPluginCollectionTest::testSetInstanceConfigurationPluginChange()

Tests plugin instances are changed if the configuration plugin key changes.

@covers ::setInstanceConfiguration

File

core/tests/Drupal/Tests/Core/Plugin/DefaultLazyPluginCollectionTest.php, line 176

Class

DefaultLazyPluginCollectionTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Plugin%21DefaultLazyPluginCollection.php/class/DefaultLazyPluginCollection/11.x" title="Provides a default plugin collection for a plugin type." class="local">\Drupal\Core\Plugin\DefaultLazyPluginCollection</a> @group Plugin

Namespace

Drupal\Tests\Core\Plugin

Code

public function testSetInstanceConfigurationPluginChange() : void {
    $configurable_plugin = $this->prophesize(ConfigurableInterface::class);
    $configurable_config = [
        'id' => 'configurable',
        'foo' => 'bar',
    ];
    $configurable_plugin->getConfiguration()
        ->willReturn($configurable_config);
    $nonconfigurable_plugin = $this->prophesize(PluginInspectionInterface::class);
    $nonconfigurable_config = [
        'id' => 'non-configurable',
        'baz' => 'qux',
    ];
    $nonconfigurable_plugin->configuration = $nonconfigurable_config;
    $configurations = [
        'instance' => $configurable_config,
    ];
    $plugin_manager = $this->prophesize(PluginManagerInterface::class);
    $plugin_manager->createInstance('configurable', $configurable_config)
        ->willReturn($configurable_plugin->reveal());
    $plugin_manager->createInstance('non-configurable', $nonconfigurable_config)
        ->willReturn($nonconfigurable_plugin->reveal());
    $collection = new DefaultLazyPluginCollection($plugin_manager->reveal(), $configurations);
    $this->assertInstanceOf(ConfigurableInterface::class, $collection->get('instance'));
    // Ensure changing the instance to a different plugin via
    // setInstanceConfiguration() results in a different plugin instance.
    $collection->setInstanceConfiguration('instance', $nonconfigurable_config);
    $this->assertNotInstanceOf(ConfigurableInterface::class, $collection->get('instance'));
}

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