1
0
mirror of synced 2025-02-10 01:09:26 +03:00

Fix EntityManagerDecorator tests

We were expecting a return value for all methods but on 6a56df9a24
we changed some methods to be void and of course that affected the
ORM.
This commit is contained in:
Luís Cobucci 2017-02-12 18:38:21 +01:00
parent 10935dd843
commit dcc80af7d9
No known key found for this signature in database
GPG Key ID: 8042585A7DBC92E1

View File

@ -3,65 +3,84 @@
namespace Doctrine\Tests\ORM\Decorator; namespace Doctrine\Tests\ORM\Decorator;
use Doctrine\ORM\Decorator\EntityManagerDecorator; use Doctrine\ORM\Decorator\EntityManagerDecorator;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\ResultSetMapping; use Doctrine\ORM\Query\ResultSetMapping;
class EntityManagerDecoratorTest extends \PHPUnit_Framework_TestCase class EntityManagerDecoratorTest extends \PHPUnit_Framework_TestCase
{ {
const VOID_METHODS = [
'persist',
'remove',
'clear',
'detach',
'refresh',
'flush',
'initializeObject',
];
/**
* @var EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $wrapped; private $wrapped;
private $decorator;
public function setUp() public function setUp()
{ {
$this->wrapped = $this->createMock(EntityManagerInterface::class); $this->wrapped = $this->createMock(EntityManagerInterface::class);
$this->decorator = $this->getMockBuilder(EntityManagerDecorator::class)
->setConstructorArgs([$this->wrapped])
->setMethods(null)
->getMock();
} }
public function getMethodParameters() public function getMethodParameters()
{ {
$class = new \ReflectionClass(EntityManager::class); $class = new \ReflectionClass(EntityManagerInterface::class);
$methods = []; $methods = [];
foreach ($class->getMethods() as $method) { foreach ($class->getMethods() as $method) {
if ($method->isConstructor() || $method->isStatic() || !$method->isPublic()) { if ($method->isConstructor() || $method->isStatic() || !$method->isPublic()) {
continue; continue;
} }
/** Special case EntityManager::createNativeQuery() */ $methods[$method->getName()] = $this->getParameters($method);
if ($method->getName() === 'createNativeQuery') {
$methods[] = [$method->getName(), ['name', new ResultSetMapping()]];
continue;
}
if ($method->getNumberOfRequiredParameters() === 0) {
$methods[] = [$method->getName(), []];
} elseif ($method->getNumberOfRequiredParameters() > 0) {
$methods[] = [$method->getName(), array_fill(0, $method->getNumberOfRequiredParameters(), 'req') ?: []];
}
if ($method->getNumberOfParameters() != $method->getNumberOfRequiredParameters()) {
$methods[] = [$method->getName(), array_fill(0, $method->getNumberOfParameters(), 'all') ?: []];
}
} }
return $methods; return $methods;
} }
private function getParameters(\ReflectionMethod $method)
{
/** Special case EntityManager::createNativeQuery() */
if ($method->getName() === 'createNativeQuery') {
return [$method->getName(), ['name', new ResultSetMapping()]];
}
if ($method->getNumberOfRequiredParameters() === 0) {
return [$method->getName(), []];
}
if ($method->getNumberOfRequiredParameters() > 0) {
return [$method->getName(), array_fill(0, $method->getNumberOfRequiredParameters(), 'req') ?: []];
}
if ($method->getNumberOfParameters() != $method->getNumberOfRequiredParameters()) {
return [$method->getName(), array_fill(0, $method->getNumberOfParameters(), 'all') ?: []];
}
return [];
}
/** /**
* @dataProvider getMethodParameters * @dataProvider getMethodParameters
*/ */
public function testAllMethodCallsAreDelegatedToTheWrappedInstance($method, array $parameters) public function testAllMethodCallsAreDelegatedToTheWrappedInstance($method, array $parameters)
{ {
$stub = $this->wrapped $return = !in_array($method, self::VOID_METHODS) ? 'INNER VALUE FROM ' . $method : null;
->expects($this->once())
$this->wrapped->expects($this->once())
->method($method) ->method($method)
->will($this->returnValue('INNER VALUE FROM ' . $method)); ->with(...$parameters)
->willReturn($return);
call_user_func_array([$stub, 'with'], $parameters); $decorator = new class ($this->wrapped) extends EntityManagerDecorator {
};
$this->assertSame('INNER VALUE FROM ' . $method, call_user_func_array([$this->decorator, $method], $parameters)); $this->assertSame($return, $decorator->$method(...$parameters));
} }
} }