From af103cce2cacc8cbeb732f10563a77ec516ec364 Mon Sep 17 00:00:00 2001 From: Gregory PLANCHAT Date: Thu, 19 Jul 2012 19:23:30 +0200 Subject: [PATCH] Updated DependencyInjection component's API Signed-off-by: Gregory PLANCHAT --- .../Definition/ArgumentDefinition.php | 4 +- .../Definition/ClassDefinition.php | 127 +++- .../Definition/MethodDefinition.php | 54 +- .../Definition/ClassDefinitionTest.php | 447 +++++++++---- .../Definition/MethodDefinitionTest.php | 610 +++++++++++++----- .../modules/WootookCore/test/phpunit.xml | 6 +- 6 files changed, 913 insertions(+), 335 deletions(-) diff --git a/src/application/modules/WootookCore/src/Wootook/Core/DependencyInjection/Definition/ArgumentDefinition.php b/src/application/modules/WootookCore/src/Wootook/Core/DependencyInjection/Definition/ArgumentDefinition.php index 9b5281e..ea033c1 100644 --- a/src/application/modules/WootookCore/src/Wootook/Core/DependencyInjection/Definition/ArgumentDefinition.php +++ b/src/application/modules/WootookCore/src/Wootook/Core/DependencyInjection/Definition/ArgumentDefinition.php @@ -55,7 +55,9 @@ class ArgumentDefinition $this->setReflector($methodDefinition->getArgumentReflector($argumentName)); - $this->_registry = $registry; + if ($registry !== null) { + $this->setRegistry($registry); + } } public function setRegistry(DependencyInjection\Registry $registry) diff --git a/src/application/modules/WootookCore/src/Wootook/Core/DependencyInjection/Definition/ClassDefinition.php b/src/application/modules/WootookCore/src/Wootook/Core/DependencyInjection/Definition/ClassDefinition.php index df3fdc4..d72783d 100644 --- a/src/application/modules/WootookCore/src/Wootook/Core/DependencyInjection/Definition/ClassDefinition.php +++ b/src/application/modules/WootookCore/src/Wootook/Core/DependencyInjection/Definition/ClassDefinition.php @@ -1,4 +1,32 @@ + * All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * --> NOTICE <-- + * This file is part of the core development branch, changing its contents will + * make you unable to use the automatic updates manager. Please refer to the + * documentation for further information about customizing Wootook. + * + */ namespace Wootook\Core\DependencyInjection\Definition; @@ -7,21 +35,47 @@ use Wootook\Core\Base\Service, Wootook\Core\DependencyInjection, Wootook\Core\Exception as CoreException; +/** + * Class definition for the DependencyInjection component, used to define a + * class's dependencies. + * + * @package WootookCore + * @subpackage DependencyInjection + * @author Grégory PLANCHAT + * @see http://wootook.org/ + */ class ClassDefinition { + /** + * @var string + */ protected $_className = null; + + /** + * @var \ReflectionClass + */ protected $_reflector = null; + + /** + * @var array + */ protected $_methodDefinitions = array(); + /** + * @var null|\Wootook\Core\DependencyInjection\Registry + */ protected $_registry = null; + /** + * @var string + */ protected $_methodDefinitionClassName = 'Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition'; /** * @param string $className * @param null|string $methodDefinitionHandlerClass */ - public function __construct($className, DependencyInjection\Registry $registry = null, $methodDefinitionHandlerClass = null) + public function __construct($className, DependencyInjection\Registry $registry = null, $methodDefinitionClassName = null) { $this->_className = $className; try { @@ -30,13 +84,19 @@ class ClassDefinition throw new CoreException\DependencyInjection\BadMethodCallException($e->getMessage(), $e->getCode(), $e); } - if (is_string($methodDefinitionHandlerClass)) { - $this->_methodDefinitionHandlerClass = $methodDefinitionHandlerClass; - } else { - $this->_methodDefinitionHandlerClass = __NAMESPACE__ . '\\MethodDefinition'; + if ($methodDefinitionClassName !== null) { + $this->setMethodDefinitionClassName($methodDefinitionClassName); + } + + if ($registry !== null) { + $this->setRegistry($registry); } } + /** + * @param \Wootook\Core\DependencyInjection\Registry $registry + * @return ClassDefinition + */ public function setRegistry(DependencyInjection\Registry $registry) { $this->_registry = $registry; @@ -44,6 +104,9 @@ class ClassDefinition return $this; } + /** + * @return null|\Wootook\Core\DependencyInjection\Registry + */ public function getRegistry() { return $this->_registry; @@ -77,11 +140,21 @@ class ClassDefinition */ public function initMethodDefinition($methodName) { + if (!is_string($methodName)) { + throw new CoreException\DependencyInjection\InvalidArgumentException('Method names only accept string.'); + } + $class = $this->getMethodDefinitionClassName(); return new $class($this, $methodName, $this->getRegistry()); } + /** + * @param $methodName + * @param MethodDefinition $methodDefinition + * @return ClassDefinition + * @throws \Wootook\Core\Exception\DependencyInjection\InvalidArgumentException + */ public function registerMethodDefinition($methodName, MethodDefinition $methodDefinition) { if (!is_string($methodName)) { @@ -93,6 +166,10 @@ class ClassDefinition return $this; } + /** + * @param $methodName + * @return ClassDefinition + */ public function addMethodDefinition($methodName) { $this->registerMethodDefinition($methodName, $this->initMethodDefinition($methodName)); @@ -100,6 +177,12 @@ class ClassDefinition return $this; } + /** + * @param $methodName + * @param MethodDefinition $definition + * @return ClassDefinition + * @throws \Wootook\Core\Exception\DependencyInjection\InvalidArgumentException + */ public function setMethodDefinition($methodName, MethodDefinition $definition) { if (!is_string($methodName)) { @@ -111,25 +194,43 @@ class ClassDefinition return $this; } + /** + * @param $methodDefinitionClassName + * @return string + * @throws \Wootook\Core\Exception\DependencyInjection\InvalidArgumentException + */ public function setMethodDefinitionClassName($methodDefinitionClassName) { if (!is_string($methodDefinitionClassName)) { throw new CoreException\DependencyInjection\InvalidArgumentException('Class names only accept string.'); } - return $this->_methodDefinitionClassName; + $this->_methodDefinitionClassName = $methodDefinitionClassName; + + return $this; } + /** + * @return string + */ public function getMethodDefinitionClassName() { return $this->_methodDefinitionClassName; } + /** + * @return array + */ public function getAllMethodDefinitions() { return $this->_methodDefinitions; } + /** + * @param array $args + * @return object + * @throws \Wootook\Core\Exception\DependencyInjection\RuntimeException + */ public function newInstance(Array $args = array()) { try { @@ -145,6 +246,10 @@ class ClassDefinition } } + /** + * @return mixed + * @throws \Wootook\Core\Exception\DependencyInjection\RuntimeException + */ public function newInstanceWithoutConstructor() { try { @@ -154,16 +259,26 @@ class ClassDefinition } } + /** + * @return array + */ public function reset() { return $this->_methodDefinitions = array(); } + /** + * @return null|\ReflectionClass + */ public function getReflector() { return $this->_reflector; } + /** + * @param \ReflectionClass $reflector + * @return ClassDefinition + */ public function setReflector(\ReflectionClass $reflector) { $this->_reflector = $reflector; diff --git a/src/application/modules/WootookCore/src/Wootook/Core/DependencyInjection/Definition/MethodDefinition.php b/src/application/modules/WootookCore/src/Wootook/Core/DependencyInjection/Definition/MethodDefinition.php index 3bf86fd..6e5720e 100644 --- a/src/application/modules/WootookCore/src/Wootook/Core/DependencyInjection/Definition/MethodDefinition.php +++ b/src/application/modules/WootookCore/src/Wootook/Core/DependencyInjection/Definition/MethodDefinition.php @@ -51,7 +51,7 @@ class MethodDefinition * @param $methodName * @param null|\Wootook\Core\DependencyInjection\Registry $registry */ - public function __construct(ClassDefinition $classDefinition, $methodName, DependencyInjection\Registry $registry = null, $argumentDefinitionHandlerClass = null) + public function __construct(ClassDefinition $classDefinition, $methodName, DependencyInjection\Registry $registry = null, $argumentDefinitionClassName = null) { $this->_classDefinition = $classDefinition; @@ -63,11 +63,13 @@ class MethodDefinition throw new CoreException\DependencyInjection\BadMethodCallException($e->getMessage(), $e->getCode(), $e); } - if (is_string($argumentDefinitionHandlerClass)) { - $this->setArgumentDefinitionClassName($argumentDefinitionHandlerClass); + if ($argumentDefinitionClassName !== null) { + $this->setArgumentDefinitionClassName($argumentDefinitionClassName); } - $this->_registry = $registry; + if ($registry !== null) { + $this->setRegistry($registry); + } } /** @@ -133,7 +135,12 @@ class MethodDefinition public function registerArgumentDefinition($argumentPosition, ArgumentDefinition $argumentDefinition) { if (is_string($argumentPosition) && isset($this->_argumentIndex[$argumentPosition])) { + $argumentName = $argumentPosition; $argumentPosition = $this->_argumentIndex[$argumentPosition]; + } else if (($reflector = $argumentDefinition->getReflector()) instanceof \ReflectionParameter) { + $argumentName = $reflector->getName(); + } else { + $argumentName = null; } if (!is_int($argumentPosition)) { @@ -141,6 +148,9 @@ class MethodDefinition } $this->_argumentDefinitions[$argumentPosition] = $argumentDefinition; + if ($argumentName !== null) { + $this->_argumentIndex[$argumentName] = $argumentPosition; + } return $this; } @@ -167,13 +177,33 @@ class MethodDefinition return $this; } + /** + * @return array + */ + public function getAllArgumentDefinitions() + { + return $this->_argumentDefinitions; + } + + /** + * @return array + */ + public function reset() + { + $this->_argumentDefinitions = array(); + + return $this; + } + public function setArgumentDefinitionClassName($argumentDefinitionClassName) { if (!is_string($argumentDefinitionClassName)) { throw new CoreException\DependencyInjection\InvalidArgumentException('Class names only accept string.'); } - return $this->_argumentDefinitionClassName; + $this->_argumentDefinitionClassName = $argumentDefinitionClassName; + + return $this; } public function getArgumentDefinitionClassName() @@ -216,23 +246,11 @@ class MethodDefinition */ public function bindArgumentRegistryEntry($argumentPosition, $registryKey) { - if (($registry = $this->getRegistry()) === null) { - throw new CoreException\DependencyInjection\RuntimeException('No registry available'); - } - $this->getArgumentDefinition($argumentPosition, true)->bindRegistryEntry($registryKey); return $this; } - /** - * @return array - */ - public function getAllArgumentDefinitions() - { - return $this->_argumentDefinitions; - } - /** * @return \ReflectionMethod */ @@ -272,7 +290,7 @@ class MethodDefinition } if (!is_int($argumentPosition) || !isset($this->_argumentReflectors[$argumentPosition])) { - throw new CoreException\DependencyInjection\InvalidArgumentException('Argument not found.'); + throw new CoreException\DependencyInjection\BadMethodCallException('Argument does not exist.'); } return $this->_argumentReflectors[$argumentPosition]; diff --git a/src/application/modules/WootookCore/test/php/WootookUnit/Core/DependencyInjection/Definition/ClassDefinitionTest.php b/src/application/modules/WootookCore/test/php/WootookUnit/Core/DependencyInjection/Definition/ClassDefinitionTest.php index 2d07a4b..60cf743 100644 --- a/src/application/modules/WootookCore/test/php/WootookUnit/Core/DependencyInjection/Definition/ClassDefinitionTest.php +++ b/src/application/modules/WootookCore/test/php/WootookUnit/Core/DependencyInjection/Definition/ClassDefinitionTest.php @@ -7,53 +7,177 @@ use Wootook\Core\Config, class ClassDefinitionTest extends \PHPUnit_Framework_TestCase { + public function methodToTest($fooArgumentOne, $fooArgumentTwo = null) + { + // This method exists only for testing method introspection with the Dependency Injection component + } + + /** + * @return \Wootook\Core\DependencyInjection\Definition\ArgumentDefinition + */ + public function getMethodDefinitionMock(Array $additionalMethods = array()) + { + $argumentDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition') + ->setMethods($additionalMethods) + ->disableOriginalConstructor(true) + ->getMock() + ; + + return $argumentDefinition; + } + + /** + * @return \Wootook\Core\DependencyInjection\Registry + */ + public function getRegistryMock(Array $additionalMethods = array()) + { + $registry = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Registry') + ->setMethods($additionalMethods) + ->disableOriginalConstructor(true) + ->getMock() + ; + + return $registry; + } + public function testClassDefinitionInstantiation_withNonExistingClass() { $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\BadMethodCallException'); new Definition\ClassDefinition('InexistentClass'); } - public function testClassDefinitionInstantiation_withSpecialMethodDefinitionClassName() + public function testClassDefinitionInstantiation_withExistingClass() { - $methodDefinitionClassName = \uniqid('WootookUnit_Core_DependencyInjection_Mock_MethodDefinition_TestNewInstance_'); - $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), $methodDefinitionClassName, false); + $definition = new Definition\ClassDefinition('stdClass'); - $definition = new Definition\ClassDefinition(__CLASS__, null, $methodDefinitionClassName); - - $this->assertInstanceOf($methodDefinitionClassName, $definition->getMethodDefinition(__FUNCTION__)); + $this->assertEquals('stdClass', $definition->getReflector()->getName()); } - public function testClassDefinitionInstantiation_assignNewMethodDefinitionInstance() + public function testClassDefinitionInstantiation_withExistingClassWithRegistry() { - $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), '', false); + /** @var \Wootook\Core\DependencyInjection\Registry $registry */ + $registry = $this->getRegistryMock(); - $definition = new Definition\ClassDefinition(__CLASS__); - $definition->setMethodDefinition(__FUNCTION__, $mock); + $definition = new Definition\ClassDefinition('stdClass', $registry); - $this->assertSame($mock, $definition->getMethodDefinition(__FUNCTION__)); + $this->assertEquals('stdClass', $definition->getReflector()->getName()); + $this->assertSame($registry, $definition->getRegistry()); } - public function testGetMethodDefinition_withInvalidMethodName() + public function testClassDefinitionInstantiation_withExistingClass_withRegistry_withSpecialMethodDefinitionClassName() + { + /** @var \Wootook\Core\DependencyInjection\Registry $registry */ + $registry = $this->getRegistryMock(); + + $definition = new Definition\ClassDefinition('stdClass', $registry, 'stdClass'); + + $this->assertEquals('stdClass', $definition->getMethodDefinitionClassName()); + $this->assertSame($registry, $definition->getRegistry()); + } + + public function testClassDefinitionInstantiation_withInvalidSpecialArgumentDefinitionClassName() + { + /** @var \Wootook\Core\DependencyInjection\Registry $registry */ + $registry = $this->getRegistryMock(); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); + $definition = new Definition\ClassDefinition('stdClass', $registry, 32); + } + + public function testClassDefinitionInstantiation_validateReflectorInstance() + { + $definition = new Definition\ClassDefinition('stdClass'); + + $this->assertInstanceOf('ReflectionClass', $definition->getReflector()); + $this->assertEquals('stdClass', $definition->getReflector()->getName()); + } + + public function testClassDefinitionAccessorsAndMutators_usingReflector() + { + $definition = new Definition\classDefinition('stdClass'); + + $this->assertInstanceOf('ReflectionClass', $definition->getReflector()); + + $reflector = new \Reflectionclass(__CLASS__); + $definition->setReflector($reflector); + + $this->assertSame($reflector, $definition->getReflector()); + } + + public function testClassDefinitionMethodDefinitions_initMethodDefinition() + { + $definition = new Definition\ClassDefinition(__CLASS__, null, 'stdClass'); + + $methodDefinition = $definition->initMethodDefinition('methodToTest'); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); + $definition->initMethodDefinition(0); + } + + public function testClassDefinitionMethodDefinitions_registerMethodDefinition() + { + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $methodDefinition */ + $methodDefinition = $this->getMethodDefinitionMock(); + + $definition = new Definition\ClassDefinition('stdClass'); + + $definition->registerMethodDefinition('methodToTest', $methodDefinition); + $this->assertCount(1, $definition->getAllMethodDefinitions()); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); + $definition->registerMethodDefinition(array(), $methodDefinition); + } + + public function testClassDefinitionMethodDefinitions_addMethodDefinition() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ArgumentDefinition $argumentDefinition */ + $methodDefinition = $this->getMethodDefinitionMock(array('__construct')); + + $definition = new Definition\ClassDefinition(__CLASS__, null, get_class($methodDefinition)); + + $definition->addMethodDefinition('methodToTest'); + $this->assertCount(1, $definition->getAllMethodDefinitions()); + $this->assertInstanceOf(get_class($methodDefinition), $definition->getAllMethodDefinitions()['methodToTest']); + } + + public function testClassDefinitionMethodDefinitions_setMethodDefinition() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ArgumentDefinition $argumentDefinition */ + $methodDefinition = $this->getMethodDefinitionMock(); + + $definition = new Definition\ClassDefinition(__CLASS__, null, 'stdClass'); + + $definition->setMethodDefinition('methodToTest', $methodDefinition); + $this->assertCount(1, $definition->getAllMethodDefinitions()); + $this->assertInstanceOf(get_class($methodDefinition), $definition->getAllMethodDefinitions()['methodToTest']); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); + $definition->setMethodDefinition(0, $methodDefinition); + } + + public function testClassDefinition_getMethodDefinitionWithInvalidMethodName() { $definition = new Definition\ClassDefinition(__CLASS__); $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); - $definition->getMethodDefinition(42); + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $definition->getMethodDefinition(1.5); } - public function testSetMethodDefinition_withInvalidMethodName() + public function testMethodDefinition_getArgumentReflector_withInexistentMethodName() { - $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), '', false); - - $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); - $definition = new Definition\ClassDefinition(__CLASS__); - $definition->setMethodDefinition(42, $mock); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\BadMethodCallException'); + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $definition->getMethodDefinition('inexistentMethod'); } public function testGetAllMethodDefinitions() { - $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), '', false); + $mock = $this->getMethodDefinitionMock(); $definition = new Definition\ClassDefinition(__CLASS__); $definition->setMethodDefinition('fooMethod', $mock); @@ -66,7 +190,7 @@ class ClassDefinitionTest extends \PHPUnit_Framework_TestCase public function testReset() { - $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), '', false); + $mock = $this->getMethodDefinitionMock(); $definition = new Definition\ClassDefinition(__CLASS__); $definition->setMethodDefinition('fooMethod', $mock); @@ -76,127 +200,166 @@ class ClassDefinitionTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array(), $definition->getAllMethodDefinitions()); } - public function testClassDefinitionAccessors_getReflector() - { - $definition = new Definition\ClassDefinition(__CLASS__); - $this->assertInstanceOf('\\ReflectionClass', $definition->getReflector()); - } +// public function testClassDefinitionInstantiation_withSpecialMethodDefinitionClassName() +// { +// $methodDefinitionClassName = \uniqid('WootookUnit_Core_DependencyInjection_Mock_MethodDefinition_TestNewInstance_'); +// $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), $methodDefinitionClassName, false); +// +// $definition = new Definition\ClassDefinition(__CLASS__, null, $methodDefinitionClassName); +// +// $this->assertInstanceOf($methodDefinitionClassName, $definition->getMethodDefinition(__FUNCTION__)); +// } - public function testClassDefinitionMutators_setReflector() - { - $definition = new Definition\ClassDefinition(__CLASS__); - $reflector = new \ReflectionClass(__CLASS__); - $definition->setReflector($reflector); - - $this->assertSame($reflector, $definition->getReflector()); - } - - public function testNewInstance_hasNoConstructor() - { - $this->markTestSkipped("Method ReflectionClass::newInstance() couldn't be mocked due to API<->Reflection inconsistencies."); - - $reflectionClassMock = $this->getMock('ReflectionClass', array('hasMethod', 'newInstance'), array('stdClass')); - $reflectionClassMock->expects($this->once()) - ->method('hasMethod') - ->with('__construct') - ->will($this->returnValue(false)) - ; - $reflectionClassMock->expects($this->once()) - ->method('newInstance') - ->will($this->returnValue(new \stdClass())) - ; - - $classDefinition = new Definition\ClassDefinition(__CLASS__); - $classDefinition->setReflector($reflectionClassMock); - - $this->assertInstanceOf('stdClass', $classDefinition->newInstance()); - } - - public function testNewInstance_usingConstructor() - { - $expectedArguments = array(42); - - $reflectionClassMock = $this->getMock('ReflectionClass', array('hasMethod', 'newInstanceArgs'), array('stdClass')); - $reflectionClassMock->expects($this->once()) - ->method('hasMethod') - ->with('__construct') - ->will($this->returnValue(true)) - ; - $reflectionClassMock->expects($this->once()) - ->method('newInstanceArgs') - ->with($expectedArguments) - ->will($this->returnValue(new \stdClass())) - ; - - $classDefinition = new Definition\ClassDefinition(__CLASS__); - $classDefinition->setReflector($reflectionClassMock); - - $methodDefinitionMock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('compileArgs'), array(), '', false); - $methodDefinitionMock->expects($this->once()) - ->method('compileArgs') - ->will($this->returnValue(array(42))) - ; - $classDefinition->setMethodDefinition('__construct', $methodDefinitionMock); - - $this->assertInstanceOf('stdClass', $classDefinition->newInstance()); - } - - public function testNewInstance_usingNonPublicConstructor() - { - $reflectionClassMock = $this->getMock('ReflectionClass', array('hasMethod', 'newInstanceArgs'), array('stdClass')); - $reflectionClassMock->expects($this->once()) - ->method('hasMethod') - ->with('__construct') - ->will($this->returnValue(true)) - ; - $reflectionClassMock->expects($this->once()) - ->method('newInstanceArgs') - ->will($this->throwException($this->getMock('ReflectionException'))) - ; - - $classDefinition = new Definition\ClassDefinition(__CLASS__); - $classDefinition->setReflector($reflectionClassMock); - - $methodDefinitionMock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('compileArgs'), array(), '', false); - $methodDefinitionMock->expects($this->once()) - ->method('compileArgs') - ->will($this->returnValue(array(42))) - ; - $classDefinition->setMethodDefinition('__construct', $methodDefinitionMock); - - $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\RuntimeException'); - - $this->assertInstanceOf('stdClass', $classDefinition->newInstance()); - } - - public function testNewInstanceWithoutConstructor() - { - $reflectionClassMock = $this->getMock('ReflectionClass', array('newInstanceWithoutConstructor'), array('stdClass')); - $reflectionClassMock->expects($this->once()) - ->method('newInstanceWithoutConstructor') - ->will($this->returnValue(new \stdClass())) - ; - - $classDefinition = new Definition\ClassDefinition(__CLASS__); - $classDefinition->setReflector($reflectionClassMock); - - $this->assertInstanceOf('stdClass', $classDefinition->newInstanceWithoutConstructor()); - } - - public function testNewInstanceWithoutConstructor_throwsException() - { - $reflectionClassMock = $this->getMock('ReflectionClass', array('newInstanceWithoutConstructor'), array('stdClass')); - $reflectionClassMock->expects($this->once()) - ->method('newInstanceWithoutConstructor') - ->will($this->throwException($this->getMock('ReflectionException'))) - ; - - $classDefinition = new Definition\ClassDefinition(__CLASS__); - $classDefinition->setReflector($reflectionClassMock); - - $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\RuntimeException'); - - $this->assertInstanceOf('stdClass', $classDefinition->newInstanceWithoutConstructor()); - } +// public function testClassDefinitionInstantiation_assignNewMethodDefinitionInstance() +// { +// $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), '', false); +// +// $definition = new Definition\ClassDefinition(__CLASS__); +// $definition->setMethodDefinition(__FUNCTION__, $mock); +// +// $this->assertSame($mock, $definition->getMethodDefinition(__FUNCTION__)); +// } +// +// public function testGetMethodDefinition_withInvalidMethodName() +// { +// $definition = new Definition\ClassDefinition(__CLASS__); +// +// $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); +// $definition->getMethodDefinition(42); +// } +// +// public function testSetMethodDefinition_withInvalidMethodName() +// { +// $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), '', false); +// +// $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); +// +// $definition = new Definition\ClassDefinition(__CLASS__); +// $definition->setMethodDefinition(42, $mock); +// } +// +// public function testClassDefinitionAccessors_getReflector() +// { +// $definition = new Definition\ClassDefinition(__CLASS__); +// +// $this->assertInstanceOf('\\ReflectionClass', $definition->getReflector()); +// } +// +// public function testClassDefinitionMutators_setReflector() +// { +// $definition = new Definition\ClassDefinition(__CLASS__); +// $reflector = new \ReflectionClass(__CLASS__); +// $definition->setReflector($reflector); +// +// $this->assertSame($reflector, $definition->getReflector()); +// } +// +// public function testNewInstance_hasNoConstructor() +// { +// $this->markTestSkipped("Method ReflectionClass::newInstance() couldn't be mocked due to API<->Reflection inconsistencies."); +// +// $reflectionClassMock = $this->getMock('ReflectionClass', array('hasMethod', 'newInstance'), array('stdClass')); +// $reflectionClassMock->expects($this->once()) +// ->method('hasMethod') +// ->with('__construct') +// ->will($this->returnValue(false)) +// ; +// $reflectionClassMock->expects($this->once()) +// ->method('newInstance') +// ->will($this->returnValue(new \stdClass())) +// ; +// +// $classDefinition = new Definition\ClassDefinition(__CLASS__); +// $classDefinition->setReflector($reflectionClassMock); +// +// $this->assertInstanceOf('stdClass', $classDefinition->newInstance()); +// } +// +// public function testNewInstance_usingConstructor() +// { +// $expectedArguments = array(42); +// +// $reflectionClassMock = $this->getMock('ReflectionClass', array('hasMethod', 'newInstanceArgs'), array('stdClass')); +// $reflectionClassMock->expects($this->once()) +// ->method('hasMethod') +// ->with('__construct') +// ->will($this->returnValue(true)) +// ; +// $reflectionClassMock->expects($this->once()) +// ->method('newInstanceArgs') +// ->with($expectedArguments) +// ->will($this->returnValue(new \stdClass())) +// ; +// +// $classDefinition = new Definition\ClassDefinition(__CLASS__); +// $classDefinition->setReflector($reflectionClassMock); +// +// $methodDefinitionMock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('compileArgs'), array(), '', false); +// $methodDefinitionMock->expects($this->once()) +// ->method('compileArgs') +// ->will($this->returnValue(array(42))) +// ; +// $classDefinition->setMethodDefinition('__construct', $methodDefinitionMock); +// +// $this->assertInstanceOf('stdClass', $classDefinition->newInstance()); +// } +// +// public function testNewInstance_usingNonPublicConstructor() +// { +// $reflectionClassMock = $this->getMock('ReflectionClass', array('hasMethod', 'newInstanceArgs'), array('stdClass')); +// $reflectionClassMock->expects($this->once()) +// ->method('hasMethod') +// ->with('__construct') +// ->will($this->returnValue(true)) +// ; +// $reflectionClassMock->expects($this->once()) +// ->method('newInstanceArgs') +// ->will($this->throwException($this->getMock('ReflectionException'))) +// ; +// +// $classDefinition = new Definition\ClassDefinition(__CLASS__); +// $classDefinition->setReflector($reflectionClassMock); +// +// $methodDefinitionMock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('compileArgs'), array(), '', false); +// $methodDefinitionMock->expects($this->once()) +// ->method('compileArgs') +// ->will($this->returnValue(array(42))) +// ; +// $classDefinition->setMethodDefinition('__construct', $methodDefinitionMock); +// +// $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\RuntimeException'); +// +// $this->assertInstanceOf('stdClass', $classDefinition->newInstance()); +// } +// +// public function testNewInstanceWithoutConstructor() +// { +// $reflectionClassMock = $this->getMock('ReflectionClass', array('newInstanceWithoutConstructor'), array('stdClass')); +// $reflectionClassMock->expects($this->once()) +// ->method('newInstanceWithoutConstructor') +// ->will($this->returnValue(new \stdClass())) +// ; +// +// $classDefinition = new Definition\ClassDefinition(__CLASS__); +// $classDefinition->setReflector($reflectionClassMock); +// +// $this->assertInstanceOf('stdClass', $classDefinition->newInstanceWithoutConstructor()); +// } +// +// public function testNewInstanceWithoutConstructor_throwsException() +// { +// $reflectionClassMock = $this->getMock('ReflectionClass', array('newInstanceWithoutConstructor'), array('stdClass')); +// $reflectionClassMock->expects($this->once()) +// ->method('newInstanceWithoutConstructor') +// ->will($this->throwException($this->getMock('ReflectionException'))) +// ; +// +// $classDefinition = new Definition\ClassDefinition(__CLASS__); +// $classDefinition->setReflector($reflectionClassMock); +// +// $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\RuntimeException'); +// +// $this->assertInstanceOf('stdClass', $classDefinition->newInstanceWithoutConstructor()); +// } } diff --git a/src/application/modules/WootookCore/test/php/WootookUnit/Core/DependencyInjection/Definition/MethodDefinitionTest.php b/src/application/modules/WootookCore/test/php/WootookUnit/Core/DependencyInjection/Definition/MethodDefinitionTest.php index 3eca6b5..945b681 100644 --- a/src/application/modules/WootookCore/test/php/WootookUnit/Core/DependencyInjection/Definition/MethodDefinitionTest.php +++ b/src/application/modules/WootookCore/test/php/WootookUnit/Core/DependencyInjection/Definition/MethodDefinitionTest.php @@ -13,252 +13,417 @@ class MethodDefinitionTest extends \PHPUnit_Framework_TestCase // This method exists only for testing method introspection with the Dependency Injection component } - public function testMethodDefinitionInstantiation_withNonExistingMethod() + /** + * @param array $additionalMethods + * @return \Wootook\Core\DependencyInjection\Definition\ClassDefinition + */ + public function getClassDefinitionMock(Array $additionalMethods = array()) { - $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition', array(), array(), '', false); + $methods = array_merge($additionalMethods, array('getReflector')); - $mock->expects($this->once()) + $classDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition') + ->setMethods($methods) + ->disableOriginalConstructor(true) + ->getMock() + ; + + $classDefinition->expects($this->once()) ->method('getReflector') ->will($this->returnValue(new \ReflectionClass(__CLASS__))) ; + return $classDefinition; + } + + /** + * @return \Wootook\Core\DependencyInjection\Definition\ArgumentDefinition + */ + public function getArgumentDefinitionMock(Array $additionalMethods = array()) + { + $argumentDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\ArgumentDefinition') + ->setMethods($additionalMethods) + ->disableOriginalConstructor(true) + ->getMock() + ; + + return $argumentDefinition; + } + + /** + * @return \Wootook\Core\DependencyInjection\Registry + */ + public function getRegistryMock(Array $additionalMethods = array()) + { + $registry = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Registry') + ->setMethods($additionalMethods) + ->disableOriginalConstructor(true) + ->getMock() + ; + + return $registry; + } + + public function testMethodDefinitionInstantiation_withNonExistingMethod() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\BadMethodCallException'); - new Definition\MethodDefinition($mock, 'inexistentMethod'); + new Definition\MethodDefinition($classDefinition, 'inexistentMethod'); + } + + public function testMethodDefinitionInstantiation_withExistingMethod() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + new Definition\MethodDefinition($classDefinition, 'methodToTest'); } public function testMethodDefinitionInstantiation_withoutRegistry_withSpecialArgumentDefinitionClassName() { - $argumentDefinitionClassName = \uniqid('WootookUnit_Core_DependencyInjection_Mock_ArgumentDefinition_TestNewInstance_'); - $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\ArgumentDefinition', array(), array(), $argumentDefinitionClassName, false); + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); - $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition', array(), array(), '', false); + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest', null, 'stdClass'); - $mock->expects($this->once()) - ->method('getReflector') - ->will($this->returnValue(new \ReflectionClass(__CLASS__))) - ; - - $definition = new Definition\MethodDefinition($mock, 'methodToTest', null, $argumentDefinitionClassName); - - $this->assertInstanceOf($argumentDefinitionClassName, $definition->getArgumentDefinition('fooArgumentOne')); + $this->assertEquals('stdClass', $definition->getArgumentDefinitionClassName()); } public function testMethodDefinitionInstantiation_withRegistry_withSpecialArgumentDefinitionClassName() { - $app = $this->getMock('Wootook\\Core\\App\\App', array(), array(), '', false); /** @var \Wootook\Core\DependencyInjection\Registry $registry */ - $registry = $this->getMock('Wootook\\Core\\DependencyInjection\\Registry', array(), array($app)); + $registry = $this->getRegistryMock(); - $argumentDefinitionClassName = \uniqid('WootookUnit_Core_DependencyInjection_Mock_ArgumentDefinition_TestNewInstance_'); - $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\ArgumentDefinition', array(), array(), $argumentDefinitionClassName, false); + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); - /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $mock */ - $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition', array('getReflector'), array(), '', false); + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest', $registry, 'stdClass'); - $mock->expects($this->once()) - ->method('getReflector') - ->will($this->returnValue(new \ReflectionClass(__CLASS__))) - ; - - $definition = new Definition\MethodDefinition($mock, 'methodToTest', $registry, $argumentDefinitionClassName); - - $this->assertInstanceOf($argumentDefinitionClassName, $definition->getArgumentDefinition('fooArgumentOne')); + $this->assertEquals('stdClass', $definition->getArgumentDefinitionClassName()); + $this->assertSame($registry, $definition->getRegistry()); } - public function testMethodDefinitionAccessors_getReflector() + public function testMethodDefinitionInstantiation_withInvalidSpecialArgumentDefinitionClassName() { - $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition', array('getReflector'), array(), '', false); + /** @var \Wootook\Core\DependencyInjection\Registry $registry */ + $registry = $this->getRegistryMock(); - $mock->expects($this->once()) - ->method('getReflector') - ->will($this->returnValue(new \ReflectionClass(__CLASS__))) - ; + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); - $definition = new Definition\MethodDefinition($mock, __FUNCTION__); - - $this->assertInstanceOf('\\ReflectionMethod', $definition->getReflector()); + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest', $registry, 32); } - public function testMethodDefinitionMutators_setReflector() + public function testMethodDefinitionInstantiation_validateReflectorInstance() { - $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition', array('getReflector'), array(), '', false); + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); - $mock->expects($this->once()) - ->method('getReflector') - ->will($this->returnValue(new \ReflectionClass(__CLASS__))) - ; + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); - $definition = new Definition\MethodDefinition($mock, __FUNCTION__); - $reflector = new \ReflectionMethod(__CLASS__, __FUNCTION__); + $this->assertInstanceOf('ReflectionMethod', $definition->getReflector()); + $this->assertEquals('methodToTest', $definition->getReflector()->getName()); + } + + public function testMethodDefinitionAccessorsAndMutators_usingReflector() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); + + $this->assertInstanceOf('ReflectionMethod', $definition->getReflector()); + + $reflector = new \ReflectionMethod(__CLASS__, 'methodToTest'); $definition->setReflector($reflector); $this->assertSame($reflector, $definition->getReflector()); } - public function testMethodDefinition_accessorAndMutatorForRegistry() + public function testMethodDefinitionAccessors_usingArgumentReflector() { - $registryMock = $this->getMock('Wootook\\Core\\DependencyInjection\\Registry', array(), array(), '', false); - $classDefinitionMock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition', array('getReflector'), array(), '', false); + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); - $classDefinitionMock->expects($this->once()) - ->method('getReflector') - ->will($this->returnValue(new \ReflectionClass(__CLASS__))) - ; + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); - $definition = new Definition\MethodDefinition($classDefinitionMock, __FUNCTION__); - $definition->setRegistry($registryMock); + $reflector = $definition->getArgumentReflector(0); + $this->assertInstanceOf('ReflectionParameter', $reflector); + $this->assertEquals('fooArgumentOne', $reflector->getName()); + $this->assertEquals(0, $reflector->getPosition()); - $this->assertSame($registryMock, $definition->getRegistry()); + $reflector = $definition->getArgumentReflector('fooArgumentOne'); + $this->assertInstanceOf('ReflectionParameter', $reflector); + $this->assertEquals('fooArgumentOne', $reflector->getName()); + $this->assertEquals(0, $reflector->getPosition()); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\BadMethodCallException'); + $definition->getArgumentReflector(array()); } - public function testMethodDefinition_bindArgumentValue_withNumericArgumentIndex() + public function testMethodDefinitionArgumentDefinitions_initArgumentDefinition() { - $classDefinition = new Definition\ClassDefinition(__CLASS__); + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest', null, 'stdClass'); + + $argumentDefinition = $definition->initArgumentDefinition(0); + $this->assertInstanceOf('stdClass', $argumentDefinition); + + $argumentDefinition = $definition->initArgumentDefinition('fooArgumentOne'); + $this->assertInstanceOf('stdClass', $argumentDefinition); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); + $definition->initArgumentDefinition(array()); + } + + public function testMethodDefinitionArgumentDefinitions_registerArgumentDefinition() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + /** @var \Wootook\Core\DependencyInjection\Definition\ArgumentDefinition $argumentDefinition */ + $argumentDefinition = $this->getArgumentDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); + + $definition->registerArgumentDefinition(0, $argumentDefinition); + $this->assertCount(1, $definition->getAllArgumentDefinitions()); + + $definition->registerArgumentDefinition('fooArgumentOne', $argumentDefinition); + $this->assertCount(1, $definition->getAllArgumentDefinitions()); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); + $definition->registerArgumentDefinition(array(), $argumentDefinition); + } + + public function testMethodDefinitionArgumentDefinitions_addArgumentDefinition() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + /** @var \Wootook\Core\DependencyInjection\Definition\ArgumentDefinition $argumentDefinition */ + $argumentDefinition = $this->getArgumentDefinitionMock(array('__construct')); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest', null, get_class($argumentDefinition)); + + $definition->addArgumentDefinition(0); + $this->assertCount(1, $definition->getAllArgumentDefinitions()); + $this->assertInstanceOf(get_class($argumentDefinition), $definition->getAllArgumentDefinitions()[0]); + } + + public function testMethodDefinitionArgumentDefinitions_setArgumentDefinition() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + /** @var \Wootook\Core\DependencyInjection\Definition\ArgumentDefinition $argumentDefinition */ + $argumentDefinition = $this->getArgumentDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest', null, 'stdClass'); + + $definition->setArgumentDefinition(0, $argumentDefinition); + $this->assertCount(1, $definition->getAllArgumentDefinitions()); + $this->assertInstanceOf(get_class($argumentDefinition), $definition->getAllArgumentDefinitions()[0]); + + $definition->setArgumentDefinition('fooArgumentOne', $argumentDefinition); + $this->assertCount(1, $definition->getAllArgumentDefinitions()); + $this->assertInstanceOf(get_class($argumentDefinition), $definition->getAllArgumentDefinitions()[0]); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); + $definition->setArgumentDefinition(array(), $argumentDefinition); + } + + public function testMethodDefinitionBindings_bindArgumentValueWithNumericArgumentIndex() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('getArgumentDefinition'), array($classDefinition, 'methodToTest')); + $argumentDefinition = $this->getArgumentDefinitionMock(array('bindValue')); + $definition->expects($this->once()) ->method('getArgumentDefinition') - ->with(0) - ->will($this->returnValue(new Definition\ArgumentDefinition($definition, 0))) + ->with(0, true) + ->will($this->returnValue($argumentDefinition)) + ; + + $argumentDefinition->expects($this->once()) + ->method('bindValue') + ->with(65) ; /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ $definition->bindArgumentValue(0, 65); } - public function testMethodDefinition_bindArgumentVariable_withNumericArgumentIndex() + public function testMethodDefinitionBindings_bindArgumentValueWithNamedArgumentIndex() { - $classDefinition = new Definition\ClassDefinition(__CLASS__); + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('getArgumentDefinition'), array($classDefinition, 'methodToTest')); + $argumentDefinition = $this->getArgumentDefinitionMock(array('bindValue')); + $definition->expects($this->once()) ->method('getArgumentDefinition') - ->with(0) - ->will($this->returnValue(new Definition\ArgumentDefinition($definition, 0))) + ->with('fooArgumentOne', true) + ->will($this->returnValue($argumentDefinition)) ; - $variable = 65; - /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ - $definition->bindArgumentVariable(0, $variable); - } - - public function testMethodDefinition_bindArgumentRegistryEntry_withNumericArgumentIndex() - { - $classDefinition = new Definition\ClassDefinition(__CLASS__); - $registry = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Registry') - ->disableOriginalConstructor() - ->setMethods(array('get')) - ->getMock(); - - $registry->expects($this->any())->method('get')->will($this->returnValue(36)); - - $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', - array('getArgumentDefinition'), array($classDefinition, 'methodToTest', $registry)); - - $definition->expects($this->once()) - ->method('getArgumentDefinition') - ->with(0) - ->will($this->returnValue(new Definition\ArgumentDefinition($definition, 0, $registry))) - ; - - /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ - $definition->bindArgumentRegistryEntry(0, 'foo'); - } - - public function testMethodDefinition_bindArgumentValue_withNamedArgumentIndex() - { - $classDefinition = new Definition\ClassDefinition(__CLASS__); - - $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', - array('getArgumentDefinition'), array($classDefinition, 'methodToTest')); - - $definition->expects($this->once()) - ->method('getArgumentDefinition') - ->with('fooArgumentOne') - ->will($this->returnValue(new Definition\ArgumentDefinition($definition, 0))) + $argumentDefinition->expects($this->once()) + ->method('bindValue') + ->with(65) ; /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ $definition->bindArgumentValue('fooArgumentOne', 65); } - public function testMethodDefinition_bindArgumentVariable_withNamedArgumentIndex() + public function testMethodDefinitionBindings_bindArgumentVariableWithNumericArgumentIndex() { - $classDefinition = new Definition\ClassDefinition(__CLASS__); + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('getArgumentDefinition'), array($classDefinition, 'methodToTest')); + $argumentDefinition = $this->getArgumentDefinitionMock(array('bindVariable')); + $definition->expects($this->once()) ->method('getArgumentDefinition') - ->with('fooArgumentOne') - ->will($this->returnValue(new Definition\ArgumentDefinition($definition, 0))) + ->with(0, true) + ->will($this->returnValue($argumentDefinition)) ; - $variable = 65; + $argumentDefinition->expects($this->once()) + ->method('bindVariable') + ->with(42) + ; + + $variable = 42; + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $definition->bindArgumentVariable(0, $variable); + } + + public function testMethodDefinitionBindings_bindArgumentVariableWithNamedArgumentIndex() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', + array('getArgumentDefinition'), array($classDefinition, 'methodToTest')); + + $argumentDefinition = $this->getArgumentDefinitionMock(array('bindVariable')); + + $definition->expects($this->once()) + ->method('getArgumentDefinition') + ->with('fooArgumentOne', true) + ->will($this->returnValue($argumentDefinition)) + ; + + $argumentDefinition->expects($this->once()) + ->method('bindVariable') + ->with(42) + ; + + $variable = 42; /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ $definition->bindArgumentVariable('fooArgumentOne', $variable); } - public function testMethodDefinition_bindArgumentRegistryEntry_withNamedArgumentIndex() + public function testMethodDefinitionBindings_bindArgumentRegistryEntryWithNumericArgumentIndex() { - $classDefinition = new Definition\ClassDefinition(__CLASS__); - $registry = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Registry') - ->disableOriginalConstructor() - ->setMethods(array('get')) - ->getMock(); - - $registry->expects($this->any())->method('get')->will($this->returnValue(36)); + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + /** @var \Wootook\Core\DependencyInjection\Registry $registry */ + $registry = $this->getRegistryMock(); $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('getArgumentDefinition'), array($classDefinition, 'methodToTest', $registry)); + $argumentDefinition = $this->getArgumentDefinitionMock(array('bindRegistryEntry')); + $definition->expects($this->once()) ->method('getArgumentDefinition') - ->with('fooArgumentOne') - ->will($this->returnValue(new Definition\ArgumentDefinition($definition, 0, $registry))) + ->with(0, true) + ->will($this->returnValue($argumentDefinition)) + ; + + $argumentDefinition->expects($this->once()) + ->method('bindRegistryEntry') + ->with('foo') + ; + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $definition->bindArgumentRegistryEntry(0, 'foo'); + } + + public function testMethodDefinitionBindings_bindArgumentRegistryEntryWithNamedArgumentIndex() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + /** @var \Wootook\Core\DependencyInjection\Registry $registry */ + $registry = $this->getRegistryMock(); + + $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', + array('getArgumentDefinition'), array($classDefinition, 'methodToTest', $registry)); + + $argumentDefinition = $this->getArgumentDefinitionMock(array('bindRegistryEntry')); + + $definition->expects($this->once()) + ->method('getArgumentDefinition') + ->with('fooArgumentOne', true) + ->will($this->returnValue($argumentDefinition)) + ; + + $argumentDefinition->expects($this->once()) + ->method('bindRegistryEntry') + ->with('foo') ; /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ $definition->bindArgumentRegistryEntry('fooArgumentOne', 'foo'); } - public function testMethodDefinition_bindArgumentRegistryEntry_withNoRegistry() + public function testMethodDefinitionBindings_bindArgumentRegistryEntryWithNoRegistry() { - $classDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition') - ->disableOriginalConstructor() - ->getMock() - ; - - $classDefinition->expects($this->once()) - ->method('getReflector') - ->will($this->returnValue(new \ReflectionClass(__CLASS__))) - ; + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('getArgumentDefinition'), array($classDefinition, 'methodToTest')); + $argumentDefinition = $this->getArgumentDefinitionMock(array('bindRegistryEntry')); + + $definition->expects($this->once()) + ->method('getArgumentDefinition') + ->with('fooArgumentOne', true) + ->will($this->returnValue($argumentDefinition)) + ; + + $argumentDefinition->expects($this->once()) + ->method('bindRegistryEntry') + ->with('foo') + ->will($this->throwException(new \Wootook\Core\Exception\DependencyInjection\RuntimeException)) + ; $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\RuntimeException'); /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ $definition->bindArgumentRegistryEntry('fooArgumentOne', 'foo'); } - public function testMethodDefinition_getArgumentDefinition_withInvalidArgumentId() + public function testMethodDefinition_getArgumentDefinitionWithInvalidArgumentId() { - $classDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition') - ->disableOriginalConstructor() - ->getMock() - ; - - $classDefinition->expects($this->once()) - ->method('getReflector') - ->will($this->returnValue(new \ReflectionClass(__CLASS__))) - ; + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); @@ -270,15 +435,8 @@ class MethodDefinitionTest extends \PHPUnit_Framework_TestCase public function testMethodDefinition_getArgumentReflector_withNumericArgumentId() { - $classDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition') - ->disableOriginalConstructor() - ->getMock() - ; - - $classDefinition->expects($this->once()) - ->method('getReflector') - ->will($this->returnValue(new \ReflectionClass(__CLASS__))) - ; + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); @@ -288,17 +446,23 @@ class MethodDefinitionTest extends \PHPUnit_Framework_TestCase $this->assertEquals(0, $reflector->getPosition()); } + public function testMethodDefinition_getArgumentReflector_withInexistentNumericArgumentId() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\BadMethodCallException'); + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $definition->getArgumentReflector(5); + } + public function testMethodDefinition_getArgumentReflector_withNamedArgumentId() { - $classDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition') - ->disableOriginalConstructor() - ->getMock() - ; - - $classDefinition->expects($this->once()) - ->method('getReflector') - ->will($this->returnValue(new \ReflectionClass(__CLASS__))) - ; + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); @@ -308,23 +472,139 @@ class MethodDefinitionTest extends \PHPUnit_Framework_TestCase $this->assertEquals(0, $reflector->getPosition()); } + public function testMethodDefinition_getArgumentReflector_withInexistentNamedArgumentId() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\BadMethodCallException'); + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $definition->getArgumentReflector('inexistentArgument'); + } + public function testMethodDefinition_getArgumentReflector_withInvalidArgumentId() { - $classDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition') - ->disableOriginalConstructor() - ->getMock() - ; + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); - $classDefinition->expects($this->once()) - ->method('getReflector') - ->will($this->returnValue(new \ReflectionClass(__CLASS__))) - ; + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\BadMethodCallException'); + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $definition->getArgumentReflector(1.5); + } + + public function testMethodDefinition_getArgumentDefinition_withNumericArgumentId() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $argumentDefinition = $definition->getArgumentDefinition(0); + $this->assertInstanceOf('Wootook\\Core\\DependencyInjection\\Definition\\ArgumentDefinition', $argumentDefinition); + } + + public function testMethodDefinition_getArgumentDefinition_withInexistentNumericArgumentId() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\BadMethodCallException'); + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $definition->getArgumentDefinition(5); + } + + public function testMethodDefinition_getArgumentDefinition_withNamedArgumentId() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $argumentDefinition = $definition->getArgumentDefinition('fooArgumentOne'); + $this->assertInstanceOf('Wootook\\Core\\DependencyInjection\\Definition\\ArgumentDefinition', $argumentDefinition); + } + + public function testMethodDefinition_getArgumentDefinition_withInexistentNamedArgumentId() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ - $definition->getArgumentReflector(1.5); + $definition->getArgumentDefinition('inexistentArgument'); + } + + public function testMethodDefinition_getArgumentDefinition_withInvalidArgumentId() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); + + $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $definition->getArgumentDefinition(1.5); + } + + public function testMethodDefinition_getArgumentDefinition_withExistingArgument() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $definition->addArgumentDefinition(0); + + $argumentDefinition = $definition->getArgumentDefinition(0); + $this->assertInstanceOf('Wootook\\Core\\DependencyInjection\\Definition\\ArgumentDefinition', $argumentDefinition); + } + + public function testMethodDefinition_getArgumentDefinition_withInexistingArgumentRegistered() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $argumentDefinition = $definition->getArgumentDefinition(0, true); + $this->assertInstanceOf('Wootook\\Core\\DependencyInjection\\Definition\\ArgumentDefinition', $argumentDefinition); + } + + public function testMethodDefinition_resetArgumentDefinitionList() + { + /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */ + $classDefinition = $this->getClassDefinitionMock(); + + $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); + + /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ + $definition->getArgumentDefinition(0, true); + $definition->getArgumentDefinition(1, true); + + $all = $definition->getAllArgumentDefinitions(); + $this->assertInternalType('array', $all); + $this->assertCount(2, $all); + + $definition->reset(); + $all = $definition->getAllArgumentDefinitions(); + $this->assertInternalType('array', $all); + $this->assertCount(0, $all); } } diff --git a/src/application/modules/WootookCore/test/phpunit.xml b/src/application/modules/WootookCore/test/phpunit.xml index 6271b05..76bb90c 100644 --- a/src/application/modules/WootookCore/test/phpunit.xml +++ b/src/application/modules/WootookCore/test/phpunit.xml @@ -21,15 +21,15 @@ --> - - --> +