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