You are here

public function ContainerTest::testGet in Service Container 7

Same name and namespace in other branches
  1. 7.2 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Tests/ContainerTest.php \Symfony\Component\DependencyInjection\Tests\ContainerTest::testGet()

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

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/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');
}