You are here

public function StaticDiscoveryDecoratorTest::testGetDefinition in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Component/Plugin/Discovery/StaticDiscoveryDecoratorTest.php \Drupal\Tests\Component\Plugin\Discovery\StaticDiscoveryDecoratorTest::testGetDefinition()

@covers ::getDefinition @dataProvider providerGetDefinition

File

core/tests/Drupal/Tests/Component/Plugin/Discovery/StaticDiscoveryDecoratorTest.php, line 66
Contains \Drupal\Tests\Component\Plugin\Discovery\StaticDiscoveryDecoratorTest.

Class

StaticDiscoveryDecoratorTest
@group Plugin @coversDefaultClass Drupal\Component\Plugin\Discovery\StaticDiscoveryDecorator

Namespace

Drupal\Tests\Component\Plugin\Discovery

Code

public function testGetDefinition($expected, $has_register_definitions, $exception_on_invalid, $definitions, $base_plugin_id) {

  // Mock our StaticDiscoveryDecorator.
  $mock_decorator = $this
    ->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\StaticDiscoveryDecorator')
    ->disableOriginalConstructor()
    ->setMethods(array(
    'registeredDefintionCallback',
  ))
    ->getMock();

  // Set up the ::$registerDefinitions property.
  $ref_register_definitions = new \ReflectionProperty($mock_decorator, 'registerDefinitions');
  $ref_register_definitions
    ->setAccessible(TRUE);
  if ($has_register_definitions) {

    // Set the callback object on the mocked decorator.
    $ref_register_definitions
      ->setValue($mock_decorator, array(
      $this
        ->getRegisterDefinitionsCallback(),
      'registerDefinitionsCallback',
    ));
  }
  else {

    // There should be no registerDefinitions callback.
    $ref_register_definitions
      ->setValue($mock_decorator, NULL);
  }

  // Set up ::$definitions to an empty array.
  $ref_definitions = new \ReflectionProperty($mock_decorator, 'definitions');
  $ref_definitions
    ->setAccessible(TRUE);
  $ref_definitions
    ->setValue($mock_decorator, array());

  // Mock a decorated object.
  $mock_decorated = $this
    ->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface')
    ->setMethods(array(
    'getDefinitions',
  ))
    ->getMockForAbstractClass();

  // Return our definitions from getDefinitions().
  $mock_decorated
    ->expects($this
    ->once())
    ->method('getDefinitions')
    ->willReturn($definitions);

  // Set up ::$decorated to our mocked decorated object.
  $ref_decorated = new \ReflectionProperty($mock_decorator, 'decorated');
  $ref_decorated
    ->setAccessible(TRUE);
  $ref_decorated
    ->setValue($mock_decorator, $mock_decorated);
  if ($exception_on_invalid) {
    $this
      ->setExpectedException('Drupal\\Component\\Plugin\\Exception\\PluginNotFoundException');
  }

  // Exercise getDefinition(). It calls parent::getDefinition().
  $this
    ->assertEquals($expected, $mock_decorator
    ->getDefinition($base_plugin_id, $exception_on_invalid));
}