18
18
use Symfony \Bridge \Doctrine \Form \ChoiceList \EntityLoaderInterface ;
19
19
use Symfony \Bridge \Doctrine \Form \ChoiceList \IdReader ;
20
20
use Symfony \Component \Form \ChoiceList \ArrayChoiceList ;
21
- use Symfony \Component \Form \ChoiceList \Factory \ChoiceListFactoryInterface ;
22
21
23
22
/**
24
23
* @author Bernhard Schussek <bschussek@gmail.com>
25
24
*/
26
25
class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
27
26
{
28
- /**
29
- * @var ChoiceListFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
30
- */
31
- private $ factory ;
32
-
33
27
/**
34
28
* @var ObjectManager|\PHPUnit_Framework_MockObject_MockObject
35
29
*/
@@ -98,44 +92,68 @@ protected function setUp()
98
92
public function testLoadChoiceList ()
99
93
{
100
94
$ loader = new DoctrineChoiceLoader (
101
- $ this ->factory ,
102
95
$ this ->om ,
103
96
$ this ->class ,
104
97
$ this ->idReader
105
98
);
106
99
107
100
$ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
108
- $ choiceList = new ArrayChoiceList (array ());
109
101
$ value = function () {};
102
+ $ choiceList = new ArrayChoiceList ($ choices , $ value );
110
103
111
104
$ this ->repository ->expects ($ this ->once ())
112
105
->method ('findAll ' )
113
106
->willReturn ($ choices );
114
107
115
- $ this ->factory ->expects ($ this ->once ())
116
- ->method ('createListFromChoices ' )
117
- ->with ($ choices , $ value )
118
- ->willReturn ($ choiceList );
108
+ $ this ->assertEquals ($ choiceList , $ loader ->loadChoiceList ($ value ));
119
109
120
- $ this ->assertSame ($ choiceList , $ loader ->loadChoiceList ($ value ));
110
+ // no further loads on subsequent calls
111
+
112
+ $ this ->assertEquals ($ choiceList , $ loader ->loadChoiceList ($ value ));
113
+ }
114
+
115
+ /**
116
+ * @group legacy
117
+ */
118
+ public function testLegacyLoadChoiceList ()
119
+ {
120
+ $ factory = $ this ->getMock ('Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface ' );
121
+ $ loader = new DoctrineChoiceLoader (
122
+ $ factory ,
123
+ $ this ->om ,
124
+ $ this ->class ,
125
+ $ this ->idReader
126
+ );
127
+
128
+ $ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
129
+ $ value = function () {};
130
+ $ choiceList = new ArrayChoiceList ($ choices , $ value );
131
+
132
+ $ this ->repository ->expects ($ this ->once ())
133
+ ->method ('findAll ' )
134
+ ->willReturn ($ choices );
135
+
136
+ $ factory ->expects ($ this ->never ())
137
+ ->method ('createListFromChoices ' );
138
+
139
+ $ this ->assertEquals ($ choiceList , $ loaded = $ loader ->loadChoiceList ($ value ));
121
140
122
141
// no further loads on subsequent calls
123
142
124
- $ this ->assertSame ($ choiceList , $ loader ->loadChoiceList ($ value ));
143
+ $ this ->assertSame ($ loaded , $ loader ->loadChoiceList ($ value ));
125
144
}
126
145
127
146
public function testLoadChoiceListUsesObjectLoaderIfAvailable ()
128
147
{
129
148
$ loader = new DoctrineChoiceLoader (
130
- $ this ->factory ,
131
149
$ this ->om ,
132
150
$ this ->class ,
133
151
$ this ->idReader ,
134
152
$ this ->objectLoader
135
153
);
136
154
137
155
$ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
138
- $ choiceList = new ArrayChoiceList (array () );
156
+ $ choiceList = new ArrayChoiceList ($ choices );
139
157
140
158
$ this ->repository ->expects ($ this ->never ())
141
159
->method ('findAll ' );
@@ -144,39 +162,27 @@ public function testLoadChoiceListUsesObjectLoaderIfAvailable()
144
162
->method ('getEntities ' )
145
163
->willReturn ($ choices );
146
164
147
- $ this ->factory ->expects ($ this ->once ())
148
- ->method ('createListFromChoices ' )
149
- ->with ($ choices )
150
- ->willReturn ($ choiceList );
151
-
152
- $ this ->assertSame ($ choiceList , $ loader ->loadChoiceList ());
165
+ $ this ->assertEquals ($ choiceList , $ loaded = $ loader ->loadChoiceList ());
153
166
154
167
// no further loads on subsequent calls
155
168
156
- $ this ->assertSame ($ choiceList , $ loader ->loadChoiceList ());
169
+ $ this ->assertSame ($ loaded , $ loader ->loadChoiceList ());
157
170
}
158
171
159
172
public function testLoadValuesForChoices ()
160
173
{
161
174
$ loader = new DoctrineChoiceLoader (
162
- $ this ->factory ,
163
175
$ this ->om ,
164
176
$ this ->class ,
165
177
$ this ->idReader
166
178
);
167
179
168
180
$ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
169
- $ choiceList = new ArrayChoiceList ($ choices );
170
181
171
182
$ this ->repository ->expects ($ this ->once ())
172
183
->method ('findAll ' )
173
184
->willReturn ($ choices );
174
185
175
- $ this ->factory ->expects ($ this ->once ())
176
- ->method ('createListFromChoices ' )
177
- ->with ($ choices )
178
- ->willReturn ($ choiceList );
179
-
180
186
$ this ->assertSame (array ('1 ' , '2 ' ), $ loader ->loadValuesForChoices (array ($ this ->obj2 , $ this ->obj3 )));
181
187
182
188
// no further loads on subsequent calls
@@ -187,7 +193,6 @@ public function testLoadValuesForChoices()
187
193
public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices ()
188
194
{
189
195
$ loader = new DoctrineChoiceLoader (
190
- $ this ->factory ,
191
196
$ this ->om ,
192
197
$ this ->class ,
193
198
$ this ->idReader
@@ -196,16 +201,12 @@ public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices()
196
201
$ this ->repository ->expects ($ this ->never ())
197
202
->method ('findAll ' );
198
203
199
- $ this ->factory ->expects ($ this ->never ())
200
- ->method ('createListFromChoices ' );
201
-
202
204
$ this ->assertSame (array (), $ loader ->loadValuesForChoices (array ()));
203
205
}
204
206
205
207
public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId ()
206
208
{
207
209
$ loader = new DoctrineChoiceLoader (
208
- $ this ->factory ,
209
210
$ this ->om ,
210
211
$ this ->class ,
211
212
$ this ->idReader
@@ -218,9 +219,6 @@ public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
218
219
$ this ->repository ->expects ($ this ->never ())
219
220
->method ('findAll ' );
220
221
221
- $ this ->factory ->expects ($ this ->never ())
222
- ->method ('createListFromChoices ' );
223
-
224
222
$ this ->idReader ->expects ($ this ->any ())
225
223
->method ('getIdValue ' )
226
224
->with ($ this ->obj2 )
@@ -232,15 +230,13 @@ public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
232
230
public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven ()
233
231
{
234
232
$ loader = new DoctrineChoiceLoader (
235
- $ this ->factory ,
236
233
$ this ->om ,
237
234
$ this ->class ,
238
235
$ this ->idReader
239
236
);
240
237
241
238
$ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
242
239
$ value = function (\stdClass $ object ) { return $ object ->name ; };
243
- $ choiceList = new ArrayChoiceList ($ choices , $ value );
244
240
245
241
$ this ->idReader ->expects ($ this ->any ())
246
242
->method ('isSingleId ' )
@@ -250,11 +246,6 @@ public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven()
250
246
->method ('findAll ' )
251
247
->willReturn ($ choices );
252
248
253
- $ this ->factory ->expects ($ this ->once ())
254
- ->method ('createListFromChoices ' )
255
- ->with ($ choices , $ value )
256
- ->willReturn ($ choiceList );
257
-
258
249
$ this ->assertSame (array ('B ' ), $ loader ->loadValuesForChoices (
259
250
array ($ this ->obj2 ),
260
251
$ value
@@ -264,7 +255,6 @@ public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven()
264
255
public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader ()
265
256
{
266
257
$ loader = new DoctrineChoiceLoader (
267
- $ this ->factory ,
268
258
$ this ->om ,
269
259
$ this ->class ,
270
260
$ this ->idReader
@@ -279,9 +269,6 @@ public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader()
279
269
$ this ->repository ->expects ($ this ->never ())
280
270
->method ('findAll ' );
281
271
282
- $ this ->factory ->expects ($ this ->never ())
283
- ->method ('createListFromChoices ' );
284
-
285
272
$ this ->idReader ->expects ($ this ->any ())
286
273
->method ('getIdValue ' )
287
274
->with ($ this ->obj2 )
@@ -296,24 +283,17 @@ public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader()
296
283
public function testLoadChoicesForValues ()
297
284
{
298
285
$ loader = new DoctrineChoiceLoader (
299
- $ this ->factory ,
300
286
$ this ->om ,
301
287
$ this ->class ,
302
288
$ this ->idReader
303
289
);
304
290
305
291
$ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
306
- $ choiceList = new ArrayChoiceList ($ choices );
307
292
308
293
$ this ->repository ->expects ($ this ->once ())
309
294
->method ('findAll ' )
310
295
->willReturn ($ choices );
311
296
312
- $ this ->factory ->expects ($ this ->once ())
313
- ->method ('createListFromChoices ' )
314
- ->with ($ choices )
315
- ->willReturn ($ choiceList );
316
-
317
297
$ this ->assertSame (array ($ this ->obj2 , $ this ->obj3 ), $ loader ->loadChoicesForValues (array ('1 ' , '2 ' )));
318
298
319
299
// no further loads on subsequent calls
@@ -324,7 +304,6 @@ public function testLoadChoicesForValues()
324
304
public function testLoadChoicesForValuesDoesNotLoadIfEmptyValues ()
325
305
{
326
306
$ loader = new DoctrineChoiceLoader (
327
- $ this ->factory ,
328
307
$ this ->om ,
329
308
$ this ->class ,
330
309
$ this ->idReader
@@ -333,16 +312,12 @@ public function testLoadChoicesForValuesDoesNotLoadIfEmptyValues()
333
312
$ this ->repository ->expects ($ this ->never ())
334
313
->method ('findAll ' );
335
314
336
- $ this ->factory ->expects ($ this ->never ())
337
- ->method ('createListFromChoices ' );
338
-
339
315
$ this ->assertSame (array (), $ loader ->loadChoicesForValues (array ()));
340
316
}
341
317
342
318
public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId ()
343
319
{
344
320
$ loader = new DoctrineChoiceLoader (
345
- $ this ->factory ,
346
321
$ this ->om ,
347
322
$ this ->class ,
348
323
$ this ->idReader ,
@@ -362,9 +337,6 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId()
362
337
$ this ->repository ->expects ($ this ->never ())
363
338
->method ('findAll ' );
364
339
365
- $ this ->factory ->expects ($ this ->never ())
366
- ->method ('createListFromChoices ' );
367
-
368
340
$ this ->objectLoader ->expects ($ this ->once ())
369
341
->method ('getEntitiesByIds ' )
370
342
->with ('idField ' , array (4 => '3 ' , 7 => '2 ' ))
@@ -386,15 +358,13 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId()
386
358
public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven ()
387
359
{
388
360
$ loader = new DoctrineChoiceLoader (
389
- $ this ->factory ,
390
361
$ this ->om ,
391
362
$ this ->class ,
392
363
$ this ->idReader
393
364
);
394
365
395
366
$ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
396
367
$ value = function (\stdClass $ object ) { return $ object ->name ; };
397
- $ choiceList = new ArrayChoiceList ($ choices , $ value );
398
368
399
369
$ this ->idReader ->expects ($ this ->any ())
400
370
->method ('isSingleId ' )
@@ -404,11 +374,6 @@ public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven()
404
374
->method ('findAll ' )
405
375
->willReturn ($ choices );
406
376
407
- $ this ->factory ->expects ($ this ->once ())
408
- ->method ('createListFromChoices ' )
409
- ->with ($ choices , $ value )
410
- ->willReturn ($ choiceList );
411
-
412
377
$ this ->assertSame (array ($ this ->obj2 ), $ loader ->loadChoicesForValues (
413
378
array ('B ' ),
414
379
$ value
@@ -418,7 +383,6 @@ public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven()
418
383
public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader ()
419
384
{
420
385
$ loader = new DoctrineChoiceLoader (
421
- $ this ->factory ,
422
386
$ this ->om ,
423
387
$ this ->class ,
424
388
$ this ->idReader ,
@@ -439,9 +403,6 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader()
439
403
$ this ->repository ->expects ($ this ->never ())
440
404
->method ('findAll ' );
441
405
442
- $ this ->factory ->expects ($ this ->never ())
443
- ->method ('createListFromChoices ' );
444
-
445
406
$ this ->objectLoader ->expects ($ this ->once ())
446
407
->method ('getEntitiesByIds ' )
447
408
->with ('idField ' , array ('2 ' ))
0 commit comments