class ReverseContainerTest

Same name in other branches
  1. 10 core/tests/Drupal/Tests/Component/DependencyInjection/ReverseContainerTest.php \Drupal\Tests\Component\DependencyInjection\ReverseContainerTest
  2. 11.x core/tests/Drupal/Tests/Component/DependencyInjection/ReverseContainerTest.php \Drupal\Tests\Component\DependencyInjection\ReverseContainerTest

@runTestsInSeparateProcesses The reverse container uses a static to maintain information across container rebuilds.

@coversDefaultClass \Drupal\Component\DependencyInjection\ReverseContainer @group DependencyInjection

Hierarchy

  • class \Drupal\Tests\Component\DependencyInjection\ReverseContainerTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of ReverseContainerTest

File

core/tests/Drupal/Tests/Component/DependencyInjection/ReverseContainerTest.php, line 17

Namespace

Drupal\Tests\Component\DependencyInjection
View source
class ReverseContainerTest extends TestCase {
    
    /**
     * @covers ::getId
     */
    public function testGetId() : void {
        $container = new ContainerBuilder();
        $service = new \stdClass();
        $container->set('bar', $service);
        $reverse_container = new ReverseContainer($container);
        $this->assertSame('bar', $reverse_container->getId($service));
        $non_service = new \stdClass();
        $this->assertNull($reverse_container->getId($non_service));
        $this->assertSame('service_container', $reverse_container->getId($container));
    }
    
    /**
     * @covers ::recordContainer
     */
    public function testRecordContainer() : void {
        $container = new ContainerBuilder();
        $service = new \stdClass();
        $container->set('bar', $service);
        $reverse_container = new ReverseContainer($container);
        $reverse_container->recordContainer();
        $container = new ContainerBuilder();
        $reverse_container = new ReverseContainer($container);
        // New container does not have a bar service.
        $this->assertNull($reverse_container->getId($service));
        // Add the bar service to make the lookup based on the old object work as
        // expected.
        $container->set('bar', new \stdClass());
        $this->assertSame('bar', $reverse_container->getId($service));
    }
    
    /**
     * @covers ::recordContainer
     * @group legacy
     */
    public function testRecordContainerStringService() : void {
        $container = new ContainerBuilder();
        $service = new \stdClass();
        $container->set('bar', $service);
        // String services are deprecated in Symfony 4.4.
        $container->set('string_service', 'A string');
        $reverse_container = new ReverseContainer($container);
        $reverse_container->recordContainer();
        $container = new ContainerBuilder();
        $reverse_container = new ReverseContainer($container);
        // New container does not have a bar service.
        $this->assertNull($reverse_container->getId($service));
        // Add the bar service to make the lookup based on the old object work as
        // expected.
        $container->set('bar', new \stdClass());
        $this->assertSame('bar', $reverse_container->getId($service));
    }

}

Members

Title Sort descending Modifiers Object type Summary
ReverseContainerTest::testGetId public function @covers ::getId
ReverseContainerTest::testRecordContainer public function @covers ::recordContainer
ReverseContainerTest::testRecordContainerStringService public function @covers ::recordContainer
@group legacy

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