|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the core-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2017 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Tests\Factory; |
|
13
|
|
|
|
|
14
|
|
|
use WBW\Bundle\CoreBundle\Factory\FormFactory; |
|
15
|
|
|
use WBW\Bundle\CoreBundle\Tests\AbstractTestCase; |
|
16
|
|
|
use WBW\Bundle\CoreBundle\Tests\Fixtures\Assets\TestChoiceValue; |
|
17
|
|
|
use WBW\Bundle\CoreBundle\Tests\Fixtures\Factory\TestFormFactory; |
|
18
|
|
|
use WBW\Library\Symfony\Assets\ChoiceValueInterface; |
|
19
|
|
|
use WBW\Library\Symfony\Assets\Navigation\NavigationNode; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Form factory test. |
|
23
|
|
|
* |
|
24
|
|
|
* @author webeweb <https://github.com/webeweb> |
|
25
|
|
|
* @package WBW\Bundle\CoreBundle\Tests\Factory |
|
26
|
|
|
*/ |
|
27
|
|
|
class FormFactoryTest extends AbstractTestCase { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Choice values. |
|
31
|
|
|
* |
|
32
|
|
|
* @var ChoiceValueInterface[] |
|
33
|
|
|
*/ |
|
34
|
|
|
private $choiceValues; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Choices. |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $choices; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Entities. |
|
45
|
|
|
* |
|
46
|
|
|
* @var NavigationNode[] |
|
47
|
|
|
*/ |
|
48
|
|
|
private $entities; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritDoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function setUp(): void { |
|
54
|
|
|
parent::setUp(); |
|
55
|
|
|
|
|
56
|
|
|
// Set a choice values mock. |
|
57
|
|
|
$this->choiceValues = [ |
|
|
|
|
|
|
58
|
|
|
$this->getMockBuilder(ChoiceValueInterface::class)->getMock(), |
|
59
|
|
|
$this->getMockBuilder(ChoiceValueInterface::class)->getMock(), |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
$this->choiceValues[0]->expects($this->any())->method("getChoiceValue")->willReturn(null); |
|
63
|
|
|
$this->choiceValues[1]->expects($this->any())->method("getChoiceValue")->willReturn("value"); |
|
64
|
|
|
|
|
65
|
|
|
// Set a choices mock. |
|
66
|
|
|
$this->choices = [ |
|
67
|
|
|
0 => "0", |
|
68
|
|
|
1 => "1", |
|
69
|
|
|
2 => "2", |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
|
|
// Set an entities mock. |
|
73
|
|
|
$this->entities = [ |
|
74
|
|
|
new NavigationNode("id1"), |
|
75
|
|
|
new NavigationNode("id2"), |
|
76
|
|
|
new NavigationNode("id3"), |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Test getChoiceLabelCallback() |
|
82
|
|
|
* |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
|
|
public function testGetChoiceLabelCallback(): void { |
|
86
|
|
|
|
|
87
|
|
|
$res = TestFormFactory::getChoiceLabelCallback([]); |
|
88
|
|
|
$this->assertIsCallable($res); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals("This option must implements [Translated]ChoiceLabelInterface", $res($this->choiceValues[0])); |
|
91
|
|
|
$this->assertEquals("This option must implements [Translated]ChoiceLabelInterface", $res($this->choiceValues[1])); |
|
92
|
|
|
|
|
93
|
|
|
$this->assertEquals("This option must implements [Translated]ChoiceLabelInterface", $res($this->choices[0])); |
|
94
|
|
|
$this->assertEquals("This option must implements [Translated]ChoiceLabelInterface", $res($this->choices[1])); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertEquals("Empty selection", $res(null)); |
|
97
|
|
|
$this->assertEquals("─ This option must implements [Translated]ChoiceLabelInterface", $res($this->entities[0])); |
|
98
|
|
|
$this->assertEquals("─ This option must implements [Translated]ChoiceLabelInterface", $res($this->entities[1])); |
|
99
|
|
|
$this->assertEquals("─ This option must implements [Translated]ChoiceLabelInterface", $res($this->entities[2])); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Test getChoiceValueCallback() |
|
104
|
|
|
* |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
|
|
public function testGetChoiceValueCallback(): void { |
|
108
|
|
|
|
|
109
|
|
|
$res = TestFormFactory::getChoiceValueCallback(); |
|
110
|
|
|
$this->assertIsCallable($res); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertEquals("", $res($this->choiceValues[0])); |
|
113
|
|
|
$this->assertEquals("value", $res($this->choiceValues[1])); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Test newChoiceType() |
|
118
|
|
|
* |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
|
|
public function testNewChoiceType(): void { |
|
122
|
|
|
|
|
123
|
|
|
$res = FormFactory::newChoiceType($this->choices); |
|
124
|
|
|
$this->assertCount(1, $res); |
|
125
|
|
|
$this->assertArrayHasKey("choices", $res); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertCount(3, $res["choices"]); |
|
128
|
|
|
$this->assertArrayHasKey(0, $res["choices"]); |
|
129
|
|
|
$this->assertArrayHasKey(1, $res["choices"]); |
|
130
|
|
|
$this->assertArrayHasKey(2, $res["choices"]); |
|
131
|
|
|
|
|
132
|
|
|
$this->assertEquals("0", $res["choices"][0]); |
|
133
|
|
|
$this->assertEquals("1", $res["choices"][1]); |
|
134
|
|
|
$this->assertEquals("2", $res["choices"][2]); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Test newChoiceType() |
|
139
|
|
|
* |
|
140
|
|
|
* @return void |
|
141
|
|
|
*/ |
|
142
|
|
|
public function testNewChoiceTypeWithGroup(): void { |
|
143
|
|
|
|
|
144
|
|
|
$res = FormFactory::newChoiceType(["optgroup" => $this->choices], true); |
|
145
|
|
|
$this->assertCount(1, $res); |
|
146
|
|
|
$this->assertArrayHasKey("choices", $res); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertCount(3, $res["choices"]["optgroup"]); |
|
149
|
|
|
$this->assertArrayHasKey(0, $res["choices"]["optgroup"]); |
|
150
|
|
|
$this->assertArrayHasKey(1, $res["choices"]["optgroup"]); |
|
151
|
|
|
$this->assertArrayHasKey(2, $res["choices"]["optgroup"]); |
|
152
|
|
|
|
|
153
|
|
|
$this->assertEquals("0", $res["choices"]["optgroup"][0]); |
|
154
|
|
|
$this->assertEquals("1", $res["choices"]["optgroup"][1]); |
|
155
|
|
|
$this->assertEquals("2", $res["choices"]["optgroup"][2]); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Test the newEntityType method. |
|
160
|
|
|
* |
|
161
|
|
|
* @return void |
|
162
|
|
|
*/ |
|
163
|
|
|
public function testNewEntityType(): void { |
|
164
|
|
|
|
|
165
|
|
|
$res = FormFactory::newEntityType(NavigationNode::class, $this->entities); |
|
166
|
|
|
$this->assertCount(3, $res); |
|
167
|
|
|
$this->assertArrayHasKey("class", $res); |
|
168
|
|
|
$this->assertArrayHasKey("choice_label", $res); |
|
169
|
|
|
$this->assertArrayHasKey("choices", $res); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertEquals(NavigationNode::class, $res["class"]); |
|
172
|
|
|
|
|
173
|
|
|
$this->assertCount(3, $res["choices"]); |
|
174
|
|
|
$this->assertSame($this->entities[0], $res["choices"][0]); |
|
175
|
|
|
$this->assertSame($this->entities[1], $res["choices"][1]); |
|
176
|
|
|
$this->assertSame($this->entities[2], $res["choices"][2]); |
|
177
|
|
|
|
|
178
|
|
|
$this->assertIsCallable($res["choice_label"]); |
|
179
|
|
|
$this->assertEquals("─ This option must implements [Translated]ChoiceLabelInterface", $res["choice_label"]($res["choices"][0])); |
|
180
|
|
|
$this->assertEquals("─ This option must implements [Translated]ChoiceLabelInterface", $res["choice_label"]($res["choices"][1])); |
|
181
|
|
|
$this->assertEquals("─ This option must implements [Translated]ChoiceLabelInterface", $res["choice_label"]($res["choices"][2])); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Test the newEntityType method. |
|
186
|
|
|
* |
|
187
|
|
|
* @return void |
|
188
|
|
|
*/ |
|
189
|
|
|
public function testNewEntityTypeWithChoiceValueInterface(): void { |
|
190
|
|
|
|
|
191
|
|
|
$arg = [ |
|
192
|
|
|
new TestChoiceValue(), |
|
193
|
|
|
new TestChoiceValue(), |
|
194
|
|
|
]; |
|
195
|
|
|
|
|
196
|
|
|
$res = FormFactory::newEntityType(TestChoiceValue::class, $arg); |
|
197
|
|
|
$this->assertCount(4, $res); |
|
198
|
|
|
$this->assertArrayHasKey("class", $res); |
|
199
|
|
|
$this->assertArrayHasKey("choice_label", $res); |
|
200
|
|
|
$this->assertArrayHasKey("choice_value", $res); |
|
201
|
|
|
$this->assertArrayHasKey("choices", $res); |
|
202
|
|
|
|
|
203
|
|
|
$this->assertEquals(TestChoiceValue::class, $res["class"]); |
|
204
|
|
|
|
|
205
|
|
|
$this->assertCount(2, $res["choices"]); |
|
206
|
|
|
$this->assertSame($arg[0], $res["choices"][0]); |
|
207
|
|
|
$this->assertSame($arg[1], $res["choices"][1]); |
|
208
|
|
|
|
|
209
|
|
|
$this->assertIsCallable($res["choice_label"]); |
|
210
|
|
|
|
|
211
|
|
|
$this->assertIsCallable($res["choice_value"]); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Test the newEntityType method. |
|
216
|
|
|
* |
|
217
|
|
|
* @return void |
|
218
|
|
|
*/ |
|
219
|
|
|
public function testNewEntityTypeWithEmpty(): void { |
|
220
|
|
|
|
|
221
|
|
|
$res = FormFactory::newEntityType(NavigationNode::class, $this->entities, ["empty" => true]); |
|
222
|
|
|
$this->assertCount(3, $res); |
|
223
|
|
|
$this->assertArrayHasKey("class", $res); |
|
224
|
|
|
$this->assertArrayHasKey("choice_label", $res); |
|
225
|
|
|
$this->assertArrayHasKey("choices", $res); |
|
226
|
|
|
|
|
227
|
|
|
$this->assertEquals(NavigationNode::class, $res["class"]); |
|
228
|
|
|
|
|
229
|
|
|
$this->assertCount(4, $res["choices"]); |
|
230
|
|
|
$this->assertNull($res["choices"][0]); |
|
231
|
|
|
$this->assertSame($this->entities[0], $res["choices"][1]); |
|
232
|
|
|
$this->assertSame($this->entities[1], $res["choices"][2]); |
|
233
|
|
|
$this->assertSame($this->entities[2], $res["choices"][3]); |
|
234
|
|
|
|
|
235
|
|
|
$this->assertIsCallable($res["choice_label"]); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Test the newEntityType method. |
|
240
|
|
|
* |
|
241
|
|
|
* @return void |
|
242
|
|
|
*/ |
|
243
|
|
|
public function testNewEntityTypeWithReflectionException(): void { |
|
244
|
|
|
|
|
245
|
|
|
$res = FormFactory::newEntityType("GitHub", $this->entities); |
|
246
|
|
|
$this->assertCount(3, $res); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..