Browse Source

[DependencyInjection] added some tests

Johannes M. Schmitt 14 years ago
parent
commit
d9848f38ac

+ 6 - 3
src/Symfony/Component/DependencyInjection/Dumper/GraphvizDumper.php

@@ -68,7 +68,10 @@ class GraphvizDumper extends Dumper
 
         $this->edges = array();
         foreach ($this->container->getDefinitions() as $id => $definition) {
-            $this->edges[$id] = $this->findEdges($id, $definition->getArguments(), true, '');
+            $this->edges[$id] = array_merge(
+                $this->findEdges($id, $definition->getArguments(), true, ''),
+                $this->findEdges($id, $definition->getProperties(), false, '')
+            );
 
             foreach ($definition->getMethodCalls() as $call) {
                 $this->edges[$id] = array_merge(
@@ -120,8 +123,8 @@ class GraphvizDumper extends Dumper
      *
      * @param string $id The service id used to find edges
      * @param array $arguments An array of arguments
-     * @param boolean $required 
-     * @param string $name 
+     * @param boolean $required
+     * @param string $name
      * @return array An array of edges
      */
     protected function findEdges($id, $arguments, $required, $name)

+ 24 - 20
src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php

@@ -60,7 +60,7 @@ class XmlDumper extends Dumper
     /**
      * Adds parameters.
      *
-     * @param DOMElement $parent 
+     * @param DOMElement $parent
      * @return void
      */
     protected function addParameters(\DOMElement $parent)
@@ -82,8 +82,8 @@ class XmlDumper extends Dumper
     /**
      * Adds method calls.
      *
-     * @param array $methodcalls 
-     * @param DOMElement $parent 
+     * @param array $methodcalls
+     * @param DOMElement $parent
      * @return void
      */
     protected function addMethodCalls(array $methodcalls, \DOMElement $parent)
@@ -101,8 +101,8 @@ class XmlDumper extends Dumper
     /**
      * Adds interface injector.
      *
-     * @param InterfaceInjector $injector 
-     * @param DOMElement $parent 
+     * @param InterfaceInjector $injector
+     * @param DOMElement $parent
      * @return void
      */
     protected function addInterfaceInjector(InterfaceInjector $injector, \DOMElement $parent)
@@ -116,7 +116,7 @@ class XmlDumper extends Dumper
     /**
      * Adds interface injectors.
      *
-     * @param DOMElement $parent 
+     * @param DOMElement $parent
      * @return void
      */
     protected function addInterfaceInjectors(\DOMElement $parent)
@@ -135,9 +135,9 @@ class XmlDumper extends Dumper
     /**
      * Adds a service.
      *
-     * @param Definition $definition 
-     * @param string $id 
-     * @param DOMElement $parent 
+     * @param Definition $definition
+     * @param string $id
+     * @param DOMElement $parent
      * @return void
      */
     protected function addService($definition, $id, \DOMElement $parent)
@@ -179,6 +179,10 @@ class XmlDumper extends Dumper
             $this->convertParameters($parameters, 'argument', $service);
         }
 
+        if ($parameters = $definition->getProperties()) {
+            $this->convertParameters($parameters, 'property', $service, 'name');
+        }
+
         $this->addMethodCalls($definition->getMethodCalls(), $service);
 
         if ($callable = $definition->getConfigurator()) {
@@ -198,9 +202,9 @@ class XmlDumper extends Dumper
     /**
      * Adds a service alias.
      *
-     * @param string $alias 
-     * @param string $id 
-     * @param DOMElement $parent 
+     * @param string $alias
+     * @param string $id
+     * @param DOMElement $parent
      * @return void
      */
     protected function addServiceAlias($alias, $id, \DOMElement $parent)
@@ -217,7 +221,7 @@ class XmlDumper extends Dumper
     /**
      * Adds services.
      *
-     * @param DOMElement $parent 
+     * @param DOMElement $parent
      * @return void
      */
     protected function addServices(\DOMElement $parent)
@@ -241,23 +245,23 @@ class XmlDumper extends Dumper
     /**
      * Converts parameters.
      *
-     * @param string $parameters 
-     * @param string $type 
-     * @param DOMElement $parent 
+     * @param string $parameters
+     * @param string $type
+     * @param DOMElement $parent
      * @return void
      */
-    protected function convertParameters($parameters, $type, \DOMElement $parent)
+    protected function convertParameters($parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
     {
         $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
         foreach ($parameters as $key => $value) {
             $element = $this->document->createElement($type);
             if ($withKeys) {
-                $element->setAttribute('key', $key);
+                $element->setAttribute($keyAttribute, $key);
             }
 
             if (is_array($value)) {
                 $element->setAttribute('type', 'collection');
-                $this->convertParameters($value, $type, $element);
+                $this->convertParameters($value, $type, $element, 'key');
             } else if (is_object($value) && $value instanceof Reference) {
                 $element->setAttribute('type', 'service');
                 $element->setAttribute('id', (string) $value);
@@ -284,7 +288,7 @@ class XmlDumper extends Dumper
     /**
      * Escapes arguments
      *
-     * @param array $arguments 
+     * @param array $arguments
      * @return array
      */
     protected function escape($arguments)

+ 14 - 10
src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php

@@ -60,8 +60,8 @@ class YamlDumper extends Dumper
     /**
      * Adds a service
      *
-     * @param string $id 
-     * @param Definition $definition 
+     * @param string $id
+     * @param Definition $definition
      * @return string
      */
     protected function addService($id, $definition)
@@ -103,6 +103,10 @@ class YamlDumper extends Dumper
             $code .= sprintf("    arguments: %s\n", Yaml::dump($this->dumpValue($definition->getArguments()), 0));
         }
 
+        if ($definition->getProperties()) {
+            $code .= sprintf("    properties: %s\n", Yaml::dump($this->dumpValue($definition->getProperties()), 0));
+        }
+
         if ($definition->getMethodCalls()) {
             $code .= sprintf("    calls:\n      %s\n", str_replace("\n", "\n      ", Yaml::dump($this->dumpValue($definition->getMethodCalls()), 1)));
         }
@@ -129,8 +133,8 @@ class YamlDumper extends Dumper
     /**
      * Adds a service alias
      *
-     * @param string $alias 
-     * @param string $id 
+     * @param string $alias
+     * @param string $id
      * @return void
      */
     protected function addServiceAlias($alias, $id)
@@ -214,8 +218,8 @@ class YamlDumper extends Dumper
     /**
      * Gets the service call.
      *
-     * @param string $id 
-     * @param Reference $reference 
+     * @param string $id
+     * @param Reference $reference
      * @return string
      */
     protected function getServiceCall($id, Reference $reference = null)
@@ -230,7 +234,7 @@ class YamlDumper extends Dumper
     /**
      * Gets parameter call.
      *
-     * @param string $id 
+     * @param string $id
      * @return string
      */
     protected function getParameterCall($id)
@@ -241,8 +245,8 @@ class YamlDumper extends Dumper
     /**
      * Prepares parameters
      *
-     * @param string $parameters 
-     * @return array 
+     * @param string $parameters
+     * @return array
      */
     protected function prepareParameters($parameters)
     {
@@ -263,7 +267,7 @@ class YamlDumper extends Dumper
     /**
      * Escapes arguments
      *
-     * @param array $arguments 
+     * @param array $arguments
      * @return array
      */
     protected function escape($arguments)

+ 7 - 1
tests/Symfony/Tests/Component/DependencyInjection/Compiler/AnalyzeServiceReferencesPassTest.php

@@ -40,11 +40,17 @@ class AnalyzeServiceReferencesPassTest extends \PHPUnit_Framework_TestCase
             ->addArgument($ref4 = new Reference('b'))
         ;
 
+        $d = $container
+            ->register('d')
+            ->setProperty('foo', $ref5 = new Reference('b'))
+        ;
+
         $graph = $this->process($container);
 
-        $this->assertEquals(2, count($edges = $graph->getNode('b')->getInEdges()));
+        $this->assertEquals(3, count($edges = $graph->getNode('b')->getInEdges()));
         $this->assertSame($ref1, $edges[0]->getValue());
         $this->assertSame($ref4, $edges[1]->getValue());
+        $this->assertSame($ref5, $edges[2]->getValue());
     }
 
     public function testProcessDetectsReferencesFromInlinedDefinitions()

+ 3 - 1
tests/Symfony/Tests/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPassTest.php

@@ -15,9 +15,10 @@ class ResolveDefinitionTemplatesPassTest extends \PHPUnit_Framework_TestCase
     public function testProcess()
     {
         $container = new ContainerBuilder();
-        $container->register('parent', 'foo')->setArguments(array('moo', 'b'));
+        $container->register('parent', 'foo')->setArguments(array('moo', 'b'))->setProperty('foo', 'moo');
         $container->setDefinition('child', new DefinitionDecorator('parent'))
             ->setArgument(0, 'a')
+            ->setProperty('foo', 'bar')
             ->setClass('bar')
         ;
 
@@ -27,6 +28,7 @@ class ResolveDefinitionTemplatesPassTest extends \PHPUnit_Framework_TestCase
         $this->assertNotInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $def);
         $this->assertEquals('bar', $def->getClass());
         $this->assertEquals(array('a', 'b'), $def->getArguments());
+        $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
     }
 
     public function testProcessAppendsMethodCallsAlways()

+ 13 - 0
tests/Symfony/Tests/Component/DependencyInjection/Compiler/ResolveInvalidReferencesPassTest.php

@@ -62,6 +62,19 @@ class ResolveInvalidReferencesPassTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('bar', (string) $arguments[0]);
     }
 
+    public function testProcessRemovesPropertiesOnInvalid()
+    {
+        $container = new ContainerBuilder();
+        $def = $container
+            ->register('foo')
+            ->setProperty('foo', new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))
+        ;
+
+        $this->process($container);
+
+        $this->assertEquals(array(), $def->getProperties());
+    }
+
     protected function process(ContainerBuilder $container, array $exceptions = array())
     {
         $pass = new ResolveInvalidReferencesPass($exceptions);

+ 18 - 0
tests/Symfony/Tests/Component/DependencyInjection/DefinitionTest.php

@@ -220,4 +220,22 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
         ;
         $this->assertSame(array('foo', 'bar'), $def->getArguments());
     }
+
+    public function testSetGetProperties()
+    {
+        $def = new Definition('stdClass');
+
+        $this->assertEquals(array(), $def->getProperties());
+        $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
+        $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
+    }
+
+    public function testSetProperty()
+    {
+        $def = new Definition('stdClass');
+
+        $this->assertEquals(array(), $def->getProperties());
+        $this->assertSame($def, $def->setProperty('foo', 'bar'));
+        $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
+    }
 }

+ 1 - 0
tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/container9.php

@@ -15,6 +15,7 @@ $container->
     setFactoryClass('FooClass')->
     setFactoryMethod('getInstance')->
     setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'bar' => '%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')->

+ 1 - 0
tests/Symfony/Tests/Component/DependencyInjection/Fixtures/graphviz/services9.dot

@@ -15,6 +15,7 @@ digraph sc {
   node_foobaz [label="foobaz\n\n", shape=record, fillcolor="#ff9999", style="filled"];
   node_foo -> node_foo_baz [label="" style="filled"];
   node_foo -> node_service_container [label="" style="filled"];
+  node_foo -> node_foo_baz [label="" style="dashed"];
   node_foo -> node_bar [label="setBar()" style="dashed"];
   node_bar -> node_foo_baz [label="" style="filled"];
   node_method_call1 -> node_foo [label="setBar()" style="dashed"];

+ 2 - 0
tests/Symfony/Tests/Component/DependencyInjection/Fixtures/includes/foo.php

@@ -2,6 +2,8 @@
 
 class FooClass
 {
+    private $foo, $moo;
+
     public $bar = null, $initialized = false, $configured = false, $called = false, $arguments = array();
 
     public function __construct($arguments = array())

+ 9 - 1
tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php

@@ -59,10 +59,18 @@ class ProjectServiceContainer extends Container
      */
     protected function getFooService()
     {
-        $instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $this->get('foo.baz'), array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo'), 'bar' => $this->getParameter('foo')), true, $this);
+        $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);
 
         $instance->setBar($this->get('bar'));
         $instance->initialize();
+        $b = new \ReflectionProperty($instance, 'foo');
+        $b->setAccessible(true);
+        $b->setValue($instance, 'bar');
+        $c = new \ReflectionProperty($instance, 'moo');
+        $c->setAccessible(true);
+        $c->setValue($instance, $a);
         sc_configure($instance);
 
         return $instance;

+ 2 - 0
tests/Symfony/Tests/Component/DependencyInjection/Fixtures/xml/services9.xml

@@ -17,6 +17,8 @@
       </argument>
       <argument>true</argument>
       <argument type="service" id="service_container"/>
+      <property name="foo">bar</property>
+      <property name="moo" type="service" id="foo.baz"/>
       <call method="setBar">
         <argument type="service" id="bar"/>
       </call>

+ 1 - 0
tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/services9.yml

@@ -11,6 +11,7 @@ services:
       - { name: foo, bar: bar }
     factory_method: getInstance
     arguments: [foo, '@foo.baz', { '%foo%': 'foo is %foo%', bar: '%foo%' }, true, '@service_container']
+    properties: { foo: bar, moo: '@foo.baz' }
     calls:
       - [setBar, ['@bar']]
       - [initialize, {  }]