You are here

public function ListPluginsTest::testExecute in Plugin 8.2

@covers ::execute

File

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

Class

ListPluginsTest
@coversDefaultClass \Drupal\plugin\Controller\ListPlugins

Namespace

Drupal\Tests\plugin\Unit\Controller

Code

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']);
}