class DefaultSingleLazyPluginCollectionTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Plugin/DefaultSingleLazyPluginCollectionTest.php \Drupal\Tests\Core\Plugin\DefaultSingleLazyPluginCollectionTest
  2. 10 core/tests/Drupal/Tests/Core/Plugin/DefaultSingleLazyPluginCollectionTest.php \Drupal\Tests\Core\Plugin\DefaultSingleLazyPluginCollectionTest
  3. 8.9.x core/tests/Drupal/Tests/Core/Plugin/DefaultSingleLazyPluginCollectionTest.php \Drupal\Tests\Core\Plugin\DefaultSingleLazyPluginCollectionTest

@coversDefaultClass \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
@group Plugin

Hierarchy

Expanded class hierarchy of DefaultSingleLazyPluginCollectionTest

File

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

Namespace

Drupal\Tests\Core\Plugin
View source
class DefaultSingleLazyPluginCollectionTest extends LazyPluginCollectionTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected function setupPluginCollection(InvocationOrder $create_count = NULL) {
    $definitions = $this->getPluginDefinitions();
    $this->pluginInstances['apple'] = new ConfigurablePlugin([
      'id' => 'apple',
      'key' => 'value',
    ], 'apple', $definitions['apple']);
    $this->pluginInstances['banana'] = new ConfigurablePlugin([
      'id' => 'banana',
      'key' => 'other_value',
    ], 'banana', $definitions['banana']);
    $create_count = $create_count ?: $this->never();
    $this->pluginManager
      ->expects($create_count)
      ->method('createInstance')
      ->willReturnCallback(function ($id) {
      return $this->pluginInstances[$id];
    });
    $this->defaultPluginCollection = new DefaultSingleLazyPluginCollection($this->pluginManager, 'apple', [
      'id' => 'apple',
      'key' => 'value',
    ]);
  }
  
  /**
   * Tests the get() method.
   */
  public function testGet() {
    $this->setupPluginCollection($this->once());
    $apple = $this->pluginInstances['apple'];
    $this->assertSame($apple, $this->defaultPluginCollection
      ->get('apple'));
  }
  
  /**
   * @covers ::addInstanceId
   * @covers ::getConfiguration
   * @covers ::setConfiguration
   */
  public function testAddInstanceId() {
    $this->setupPluginCollection($this->any());
    $this->assertEquals([
      'id' => 'apple',
      'key' => 'value',
    ], $this->defaultPluginCollection
      ->get('apple')
      ->getConfiguration());
    $this->assertEquals([
      'id' => 'apple',
      'key' => 'value',
    ], $this->defaultPluginCollection
      ->getConfiguration());
    $this->defaultPluginCollection
      ->addInstanceId('banana', [
      'id' => 'banana',
      'key' => 'other_value',
    ]);
    $this->assertEquals([
      'id' => 'apple',
      'key' => 'value',
    ], $this->defaultPluginCollection
      ->get('apple')
      ->getConfiguration());
    $this->assertEquals([
      'id' => 'banana',
      'key' => 'other_value',
    ], $this->defaultPluginCollection
      ->getConfiguration());
    $this->assertEquals([
      'id' => 'banana',
      'key' => 'other_value',
    ], $this->defaultPluginCollection
      ->get('banana')
      ->getConfiguration());
  }
  
  /**
   * @covers ::getInstanceIds
   */
  public function testGetInstanceIds() {
    $this->setupPluginCollection($this->any());
    $this->assertEquals([
      'apple' => 'apple',
    ], $this->defaultPluginCollection
      ->getInstanceIds());
    $this->defaultPluginCollection
      ->addInstanceId('banana', [
      'id' => 'banana',
      'key' => 'other_value',
    ]);
    $this->assertEquals([
      'banana' => 'banana',
    ], $this->defaultPluginCollection
      ->getInstanceIds());
  }

}

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