You are here

public function ReflectionFactoryTest::testCreateInstance in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Component/Plugin/Factory/ReflectionFactoryTest.php \Drupal\Tests\Component\Plugin\Factory\ReflectionFactoryTest::testCreateInstance()

@covers ::createInstance @dataProvider providerGetInstanceArguments

File

core/tests/Drupal/Tests/Component/Plugin/Factory/ReflectionFactoryTest.php, line 88
Contains \Drupal\Tests\Component\Plugin\Factory\ReflectionFactoryTest.

Class

ReflectionFactoryTest
@group Plugin @coversDefaultClass \Drupal\Component\Plugin\Factory\ReflectionFactory

Namespace

Drupal\Tests\Component\Plugin\Factory

Code

public function testCreateInstance($expected, $reflector_name, $plugin_id, $plugin_definition, $configuration) {

  // Create a mock DiscoveryInterface which can return our plugin definition.
  $mock_discovery = $this
    ->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface')
    ->setMethods([
    'getDefinition',
    'getDefinitions',
    'hasDefinition',
  ])
    ->getMock();
  $mock_discovery
    ->expects($this
    ->never())
    ->method('getDefinitions');
  $mock_discovery
    ->expects($this
    ->never())
    ->method('hasDefinition');
  $mock_discovery
    ->expects($this
    ->once())
    ->method('getDefinition')
    ->willReturn($plugin_definition);

  // Create a stub ReflectionFactory object. We use StubReflectionFactory
  // because createInstance() has a dependency on a static method.
  // StubReflectionFactory overrides this static method.
  $reflection_factory = new StubReflectionFactory($mock_discovery);

  // Finally test that createInstance() returns an object of the class we
  // want.
  $this
    ->assertInstanceOf($reflector_name, $reflection_factory
    ->createInstance($plugin_id));
}