function YamlFileLoaderTest::testParseDefinitionsWithProvider

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/DependencyInjection/YamlFileLoaderTest.php \Drupal\Tests\Core\DependencyInjection\YamlFileLoaderTest::testParseDefinitionsWithProvider()
  2. 8.9.x core/tests/Drupal/Tests/Core/DependencyInjection/YamlFileLoaderTest.php \Drupal\Tests\Core\DependencyInjection\YamlFileLoaderTest::testParseDefinitionsWithProvider()
  3. 11.x core/tests/Drupal/Tests/Core/DependencyInjection/YamlFileLoaderTest.php \Drupal\Tests\Core\DependencyInjection\YamlFileLoaderTest::testParseDefinitionsWithProvider()

File

core/tests/Drupal/Tests/Core/DependencyInjection/YamlFileLoaderTest.php, line 31

Class

YamlFileLoaderTest
@coversDefaultClass \Drupal\Core\DependencyInjection\YamlFileLoader @group DependencyInjection

Namespace

Drupal\Tests\Core\DependencyInjection

Code

public function testParseDefinitionsWithProvider() : void {
    $yml = <<<YAML
services:
  example_service_1:
    class: \\Drupal\\Core\\ExampleClass
  example_service_2: '@example_service_1'
  example_private_service:
    class: \\Drupal\\Core\\ExampleClass
    public: false
  Drupal\\Core\\ExampleClass: ~
  example_tagged_iterator:
    class: \\Drupal\\Core\\ExampleClass
    arguments: [!tagged_iterator foo.bar]"
  example_service_closure:
    class: \\Drupal\\Core\\ExampleClass
    arguments: [!service_closure '@example_service_1']"
YAML;
    vfsStream::setup('drupal', NULL, [
        'modules' => [
            'example' => [
                'example.yml' => $yml,
            ],
        ],
    ]);
    $builder = new ContainerBuilder();
    $yaml_file_loader = new YamlFileLoader($builder);
    $yaml_file_loader->load('vfs://drupal/modules/example/example.yml');
    $this->assertEquals([
        '_provider' => [
            [
                'provider' => 'example',
            ],
        ],
    ], $builder->getDefinition('example_service_1')
        ->getTags());
    $this->assertEquals('example_service_1', $builder->getAlias('example_service_2')
        ->__toString());
    $this->assertTrue($builder->getDefinition('example_service_1')
        ->isPublic());
    $this->assertFalse($builder->getDefinition('example_private_service')
        ->isPublic());
    $builder->compile();
    $this->assertTrue($builder->has('example_service_1'));
    $this->assertFalse($builder->has('example_private_service'));
    $this->assertTrue($builder->has('Drupal\\Core\\ExampleClass'));
    $this->assertSame('Drupal\\Core\\ExampleClass', $builder->getDefinition('Drupal\\Core\\ExampleClass')
        ->getClass());
    $this->assertInstanceOf(TaggedIteratorArgument::class, $builder->getDefinition('example_tagged_iterator')
        ->getArgument(0));
    // Test service closures.
    $service_closure = $builder->getDefinition('example_service_closure')
        ->getArgument(0);
    $this->assertInstanceOf(ServiceClosureArgument::class, $service_closure);
    $ref = $service_closure->getValues()[0];
    $this->assertInstanceOf(Reference::class, $ref);
    $this->assertEquals('example_service_1', $ref);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.