This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php

205 lines
8.7 KiB
PHP
Raw Normal View History

<?php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
2010-04-07 02:07:59 +01:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
class ContainerTest extends \PHPUnit_Framework_TestCase
{
2010-06-27 17:28:29 +01:00
/**
* @covers Symfony\Component\DependencyInjection\Container::__construct
2010-06-27 17:28:29 +01:00
*/
public function testConstructor()
{
$sc = new Container();
2010-06-27 17:28:29 +01:00
$this->assertEquals(spl_object_hash($sc), spl_object_hash($sc->get('service_container')), '__construct() automatically registers itself as a service');
2010-06-27 17:28:29 +01:00
$sc = new Container(new ParameterBag(array('foo' => 'bar')));
$this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '__construct() takes an array of parameters as its first argument');
}
2010-06-27 17:28:29 +01:00
/**
* @covers Symfony\Component\DependencyInjection\Container::freeze
2010-06-27 17:28:29 +01:00
*/
public function testFreeze()
{
2010-06-27 17:28:29 +01:00
$sc = new Container(new ParameterBag(array('foo' => 'bar')));
$sc->freeze();
$this->assertInstanceOf('Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag', $sc->getParameterBag(), '->freeze() changes the parameter bag to a FrozenParameterBag instance');
2010-06-27 17:28:29 +01:00
$this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '->freeze() copies the current parameters to the new parameter bag');
}
2010-06-27 17:28:29 +01:00
/**
* @covers Symfony\Component\DependencyInjection\Container::isFrozen
2010-06-27 17:28:29 +01:00
*/
public function testIsFrozen()
{
$sc = new Container(new ParameterBag(array('foo' => 'bar')));
$this->assertFalse($sc->isFrozen(), '->isFrozen() returns false if the parameters are not frozen');
$sc->freeze();
$this->assertTrue($sc->isFrozen(), '->isFrozen() returns true if the parameters are frozen');
}
2010-06-27 17:28:29 +01:00
/**
* @covers Symfony\Component\DependencyInjection\Container::getParameterBag
2010-06-27 17:28:29 +01:00
*/
public function testGetParameterBag()
{
$sc = new Container();
$this->assertEquals(array(), $sc->getParameterBag()->all(), '->getParameterBag() returns an empty array if no parameter has been defined');
}
2010-06-27 17:28:29 +01:00
/**
* @covers Symfony\Component\DependencyInjection\Container::setParameter
* @covers Symfony\Component\DependencyInjection\Container::getParameter
2010-06-27 17:28:29 +01:00
*/
public function testGetSetParameter()
{
2010-06-27 17:28:29 +01:00
$sc = new Container(new ParameterBag(array('foo' => 'bar')));
$sc->setParameter('bar', 'foo');
$this->assertEquals('foo', $sc->getParameter('bar'), '->setParameter() sets the value of a new parameter');
$sc->setParameter('foo', 'baz');
$this->assertEquals('baz', $sc->getParameter('foo'), '->setParameter() overrides previously set parameter');
$sc->setParameter('Foo', 'baz1');
$this->assertEquals('baz1', $sc->getParameter('foo'), '->setParameter() converts the key to lowercase');
$this->assertEquals('baz1', $sc->getParameter('FOO'), '->getParameter() converts the key to lowercase');
try {
$sc->getParameter('baba');
$this->fail('->getParameter() thrown an \InvalidArgumentException if the key does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
$this->assertEquals('The parameter "baba" must be defined.', $e->getMessage(), '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
}
}
2010-06-27 17:28:29 +01:00
/**
* @covers Symfony\Component\DependencyInjection\Container::getServiceIds
2010-06-27 17:28:29 +01:00
*/
public function testGetServiceIds()
{
2010-06-27 17:28:29 +01:00
$sc = new Container();
$sc->set('foo', $obj = new \stdClass());
$sc->set('bar', $obj = new \stdClass());
$this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids');
2010-06-27 17:28:29 +01:00
$sc = new ProjectServiceContainer();
$this->assertEquals(array('bar', 'foo_bar', 'foo.baz', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods');
}
2010-06-27 17:28:29 +01:00
/**
* @covers Symfony\Component\DependencyInjection\Container::__call
2010-06-27 17:28:29 +01:00
*/
public function testGetCall()
{
$sc = new Container();
2010-06-27 17:28:29 +01:00
$sc->set('foo_bar.foo', $foo = new \stdClass());
$this->assertEquals($foo, $sc->getFooBar_FooService(), '__call() finds services is the method is getXXXService()');
2010-06-27 17:28:29 +01:00
try {
$sc->getFooBar_Foo();
$this->fail('__call() throws a \BadMethodCallException exception if the method is not a service method');
} catch (\Exception $e) {
$this->assertInstanceOf('\BadMethodCallException', $e, '__call() throws a \BadMethodCallException exception if the method is not a service method');
$this->assertEquals('Call to undefined method Symfony\Component\DependencyInjection\Container::getFooBar_Foo.', $e->getMessage(), '__call() throws a \BadMethodCallException exception if the method is not a service method');
2010-06-27 17:28:29 +01:00
}
}
2010-06-27 17:28:29 +01:00
/**
* @covers Symfony\Component\DependencyInjection\Container::set
2010-06-27 17:28:29 +01:00
*/
public function testSet()
{
$sc = new Container();
2010-06-27 17:28:29 +01:00
$sc->set('foo', $foo = new \stdClass());
$this->assertEquals($foo, $sc->get('foo'), '->set() sets a service');
}
2010-06-27 17:28:29 +01:00
/**
* @covers Symfony\Component\DependencyInjection\Container::get
2010-06-27 17:28:29 +01:00
*/
public function testGet()
{
$sc = new ProjectServiceContainer();
2010-06-27 17:28:29 +01:00
$sc->set('foo', $foo = new \stdClass());
$this->assertEquals($foo, $sc->get('foo'), '->get() returns the service for the given id');
$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');
2010-06-27 17:28:29 +01:00
$sc->set('bar', $bar = new \stdClass());
$this->assertEquals(spl_object_hash($sc->get('bar')), spl_object_hash($bar), '->getServiceIds() prefers to return a service defined with a getXXXService() method than one defined with set()');
try {
2010-06-27 17:28:29 +01:00
$sc->get(new \stdClass());
$this->fail('->get() throws a \InvalidArgumentException exception if the service id is not a string');
} catch (\Exception $e) {
2010-06-27 17:28:29 +01:00
$this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws a \InvalidArgumentException exception if the service id is not a string');
}
try {
2010-06-27 17:28:29 +01:00
$sc->get('');
$this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
} catch (\Exception $e) {
2010-06-27 17:28:29 +01:00
$this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws a \InvalidArgumentException exception if the service is empty');
$this->assertEquals('The service "" does not exist.', $e->getMessage(), '->get() throws a \InvalidArgumentException exception if the service is empty');
}
2010-06-27 17:28:29 +01:00
$this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE));
}
2010-06-27 17:28:29 +01:00
/**
* @covers Symfony\Component\DependencyInjection\Container::has
2010-06-27 17:28:29 +01:00
*/
public function testHas()
{
2010-06-27 17:28:29 +01:00
$sc = new ProjectServiceContainer();
$sc->set('foo', new \stdClass());
$this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist');
$this->assertTrue($sc->has('foo'), '->has() returns true if the service exists');
$this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
$this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
$this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
}
}
class ProjectServiceContainer extends Container
{
public $__bar, $__foo_bar, $__foo_baz;
public function __construct()
{
parent::__construct();
$this->__bar = new \stdClass();
$this->__foo_bar = new \stdClass();
$this->__foo_baz = new \stdClass();
}
protected function getBarService()
{
return $this->__bar;
}
protected function getFooBarService()
{
return $this->__foo_bar;
}
protected function getFoo_BazService()
{
return $this->__foo_baz;
}
}