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\ObjectMultiCheckbox; |
9
|
|
|
use function get_class; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Tests for the ObjectMultiCheckbox element |
13
|
|
|
* |
14
|
|
|
* @link http://www.doctrine-project.org/ |
15
|
|
|
* |
16
|
|
|
* @covers \DoctrineModule\Form\Element\ObjectMultiCheckbox |
17
|
|
|
*/ |
18
|
|
|
class ObjectMultiCheckboxTest extends ProxyAwareElementTestCase |
19
|
|
|
{ |
20
|
|
|
/** @var ArrayCollection */ |
21
|
|
|
protected $values; |
22
|
|
|
|
23
|
|
|
/** @var ObjectMultiCheckbox */ |
24
|
|
|
protected $element; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritDoc}. |
28
|
|
|
*/ |
29
|
|
|
protected function setUp() : void |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
|
|
|
|
32
|
|
|
$this->element = new ObjectMultiCheckbox(); |
33
|
|
|
|
34
|
|
|
$this->prepareProxy(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testSetValueWithCollection() : void |
38
|
|
|
{ |
39
|
|
|
$this->element->setValue( |
40
|
|
|
$this->values |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
$this->assertEquals( |
|
|
|
|
44
|
|
|
[1, 2], |
45
|
|
|
$this->element->getValue() |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testSetValueWithArray() : void |
50
|
|
|
{ |
51
|
|
|
$this->element->setValue( |
52
|
|
|
$this->values->toArray() |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$this->assertEquals( |
|
|
|
|
56
|
|
|
[1, 2], |
57
|
|
|
$this->element->getValue() |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGetValueOptionsDoesntCauseInfiniteLoopIfProxyReturnsEmptyArrayAndValidatorIsInitialized() : void |
62
|
|
|
{ |
63
|
|
|
$element = $this->createPartialMock(get_class($this->element), ['setValueOptions']); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$options = []; |
66
|
|
|
|
67
|
|
|
$proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); |
|
|
|
|
68
|
|
|
$proxy->expects($this->exactly(2)) |
|
|
|
|
69
|
|
|
->method('getValueOptions') |
70
|
|
|
->will($this->returnValue($options)); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$element->expects($this->never()) |
|
|
|
|
73
|
|
|
->method('setValueOptions'); |
74
|
|
|
|
75
|
|
|
$this->setProxyViaReflection($proxy, $element); |
|
|
|
|
76
|
|
|
$element->getInputSpecification(); |
|
|
|
|
77
|
|
|
$this->assertEquals($options, $element->getValueOptions()); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testGetValueOptionsDoesntInvokeProxyIfOptionsNotEmpty() : void |
81
|
|
|
{ |
82
|
|
|
$options = ['foo' => 'bar']; |
83
|
|
|
|
84
|
|
|
$proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); |
|
|
|
|
85
|
|
|
$proxy->expects($this->once()) |
|
|
|
|
86
|
|
|
->method('getValueOptions') |
87
|
|
|
->will($this->returnValue($options)); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$this->setProxyViaReflection($proxy); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$this->assertEquals($options, $this->element->getValueOptions()); |
|
|
|
|
92
|
|
|
$this->assertEquals($options, $this->element->getValueOptions()); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testOptionsCanBeSetSingle() : void |
96
|
|
|
{ |
97
|
|
|
$proxy = $this->createMock('DoctrineModule\Form\Element\Proxy'); |
|
|
|
|
98
|
|
|
$proxy->expects($this->once())->method('setOptions')->with(['is_method' => true]); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$this->setProxyViaReflection($proxy); |
|
|
|
|
101
|
|
|
|
102
|
|
|
$this->element->setOption('is_method', true); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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: