Updated DependencyInjection component's API

Signed-off-by: Gregory PLANCHAT <g.planchat@gmail.com>
This commit is contained in:
Gregory PLANCHAT 2012-07-19 19:23:30 +02:00
commit af103cce2c
6 changed files with 872 additions and 294 deletions

View file

@ -55,7 +55,9 @@ class ArgumentDefinition
$this->setReflector($methodDefinition->getArgumentReflector($argumentName)); $this->setReflector($methodDefinition->getArgumentReflector($argumentName));
$this->_registry = $registry; if ($registry !== null) {
$this->setRegistry($registry);
}
} }
public function setRegistry(DependencyInjection\Registry $registry) public function setRegistry(DependencyInjection\Registry $registry)

View file

@ -1,4 +1,32 @@
<?php <?php
/**
* This file is part of Wootook
*
* @license http://www.gnu.org/licenses/agpl-3.0.txt
* @see http://wootook.org/
*
* Copyright (c) 2011-Present, Grégory PLANCHAT <g.planchat@gmail.com>
* 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 <http://www.gnu.org/licenses/>.
*
* --> 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; namespace Wootook\Core\DependencyInjection\Definition;
@ -7,21 +35,47 @@ use Wootook\Core\Base\Service,
Wootook\Core\DependencyInjection, Wootook\Core\DependencyInjection,
Wootook\Core\Exception as CoreException; 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 <g.planchat@gmail.com>
* @see http://wootook.org/
*/
class ClassDefinition class ClassDefinition
{ {
/**
* @var string
*/
protected $_className = null; protected $_className = null;
/**
* @var \ReflectionClass
*/
protected $_reflector = null; protected $_reflector = null;
/**
* @var array
*/
protected $_methodDefinitions = array(); protected $_methodDefinitions = array();
/**
* @var null|\Wootook\Core\DependencyInjection\Registry
*/
protected $_registry = null; protected $_registry = null;
/**
* @var string
*/
protected $_methodDefinitionClassName = 'Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition'; protected $_methodDefinitionClassName = 'Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition';
/** /**
* @param string $className * @param string $className
* @param null|string $methodDefinitionHandlerClass * @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; $this->_className = $className;
try { try {
@ -30,13 +84,19 @@ class ClassDefinition
throw new CoreException\DependencyInjection\BadMethodCallException($e->getMessage(), $e->getCode(), $e); throw new CoreException\DependencyInjection\BadMethodCallException($e->getMessage(), $e->getCode(), $e);
} }
if (is_string($methodDefinitionHandlerClass)) { if ($methodDefinitionClassName !== null) {
$this->_methodDefinitionHandlerClass = $methodDefinitionHandlerClass; $this->setMethodDefinitionClassName($methodDefinitionClassName);
} else { }
$this->_methodDefinitionHandlerClass = __NAMESPACE__ . '\\MethodDefinition';
if ($registry !== null) {
$this->setRegistry($registry);
} }
} }
/**
* @param \Wootook\Core\DependencyInjection\Registry $registry
* @return ClassDefinition
*/
public function setRegistry(DependencyInjection\Registry $registry) public function setRegistry(DependencyInjection\Registry $registry)
{ {
$this->_registry = $registry; $this->_registry = $registry;
@ -44,6 +104,9 @@ class ClassDefinition
return $this; return $this;
} }
/**
* @return null|\Wootook\Core\DependencyInjection\Registry
*/
public function getRegistry() public function getRegistry()
{ {
return $this->_registry; return $this->_registry;
@ -77,11 +140,21 @@ class ClassDefinition
*/ */
public function initMethodDefinition($methodName) public function initMethodDefinition($methodName)
{ {
if (!is_string($methodName)) {
throw new CoreException\DependencyInjection\InvalidArgumentException('Method names only accept string.');
}
$class = $this->getMethodDefinitionClassName(); $class = $this->getMethodDefinitionClassName();
return new $class($this, $methodName, $this->getRegistry()); 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) public function registerMethodDefinition($methodName, MethodDefinition $methodDefinition)
{ {
if (!is_string($methodName)) { if (!is_string($methodName)) {
@ -93,6 +166,10 @@ class ClassDefinition
return $this; return $this;
} }
/**
* @param $methodName
* @return ClassDefinition
*/
public function addMethodDefinition($methodName) public function addMethodDefinition($methodName)
{ {
$this->registerMethodDefinition($methodName, $this->initMethodDefinition($methodName)); $this->registerMethodDefinition($methodName, $this->initMethodDefinition($methodName));
@ -100,6 +177,12 @@ class ClassDefinition
return $this; return $this;
} }
/**
* @param $methodName
* @param MethodDefinition $definition
* @return ClassDefinition
* @throws \Wootook\Core\Exception\DependencyInjection\InvalidArgumentException
*/
public function setMethodDefinition($methodName, MethodDefinition $definition) public function setMethodDefinition($methodName, MethodDefinition $definition)
{ {
if (!is_string($methodName)) { if (!is_string($methodName)) {
@ -111,25 +194,43 @@ class ClassDefinition
return $this; return $this;
} }
/**
* @param $methodDefinitionClassName
* @return string
* @throws \Wootook\Core\Exception\DependencyInjection\InvalidArgumentException
*/
public function setMethodDefinitionClassName($methodDefinitionClassName) public function setMethodDefinitionClassName($methodDefinitionClassName)
{ {
if (!is_string($methodDefinitionClassName)) { if (!is_string($methodDefinitionClassName)) {
throw new CoreException\DependencyInjection\InvalidArgumentException('Class names only accept string.'); throw new CoreException\DependencyInjection\InvalidArgumentException('Class names only accept string.');
} }
return $this->_methodDefinitionClassName; $this->_methodDefinitionClassName = $methodDefinitionClassName;
return $this;
} }
/**
* @return string
*/
public function getMethodDefinitionClassName() public function getMethodDefinitionClassName()
{ {
return $this->_methodDefinitionClassName; return $this->_methodDefinitionClassName;
} }
/**
* @return array
*/
public function getAllMethodDefinitions() public function getAllMethodDefinitions()
{ {
return $this->_methodDefinitions; return $this->_methodDefinitions;
} }
/**
* @param array $args
* @return object
* @throws \Wootook\Core\Exception\DependencyInjection\RuntimeException
*/
public function newInstance(Array $args = array()) public function newInstance(Array $args = array())
{ {
try { try {
@ -145,6 +246,10 @@ class ClassDefinition
} }
} }
/**
* @return mixed
* @throws \Wootook\Core\Exception\DependencyInjection\RuntimeException
*/
public function newInstanceWithoutConstructor() public function newInstanceWithoutConstructor()
{ {
try { try {
@ -154,16 +259,26 @@ class ClassDefinition
} }
} }
/**
* @return array
*/
public function reset() public function reset()
{ {
return $this->_methodDefinitions = array(); return $this->_methodDefinitions = array();
} }
/**
* @return null|\ReflectionClass
*/
public function getReflector() public function getReflector()
{ {
return $this->_reflector; return $this->_reflector;
} }
/**
* @param \ReflectionClass $reflector
* @return ClassDefinition
*/
public function setReflector(\ReflectionClass $reflector) public function setReflector(\ReflectionClass $reflector)
{ {
$this->_reflector = $reflector; $this->_reflector = $reflector;

View file

@ -51,7 +51,7 @@ class MethodDefinition
* @param $methodName * @param $methodName
* @param null|\Wootook\Core\DependencyInjection\Registry $registry * @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; $this->_classDefinition = $classDefinition;
@ -63,11 +63,13 @@ class MethodDefinition
throw new CoreException\DependencyInjection\BadMethodCallException($e->getMessage(), $e->getCode(), $e); throw new CoreException\DependencyInjection\BadMethodCallException($e->getMessage(), $e->getCode(), $e);
} }
if (is_string($argumentDefinitionHandlerClass)) { if ($argumentDefinitionClassName !== null) {
$this->setArgumentDefinitionClassName($argumentDefinitionHandlerClass); $this->setArgumentDefinitionClassName($argumentDefinitionClassName);
} }
$this->_registry = $registry; if ($registry !== null) {
$this->setRegistry($registry);
}
} }
/** /**
@ -133,7 +135,12 @@ class MethodDefinition
public function registerArgumentDefinition($argumentPosition, ArgumentDefinition $argumentDefinition) public function registerArgumentDefinition($argumentPosition, ArgumentDefinition $argumentDefinition)
{ {
if (is_string($argumentPosition) && isset($this->_argumentIndex[$argumentPosition])) { if (is_string($argumentPosition) && isset($this->_argumentIndex[$argumentPosition])) {
$argumentName = $argumentPosition;
$argumentPosition = $this->_argumentIndex[$argumentPosition]; $argumentPosition = $this->_argumentIndex[$argumentPosition];
} else if (($reflector = $argumentDefinition->getReflector()) instanceof \ReflectionParameter) {
$argumentName = $reflector->getName();
} else {
$argumentName = null;
} }
if (!is_int($argumentPosition)) { if (!is_int($argumentPosition)) {
@ -141,6 +148,9 @@ class MethodDefinition
} }
$this->_argumentDefinitions[$argumentPosition] = $argumentDefinition; $this->_argumentDefinitions[$argumentPosition] = $argumentDefinition;
if ($argumentName !== null) {
$this->_argumentIndex[$argumentName] = $argumentPosition;
}
return $this; return $this;
} }
@ -167,13 +177,33 @@ class MethodDefinition
return $this; return $this;
} }
/**
* @return array
*/
public function getAllArgumentDefinitions()
{
return $this->_argumentDefinitions;
}
/**
* @return array
*/
public function reset()
{
$this->_argumentDefinitions = array();
return $this;
}
public function setArgumentDefinitionClassName($argumentDefinitionClassName) public function setArgumentDefinitionClassName($argumentDefinitionClassName)
{ {
if (!is_string($argumentDefinitionClassName)) { if (!is_string($argumentDefinitionClassName)) {
throw new CoreException\DependencyInjection\InvalidArgumentException('Class names only accept string.'); throw new CoreException\DependencyInjection\InvalidArgumentException('Class names only accept string.');
} }
return $this->_argumentDefinitionClassName; $this->_argumentDefinitionClassName = $argumentDefinitionClassName;
return $this;
} }
public function getArgumentDefinitionClassName() public function getArgumentDefinitionClassName()
@ -216,23 +246,11 @@ class MethodDefinition
*/ */
public function bindArgumentRegistryEntry($argumentPosition, $registryKey) public function bindArgumentRegistryEntry($argumentPosition, $registryKey)
{ {
if (($registry = $this->getRegistry()) === null) {
throw new CoreException\DependencyInjection\RuntimeException('No registry available');
}
$this->getArgumentDefinition($argumentPosition, true)->bindRegistryEntry($registryKey); $this->getArgumentDefinition($argumentPosition, true)->bindRegistryEntry($registryKey);
return $this; return $this;
} }
/**
* @return array
*/
public function getAllArgumentDefinitions()
{
return $this->_argumentDefinitions;
}
/** /**
* @return \ReflectionMethod * @return \ReflectionMethod
*/ */
@ -272,7 +290,7 @@ class MethodDefinition
} }
if (!is_int($argumentPosition) || !isset($this->_argumentReflectors[$argumentPosition])) { 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]; return $this->_argumentReflectors[$argumentPosition];

View file

@ -7,53 +7,177 @@ use Wootook\Core\Config,
class ClassDefinitionTest extends \PHPUnit_Framework_TestCase 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() public function testClassDefinitionInstantiation_withNonExistingClass()
{ {
$this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\BadMethodCallException'); $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\BadMethodCallException');
new Definition\ClassDefinition('InexistentClass'); new Definition\ClassDefinition('InexistentClass');
} }
public function testClassDefinitionInstantiation_withSpecialMethodDefinitionClassName() public function testClassDefinitionInstantiation_withExistingClass()
{ {
$methodDefinitionClassName = \uniqid('WootookUnit_Core_DependencyInjection_Mock_MethodDefinition_TestNewInstance_'); $definition = new Definition\ClassDefinition('stdClass');
$this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), $methodDefinitionClassName, false);
$definition = new Definition\ClassDefinition(__CLASS__, null, $methodDefinitionClassName); $this->assertEquals('stdClass', $definition->getReflector()->getName());
$this->assertInstanceOf($methodDefinitionClassName, $definition->getMethodDefinition(__FUNCTION__));
} }
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 = new Definition\ClassDefinition('stdClass', $registry);
$definition->setMethodDefinition(__FUNCTION__, $mock);
$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__); $definition = new Definition\ClassDefinition(__CLASS__);
$this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); $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 = 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() public function testGetAllMethodDefinitions()
{ {
$mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), '', false); $mock = $this->getMethodDefinitionMock();
$definition = new Definition\ClassDefinition(__CLASS__); $definition = new Definition\ClassDefinition(__CLASS__);
$definition->setMethodDefinition('fooMethod', $mock); $definition->setMethodDefinition('fooMethod', $mock);
@ -66,7 +190,7 @@ class ClassDefinitionTest extends \PHPUnit_Framework_TestCase
public function testReset() public function testReset()
{ {
$mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), '', false); $mock = $this->getMethodDefinitionMock();
$definition = new Definition\ClassDefinition(__CLASS__); $definition = new Definition\ClassDefinition(__CLASS__);
$definition->setMethodDefinition('fooMethod', $mock); $definition->setMethodDefinition('fooMethod', $mock);
@ -76,127 +200,166 @@ class ClassDefinitionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(), $definition->getAllMethodDefinitions()); $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() // public function testClassDefinitionInstantiation_assignNewMethodDefinitionInstance()
{ // {
$definition = new Definition\ClassDefinition(__CLASS__); // $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), '', false);
$reflector = new \ReflectionClass(__CLASS__); //
$definition->setReflector($reflector); // $definition = new Definition\ClassDefinition(__CLASS__);
// $definition->setMethodDefinition(__FUNCTION__, $mock);
$this->assertSame($reflector, $definition->getReflector()); //
} // $this->assertSame($mock, $definition->getMethodDefinition(__FUNCTION__));
// }
public function testNewInstance_hasNoConstructor() //
{ // public function testGetMethodDefinition_withInvalidMethodName()
$this->markTestSkipped("Method ReflectionClass::newInstance() couldn't be mocked due to API<->Reflection inconsistencies."); // {
// $definition = new Definition\ClassDefinition(__CLASS__);
$reflectionClassMock = $this->getMock('ReflectionClass', array('hasMethod', 'newInstance'), array('stdClass')); //
$reflectionClassMock->expects($this->once()) // $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException');
->method('hasMethod') // $definition->getMethodDefinition(42);
->with('__construct') // }
->will($this->returnValue(false)) //
; // public function testSetMethodDefinition_withInvalidMethodName()
$reflectionClassMock->expects($this->once()) // {
->method('newInstance') // $mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array(), array(), '', false);
->will($this->returnValue(new \stdClass())) //
; // $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException');
//
$classDefinition = new Definition\ClassDefinition(__CLASS__); // $definition = new Definition\ClassDefinition(__CLASS__);
$classDefinition->setReflector($reflectionClassMock); // $definition->setMethodDefinition(42, $mock);
// }
$this->assertInstanceOf('stdClass', $classDefinition->newInstance()); //
} // public function testClassDefinitionAccessors_getReflector()
// {
public function testNewInstance_usingConstructor() // $definition = new Definition\ClassDefinition(__CLASS__);
{ //
$expectedArguments = array(42); // $this->assertInstanceOf('\\ReflectionClass', $definition->getReflector());
// }
$reflectionClassMock = $this->getMock('ReflectionClass', array('hasMethod', 'newInstanceArgs'), array('stdClass')); //
$reflectionClassMock->expects($this->once()) // public function testClassDefinitionMutators_setReflector()
->method('hasMethod') // {
->with('__construct') // $definition = new Definition\ClassDefinition(__CLASS__);
->will($this->returnValue(true)) // $reflector = new \ReflectionClass(__CLASS__);
; // $definition->setReflector($reflector);
$reflectionClassMock->expects($this->once()) //
->method('newInstanceArgs') // $this->assertSame($reflector, $definition->getReflector());
->with($expectedArguments) // }
->will($this->returnValue(new \stdClass())) //
; // public function testNewInstance_hasNoConstructor()
// {
$classDefinition = new Definition\ClassDefinition(__CLASS__); // $this->markTestSkipped("Method ReflectionClass::newInstance() couldn't be mocked due to API<->Reflection inconsistencies.");
$classDefinition->setReflector($reflectionClassMock); //
// $reflectionClassMock = $this->getMock('ReflectionClass', array('hasMethod', 'newInstance'), array('stdClass'));
$methodDefinitionMock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('compileArgs'), array(), '', false); // $reflectionClassMock->expects($this->once())
$methodDefinitionMock->expects($this->once()) // ->method('hasMethod')
->method('compileArgs') // ->with('__construct')
->will($this->returnValue(array(42))) // ->will($this->returnValue(false))
; // ;
$classDefinition->setMethodDefinition('__construct', $methodDefinitionMock); // $reflectionClassMock->expects($this->once())
// ->method('newInstance')
$this->assertInstanceOf('stdClass', $classDefinition->newInstance()); // ->will($this->returnValue(new \stdClass()))
} // ;
//
public function testNewInstance_usingNonPublicConstructor() // $classDefinition = new Definition\ClassDefinition(__CLASS__);
{ // $classDefinition->setReflector($reflectionClassMock);
$reflectionClassMock = $this->getMock('ReflectionClass', array('hasMethod', 'newInstanceArgs'), array('stdClass')); //
$reflectionClassMock->expects($this->once()) // $this->assertInstanceOf('stdClass', $classDefinition->newInstance());
->method('hasMethod') // }
->with('__construct') //
->will($this->returnValue(true)) // public function testNewInstance_usingConstructor()
; // {
$reflectionClassMock->expects($this->once()) // $expectedArguments = array(42);
->method('newInstanceArgs') //
->will($this->throwException($this->getMock('ReflectionException'))) // $reflectionClassMock = $this->getMock('ReflectionClass', array('hasMethod', 'newInstanceArgs'), array('stdClass'));
; // $reflectionClassMock->expects($this->once())
// ->method('hasMethod')
$classDefinition = new Definition\ClassDefinition(__CLASS__); // ->with('__construct')
$classDefinition->setReflector($reflectionClassMock); // ->will($this->returnValue(true))
// ;
$methodDefinitionMock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('compileArgs'), array(), '', false); // $reflectionClassMock->expects($this->once())
$methodDefinitionMock->expects($this->once()) // ->method('newInstanceArgs')
->method('compileArgs') // ->with($expectedArguments)
->will($this->returnValue(array(42))) // ->will($this->returnValue(new \stdClass()))
; // ;
$classDefinition->setMethodDefinition('__construct', $methodDefinitionMock); //
// $classDefinition = new Definition\ClassDefinition(__CLASS__);
$this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\RuntimeException'); // $classDefinition->setReflector($reflectionClassMock);
//
$this->assertInstanceOf('stdClass', $classDefinition->newInstance()); // $methodDefinitionMock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('compileArgs'), array(), '', false);
} // $methodDefinitionMock->expects($this->once())
// ->method('compileArgs')
public function testNewInstanceWithoutConstructor() // ->will($this->returnValue(array(42)))
{ // ;
$reflectionClassMock = $this->getMock('ReflectionClass', array('newInstanceWithoutConstructor'), array('stdClass')); // $classDefinition->setMethodDefinition('__construct', $methodDefinitionMock);
$reflectionClassMock->expects($this->once()) //
->method('newInstanceWithoutConstructor') // $this->assertInstanceOf('stdClass', $classDefinition->newInstance());
->will($this->returnValue(new \stdClass())) // }
; //
// public function testNewInstance_usingNonPublicConstructor()
$classDefinition = new Definition\ClassDefinition(__CLASS__); // {
$classDefinition->setReflector($reflectionClassMock); // $reflectionClassMock = $this->getMock('ReflectionClass', array('hasMethod', 'newInstanceArgs'), array('stdClass'));
// $reflectionClassMock->expects($this->once())
$this->assertInstanceOf('stdClass', $classDefinition->newInstanceWithoutConstructor()); // ->method('hasMethod')
} // ->with('__construct')
// ->will($this->returnValue(true))
public function testNewInstanceWithoutConstructor_throwsException() // ;
{ // $reflectionClassMock->expects($this->once())
$reflectionClassMock = $this->getMock('ReflectionClass', array('newInstanceWithoutConstructor'), array('stdClass')); // ->method('newInstanceArgs')
$reflectionClassMock->expects($this->once()) // ->will($this->throwException($this->getMock('ReflectionException')))
->method('newInstanceWithoutConstructor') // ;
->will($this->throwException($this->getMock('ReflectionException'))) //
; // $classDefinition = new Definition\ClassDefinition(__CLASS__);
// $classDefinition->setReflector($reflectionClassMock);
$classDefinition = new Definition\ClassDefinition(__CLASS__); //
$classDefinition->setReflector($reflectionClassMock); // $methodDefinitionMock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', array('compileArgs'), array(), '', false);
// $methodDefinitionMock->expects($this->once())
$this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\RuntimeException'); // ->method('compileArgs')
// ->will($this->returnValue(array(42)))
$this->assertInstanceOf('stdClass', $classDefinition->newInstanceWithoutConstructor()); // ;
} // $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());
// }
} }

