You are here

class ListPluginsTest in Plugin 8.2

@coversDefaultClass \Drupal\plugin\Controller\ListPlugins

@group Plugin

Hierarchy

Expanded class hierarchy of ListPluginsTest

File

tests/src/Unit/Controller/ListPluginsTest.php, line 25

Namespace

Drupal\Tests\plugin\Unit\Controller
View source
class ListPluginsTest extends UnitTestCase {

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

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

  /**
   * The class resolver.
   *
   * @var \Drupal\Core\DependencyInjection\ClassResolverInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $classResolver;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $currentUser;

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

  /**
   * The string translator.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $stringTranslation;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->container = $this
      ->prophesize(ContainerInterface::class);
    $this->classResolver = $this
      ->createMock(ClassResolverInterface::class);
    $this->moduleHandler = $this
      ->createMock(ModuleHandlerInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->sut = new ListPlugins($this->stringTranslation, $this->moduleHandler, $this->classResolver);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'class_resolver',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->classResolver,
      ],
      [
        'module_handler',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->moduleHandler,
      ],
      [
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = ListPlugins::create($container);
    $this
      ->assertInstanceOf(ListPlugins::class, $sut);
  }

  /**
   * @covers ::title
   */
  public function testTitle() {
    $class_resolver = $this
      ->createMock(ClassResolverInterface::class);
    $typed_config_manager = $this
      ->createMock(TypedConfigManagerInterface::class);
    $typed_config_manager
      ->expects($this
      ->atLeastOnce())
      ->method('hasConfigSchema')
      ->willReturn(TRUE);
    $plugin_type_id = $this
      ->randomMachineName();
    $plugin_type_label = $this
      ->randomMachineName();
    $plugin_type_definition = [
      'id' => $plugin_type_id,
      'label' => $plugin_type_label,
      'provider' => $this
        ->randomMachineName(),
      'plugin_manager_service_id' => 'foo.bar',
    ];
    $plugin_type = new PluginType($plugin_type_definition, $this->container
      ->reveal(), $this->stringTranslation, $class_resolver, $typed_config_manager);
    $title = $this->sut
      ->title($plugin_type);
    $this
      ->assertStringContainsString($plugin_type_label, (string) $title);
  }

  /**
   * @covers ::execute
   */
  public function testExecute() {
    $plugin_manager = $this
      ->createMock(PluginManagerInterface::class);
    $plugin_definition_id_a = $this
      ->randomMachineName();
    $plugin_definition_label_a = $this
      ->randomMachineName();
    $plugin_definition_a = $this
      ->createMock(PluginLabelDefinitionInterface::class);
    $plugin_definition_a
      ->expects($this
      ->atLeastOnce())
      ->method('getId')
      ->willReturn($plugin_definition_id_a);
    $plugin_definition_a
      ->expects($this
      ->atLeastOnce())
      ->method('getLabel')
      ->willReturn($plugin_definition_label_a);
    $plugin_definition_id_b = $this
      ->randomMachineName();
    $plugin_definition_description_b = $this
      ->randomMachineName();
    $plugin_definition_b = $this
      ->createMock(PluginDescriptionDefinitionInterface::class);
    $plugin_definition_b
      ->expects($this
      ->atLeastOnce())
      ->method('getId')
      ->willReturn($plugin_definition_id_b);
    $plugin_definition_b
      ->expects($this
      ->atLeastOnce())
      ->method('getDescription')
      ->willReturn($plugin_definition_description_b);
    $plugin_definition_id_c = $this
      ->randomMachineName();
    $plugin_definition_operations_c = [
      'foo' => [
        'title' => 'Foo',
        'url' => new Url('foo'),
      ],
    ];
    $plugin_definition_operations_provider_c = $this
      ->createMock(PluginOperationsProviderInterface::class);
    $plugin_definition_operations_provider_c
      ->expects($this
      ->atLeastOnce())
      ->method('getOperations')
      ->with($plugin_definition_id_c)
      ->willReturn($plugin_definition_operations_c);
    $plugin_definition_c = $this
      ->createMock(PluginOperationsProviderDefinitionInterface::class);
    $plugin_definition_c
      ->expects($this
      ->atLeastOnce())
      ->method('getId')
      ->willReturn($plugin_definition_id_c);
    $plugin_definition_c
      ->expects($this
      ->atLeastOnce())
      ->method('getOperationsProviderClass')
      ->willReturn(get_class($plugin_definition_operations_provider_c));
    $plugin_definitions = [
      $plugin_definition_id_a => $plugin_definition_a,
      $plugin_definition_id_b => $plugin_definition_b,
      $plugin_definition_id_c => $plugin_definition_c,
    ];
    $this->classResolver
      ->expects($this
      ->atLeastOnce())
      ->method('getInstanceFromDefinition')
      ->with(get_class($plugin_definition_operations_provider_c))
      ->willReturn($plugin_definition_operations_provider_c);
    $plugin_manager
      ->expects($this
      ->atLeastOnce())
      ->method('getDefinitions')
      ->willReturn($plugin_definitions);
    $plugin_type = $this
      ->createMock(PluginTypeInterface::class);
    $plugin_type
      ->expects($this
      ->atLeastOnce())
      ->method('ensureTypedPluginDefinition')
      ->willReturnArgument(0);
    $plugin_type
      ->expects($this
      ->atLeastOnce())
      ->method('getPluginManager')
      ->willReturn($plugin_manager);
    $build = $this->sut
      ->execute($plugin_type);
    $this
      ->assertSame($plugin_definition_id_a, $build[$plugin_definition_id_a]['id']['#markup']);
    $this
      ->assertSame($plugin_definition_label_a, (string) $build[$plugin_definition_id_a]['label']['#markup']);
    $this
      ->assertNull($build[$plugin_definition_id_a]['description']['#markup']);
    $this
      ->assertSame([], $build[$plugin_definition_id_a]['operations']['#links']);
    $this
      ->assertSame($plugin_definition_id_b, $build[$plugin_definition_id_b]['id']['#markup']);
    $this
      ->assertEmpty($build[$plugin_definition_id_b]['label']['#markup']);
    $this
      ->assertSame($plugin_definition_description_b, (string) $build[$plugin_definition_id_b]['description']['#markup']);
    $this
      ->assertSame([], $build[$plugin_definition_id_b]['operations']['#links']);
    $this
      ->assertSame($plugin_definition_id_b, $build[$plugin_definition_id_b]['id']['#markup']);
    $this
      ->assertEmpty($build[$plugin_definition_id_b]['label']['#markup']);
    $this
      ->assertSame($plugin_definition_description_b, (string) $build[$plugin_definition_id_b]['description']['#markup']);
    $this
      ->assertSame($plugin_definition_operations_c, $build[$plugin_definition_id_c]['operations']['#links']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ListPluginsTest::$classResolver protected property The class resolver.
ListPluginsTest::$container protected property The service container.
ListPluginsTest::$currentUser protected property The current user.
ListPluginsTest::$moduleHandler protected property The module handler.
ListPluginsTest::$stringTranslation protected property The string translator.
ListPluginsTest::$sut protected property The class under test.
ListPluginsTest::setUp protected function Overrides UnitTestCase::setUp
ListPluginsTest::testCreate function @covers ::create @covers ::__construct
ListPluginsTest::testExecute public function @covers ::execute
ListPluginsTest::testTitle public function @covers ::title
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.
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.