class LazyPluginCollectionTestBase

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

Provides a base class for plugin collection tests.

Hierarchy

Expanded class hierarchy of LazyPluginCollectionTestBase

File

core/tests/Drupal/Tests/Core/Plugin/LazyPluginCollectionTestBase.php, line 14

Namespace

Drupal\Tests\Core\Plugin
View source
abstract class LazyPluginCollectionTestBase extends UnitTestCase {
    
    /**
     * The mocked plugin manager.
     *
     * @var \Drupal\Component\Plugin\PluginManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $pluginManager;
    
    /**
     * The tested plugin collection.
     *
     * @var \Drupal\Core\Plugin\DefaultLazyPluginCollection|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $defaultPluginCollection;
    
    /**
     * Stores all setup plugin instances.
     *
     * @var \Drupal\Component\Plugin\PluginInspectionInterface[]
     */
    protected $pluginInstances;
    
    /**
     * Contains the plugin configuration.
     *
     * @var array
     */
    protected $config = [
        'banana' => [
            'id' => 'banana',
            'key' => 'value',
        ],
        'cherry' => [
            'id' => 'cherry',
            'key' => 'value',
        ],
        'apple' => [
            'id' => 'apple',
            'key' => 'value',
        ],
    ];
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->pluginManager = $this->createMock('Drupal\\Component\\Plugin\\PluginManagerInterface');
        $this->pluginManager
            ->expects($this->any())
            ->method('getDefinitions')
            ->willReturn($this->getPluginDefinitions());
    }
    
    /**
     * Sets up the default plugin collection.
     *
     * @param \PHPUnit\Framework\MockObject\Rule\InvocationOrder|null $create_count
     *   (optional) The number of times that createInstance() is expected to be
     *   called. For example, $this->any(), $this->once(), $this->exactly(6).
     *   Defaults to $this->never().
     */
    protected function setupPluginCollection(?InvocationOrder $create_count = NULL) {
        $this->pluginInstances = [];
        $map = [];
        foreach ($this->getPluginDefinitions() as $plugin_id => $definition) {
            // Create a mock plugin instance.
            $this->pluginInstances[$plugin_id] = $this->getPluginMock($plugin_id, $definition);
            $map[] = [
                $plugin_id,
                $this->config[$plugin_id],
                $this->pluginInstances[$plugin_id],
            ];
        }
        $create_count = $create_count ?: $this->never();
        $this->pluginManager
            ->expects($create_count)
            ->method('createInstance')
            ->willReturnCallback([
            $this,
            'returnPluginMap',
        ]);
        $this->defaultPluginCollection = new DefaultLazyPluginCollection($this->pluginManager, $this->config);
    }
    
    /**
     * Return callback for createInstance.
     *
     * @param string $plugin_id
     *   The plugin ID to return the mock plugin for.
     *
     * @return \Drupal\Component\Plugin\PluginInspectionInterface|\PHPUnit\Framework\MockObject\MockObject
     *   The mock plugin object.
     */
    public function returnPluginMap($plugin_id) {
        return $this->pluginInstances[$plugin_id];
    }
    
    /**
     * Returns a mocked plugin object.
     *
     * @param string $plugin_id
     *   The plugin ID.
     * @param array $definition
     *   The plugin definition.
     *
     * @return \Drupal\Component\Plugin\PluginInspectionInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected function getPluginMock($plugin_id, array $definition) {
        // Create a mock plugin instance.
        $mock = $this->createMock('Drupal\\Component\\Plugin\\PluginInspectionInterface');
        $mock->expects($this->any())
            ->method('getPluginId')
            ->willReturn($plugin_id);
        return $mock;
    }
    
    /**
     * Returns some example plugin definitions.
     *
     * @return array
     *   The example plugin definitions.
     */
    protected function getPluginDefinitions() {
        $definitions = [
            'apple' => [
                'id' => 'apple',
                'label' => 'Apple',
                'color' => 'green',
                'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Apple',
                'provider' => 'plugin_test',
            ],
            'banana' => [
                'id' => 'banana',
                'label' => 'Banana',
                'color' => 'yellow',
                'uses' => [
                    'bread' => 'Banana bread',
                ],
                'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Banana',
                'provider' => 'plugin_test',
            ],
            'cherry' => [
                'id' => 'cherry',
                'label' => 'Cherry',
                'color' => 'red',
                'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Cherry',
                'provider' => 'plugin_test',
            ],
        ];
        return $definitions;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
LazyPluginCollectionTestBase::$config protected property Contains the plugin configuration.
LazyPluginCollectionTestBase::$defaultPluginCollection protected property The tested plugin collection.
LazyPluginCollectionTestBase::$pluginInstances protected property Stores all setup plugin instances. 1
LazyPluginCollectionTestBase::$pluginManager protected property The mocked plugin manager.
LazyPluginCollectionTestBase::getPluginDefinitions protected function Returns some example plugin definitions.
LazyPluginCollectionTestBase::getPluginMock protected function Returns a mocked plugin object. 1
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
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
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
UnitTestCase::__get public function

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