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:
parent
10935dd843
commit
dcc80af7d9
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user