View file

@ -13,252 +13,417 @@ class MethodDefinitionTest extends \PHPUnit_Framework_TestCase
// This method exists only for testing method introspection with the Dependency Injection component // 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') ->method('getReflector')
->will($this->returnValue(new \ReflectionClass(__CLASS__))) ->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'); $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() public function testMethodDefinitionInstantiation_withoutRegistry_withSpecialArgumentDefinitionClassName()
{ {
$argumentDefinitionClassName = \uniqid('WootookUnit_Core_DependencyInjection_Mock_ArgumentDefinition_TestNewInstance_'); /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */
$this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\ArgumentDefinition', array(), array(), $argumentDefinitionClassName, false); $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()) $this->assertEquals('stdClass', $definition->getArgumentDefinitionClassName());
->method('getReflector')
->will($this->returnValue(new \ReflectionClass(__CLASS__)))
;
$definition = new Definition\MethodDefinition($mock, 'methodToTest', null, $argumentDefinitionClassName);
$this->assertInstanceOf($argumentDefinitionClassName, $definition->getArgumentDefinition('fooArgumentOne'));
} }
public function testMethodDefinitionInstantiation_withRegistry_withSpecialArgumentDefinitionClassName() public function testMethodDefinitionInstantiation_withRegistry_withSpecialArgumentDefinitionClassName()
{ {
$app = $this->getMock('Wootook\\Core\\App\\App', array(), array(), '', false);
/** @var \Wootook\Core\DependencyInjection\Registry $registry */ /** @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_'); /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */
$this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\ArgumentDefinition', array(), array(), $argumentDefinitionClassName, false); $classDefinition = $this->getClassDefinitionMock();
/** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $mock */ $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest', $registry, 'stdClass');
$mock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition', array('getReflector'), array(), '', false);
$mock->expects($this->once()) $this->assertEquals('stdClass', $definition->getArgumentDefinitionClassName());
->method('getReflector') $this->assertSame($registry, $definition->getRegistry());
->will($this->returnValue(new \ReflectionClass(__CLASS__)))
;
$definition = new Definition\MethodDefinition($mock, 'methodToTest', $registry, $argumentDefinitionClassName);
$this->assertInstanceOf($argumentDefinitionClassName, $definition->getArgumentDefinition('fooArgumentOne'));
} }
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()) /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */
->method('getReflector') $classDefinition = $this->getClassDefinitionMock();
->will($this->returnValue(new \ReflectionClass(__CLASS__)))
;
$definition = new Definition\MethodDefinition($mock, __FUNCTION__); $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException');
$definition = new Definition\MethodDefinition($classDefinition, 'methodToTest', $registry, 32);
$this->assertInstanceOf('\\ReflectionMethod', $definition->getReflector());
} }
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()) $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest');
->method('getReflector')
->will($this->returnValue(new \ReflectionClass(__CLASS__)))
;
$definition = new Definition\MethodDefinition($mock, __FUNCTION__); $this->assertInstanceOf('ReflectionMethod', $definition->getReflector());
$reflector = new \ReflectionMethod(__CLASS__, __FUNCTION__); $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); $definition->setReflector($reflector);
$this->assertSame($reflector, $definition->getReflector()); $this->assertSame($reflector, $definition->getReflector());
} }
public function testMethodDefinition_accessorAndMutatorForRegistry() public function testMethodDefinitionAccessors_usingArgumentReflector()
{ {
$registryMock = $this->getMock('Wootook\\Core\\DependencyInjection\\Registry', array(), array(), '', false); /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */
$classDefinitionMock = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition', array('getReflector'), array(), '', false); $classDefinition = $this->getClassDefinitionMock();
$classDefinitionMock->expects($this->once()) $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest');
->method('getReflector')
->will($this->returnValue(new \ReflectionClass(__CLASS__)))
;
$definition = new Definition\MethodDefinition($classDefinitionMock, __FUNCTION__); $reflector = $definition->getArgumentReflector(0);
$definition->setRegistry($registryMock); $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', $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition',
array('getArgumentDefinition'), array($classDefinition, 'methodToTest')); array('getArgumentDefinition'), array($classDefinition, 'methodToTest'));
$argumentDefinition = $this->getArgumentDefinitionMock(array('bindValue'));
$definition->expects($this->once()) $definition->expects($this->once())
->method('getArgumentDefinition') ->method('getArgumentDefinition')
->with(0) ->with(0, true)
->will($this->returnValue(new Definition\ArgumentDefinition($definition, 0))) ->will($this->returnValue($argumentDefinition))
;
$argumentDefinition->expects($this->once())
->method('bindValue')
->with(65)
; ;
/** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */
$definition->bindArgumentValue(0, 65); $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', $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition',
array('getArgumentDefinition'), array($classDefinition, 'methodToTest')); array('getArgumentDefinition'), array($classDefinition, 'methodToTest'));
$argumentDefinition = $this->getArgumentDefinitionMock(array('bindValue'));
$definition->expects($this->once()) $definition->expects($this->once())
->method('getArgumentDefinition') ->method('getArgumentDefinition')
->with(0) ->with('fooArgumentOne', true)
->will($this->returnValue(new Definition\ArgumentDefinition($definition, 0))) ->will($this->returnValue($argumentDefinition))
; ;
$variable = 65; $argumentDefinition->expects($this->once())
/** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ ->method('bindValue')
$definition->bindArgumentVariable(0, $variable); ->with(65)
}
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)))
; ;
/** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */
$definition->bindArgumentValue('fooArgumentOne', 65); $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', $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition',
array('getArgumentDefinition'), array($classDefinition, 'methodToTest')); array('getArgumentDefinition'), array($classDefinition, 'methodToTest'));
$argumentDefinition = $this->getArgumentDefinitionMock(array('bindVariable'));
$definition->expects($this->once()) $definition->expects($this->once())
->method('getArgumentDefinition') ->method('getArgumentDefinition')
->with('fooArgumentOne') ->with(0, true)
->will($this->returnValue(new Definition\ArgumentDefinition($definition, 0))) ->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 */ /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */
$definition->bindArgumentVariable('fooArgumentOne', $variable); $definition->bindArgumentVariable('fooArgumentOne', $variable);
} }
public function testMethodDefinition_bindArgumentRegistryEntry_withNamedArgumentIndex() public function testMethodDefinitionBindings_bindArgumentRegistryEntryWithNumericArgumentIndex()
{ {
$classDefinition = new Definition\ClassDefinition(__CLASS__); /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */
$registry = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Registry') $classDefinition = $this->getClassDefinitionMock();
->disableOriginalConstructor() /** @var \Wootook\Core\DependencyInjection\Registry $registry */
->setMethods(array('get')) $registry = $this->getRegistryMock();
->getMock();
$registry->expects($this->any())->method('get')->will($this->returnValue(36));
$definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition',
array('getArgumentDefinition'), array($classDefinition, 'methodToTest', $registry)); array('getArgumentDefinition'), array($classDefinition, 'methodToTest', $registry));
$argumentDefinition = $this->getArgumentDefinitionMock(array('bindRegistryEntry'));
$definition->expects($this->once()) $definition->expects($this->once())
->method('getArgumentDefinition') ->method('getArgumentDefinition')
->with('fooArgumentOne') ->with(0, true)
->will($this->returnValue(new Definition\ArgumentDefinition($definition, 0, $registry))) ->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 */ /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */
$definition->bindArgumentRegistryEntry('fooArgumentOne', 'foo'); $definition->bindArgumentRegistryEntry('fooArgumentOne', 'foo');
} }
public function testMethodDefinition_bindArgumentRegistryEntry_withNoRegistry() public function testMethodDefinitionBindings_bindArgumentRegistryEntryWithNoRegistry()
{ {
$classDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition') /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */
->disableOriginalConstructor() $classDefinition = $this->getClassDefinitionMock();
->getMock()
;
$classDefinition->expects($this->once())
->method('getReflector')
->will($this->returnValue(new \ReflectionClass(__CLASS__)))
;
$definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition', $definition = $this->getMock('Wootook\\Core\\DependencyInjection\\Definition\\MethodDefinition',
array('getArgumentDefinition'), array($classDefinition, 'methodToTest')); 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'); $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\RuntimeException');
/** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ /** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */
$definition->bindArgumentRegistryEntry('fooArgumentOne', 'foo'); $definition->bindArgumentRegistryEntry('fooArgumentOne', 'foo');
} }
public function testMethodDefinition_getArgumentDefinition_withInvalidArgumentId() public function testMethodDefinition_getArgumentDefinitionWithInvalidArgumentId()
{ {
$classDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition') /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */
->disableOriginalConstructor() $classDefinition = $this->getClassDefinitionMock();
->getMock()
;
$classDefinition->expects($this->once())
->method('getReflector')
->will($this->returnValue(new \ReflectionClass(__CLASS__)))
;
$definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest');
@ -270,15 +435,8 @@ class MethodDefinitionTest extends \PHPUnit_Framework_TestCase
public function testMethodDefinition_getArgumentReflector_withNumericArgumentId() public function testMethodDefinition_getArgumentReflector_withNumericArgumentId()
{ {
$classDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition') /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */
->disableOriginalConstructor() $classDefinition = $this->getClassDefinitionMock();
->getMock()
;
$classDefinition->expects($this->once())
->method('getReflector')
->will($this->returnValue(new \ReflectionClass(__CLASS__)))
;
$definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest');
@ -288,17 +446,23 @@ class MethodDefinitionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(0, $reflector->getPosition()); $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() public function testMethodDefinition_getArgumentReflector_withNamedArgumentId()
{ {
$classDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition') /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */
->disableOriginalConstructor() $classDefinition = $this->getClassDefinitionMock();
->getMock()
;
$classDefinition->expects($this->once())
->method('getReflector')
->will($this->returnValue(new \ReflectionClass(__CLASS__)))
;
$definition = new Definition\MethodDefinition($classDefinition, 'methodToTest'); $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest');
@ -308,23 +472,139 @@ class MethodDefinitionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(0, $reflector->getPosition()); $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() public function testMethodDefinition_getArgumentReflector_withInvalidArgumentId()
{ {
$classDefinition = $this->getMockBuilder('Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition') /** @var \Wootook\Core\DependencyInjection\Definition\ClassDefinition $classDefinition */
->disableOriginalConstructor() $classDefinition = $this->getClassDefinitionMock();
->getMock()
;
$classDefinition->expects($this->once()) $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest');
->method('getReflector')
->will($this->returnValue(new \ReflectionClass(__CLASS__))) $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'); $definition = new Definition\MethodDefinition($classDefinition, 'methodToTest');
$this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException'); $this->setExpectedException('Wootook\\Core\\Exception\\DependencyInjection\\InvalidArgumentException');
/** @var \Wootook\Core\DependencyInjection\Definition\MethodDefinition $definition */ /** @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);
} }
} }

View file

@ -21,15 +21,15 @@
</testsuite> </testsuite>
--> -->
</testsuites> </testsuites>
<!--
<filter> <filter>
<whitelist> <whitelist>
<directory suffix=".php">../src/Wootook/Core</directory> <directory suffix=".php">../src/Wootook/Core</directory>
</whitelist> </whitelist>
</filter> </filter>
<!-- -->
<logging> <logging>
<log type="coverage-html" target="./coverage/unit/html" charset="UTF-8" yui="true" highlight="false" lowUpperBound="40" highLowerBound="80"/> <log type="coverage-html" target="./coverage/unit/html" charset="UTF-8" yui="true" highlight="false" lowUpperBound="40" highLowerBound="80"/>
</logging> </logging>
--> <!-- -->
</phpunit> </phpunit>