1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DoctrineModuleTest\Form\Element; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use DoctrineModule\Form\Element\ObjectSelect; |
9
|
|
|
use function get_class; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Tests for the ObjectSelect element |
13
|
|
|
* |
14
|
|
|
* @link http://www.doctrine-project.org/ |
15
|
|
|
* |
16
|
|
|
* @covers \DoctrineModule\Form\Element\ObjectSelect |
17
|
|
|
*/ |
18
|
|
|
class ObjectSelectTest extends ProxyAwareElementTestCase |
19
|
|
|
{ |
20
|
|
|
/** @var ArrayCollection */ |
21
|
|
|
protected $values; |
22
|
|
|
|
23
|
|
|
/** @var ObjectSelect */ |
24
|
|
|
protected $element; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritDoc}. |
28
|
|
|
*/ |
29
|
|
|
protected function setUp() : void |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
|
|
|
|
32
|
|
|
$this->element = new ObjectSelect(); |
33
|
|
|
|
34
|
|
|
$this->prepareProxy(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testSetValueWithCollection() : void |
38
|
|
|
{ |
39
|
|
|
$this->element->setAttribute('multiple', true); |
40
|
|
|
|
41
|
|
|
$this->element->setValue( |
42
|
|
|
$this->values |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$this->assertEquals( |
|
|
|
|
46
|
|
|
[1, 2], |
47
|
|
|
$this->element->getValue() |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testSetValueWithArray() : void |
52
|
|
|
{ |
53
|
|
|
$this->element->setAttribute('multiple', true); |
54
|
|
|
|
55
|
|
|
$this->element->setValue( |
56
|
|
|
$this->values->toArray() |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$this->assertEquals( |
|
|
|
|
60
|
|
|
[1, 2], |
61
|
|
|
$this->element->getValue() |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testSetValueSingleValue() : void |
66
|
|
|
{ |
67
|
|
|
$value = $this->values->toArray(); |
68
|
|
|
|
69
|
|
|
$this->element->setValue( |
70
|
|
|
$value[0] |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$this->assertEquals( |
|
|
|
|
74
|
|
|
1, |
75
|
|
|
$this->element->getValue() |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testGetValueOptionsDoesntCauseInfiniteLoopIfProxyReturnsEmptyArrayAndValidatorIsInitialized() : void |
80
|
|
|
{ |
81
|
|
|
$element = $this->createPartialMock(get_class($this->element), ['setValueOptions']); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$options = []; |
84
|
|
|
|
85
|
|
|
$proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); |
|
|
|
|
86
|
|
|
$proxy->expects($this->exactly(2)) |
|
|
|
|
87
|
|
|
->method('getValueOptions') |
88
|
|
|
->will($this->returnValue($options)); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$element->expects($this->never()) |
|
|
|
|
91
|
|
|
->method('setValueOptions'); |
92
|
|
|
|
93
|
|
|
$this->setProxyViaReflection($proxy, $element); |
|
|
|
|
94
|
|
|
$element->getInputSpecification(); |
|
|
|
|
95
|
|
|
$this->assertEquals($options, $element->getValueOptions()); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testGetValueOptionsDoesntInvokeProxyIfOptionsNotEmpty() : void |
99
|
|
|
{ |
100
|
|
|
$options = ['foo' => 'bar']; |
101
|
|
|
|
102
|
|
|
$proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); |
|
|
|
|
103
|
|
|
$proxy->expects($this->once()) |
|
|
|
|
104
|
|
|
->method('getValueOptions') |
105
|
|
|
->will($this->returnValue($options)); |
|
|
|
|
106
|
|
|
|
107
|
|
|
$this->setProxyViaReflection($proxy); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$this->assertEquals($options, $this->element->getValueOptions()); |
|
|
|
|
110
|
|
|
$this->assertEquals($options, $this->element->getValueOptions()); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testOptionsCanBeSetSingle() : void |
114
|
|
|
{ |
115
|
|
|
$proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); |
|
|
|
|
116
|
|
|
$proxy->expects($this->once())->method('setOptions')->with(['is_method' => true]); |
|
|
|
|
117
|
|
|
|
118
|
|
|
$this->setProxyViaReflection($proxy); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$this->element->setOption('is_method', true); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: