You are here

class ContainerBuilderTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/DependencyInjection/ContainerBuilderTest.php \Drupal\Tests\Core\DependencyInjection\ContainerBuilderTest
  2. 9 core/tests/Drupal/Tests/Core/DependencyInjection/ContainerBuilderTest.php \Drupal\Tests\Core\DependencyInjection\ContainerBuilderTest

@coversDefaultClass \Drupal\Core\DependencyInjection\ContainerBuilder @group DependencyInjection

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, PhpUnitWarnings

Expanded class hierarchy of ContainerBuilderTest

File

core/tests/Drupal/Tests/Core/DependencyInjection/ContainerBuilderTest.php, line 14

Namespace

Drupal\Tests\Core\DependencyInjection
View source
class ContainerBuilderTest extends UnitTestCase {

  /**
   * @covers ::get
   */
  public function testGet() {
    $container = new ContainerBuilder();
    $container
      ->register('bar', 'Drupal\\Tests\\Core\\DependencyInjection\\Fixture\\BarClass');
    $result = $container
      ->get('bar');
    $this
      ->assertInstanceOf(BarClass::class, $result);
  }

  /**
   * @covers ::set
   */
  public function testSet() {
    $container = new ContainerBuilder();
    $class = new BarClass();
    $container
      ->set('bar', $class);
    $this
      ->assertEquals('bar', $class->_serviceId);
  }

  /**
   * @covers ::set
   */
  public function testSetException() {
    $container = new ContainerBuilder();
    $class = new BarClass();
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('Service ID names must be lowercase: Bar');
    $container
      ->set('Bar', $class);
  }

  /**
   * @covers ::setParameter
   */
  public function testSetParameterException() {
    $container = new ContainerBuilder();
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('Parameter names must be lowercase: Buzz');
    $container
      ->setParameter('Buzz', 'buzz');
  }

  /**
   * @covers ::register
   */
  public function testRegisterException() {
    $container = new ContainerBuilder();
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('Service ID names must be lowercase: Bar');
    $container
      ->register('Bar');
  }

  /**
   * @covers ::register
   */
  public function testRegister() {
    $container = new ContainerBuilder();
    $service = $container
      ->register('bar');
    $this
      ->assertTrue($service
      ->isPublic());
  }

  /**
   * @covers ::setDefinition
   */
  public function testSetDefinition() {

    // Test a service with public set to true.
    $container = new ContainerBuilder();
    $definition = new Definition();
    $definition
      ->setPublic(TRUE);
    $service = $container
      ->setDefinition('foo', $definition);
    $this
      ->assertTrue($service
      ->isPublic());

    // Test a service with public set to false.
    $definition = new Definition();
    $definition
      ->setPublic(FALSE);
    $service = $container
      ->setDefinition('foo', $definition);
    $this
      ->assertFalse($service
      ->isPublic());
  }

  /**
   * @covers ::setAlias
   */
  public function testSetAlias() {
    $container = new ContainerBuilder();
    $container
      ->register('bar');
    $alias = $container
      ->setAlias('foo', 'bar');
    $this
      ->assertTrue($alias
      ->isPublic());
  }

  /**
   * Tests serialization.
   */
  public function testSerialize() {
    $container = new ContainerBuilder();
    $this
      ->expectException(\AssertionError::class);
    serialize($container);
  }

  /**
   * Tests constructor and resource tracking disabling.
   *
   * This test runs in a separate process to ensure the aliased class does not
   * affect any other tests.
   *
   * @runInSeparateProcess
   * @preserveGlobalState disabled
   */
  public function testConstructor() {
    class_alias(TestInterface::class, 'Symfony\\Component\\Config\\Resource\\ResourceInterface');
    $container = new ContainerBuilder();
    $this
      ->assertFalse($container
      ->isTrackingResources());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContainerBuilderTest::testConstructor public function Tests constructor and resource tracking disabling.
ContainerBuilderTest::testGet public function @covers ::get
ContainerBuilderTest::testRegister public function @covers ::register
ContainerBuilderTest::testRegisterException public function @covers ::register
ContainerBuilderTest::testSerialize public function Tests serialization.
ContainerBuilderTest::testSet public function @covers ::set
ContainerBuilderTest::testSetAlias public function @covers ::setAlias
ContainerBuilderTest::testSetDefinition public function @covers ::setDefinition
ContainerBuilderTest::testSetException public function @covers ::set
ContainerBuilderTest::testSetParameterException public function @covers ::setParameter
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 206
UnitTestCase::setUpBeforeClass public static function