You are here

class PluginTypeManagerTest in Plugin 8.2

Same name in this branch
  1. 8.2 tests/src/Unit/PluginType/PluginTypeManagerTest.php \Drupal\Tests\plugin\Unit\PluginType\PluginTypeManagerTest
  2. 8.2 tests/src/Kernel/PluginType/PluginTypeManagerTest.php \Drupal\Tests\plugin\Kernel\PluginType\PluginTypeManagerTest

@coversDefaultClass \Drupal\plugin\PluginType\PluginTypeManager

@group Plugin

Hierarchy

Expanded class hierarchy of PluginTypeManagerTest

File

tests/src/Unit/PluginType/PluginTypeManagerTest.php, line 25

Namespace

Drupal\Tests\plugin\Unit\PluginType
View source
class PluginTypeManagerTest extends UnitTestCase {

  /**
   * The service container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $container;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

  /**
   * The plugin type's plugin managers.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface[]
   *   Keys are plugin type IDs.
   */
  protected $pluginManagers = [];

  /**
   * The plugin type definitions.
   *
   * @var array[]
   */
  protected $pluginTypeDefinitions = [];

  /**
   * The class under test.
   *
   * @var \Drupal\plugin\PluginType\PluginTypeManager
   */
  protected $sut;

  /**
   * The typed configuration manager.
   *
   * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $typedConfigurationManager;

  /**
   * Builds a plugin type definition file.
   *
   * @param string $id
   *
   * @return string
   */
  protected function buildPluginDefinitionYaml($id, $label, $description, $provider, $plugin_manager_service_id) {
    return <<<EOT
{<span class="php-variable">$id</span>}:
  label: "{<span class="php-variable">$label</span>}"
  description: "{<span class="php-variable">$description</span>}"
  provider: {<span class="php-variable">$provider</span>}
  plugin_manager_service_id: {<span class="php-variable">$plugin_manager_service_id</span>}
EOT;
  }
  protected function setUp() : void {
    FileCacheFactory::setPrefix($this
      ->randomMachineName());
    $plugin_type_id_a = 'foo';
    $this->pluginTypeDefinitions[$plugin_type_id_a] = [
      'label' => 'Foo',
      'description' => 'This is Foo.',
      'provider' => 'foo',
      'plugin_manager_service_id' => 'plugin.manager.foo',
    ];
    $plugin_type_id_b = 'bar';
    $this->pluginTypeDefinitions[$plugin_type_id_b] = [
      'label' => 'Bar',
      'description' => 'I am Bar(t).',
      'provider' => 'bar',
      'plugin_manager_service_id' => 'plugin.manager.bar',
    ];
    $this->pluginManagers = [
      $plugin_type_id_a => $this
        ->createMock(PluginManagerInterface::class),
      $plugin_type_id_b => $this
        ->createMock(PluginManagerInterface::class),
    ];
    vfsStreamWrapper::register();
    $root = new vfsStreamDirectory('modules');
    vfsStreamWrapper::setRoot($root);
    $this->moduleHandler = $this
      ->createMock(ModuleHandlerInterface::class);
    $this->moduleHandler
      ->expects($this
      ->any())
      ->method('getModuleDirectories')
      ->willReturn(array(
      'module_a' => vfsStream::url('modules/module_a'),
      'module_b' => vfsStream::url('modules/module_b'),
    ));
    $class_resolver = $this
      ->createMock(ClassResolverInterface::class);
    $this->typedConfigurationManager = $this
      ->createMock(TypedConfigManagerInterface::class);
    $this->container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'class_resolver',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $class_resolver,
      ],
      [
        'config.typed',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->typedConfigurationManager,
      ],
      [
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this
          ->getStringTranslationStub(),
      ],
      [
        $this->pluginTypeDefinitions[$plugin_type_id_a]['plugin_manager_service_id'],
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->pluginManagers[$plugin_type_id_a],
      ],
      [
        $this->pluginTypeDefinitions[$plugin_type_id_b]['plugin_manager_service_id'],
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->pluginManagers[$plugin_type_id_b],
      ],
    ];
    $this->container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $url = vfsStream::url('modules');
    mkdir($url . '/module_a');
    file_put_contents($url . '/module_a/module_a.plugin_type.yml', $this
      ->buildPluginDefinitionYaml($plugin_type_id_a, $this->pluginTypeDefinitions[$plugin_type_id_a]['label'], $this->pluginTypeDefinitions[$plugin_type_id_a]['description'], $this->pluginTypeDefinitions[$plugin_type_id_a]['provider'], $this->pluginTypeDefinitions[$plugin_type_id_a]['plugin_manager_service_id']));
    mkdir($url . '/module_b');
    file_put_contents($url . '/module_b/module_b.plugin_type.yml', $this
      ->buildPluginDefinitionYaml($plugin_type_id_b, $this->pluginTypeDefinitions[$plugin_type_id_b]['label'], $this->pluginTypeDefinitions[$plugin_type_id_b]['description'], $this->pluginTypeDefinitions[$plugin_type_id_b]['provider'], $this->pluginTypeDefinitions[$plugin_type_id_b]['plugin_manager_service_id']));
    $this->sut = new PluginTypeManager($this->container, $this->moduleHandler);
  }

  /**
   * @covers ::__construct
   */
  public function testConstruct() {
    $this->sut = new PluginTypeManager($this->container, $this->moduleHandler);
    $this
      ->assertInstanceOf(PluginTypeManager::class, $this->sut);
  }

  /**
   * @covers ::hasPluginType
   *
   * @dataProvider providerHasPluginType
   */
  public function testHasPluginType($expected, $plugin_type_id, $module_name, $module_exists) {
    $modules = [];
    if ($module_exists) {
      $extension = $this
        ->getMockBuilder(Extension::class)
        ->disableOriginalConstructor()
        ->getMock();
      $extension
        ->expects($this
        ->atLeastOnce())
        ->method('getName')
        ->willReturn($module_name);
      $modules[] = $extension;
    }
    $this->moduleHandler
      ->expects($this
      ->any())
      ->method('getModuleList')
      ->willReturn($modules);
    $this->typedConfigurationManager
      ->expects($this
      ->any())
      ->method('hasConfigSchema')
      ->with(sprintf('plugin.plugin_configuration.%s.*', $plugin_type_id))
      ->willReturn(TRUE);
    $this
      ->assertSame($expected, $this->sut
      ->hasPluginType($plugin_type_id));
  }

  /**
   * Provides data to self::testHasPluginType().
   */
  public function providerHasPluginType() {
    $data = [];

    // This hardcoded the IDs in $this->pluginTypeDefinitions.
    foreach ([
      'foo',
      'bar',
    ] as $key) {
      $data[] = [
        TRUE,
        $key,
        $key,
        TRUE,
      ];
      $data[] = [
        FALSE,
        $key,
        $key,
        FALSE,
      ];
    }
    $data[] = [
      FALSE,
      $this
        ->randomMachineName(),
      $this
        ->randomMachineName(),
      TRUE,
    ];
    $data[] = [
      FALSE,
      $this
        ->randomMachineName(),
      $this
        ->randomMachineName(),
      FALSE,
    ];
    return $data;
  }

  /**
   * @covers ::getPluginType
   *
   * @dataProvider providerGetPluginType
   */
  public function testGetPluginType($expected_success, $plugin_type_id, $module_name, $module_exists) {
    $modules = [];
    if ($module_exists) {
      $extension = $this
        ->getMockBuilder(Extension::class)
        ->disableOriginalConstructor()
        ->getMock();
      $extension
        ->expects($this
        ->atLeastOnce())
        ->method('getName')
        ->willReturn($module_name);
      $modules[] = $extension;
    }
    $this->moduleHandler
      ->expects($this
      ->any())
      ->method('getModuleList')
      ->willReturn($modules);
    $this->typedConfigurationManager
      ->expects($this
      ->any())
      ->method('hasConfigSchema')
      ->with(sprintf('plugin.plugin_configuration.%s.*', $plugin_type_id))
      ->willReturn(TRUE);
    if ($expected_success) {
      $this
        ->assertInstanceOf(PluginTypeInterface::class, $this->sut
        ->getPluginType($plugin_type_id));
    }
    else {
      $this
        ->expectException('\\InvalidArgumentException');
      $this->sut
        ->getPluginType($plugin_type_id);
    }
  }

  /**
   * Provides data to self::testGetPluginType().
   */
  public function providerGetPluginType() {
    $data = [];

    // This hardcoded the IDs in $this->pluginTypeDefinitions.
    foreach ([
      'foo',
      'bar',
    ] as $key) {
      $data[] = [
        TRUE,
        $key,
        $key,
        TRUE,
      ];
      $data[] = [
        FALSE,
        $key,
        $key,
        FALSE,
      ];
    }
    $data[] = [
      FALSE,
      $this
        ->randomMachineName(),
      $this
        ->randomMachineName(),
      TRUE,
    ];
    $data[] = [
      FALSE,
      $this
        ->randomMachineName(),
      $this
        ->randomMachineName(),
      FALSE,
    ];
    return $data;
  }

  /**
   * @covers ::getPluginType
   */
  public function testGetPluginTypeWithInvalidPluginTypeId() {
    $this
      ->expectException(InvalidArgumentException::class);
    $this->moduleHandler
      ->expects($this
      ->any())
      ->method('getModuleList')
      ->willReturn([]);
    $this->sut
      ->getPluginType($this
      ->randomMachineName());
  }

  /**
   * @covers ::getPluginTypes
   */
  public function testGetPluginTypes() {
    $modules = array_map(function (array $plugin_type_definition) {
      $extension = $this
        ->getMockBuilder(Extension::class)
        ->disableOriginalConstructor()
        ->getMock();
      $extension
        ->expects($this
        ->atLeastOnce())
        ->method('getName')
        ->willReturn($plugin_type_definition['provider']);
      return $extension;
    }, $this->pluginTypeDefinitions);
    $this->moduleHandler
      ->expects($this
      ->any())
      ->method('getModuleList')
      ->willReturn($modules);
    $this->typedConfigurationManager
      ->expects($this
      ->any())
      ->method('hasConfigSchema')
      ->willReturn('TRUE');
    foreach ($this->sut
      ->getPluginTypes() as $plugin_type) {
      $this
        ->assertPluginTypeIntegrity($plugin_type
        ->getId(), $this->pluginTypeDefinitions[$plugin_type
        ->getId()], $this->pluginManagers[$plugin_type
        ->getId()], $plugin_type);
    }
  }

  /**
   * Asserts the integrity of a plugin type based on its definition.
   *
   * @param string $plugin_type_id
   * @param mixed[] $plugin_type_definition
   * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
   * @param mixed $plugin_type
   */
  protected function assertPluginTypeIntegrity($plugin_type_id, $plugin_type_definition, PluginManagerInterface $plugin_manager, $plugin_type) {
    $this
      ->assertInstanceOf(PluginTypeInterface::class, $plugin_type);
    $this
      ->assertSame($plugin_type_id, $plugin_type
      ->getId());
    $this
      ->assertSame($plugin_type_definition['label'], $plugin_type
      ->getLabel()
      ->getUntranslatedString());
    $this
      ->assertSame($plugin_type_definition['description'], $plugin_type
      ->getDescription()
      ->getUntranslatedString());
    $this
      ->assertSame($plugin_type_definition['provider'], $plugin_type
      ->getProvider());
    $this
      ->assertSame($plugin_manager, $plugin_type
      ->getPluginManager());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
PluginTypeManagerTest::$container protected property The service container.
PluginTypeManagerTest::$moduleHandler protected property The module handler.
PluginTypeManagerTest::$pluginManagers protected property The plugin type's plugin managers.
PluginTypeManagerTest::$pluginTypeDefinitions protected property The plugin type definitions.
PluginTypeManagerTest::$sut protected property The class under test.
PluginTypeManagerTest::$typedConfigurationManager protected property The typed configuration manager.
PluginTypeManagerTest::assertPluginTypeIntegrity protected function Asserts the integrity of a plugin type based on its definition.
PluginTypeManagerTest::buildPluginDefinitionYaml protected function Builds a plugin type definition file.
PluginTypeManagerTest::providerGetPluginType public function Provides data to self::testGetPluginType().
PluginTypeManagerTest::providerHasPluginType public function Provides data to self::testHasPluginType().
PluginTypeManagerTest::setUp protected function Overrides UnitTestCase::setUp
PluginTypeManagerTest::testConstruct public function @covers ::__construct
PluginTypeManagerTest::testGetPluginType public function @covers ::getPluginType
PluginTypeManagerTest::testGetPluginTypes public function @covers ::getPluginTypes
PluginTypeManagerTest::testGetPluginTypeWithInvalidPluginTypeId public function @covers ::getPluginType
PluginTypeManagerTest::testHasPluginType public function @covers ::hasPluginType
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.