You are here

class PluginCollectionItemDeriverTest in Plugin 8.2

@coversDefaultClass \Drupal\plugin\Plugin\Field\FieldType\PluginCollectionItemDeriver

@group Plugin

Hierarchy

Expanded class hierarchy of PluginCollectionItemDeriverTest

File

tests/src/Unit/Plugin/Field/FieldType/PluginCollectionItemDeriverTest.php, line 19

Namespace

Drupal\Tests\plugin\Unit\Plugin\Field\FieldType
View source
class PluginCollectionItemDeriverTest extends UnitTestCase {

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

  /**
   * The plugin type manager.
   *
   * @var \Drupal\plugin\PluginType\PluginTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $pluginTypeManager;

  /**
   * The class under test.
   *
   * @var \Drupal\plugin\Plugin\Field\FieldType\PluginCollectionItemDeriver
   */
  protected $sut;
  protected function setUp() : void {
    parent::setUp();
    $this->container = $this
      ->prophesize(ContainerInterface::class);
    $this->pluginTypeManager = $this
      ->createMock(PluginTypeManagerInterface::class);
    $this->sut = new PluginCollectionItemDeriver($this->pluginTypeManager);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'plugin.plugin_type_manager',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->pluginTypeManager,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = PluginCollectionItemDeriver::create($container, $this
      ->randomMachineName());
    $this
      ->assertInstanceOf(PluginCollectionItemDeriver::class, $sut);
  }

  /**
   * @covers ::getDerivativeDefinitions
   */
  function testGetDerivativeDefinitions() {
    $string_translation = $this
      ->getStringTranslationStub();
    $class_resolver = $this
      ->createMock(ClassResolverInterface::class);
    $typed_config_manager = $this
      ->createMock(TypedConfigManagerInterface::class);
    $typed_config_manager
      ->expects($this
      ->atLeastOnce())
      ->method('hasConfigSchema')
      ->willReturn(TRUE);
    $provider = $this
      ->randomMachineName();
    $plugin_type_id_a = $this
      ->randomMachineName();
    $plugin_type_label_a = $this
      ->randomMachineName();
    $plugin_type_description_a = $this
      ->randomMachineName();
    $plugin_type_definition_a = [
      'id' => $plugin_type_id_a,
      'label' => $plugin_type_label_a,
      'description' => $plugin_type_description_a,
      'provider' => $this
        ->randomMachineName(),
      'plugin_manager_service_id' => 'foo.bar',
    ];
    $plugin_type_a = new PluginType($plugin_type_definition_a, $this->container
      ->reveal(), $string_translation, $class_resolver, $typed_config_manager);
    $plugin_type_id_b = $this
      ->randomMachineName();
    $plugin_type_label_b = $this
      ->randomMachineName();
    $plugin_type_description_b = '';
    $plugin_type_definition_b = [
      'id' => $plugin_type_id_b,
      'label' => $plugin_type_label_b,
      'description' => $plugin_type_description_b,
      'provider' => $this
        ->randomMachineName(),
      'plugin_manager_service_id' => 'foo.bar',
    ];
    $plugin_type_b = new PluginType($plugin_type_definition_b, $this->container
      ->reveal(), $string_translation, $class_resolver, $typed_config_manager);
    $plugin_types = [
      $plugin_type_a,
      $plugin_type_b,
    ];
    $this->pluginTypeManager
      ->expects($this
      ->atLeastOnce())
      ->method('getPluginTypes')
      ->willReturn($plugin_types);
    $base_plugin_definition = [
      'provider' => $provider,
    ];
    $derivative_definitions = $this->sut
      ->getDerivativeDefinitions($base_plugin_definition);
    $this
      ->assertSame($plugin_type_label_a, (string) $derivative_definitions[$plugin_type_id_a]['label']);
    $this
      ->assertSame($plugin_type_description_a, (string) $derivative_definitions[$plugin_type_id_a]['description']);
    $this
      ->assertSame($provider, $derivative_definitions[$plugin_type_id_a]['provider']);
    $this
      ->assertSame($plugin_type_id_a, $derivative_definitions[$plugin_type_id_a]['plugin_type_id']);
    $this
      ->assertSame($plugin_type_label_b, (string) $derivative_definitions[$plugin_type_id_b]['label']);
    $this
      ->assertSame($plugin_type_description_b, (string) $derivative_definitions[$plugin_type_id_b]['description']);
    $this
      ->assertSame($provider, $derivative_definitions[$plugin_type_id_b]['provider']);
    $this
      ->assertSame($plugin_type_id_b, $derivative_definitions[$plugin_type_id_b]['plugin_type_id']);
  }

}

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.
PluginCollectionItemDeriverTest::$container protected property The service container.
PluginCollectionItemDeriverTest::$pluginTypeManager protected property The plugin type manager.
PluginCollectionItemDeriverTest::$sut protected property The class under test.
PluginCollectionItemDeriverTest::setUp protected function Overrides UnitTestCase::setUp
PluginCollectionItemDeriverTest::testCreate function @covers ::create @covers ::__construct
PluginCollectionItemDeriverTest::testGetDerivativeDefinitions function @covers ::getDerivativeDefinitions
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.