class DefaultLazyPluginCollectionTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Plugin/DefaultLazyPluginCollectionTest.php \Drupal\Tests\Core\Plugin\DefaultLazyPluginCollectionTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Plugin/DefaultLazyPluginCollectionTest.php \Drupal\Tests\Core\Plugin\DefaultLazyPluginCollectionTest
  3. 10 core/tests/Drupal/Tests/Core/Plugin/DefaultLazyPluginCollectionTest.php \Drupal\Tests\Core\Plugin\DefaultLazyPluginCollectionTest

@coversDefaultClass \Drupal\Core\Plugin\DefaultLazyPluginCollection @group Plugin

Hierarchy

Expanded class hierarchy of DefaultLazyPluginCollectionTest

File

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

Namespace

Drupal\Tests\Core\Plugin
View source
class DefaultLazyPluginCollectionTest extends LazyPluginCollectionTestBase {
    
    /**
     * Stores all setup plugin instances.
     *
     * @var \Drupal\Component\Plugin\ConfigurableInterface[]
     */
    protected $pluginInstances;
    
    /**
     * @covers ::has
     */
    public function testHas() : void {
        $this->setupPluginCollection();
        $definitions = $this->getPluginDefinitions();
        $this->assertFalse($this->defaultPluginCollection
            ->has($this->randomMachineName()), 'Nonexistent plugin found.');
        foreach (array_keys($definitions) as $plugin_id) {
            $this->assertTrue($this->defaultPluginCollection
                ->has($plugin_id));
        }
    }
    
    /**
     * @covers ::get
     */
    public function testGet() : void {
        $this->setupPluginCollection($this->once());
        $apple = $this->pluginInstances['apple'];
        $this->assertSame($apple, $this->defaultPluginCollection
            ->get('apple'));
    }
    
    /**
     * @covers ::get
     */
    public function testGetNotExistingPlugin() : void {
        $this->setupPluginCollection();
        $this->expectException(PluginNotFoundException::class);
        $this->expectExceptionMessage("Plugin ID 'pear' was not found.");
        $this->defaultPluginCollection
            ->get('pear');
    }
    
    /**
     * Provides test data for testSortHelper.
     *
     * @return array
     *   The test data.
     */
    public static function providerTestSortHelper() {
        return [
            [
                'apple',
                'apple',
                0,
            ],
            [
                'apple',
                'cherry',
                -1,
            ],
            [
                'cherry',
                'apple',
                1,
            ],
            [
                'cherry',
                'banana',
                1,
            ],
        ];
    }
    
    /**
     * @param string $plugin_id_1
     *   The first plugin ID.
     * @param string $plugin_id_2
     *   The second plugin ID.
     * @param int $expected
     *   The expected result.
     *
     * @covers ::sortHelper
     * @dataProvider providerTestSortHelper
     */
    public function testSortHelper($plugin_id_1, $plugin_id_2, $expected) : void {
        $this->setupPluginCollection($this->any());
        if ($expected != 0) {
            $expected = $expected > 0 ? 1 : -1;
        }
        $this->assertEquals($expected, $this->defaultPluginCollection
            ->sortHelper($plugin_id_1, $plugin_id_2));
    }
    
    /**
     * @covers ::getConfiguration
     */
    public function testGetConfiguration() : void {
        $this->setupPluginCollection($this->exactly(3));
        // The expected order matches $this->config.
        $expected = [
            'banana',
            'cherry',
            'apple',
        ];
        $config = $this->defaultPluginCollection
            ->getConfiguration();
        $this->assertSame($expected, array_keys($config), 'The order of the configuration is unchanged.');
        $ids = $this->defaultPluginCollection
            ->getInstanceIds();
        $this->assertSame($expected, array_keys($ids), 'The order of the instances is unchanged.');
        $this->defaultPluginCollection
            ->sort();
        $config = $this->defaultPluginCollection
            ->getConfiguration();
        $this->assertSame($expected, array_keys($config), 'After sorting, the order of the configuration is unchanged.');
        $ids = $this->defaultPluginCollection
            ->getInstanceIds();
        sort($expected);
        $this->assertSame($expected, array_keys($ids), 'After sorting, the order of the instances is also sorted.');
    }
    
