You are here

class PluginLabelTest in Plugin 8.2

@coversDefaultClass \Drupal\plugin\Plugin\Field\FieldFormatter\PluginLabel

@group Plugin

Hierarchy

Expanded class hierarchy of PluginLabelTest

File

tests/src/Unit/Plugin/Field/FieldFormatter/PluginLabelTest.php, line 20

Namespace

Drupal\Tests\plugin\Unit\Plugin\Field\FieldFormatter
View source
class PluginLabelTest extends UnitTestCase {

  /**
   * The field definition.
   *
   * @var \Drupal\Core\Field\FieldDefinitionInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $fieldDefinition;

  /**
   * The class under test.
   *
   * @var \Drupal\plugin\Plugin\Field\FieldFormatter\PluginLabel
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->fieldDefinition = $this
      ->createMock(FieldDefinitionInterface::class);
    $this->sut = new PluginLabel('plugin_label', [], $this->fieldDefinition, [], $this
      ->randomMachineName(), $this
      ->randomMachineName(), []);
  }

  /**
   * @covers ::viewElements
   */
  public function testViewElements() {
    $plugin_definition_label_a = $this
      ->randomMachineName();
    $plugin_definition_a = $this
      ->createMock(PluginLabelDefinitionInterface::class);
    $plugin_definition_a
      ->expects($this
      ->atLeastOnce())
      ->method('getLabel')
      ->willReturn($plugin_definition_label_a);
    $plugin_definition_id_b = $this
      ->randomMachineName();
    $plugin_definition_b = $this
      ->createMock(PluginDefinitionInterface::class);
    $plugin_definition_b
      ->expects($this
      ->atLeastOnce())
      ->method('getId')
      ->willReturn($plugin_definition_id_b);
    $plugin_instance_a = $this
      ->createMock(PluginInspectionInterface::class);
    $plugin_instance_a
      ->expects($this
      ->atLeastOnce())
      ->method('getPluginDefinition')
      ->willReturn($plugin_definition_a);
    $plugin_instance_b = $this
      ->createMock(PluginInspectionInterface::class);
    $plugin_instance_b
      ->expects($this
      ->atLeastOnce())
      ->method('getPluginDefinition')
      ->willReturn($plugin_definition_b);
    $item_a = $this
      ->createMock(PluginCollectionItemInterface::class);
    $item_a
      ->expects($this
      ->atLeastOnce())
      ->method('getContainedPluginInstance')
      ->willReturn($plugin_instance_a);
    $item_b = $this
      ->createMock(PluginCollectionItemInterface::class);
    $item_b
      ->expects($this
      ->atLeastOnce())
      ->method('getContainedPluginInstance')
      ->willReturn($plugin_instance_b);

    /** @var \Drupal\plugin\Plugin\Field\FieldType\PluginCollectionItemInterface[]|\PHPUnit_Framework_MockObject_MockObject[] $items */
    $items = [
      $item_a,
      $item_b,
    ];
    $plugin_type = $this
      ->createMock(PluginTypeInterface::class);
    $plugin_type
      ->expects($this
      ->atLeastOnce())
      ->method('ensureTypedPluginDefinition')
      ->willReturnArgument(0);
    foreach ($items as $item) {
      $item
        ->expects($this
        ->atLeastOnce())
        ->method('getPluginType')
        ->willReturn($plugin_type);
    }
    $iterator = new \ArrayIterator($items);
    $item_list = $this
      ->getMockBuilder(PluginCollectionItemList::class)
      ->disableOriginalConstructor()
      ->setMethods([
      'getEntity',
      'getIterator',
    ])
      ->getMock();
    $item_list
      ->expects($this
      ->atLeastOnce())
      ->method('getIterator')
      ->willReturn($iterator);
    $expected_build = [
      [
        '#markup' => $plugin_definition_label_a,
      ],
      [
        '#markup' => $plugin_definition_id_b,
      ],
    ];
    $langcode = 'UA';
    $this
      ->assertSame($expected_build, $this->sut
      ->viewElements($item_list, $langcode));
  }

}

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.
PluginLabelTest::$fieldDefinition protected property The field definition.
PluginLabelTest::$sut protected property The class under test.
PluginLabelTest::setUp protected function Overrides UnitTestCase::setUp
PluginLabelTest::testViewElements public function @covers ::viewElements
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.