[DependencyInjection] added some tests for PhpDumper when the container is compiled

This commit is contained in:
Fabien Potencier 2013-01-05 09:50:11 +01:00
parent 3827e3ed1e
commit e939a4236c
7 changed files with 228 additions and 19 deletions

View File

@ -96,10 +96,17 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
public function testAddService()
{
// without compilation
$container = include self::$fixturesPath.'/containers/container9.php';
$dumper = new PhpDumper($container);
$this->assertEquals(str_replace('%path%', str_replace('\\','\\\\',self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9.php')), $dumper->dump(), '->dump() dumps services');
// with compilation
$container = include self::$fixturesPath.'/containers/container9.php';
$container->compile();
$dumper = new PhpDumper($container);
$this->assertEquals(str_replace('%path%', str_replace('\\','\\\\',self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9_compiled.php')), $dumper->dump(), '->dump() dumps services');
$dumper = new PhpDumper($container = new ContainerBuilder());
$container->register('foo', 'FooClass')->addArgument(new \stdClass());
try {

View File

@ -14,9 +14,8 @@ $container->
addTag('foo', array('bar' => 'bar'))->
setFactoryClass('FooClass')->
setFactoryMethod('getInstance')->
setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, new Reference('service_container')))->
setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'foobar' => '%foo%'), true, new Reference('service_container')))->
setProperties(array('foo' => 'bar', 'moo' => new Reference('foo.baz')))->
setScope('prototype')->
addMethodCall('setBar', array(new Reference('bar')))->
addMethodCall('initialize')->
setConfigurator('sc_configure')
@ -33,7 +32,10 @@ $container->
setFactoryMethod('getInstance')->
setConfigurator(array('%baz_class%', 'configureStatic1'))
;
$container->register('foo_bar', '%foo_class%');
$container->
register('foo_bar', '%foo_class%')->
setScope('prototype')
;
$container->getParameterBag()->clear();
$container->getParameterBag()->add(array(
'baz_class' => 'BazClass',
@ -50,7 +52,7 @@ $container->
addMethodCall('setBar', array(new Reference('foobaz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))
;
$container->
register('factory_service')->
register('factory_service', 'Bar')->
setFactoryService('foo.baz')->
setFactoryMethod('getInstance')
;

View File

@ -3,12 +3,12 @@ digraph sc {
node [fontsize="11" fontname="Arial" shape="record"];
edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];
node_foo [label="foo (alias_for_foo)\nFooClass\n", shape=record, fillcolor="#eeeeee", style="dotted"];
node_foo [label="foo (alias_for_foo)\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_bar [label="bar\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_foo_baz [label="foo.baz\nBazClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_foo_bar [label="foo_bar\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_foo_bar [label="foo_bar\nFooClass\n", shape=record, fillcolor="#eeeeee", style="dotted"];
node_method_call1 [label="method_call1\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_factory_service [label="factory_service\n\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_factory_service [label="factory_service\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"];
node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"];
node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"];

View File

@ -46,7 +46,7 @@ class ProjectServiceContainer extends Container
* This service is shared.
* This method always returns the same instance of the service.
*
* @return Object An instance returned by foo.baz::getInstance().
* @return Bar A Bar instance.
*/
protected function getFactoryServiceService()
{
@ -56,13 +56,16 @@ class ProjectServiceContainer extends Container
/**
* Gets the 'foo' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return FooClass A FooClass instance.
*/
protected function getFooService()
{
$a = $this->get('foo.baz');
$instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo'), 'bar' => $this->getParameter('foo')), true, $this);
$this->services['foo'] = $instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo'), 'foobar' => $this->getParameter('foo')), true, $this);
$instance->setBar($this->get('bar'));
$instance->initialize();
@ -93,15 +96,12 @@ class ProjectServiceContainer extends Container
/**
* Gets the 'foo_bar' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return Object A %foo_class% instance.
*/
protected function getFooBarService()
{
$class = $this->getParameter('foo_class');
return $this->services['foo_bar'] = new $class();
return new $class();
}
/**

View File

@ -0,0 +1,199 @@
<?php
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
/**
* ProjectServiceContainer
*
* This class has been auto-generated
* by the Symfony Dependency Injection Component.
*/
class ProjectServiceContainer extends Container
{
/**
* Constructor.
*/
public function __construct()
{
$this->parameters = $this->getDefaultParameters();
$this->services =
$this->scopedServices =
$this->scopeStacks = array();
$this->set('service_container', $this);
$this->scopes = array();
$this->scopeChildren = array();
}
/**
* Gets the 'bar' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return FooClass A FooClass instance.
*/
protected function getBarService()
{
$this->services['bar'] = $instance = new \FooClass('foo', $this->get('foo.baz'), $this->getParameter('foo_bar'));
$this->get('foo.baz')->configure($instance);
return $instance;
}
/**
* Gets the 'factory_service' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return Bar A Bar instance.
*/
protected function getFactoryServiceService()
{
return $this->services['factory_service'] = $this->get('foo.baz')->getInstance();
}
/**
* Gets the 'foo' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return FooClass A FooClass instance.
*/
protected function getFooService()
{
$a = $this->get('foo.baz');
$this->services['foo'] = $instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $a, array('bar' => 'foo is bar', 'foobar' => 'bar'), true, $this);
$instance->setBar($this->get('bar'));
$instance->initialize();
$instance->foo = 'bar';
$instance->moo = $a;
sc_configure($instance);
return $instance;
}
/**
* Gets the 'foo.baz' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return BazClass A BazClass instance.
*/
protected function getFoo_BazService()
{
$this->services['foo.baz'] = $instance = call_user_func(array('BazClass', 'getInstance'));
call_user_func(array('BazClass', 'configureStatic1'), $instance);
return $instance;
}
/**
* Gets the 'foo_bar' service.
*
* @return FooClass A FooClass instance.
*/
protected function getFooBarService()
{
return new \FooClass();
}
/**
* Gets the 'method_call1' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return FooClass A FooClass instance.
*/
protected function getMethodCall1Service()
{
require_once '%path%foo.php';
$this->services['method_call1'] = $instance = new \FooClass();
$instance->setBar($this->get('foo'));
$instance->setBar(NULL);
return $instance;
}
/**
* Gets the alias_for_foo service alias.
*
* @return FooClass An instance of the foo service
*/
protected function getAliasForFooService()
{
return $this->get('foo');
}
/**
* {@inheritdoc}
*/
public function getParameter($name)
{
$name = strtolower($name);
if (!array_key_exists($name, $this->parameters)) {
throw new \InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
}
return $this->parameters[$name];
}
/**
* {@inheritdoc}
*/
public function hasParameter($name)
{
return array_key_exists(strtolower($name), $this->parameters);
}
/**
* {@inheritdoc}
*/
public function setParameter($name, $value)
{
throw new \LogicException('Impossible to call set() on a frozen ParameterBag.');
}
/**
* {@inheritDoc}
*/
public function getParameterBag()
{
if (null === $this->parameterBag) {
$this->parameterBag = new FrozenParameterBag($this->parameters);
}
return $this->parameterBag;
}
/**
* Gets the default parameters.
*
* @return array An array of the default parameters
*/
protected function getDefaultParameters()
{
return array(
'baz_class' => 'BazClass',
'foo_class' => 'FooClass',
'foo' => 'bar',
);
}
}

View File

@ -6,14 +6,14 @@
<parameter key="foo">bar</parameter>
</parameters>
<services>
<service id="foo" class="FooClass" factory-method="getInstance" scope="prototype">
<service id="foo" class="FooClass" factory-method="getInstance">
<tag name="foo" foo="foo"/>
<tag name="foo" bar="bar"/>
<argument>foo</argument>
<argument type="service" id="foo.baz"/>
<argument type="collection">
<argument key="%foo%">foo is %foo%</argument>
<argument key="bar">%foo%</argument>
<argument key="foobar">%foo%</argument>
</argument>
<argument>true</argument>
<argument type="service" id="service_container"/>
@ -34,7 +34,7 @@
<service id="foo.baz" class="%baz_class%" factory-method="getInstance">
<configurator class="%baz_class%" method="configureStatic1"/>
</service>
<service id="foo_bar" class="%foo_class%"/>
<service id="foo_bar" class="%foo_class%" scope="prototype"/>
<service id="method_call1" class="FooClass">
<file>%path%foo.php</file>
<call method="setBar">
@ -50,7 +50,7 @@
<argument type="service" id="foobaz" on-invalid="ignore"/>
</call>
</service>
<service id="factory_service" factory-method="getInstance" factory-service="foo.baz"/>
<service id="factory_service" class="Bar" factory-method="getInstance" factory-service="foo.baz"/>
<service id="alias_for_foo" alias="foo"/>
</services>
</container>

View File

@ -10,13 +10,12 @@ services:
- { name: foo, foo: foo }
- { name: foo, bar: bar }
factory_method: getInstance
arguments: [foo, '@foo.baz', { '%foo%': 'foo is %foo%', bar: '%foo%' }, true, '@service_container']
arguments: [foo, '@foo.baz', { '%foo%': 'foo is %foo%', foobar: '%foo%' }, true, '@service_container']
properties: { foo: bar, moo: '@foo.baz' }
calls:
- [setBar, ['@bar']]
- [initialize, { }]
scope: prototype
configurator: sc_configure
bar:
class: FooClass
@ -28,6 +27,7 @@ services:
configurator: ['%baz_class%', configureStatic1]
foo_bar:
class: %foo_class%
scope: prototype
method_call1:
class: FooClass
file: %path%foo.php
@ -38,6 +38,7 @@ services:
- [setBar, ['@?foobaz']]
factory_service:
class: Bar
factory_method: getInstance
factory_service: foo.baz
alias_for_foo: @foo