1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Decorator; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Decorator\EntityManagerDecorator; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
8
|
|
|
|
9
|
|
|
class EntityManagerDecoratorTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
const VOID_METHODS = [ |
12
|
|
|
'persist', |
13
|
|
|
'remove', |
14
|
|
|
'clear', |
15
|
|
|
'detach', |
16
|
|
|
'refresh', |
17
|
|
|
'flush', |
18
|
|
|
'initializeObject', |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
23
|
|
|
*/ |
24
|
|
|
private $wrapped; |
25
|
|
|
|
26
|
|
|
public function setUp() |
27
|
|
|
{ |
28
|
|
|
$this->wrapped = $this->createMock(EntityManagerInterface::class); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getMethodParameters() |
32
|
|
|
{ |
33
|
|
|
$class = new \ReflectionClass(EntityManagerInterface::class); |
34
|
|
|
$methods = []; |
35
|
|
|
|
36
|
|
|
foreach ($class->getMethods() as $method) { |
37
|
|
|
if ($method->isConstructor() || $method->isStatic() || !$method->isPublic()) { |
38
|
|
|
continue; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$methods[$method->getName()] = $this->getParameters($method); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $methods; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function getParameters(\ReflectionMethod $method) |
48
|
|
|
{ |
49
|
|
|
/** Special case EntityManager::createNativeQuery() */ |
50
|
|
|
if ($method->getName() === 'createNativeQuery') { |
51
|
|
|
return [$method->getName(), ['name', new ResultSetMapping()]]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($method->getNumberOfRequiredParameters() === 0) { |
55
|
|
|
return [$method->getName(), []]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($method->getNumberOfRequiredParameters() > 0) { |
59
|
|
|
return [$method->getName(), array_fill(0, $method->getNumberOfRequiredParameters(), 'req') ?: []]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($method->getNumberOfParameters() != $method->getNumberOfRequiredParameters()) { |
63
|
|
|
return [$method->getName(), array_fill(0, $method->getNumberOfParameters(), 'all') ?: []]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @dataProvider getMethodParameters |
71
|
|
|
*/ |
72
|
|
|
public function testAllMethodCallsAreDelegatedToTheWrappedInstance($method, array $parameters) |
73
|
|
|
{ |
74
|
|
|
$return = !in_array($method, self::VOID_METHODS) ? 'INNER VALUE FROM ' . $method : null; |
75
|
|
|
|
76
|
|
|
$this->wrapped->expects($this->once()) |
77
|
|
|
->method($method) |
78
|
|
|
->with(...$parameters) |
79
|
|
|
->willReturn($return); |
80
|
|
|
|
81
|
|
|
$decorator = new class ($this->wrapped) extends EntityManagerDecorator { |
82
|
|
|
}; |
83
|
|
|
|
84
|
|
|
$this->assertSame($return, $decorator->$method(...$parameters)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|