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