    /**
     * @covers ::addInstanceId
     */
    public function testAddInstanceId() : void {
        $this->setupPluginCollection($this->exactly(4));
        $expected = [
            'banana' => 'banana',
            'cherry' => 'cherry',
            'apple' => 'apple',
        ];
        $this->defaultPluginCollection
            ->addInstanceId('apple');
        $result = $this->defaultPluginCollection
            ->getInstanceIds();
        $this->assertSame($expected, $result);
        $this->assertSame($expected, array_intersect_key($result, $this->defaultPluginCollection
            ->getConfiguration()));
        $expected = [
            'cherry' => 'cherry',
            'apple' => 'apple',
            'banana' => 'banana',
        ];
        $this->defaultPluginCollection
            ->removeInstanceId('banana');
        $this->defaultPluginCollection
            ->addInstanceId('banana', $this->config['banana']);
        $result = $this->defaultPluginCollection
            ->getInstanceIds();
        $this->assertSame($expected, $result);
        $this->assertSame($expected, array_intersect_key($result, $this->defaultPluginCollection
            ->getConfiguration()));
    }
    
    /**
     * @covers ::removeInstanceId
     */
    public function testRemoveInstanceId() : void {
        $this->setupPluginCollection($this->exactly(2));
        $this->defaultPluginCollection
            ->removeInstanceId('cherry');
        $config = $this->defaultPluginCollection
            ->getConfiguration();
        $this->assertArrayNotHasKey('cherry', $config, 'After removing an instance, the configuration is updated.');
    }
    
    /**
     * @covers ::setInstanceConfiguration
     */
    public function testSetInstanceConfiguration() : void {
        $this->setupPluginCollection($this->exactly(3));
        $expected = [
            'id' => 'cherry',
            'key' => 'value',
            'custom' => 'bananas',
        ];
        $this->defaultPluginCollection
            ->setInstanceConfiguration('cherry', $expected);
        $config = $this->defaultPluginCollection
            ->getConfiguration();
        $this->assertSame($expected, $config['cherry']);
    }
    
    /**
     * Tests plugin instances are changed if the configuration plugin key changes.
     *
     * @covers ::setInstanceConfiguration
     */
    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'));
    }
    
    /**
     * @covers ::count
     */
    public function testCount() : void {
        $this->setupPluginCollection();
        $this->assertCount(3, $this->defaultPluginCollection);
    }
    
    /**
     * @covers ::clear
     */
    public function testClear() : void {
        $this->setupPluginCollection($this->exactly(6));
        $this->defaultPluginCollection
            ->getConfiguration();
        $this->defaultPluginCollection
            ->getConfiguration();
        $this->defaultPluginCollection
            ->clear();
        $this->defaultPluginCollection
            ->getConfiguration();
    }
    
    /**
     * @covers ::set
     */
    public function testSet() : void {
        $this->setupPluginCollection($this->exactly(4));
        $instance = $this->pluginManager
            ->createInstance('cherry', $this->config['cherry']);
        $this->defaultPluginCollection
            ->set('cherry2', $instance);
        $this->defaultPluginCollection
            ->setInstanceConfiguration('cherry2', $this->config['cherry']);
        $expected = [
            'banana',
            'cherry',
            'apple',
            'cherry2',
        ];
        $config = $this->defaultPluginCollection
            ->getConfiguration();
        $this->assertSame($expected, array_keys($config));
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getPluginMock($plugin_id, array $definition) {
        return new TestConfigurablePlugin($this->config[$plugin_id], $plugin_id, $definition);
    }
    
    /**
     * @covers ::getConfiguration
     */
    public function testConfigurableGetConfiguration() : void {
        $this->setupPluginCollection($this->exactly(3));
        $config = $this->defaultPluginCollection
            ->getConfiguration();
        $this->assertSame($this->config, $config);
    }
    
    /**
     * @covers ::setConfiguration
     */
    public function testConfigurableSetConfiguration() : void {
        $this->setupPluginCollection($this->exactly(2));
        $this->defaultPluginCollection
            ->setConfiguration([
            'apple' => [
                'value' => 'pineapple',
                'id' => 'apple',
            ],
        ]);
        $config = $this->defaultPluginCollection
            ->getConfiguration();
        $this->assertSame([
            'apple' => [
                'value' => 'pineapple',
                'id' => 'apple',
            ],
        ], $config);
        $plugin = $this->pluginInstances['apple'];
        $this->assertSame([
            'value' => 'pineapple',
            'id' => 'apple',
        ], $plugin->getConfiguration());
        $this->defaultPluginCollection
            ->setConfiguration([]);
        $this->assertSame([], $this->defaultPluginCollection
            ->getConfiguration());
        $this->defaultPluginCollection
            ->setConfiguration([
            'cherry' => [
                'value' => 'kiwi',
                'id' => 'cherry',
            ],
        ]);
        $expected['cherry'] = [
            'value' => 'kiwi',
            'id' => 'cherry',
        ];
        $config = $this->defaultPluginCollection
            ->getConfiguration();
        $this->assertSame($expected, $config);
    }
    
    /**
     * Tests that plugin methods are correctly attached to interfaces.
     *
     * @covers ::getConfiguration
     */
    public function testConfigurableInterface() : 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 = [
            'configurable' => $configurable_config,
            'non-configurable' => $nonconfigurable_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->assertSame($configurations, $collection->getConfiguration());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DefaultLazyPluginCollectionTest::$pluginInstances protected property Stores all setup plugin instances. Overrides LazyPluginCollectionTestBase::$pluginInstances
DefaultLazyPluginCollectionTest::getPluginMock protected function Returns a mocked plugin object. Overrides LazyPluginCollectionTestBase::getPluginMock
DefaultLazyPluginCollectionTest::providerTestSortHelper public static function Provides test data for testSortHelper.
DefaultLazyPluginCollectionTest::testAddInstanceId public function @covers ::addInstanceId
DefaultLazyPluginCollectionTest::testClear public function @covers ::clear
DefaultLazyPluginCollectionTest::testConfigurableGetConfiguration public function @covers ::getConfiguration
DefaultLazyPluginCollectionTest::testConfigurableInterface public function Tests that plugin methods are correctly attached to interfaces.
DefaultLazyPluginCollectionTest::testConfigurableSetConfiguration public function @covers ::setConfiguration
DefaultLazyPluginCollectionTest::testCount public function @covers ::count
DefaultLazyPluginCollectionTest::testGet public function @covers ::get
DefaultLazyPluginCollectionTest::testGetConfiguration public function @covers ::getConfiguration
DefaultLazyPluginCollectionTest::testGetNotExistingPlugin public function @covers ::get
DefaultLazyPluginCollectionTest::testHas public function @covers ::has
DefaultLazyPluginCollectionTest::testRemoveInstanceId public function @covers ::removeInstanceId
DefaultLazyPluginCollectionTest::testSet public function @covers ::set
DefaultLazyPluginCollectionTest::testSetInstanceConfiguration public function @covers ::setInstanceConfiguration
DefaultLazyPluginCollectionTest::testSetInstanceConfigurationPluginChange public function Tests plugin instances are changed if the configuration plugin key changes.
DefaultLazyPluginCollectionTest::testSortHelper public function @covers ::sortHelper
@dataProvider providerTestSortHelper
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
LazyPluginCollectionTestBase::$config protected property Contains the plugin configuration.
LazyPluginCollectionTestBase::$defaultPluginCollection protected property The tested plugin collection.
LazyPluginCollectionTestBase::$pluginManager protected property The mocked plugin manager.
LazyPluginCollectionTestBase::getPluginDefinitions protected function Returns some example plugin definitions.
LazyPluginCollectionTestBase::returnPluginMap public function Return callback for createInstance.
LazyPluginCollectionTestBase::setUp protected function Overrides UnitTestCase::setUp
LazyPluginCollectionTestBase::setupPluginCollection protected function Sets up the default plugin collection. 1
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

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