|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DoctrineModuleTest\Form\Element; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
8
|
|
|
use DoctrineModuleTest\Form\Element\TestAsset\FormObject; |
|
9
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use ReflectionProperty; |
|
12
|
|
|
use function array_shift; |
|
13
|
|
|
use function func_get_args; |
|
14
|
|
|
use function get_class; |
|
15
|
|
|
|
|
16
|
|
|
class ProxyAwareElementTestCase extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var MockObject */ |
|
19
|
|
|
protected $element; |
|
20
|
|
|
|
|
21
|
|
|
protected function prepareProxy() : void |
|
22
|
|
|
{ |
|
23
|
|
|
$objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject'; |
|
24
|
|
|
$objectOne = new FormObject(); |
|
25
|
|
|
$objectTwo = new FormObject(); |
|
26
|
|
|
|
|
27
|
|
|
$objectOne->setId(1) |
|
28
|
|
|
->setUsername('object one username') |
|
29
|
|
|
->setPassword('object one password') |
|
30
|
|
|
->setEmail('object one email') |
|
31
|
|
|
->setFirstname('object one firstname') |
|
32
|
|
|
->setSurname('object one surname'); |
|
33
|
|
|
|
|
34
|
|
|
$objectTwo->setId(2) |
|
35
|
|
|
->setUsername('object two username') |
|
36
|
|
|
->setPassword('object two password') |
|
37
|
|
|
->setEmail('object two email') |
|
38
|
|
|
->setFirstname('object two firstname') |
|
39
|
|
|
->setSurname('object two surname'); |
|
40
|
|
|
|
|
41
|
|
|
$result = new ArrayCollection([$objectOne, $objectTwo]); |
|
42
|
|
|
$this->values = $result; |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata'); |
|
45
|
|
|
$metadata |
|
46
|
|
|
->expects($this->any()) |
|
47
|
|
|
->method('getIdentifierValues') |
|
48
|
|
|
->will( |
|
49
|
|
|
$this->returnCallback( |
|
50
|
|
|
static function () use ($objectOne, $objectTwo) { |
|
51
|
|
|
$input = func_get_args(); |
|
52
|
|
|
$input = array_shift($input); |
|
53
|
|
|
|
|
54
|
|
|
if ($input === $objectOne) { |
|
55
|
|
|
return ['id' => 1]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($input === $objectTwo) { |
|
59
|
|
|
return ['id' => 2]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return []; |
|
63
|
|
|
} |
|
64
|
|
|
) |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository'); |
|
68
|
|
|
$objectRepository->expects($this->any()) |
|
69
|
|
|
->method('findAll') |
|
70
|
|
|
->will($this->returnValue($result)); |
|
71
|
|
|
|
|
72
|
|
|
$objectManager = $this->createMock('Doctrine\Persistence\ObjectManager'); |
|
73
|
|
|
$objectManager->expects($this->any()) |
|
74
|
|
|
->method('getClassMetadata') |
|
75
|
|
|
->with($this->equalTo($objectClass)) |
|
76
|
|
|
->will($this->returnValue($metadata)); |
|
77
|
|
|
|
|
78
|
|
|
$objectManager |
|
79
|
|
|
->expects($this->any()) |
|
80
|
|
|
->method('getRepository') |
|
81
|
|
|
->with($this->equalTo($objectClass)) |
|
82
|
|
|
->will($this->returnValue($objectRepository)); |
|
83
|
|
|
|
|
84
|
|
|
$this->element->getProxy()->setOptions([ |
|
|
|
|
|
|
85
|
|
|
'object_manager' => $objectManager, |
|
86
|
|
|
'target_class' => $objectClass, |
|
87
|
|
|
]); |
|
88
|
|
|
|
|
89
|
|
|
$this->metadata = $metadata; |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Proxy should stay read only, use with care |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function setProxyViaReflection(MockObject $proxy, ?MockObject $element = null) : void |
|
96
|
|
|
{ |
|
97
|
|
|
if (! $element) { |
|
98
|
|
|
$element = $this->element; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$prop = new ReflectionProperty(get_class($this->element), 'proxy'); |
|
102
|
|
|
$prop->setAccessible(true); |
|
103
|
|
|
$prop->setValue($element, $proxy); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: