function DevelGenerateManagerTest::testCreateInstance

Same name in other branches
  1. 8.x-1.x devel_generate/tests/src/DevelGenerateManagerTest.php \Drupal\Tests\devel_generate\DevelGenerateManagerTest::testCreateInstance()
  2. 4.x devel_generate/tests/src/Unit/DevelGenerateManagerTest.php \Drupal\Tests\devel_generate\Unit\DevelGenerateManagerTest::testCreateInstance()

Test creating an instance of the DevelGenerateManager.

File

devel_generate/tests/src/Unit/DevelGenerateManagerTest.php, line 48

Class

DevelGenerateManagerTest
@coversDefaultClass \Drupal\devel_generate\DevelGeneratePluginManager @group devel_generate

Namespace

Drupal\Tests\devel_generate\Unit

Code

public function testCreateInstance() : void {
    $namespaces = new \ArrayObject([
        'Drupal\\devel_generate_example' => realpath(__DIR__ . '/../../../modules/devel_generate_example/lib'),
    ]);
    $cache_backend = $this->createMock(CacheBackendInterface::class);
    $module_handler = $this->createMock(ModuleHandlerInterface::class);
    $entity_type_manager = $this->createMock(EntityTypeManager::class);
    $messenger = $this->createMock(MessengerInterface::class);
    $language_manager = $this->createMock(LanguageManagerInterface::class);
    $string_translation = $this->createMock(TranslationInterface::class);
    $entityFieldManager = $this->createMock(EntityFieldManagerInterface::class);
    $manager = new DevelGeneratePluginManager($namespaces, $cache_backend, $module_handler, $entity_type_manager, $messenger, $language_manager, $string_translation, $entityFieldManager);
    // Use reflection to set the protected discovery property.
    $reflection = new \ReflectionClass($manager);
    $property = $reflection->getProperty('discovery');
    $property->setValue($manager, $this->discovery);
    $container = new ContainerBuilder();
    $time = $this->createMock(TimeInterface::class);
    $container->set('entity_type.manager', $entity_type_manager);
    $container->set('messenger', $messenger);
    $container->set('language_manager', $language_manager);
    $container->set('module_handler', $module_handler);
    $container->set('string_translation', $string_translation);
    $container->set('entity_field.manager', $entityFieldManager);
    $container->set('datetime.time', $time);
    \Drupal::setContainer($container);
    $example_instance = $manager->createInstance('devel_generate_example');
    $plugin_def = $example_instance->getPluginDefinition();
    $this->assertInstanceOf(ExampleDevelGenerate::class, $example_instance);
    $this->assertArrayHasKey('url', $plugin_def);
    $this->assertTrue($plugin_def['url'] == 'devel_generate_example');
}