Parcourir la source

Throw exceptions in case someone forgot to set method name in call.

Bug fix: yes
Feature add: no
Symfony2 tests pass: yes
Symfony2 tests added: yes

In general without this exception generated by php dumper container class, will cause PHP fatal error, bacause method call will look like this: `$instance->(/* arguments*/);`.
Dariusz Górecki il y a 13 ans
Parent
commit
769c17bb95

+ 7 - 0
src/Symfony/Component/DependencyInjection/Definition.php

@@ -11,6 +11,8 @@
 
 namespace Symfony\Component\DependencyInjection;
 
+use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
+
 /**
  * Definition represents a service definition.
  *
@@ -308,10 +310,15 @@ class Definition
      *
      * @return Definition The current instance
      *
+     * @throws InvalidArgumentException on empty $method param
+     *
      * @api
      */
     public function addMethodCall($method, array $arguments = array())
     {
+        if (empty($method)) {
+            throw new InvalidArgumentException(sprintf('Method name cannot be empty.'));
+        }
         $this->calls[] = array($method, $arguments);
 
         return $this;

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

@@ -95,6 +95,16 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->removeMethodCall() removes a method to call');
     }
 
+    /**
+     * @expectedException Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
+     * @expectedExceptionMessage Method name cannot be empty.
+     */
+    public function testExceptionOnEmptyMethodCall()
+    {
+        $def = new Definition('stdClass');
+        $def->addMethodCall('');
+    }
+
     /**
      * @covers Symfony\Component\DependencyInjection\Definition::setFile
      * @covers Symfony\Component\DependencyInjection\Definition::getFile