1818use Symfony \Bridge \Doctrine \Form \ChoiceList \EntityLoaderInterface ;
1919use Symfony \Bridge \Doctrine \Form \ChoiceList \IdReader ;
2020use Symfony \Component \Form \ChoiceList \ArrayChoiceList ;
21- use Symfony \Component \Form \ChoiceList \Factory \ChoiceListFactoryInterface ;
2221
2322/**
2423 * @author Bernhard Schussek <bschussek@gmail.com>
2524 */
2625class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
2726{
28- /**
29- * @var ChoiceListFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
30- */
31- private $ factory ;
32-
3327 /**
3428 * @var ObjectManager|\PHPUnit_Framework_MockObject_MockObject
3529 */
@@ -98,44 +92,68 @@ protected function setUp()
9892 public function testLoadChoiceList ()
9993 {
10094 $ loader = new DoctrineChoiceLoader (
101- $ this ->factory ,
10295 $ this ->om ,
10396 $ this ->class ,
10497 $ this ->idReader
10598 );
10699
107100 $ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
108- $ choiceList = new ArrayChoiceList (array ());
109101 $ value = function () {};
102+ $ choiceList = new ArrayChoiceList ($ choices , $ value );
110103
111104 $ this ->repository ->expects ($ this ->once ())
112105 ->method ('findAll ' )
113106 ->willReturn ($ choices );
114107
115- $ this ->factory ->expects ($ this ->once ())
116- ->method ('createListFromChoices ' )
117- ->with ($ choices , $ value )
118- ->willReturn ($ choiceList );
108+ $ this ->assertEquals ($ choiceList , $ loader ->loadChoiceList ($ value ));
119109
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 ));
121140
122141 // no further loads on subsequent calls
123142
124- $ this ->assertSame ($ choiceList , $ loader ->loadChoiceList ($ value ));
143+ $ this ->assertSame ($ loaded , $ loader ->loadChoiceList ($ value ));
125144 }
126145
127146 public function testLoadChoiceListUsesObjectLoaderIfAvailable ()
128147 {
129148 $ loader = new DoctrineChoiceLoader (
130- $ this ->factory ,
131149 $ this ->om ,
132150 $ this ->class ,
133151 $ this ->idReader ,
134152 $ this ->objectLoader
135153 );
136154
137155 $ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
138- $ choiceList = new ArrayChoiceList (array () );
156+ $ choiceList = new ArrayChoiceList ($ choices );
139157
140158 $ this ->repository ->expects ($ this ->never ())
141159 ->method ('findAll ' );
@@ -144,39 +162,27 @@ public function testLoadChoiceListUsesObjectLoaderIfAvailable()
144162 ->method ('getEntities ' )
145163 ->willReturn ($ choices );
146164
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 ());
153166
154167 // no further loads on subsequent calls
155168
156- $ this ->assertSame ($ choiceList , $ loader ->loadChoiceList ());
169+ $ this ->assertSame ($ loaded , $ loader ->loadChoiceList ());
157170 }
158171
159172 public function testLoadValuesForChoices ()
160173 {
161174 $ loader = new DoctrineChoiceLoader (
162- $ this ->factory ,
163175 $ this ->om ,
164176 $ this ->class ,
165177 $ this ->idReader
166178 );
167179
168180 $ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
169- $ choiceList = new ArrayChoiceList ($ choices );
170181
171182 $ this ->repository ->expects ($ this ->once ())
172183 ->method ('findAll ' )
173184 ->willReturn ($ choices );
174185
175- $ this ->factory ->expects ($ this ->once ())
176- ->method ('createListFromChoices ' )
177- ->with ($ choices )
178- ->willReturn ($ choiceList );
179-
180186 $ this ->assertSame (array ('1 ' , '2 ' ), $ loader ->loadValuesForChoices (array ($ this ->obj2 , $ this ->obj3 )));
181187
182188 // no further loads on subsequent calls
@@ -187,7 +193,6 @@ public function testLoadValuesForChoices()
187193 public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices ()
188194 {
189195 $ loader = new DoctrineChoiceLoader (
190- $ this ->factory ,
191196 $ this ->om ,
192197 $ this ->class ,
193198 $ this ->idReader
@@ -196,16 +201,12 @@ public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices()
196201 $ this ->repository ->expects ($ this ->never ())
197202 ->method ('findAll ' );
198203
199- $ this ->factory ->expects ($ this ->never ())
200- ->method ('createListFromChoices ' );
201-
202204 $ this ->assertSame (array (), $ loader ->loadValuesForChoices (array ()));
203205 }
204206
205207 public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId ()
206208 {
207209 $ loader = new DoctrineChoiceLoader (
208- $ this ->factory ,
209210 $ this ->om ,
210211 $ this ->class ,
211212 $ this ->idReader
@@ -218,9 +219,6 @@ public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
218219 $ this ->repository ->expects ($ this ->never ())
219220 ->method ('findAll ' );
220221
221- $ this ->factory ->expects ($ this ->never ())
222- ->method ('createListFromChoices ' );
223-
224222 $ this ->idReader ->expects ($ this ->any ())
225223 ->method ('getIdValue ' )
226224 ->with ($ this ->obj2 )
@@ -232,15 +230,13 @@ public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
232230 public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven ()
233231 {
234232 $ loader = new DoctrineChoiceLoader (
235- $ this ->factory ,
236233 $ this ->om ,
237234 $ this ->class ,
238235 $ this ->idReader
239236 );
240237
241238 $ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
242239 $ value = function (\stdClass $ object ) { return $ object ->name ; };
243- $ choiceList = new ArrayChoiceList ($ choices , $ value );
244240
245241 $ this ->idReader ->expects ($ this ->any ())
246242 ->method ('isSingleId ' )
@@ -250,11 +246,6 @@ public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven()
250246 ->method ('findAll ' )
251247 ->willReturn ($ choices );
252248
253- $ this ->factory ->expects ($ this ->once ())
254- ->method ('createListFromChoices ' )
255- ->with ($ choices , $ value )
256- ->willReturn ($ choiceList );
257-
258249 $ this ->assertSame (array ('B ' ), $ loader ->loadValuesForChoices (
259250 array ($ this ->obj2 ),
260251 $ value
@@ -264,7 +255,6 @@ public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven()
264255 public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader ()
265256 {
266257 $ loader = new DoctrineChoiceLoader (
267- $ this ->factory ,
268258 $ this ->om ,
269259 $ this ->class ,
270260 $ this ->idReader
@@ -279,9 +269,6 @@ public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader()
279269 $ this ->repository ->expects ($ this ->never ())
280270 ->method ('findAll ' );
281271
282- $ this ->factory ->expects ($ this ->never ())
283- ->method ('createListFromChoices ' );
284-
285272 $ this ->idReader ->expects ($ this ->any ())
286273 ->method ('getIdValue ' )
287274 ->with ($ this ->obj2 )
@@ -296,24 +283,17 @@ public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader()
296283 public function testLoadChoicesForValues ()
297284 {
298285 $ loader = new DoctrineChoiceLoader (
299- $ this ->factory ,
300286 $ this ->om ,
301287 $ this ->class ,
302288 $ this ->idReader
303289 );
304290
305291 $ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
306- $ choiceList = new ArrayChoiceList ($ choices );
307292
308293 $ this ->repository ->expects ($ this ->once ())
309294 ->method ('findAll ' )
310295 ->willReturn ($ choices );
311296
312- $ this ->factory ->expects ($ this ->once ())
313- ->method ('createListFromChoices ' )
314- ->with ($ choices )
315- ->willReturn ($ choiceList );
316-
317297 $ this ->assertSame (array ($ this ->obj2 , $ this ->obj3 ), $ loader ->loadChoicesForValues (array ('1 ' , '2 ' )));
318298
319299 // no further loads on subsequent calls
@@ -324,7 +304,6 @@ public function testLoadChoicesForValues()
324304 public function testLoadChoicesForValuesDoesNotLoadIfEmptyValues ()
325305 {
326306 $ loader = new DoctrineChoiceLoader (
327- $ this ->factory,
328307 $ this ->om ,
329308 $ this ->class ,
330309 $ this ->idReader
@@ -333,16 +312,12 @@ public function testLoadChoicesForValuesDoesNotLoadIfEmptyValues()
333312 $ this ->repository ->expects ($ this ->never ())
334313 ->method ('findAll ' );
335314
336- $ this ->factory ->expects ($ this ->never ())
337- ->method ('createListFromChoices ' );
338-
339315 $ this ->assertSame (array (), $ loader ->loadChoicesForValues (array ()));
340316 }
341317
342318 public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId ()
343319 {
344320 $ loader = new DoctrineChoiceLoader (
345- $ this ->factory ,
346321 $ this ->om ,
347322 $ this ->class ,
348323 $ this ->idReader ,
@@ -362,9 +337,6 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId()
362337 $ this ->repository ->expects ($ this ->never ())
363338 ->method ('findAll ' );
364339
365- $ this ->factory ->expects ($ this ->never ())
366- ->method ('createListFromChoices ' );
367-
368340 $ this ->objectLoader ->expects ($ this ->once ())
369341 ->method ('getEntitiesByIds ' )
370342 ->with ('idField ' , array (4 => '3 ' , 7 => '2 ' ))
@@ -386,15 +358,13 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId()
386358 public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven ()
387359 {
388360 $ loader = new DoctrineChoiceLoader (
389- $ this ->factory ,
390361 $ this ->om ,
391362 $ this ->class ,
392363 $ this ->idReader
393364 );
394365
395366 $ choices = array ($ this ->obj1 , $ this ->obj2 , $ this ->obj3 );
396367 $ value = function (\stdClass $ object ) { return $ object ->name ; };
397- $ choiceList = new ArrayChoiceList ($ choices , $ value );
398368
399369 $ this ->idReader ->expects ($ this ->any ())
400370 ->method ('isSingleId ' )
@@ -404,11 +374,6 @@ public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven()
404374 ->method ('findAll ' )
405375 ->willReturn ($ choices );
406376
407- $ this ->factory ->expects ($ this ->once ())
408- ->method ('createListFromChoices ' )
409- ->with ($ choices , $ value )
410- ->willReturn ($ choiceList );
411-
412377 $ this ->assertSame (array ($ this ->obj2 ), $ loader ->loadChoicesForValues (
413378 array ('B ' ),
414379 $ value
@@ -418,7 +383,6 @@ public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven()
418383 public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader ()
419384 {
420385 $ loader = new DoctrineChoiceLoader (
421- $ this ->factory ,
422386 $ this ->om ,
423387 $ this ->class ,
424388 $ this ->idReader ,
@@ -439,9 +403,6 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader()
439403 $ this ->repository ->expects ($ this ->never ())
440404 ->method ('findAll ' );
441405
442- $ this ->factory ->expects ($ this ->never ())
443- ->method ('createListFromChoices ' );
444-
445406 $ this ->objectLoader ->expects ($ this ->once ())
446407 ->method ('getEntitiesByIds ' )
447408 ->with ('idField ' , array ('2 ' ))
0 commit comments