You are here

public function ContainerTest::testGet in Zircon Profile 8.0

Same name in this branch
  1. 8.0 vendor/symfony/dependency-injection/Tests/ContainerTest.php \Symfony\Component\DependencyInjection\Tests\ContainerTest::testGet()
  2. 8.0 core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php \Drupal\Tests\Component\DependencyInjection\ContainerTest::testGet()
Same name and namespace in other branches
  1. 8 vendor/symfony/dependency-injection/Tests/ContainerTest.php \Symfony\Component\DependencyInjection\Tests\ContainerTest::testGet()

@covers Symfony\Component\DependencyInjection\Container::get

File

vendor/symfony/dependency-injection/Tests/ContainerTest.php, line 215

Class

ContainerTest

Namespace

Symfony\Component\DependencyInjection\Tests

Code

public function testGet() {
  $sc = new ProjectServiceContainer();
  $sc
    ->set('foo', $foo = new \stdClass());
  $this
    ->assertEquals($foo, $sc
    ->get('foo'), '->get() returns the service for the given id');
  $this
    ->assertEquals($foo, $sc
    ->get('Foo'), '->get() returns the service for the given id, and converts id to lowercase');
  $this
    ->assertEquals($sc->__bar, $sc
    ->get('bar'), '->get() returns the service for the given id');
  $this
    ->assertEquals($sc->__foo_bar, $sc
    ->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
  $this
    ->assertEquals($sc->__foo_baz, $sc
    ->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
  $this
    ->assertEquals($sc->__foo_baz, $sc
    ->get('foo\\baz'), '->get() returns the service if a get*Method() is defined');
  $sc
    ->set('bar', $bar = new \stdClass());
  $this
    ->assertEquals($bar, $sc
    ->get('bar'), '->get() prefers to return a service defined with set() than one defined with a getXXXMethod()');
  try {
    $sc
      ->get('');
    $this
      ->fail('->get() throws a \\InvalidArgumentException exception if the service is empty');
  } catch (\Exception $e) {
    $this
      ->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\ServiceNotFoundException', $e, '->get() throws a ServiceNotFoundException exception if the service is empty');
  }
  $this
    ->assertNull($sc
    ->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service is empty');
}