Completed
Push — master ( e0017c...6b1304 )
by Tom
14s queued 11s
created

DoctrineModuleTest/Form/Element/ProxyTest.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModuleTest\Form\Element;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Persistence\Mapping\ClassMetadata;
9
use DoctrineModule\Form\Element\Proxy;
10
use DoctrineModuleTest\Form\Element\TestAsset\FormObject;
11
use PHPUnit\Framework\TestCase;
12
use RuntimeException;
13
use stdClass;
14
use function array_shift;
15
use function func_get_args;
16
use function get_class;
17
18
/**
19
 * Tests for the Collection pagination adapter
20
 *
21
 * @link    http://www.doctrine-project.org/
22
 *
23
 * @covers  \DoctrineModule\Form\Element\Proxy
24
 */
25
class ProxyTest extends TestCase
26
{
27
    /** @var ClassMetadata */
28
    protected $metadata;
29
30
    /** @var Proxy */
31
    protected $proxy;
32
33
    /**
34
     * {@inheritDoc}.
35
     */
36
    protected function setUp() : void
37
    {
38
        parent::setUp();
39
        $this->proxy = new Proxy();
40
    }
41
42
    public function testExceptionThrownForMissingObjectManager() : void
43
    {
44
        $this->expectException(RuntimeException::class);
45
        $this->expectExceptionMessage('No object manager was set');
46
47
        $this->proxy->setOptions(['target_class' => 'DoctrineModuleTest\Form\Element\TestAsset\FormObject']);
48
        $this->proxy->getValueOptions();
49
    }
50
51
    public function testExceptionThrownForMissingTargetClass() : void
52
    {
53
        $this->expectException(RuntimeException::class);
54
        $this->expectExceptionMessage('No target class was set');
55
56
        $this->proxy->setOptions([
57
            'object_manager' => $this->createMock('Doctrine\Persistence\ObjectManager'),
58
        ]);
59
        $this->proxy->getValueOptions();
60
    }
61
62
    public function testExceptionThrownForMissingFindMethodName() : void
63
    {
64
        $this->expectException(RuntimeException::class);
65
        $this->expectExceptionMessage('No method name was set');
66
67
        $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
68
        $metadata    = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata');
69
70
        $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager');
71
        $objectManager->expects($this->once())
72
            ->method('getClassMetadata')
73
            ->with($this->equalTo($objectClass))
74
            ->will($this->returnValue($metadata));
75
76
        $this->proxy->setOptions([
77
            'object_manager' => $objectManager,
78
            'target_class'   => $objectClass,
79
            'find_method'    => ['no_name'],
80
        ]);
81
82
        $this->proxy->getValueOptions();
83
    }
84
85
    public function testExceptionFindMethodNameNotExistentInRepository() : void
86
    {
87
        $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
88
        $metadata    = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata');
89
90
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
91
92
        $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager');
93
        $objectManager->expects($this->once())
94
            ->method('getClassMetadata')
95
            ->with($this->equalTo($objectClass))
96
            ->will($this->returnValue($metadata));
97
98
        $objectManager->expects($this->once())
99
            ->method('getRepository')
100
            ->with($this->equalTo($objectClass))
101
            ->will($this->returnValue($objectRepository));
102
103
        $this->proxy->setOptions([
104
            'object_manager' => $objectManager,
105
            'target_class'   => $objectClass,
106
            'find_method'    => ['name' => 'NotExistent'],
107
        ]);
108
109
        $this->expectException(
110
            'RuntimeException'
111
        );
112
        $this->expectExceptionMessage(
113
            'Method "NotExistent" could not be found in repository "' . get_class($objectRepository) . '"'
114
        );
115
116
        $this->proxy->getValueOptions();
117
    }
118
119
    public function testExceptionThrownForMissingRequiredParameter() : void
120
    {
121
        $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
122
        $metadata    = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata');
123
124
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
125
126
        $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager');
127
        $objectManager->expects($this->once())
0 ignored issues
show
The method once() does not seem to exist on object<DoctrineModuleTest\Form\Element\ProxyTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method expects cannot be called on $objectManager (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
128
            ->method('getClassMetadata')
129
            ->with($this->equalTo($objectClass))
0 ignored issues
show
The method equalTo() does not seem to exist on object<DoctrineModuleTest\Form\Element\ProxyTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
            ->will($this->returnValue($metadata));
0 ignored issues
show
The method returnValue() does not seem to exist on object<DoctrineModuleTest\Form\Element\ProxyTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
132
        $objectManager->expects($this->once())
0 ignored issues
show
The method once() does not seem to exist on object<DoctrineModuleTest\Form\Element\ProxyTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method expects cannot be called on $objectManager (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
133
            ->method('getRepository')
134
            ->with($this->equalTo($objectClass))
0 ignored issues
show
The method equalTo() does not seem to exist on object<DoctrineModuleTest\Form\Element\ProxyTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
            ->will($this->returnValue($objectRepository));
0 ignored issues
show
The method returnValue() does not seem to exist on object<DoctrineModuleTest\Form\Element\ProxyTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
137
        $this->proxy->setOptions([
138
            'object_manager' => $objectManager,
139
            'target_class'   => $objectClass,
140
            'find_method'    => [
141
                'name' => 'findBy',
142
                'params' => [],
143
            ],
144
        ]);
145
146
        $this->expectException(
147
            'RuntimeException'
148
        );
149
        $this->expectExceptionMessage(
150
            'Required parameter "criteria" with no default value for method "findBy" in repository "'
151
            . get_class($objectRepository) . '" was not provided'
152
        );
153
154
        $this->proxy->getValueOptions();
155
    }
156
157
    public function testToStringIsUsedForGetValueOptions() : void
158
    {
159
        $this->prepareProxy();
160
161
        $result = $this->proxy->getValueOptions();
162
        $this->assertEquals($result[0]['label'], 'object one username');
163
        $this->assertEquals($result[1]['label'], 'object two username');
164
        $this->assertEquals($result[0]['value'], 1);
165
        $this->assertEquals($result[1]['value'], 2);
166
    }
167
168
    public function testPropertyGetterUsedForGetValueOptions() : void
169
    {
170
        $this->prepareProxy();
171
172
        $this->proxy->setOptions(['property' => 'password']);
173
174
        $this->metadata->expects($this->exactly(2))
175
            ->method('hasField')
176
            ->with($this->equalTo('password'))
177
            ->will($this->returnValue(true));
178
179
        $result = $this->proxy->getValueOptions();
180
        $this->assertEquals($result[0]['label'], 'object one password');
181
        $this->assertEquals($result[1]['label'], 'object two password');
182
        $this->assertEquals($result[0]['value'], 1);
183
        $this->assertEquals($result[1]['value'], 2);
184
    }
185
186
    public function testPublicPropertyUsedForGetValueOptions() : void
187
    {
188
        $this->prepareProxy();
189
190
        $this->proxy->setOptions(['property' => 'email']);
191
192
        $this
193
            ->metadata
194
            ->expects($this->exactly(2))
195
            ->method('hasField')
196
            ->with($this->equalTo('email'))
197
            ->will($this->returnValue(true));
198
199
        $result = $this->proxy->getValueOptions();
200
        $this->assertEquals($result[0]['label'], 'object one email');
201
        $this->assertEquals($result[1]['label'], 'object two email');
202
        $this->assertEquals($result[0]['value'], 1);
203
        $this->assertEquals($result[1]['value'], 2);
204
    }
205
206
    public function testIsMethodOptionUsedForGetValueOptions() : void
207
    {
208
        $this->prepareProxy();
209
210
        $this->proxy->setOptions([
211
            'property'  => 'name',
212
            'is_method' => true,
213
        ]);
214
215
        $this->metadata->expects($this->never())
216
            ->method('hasField');
217
218
        $result = $this->proxy->getValueOptions();
219
        $this->assertEquals($result[0]['label'], 'object one firstname object one surname');
220
        $this->assertEquals($result[1]['label'], 'object two firstname object two surname');
221
        $this->assertEquals($result[0]['value'], 1);
222
        $this->assertEquals($result[1]['value'], 2);
223
    }
224
225
    public function testDisplayEmptyItemAndEmptyItemLabelOptionsUsedForGetValueOptions() : void
226
    {
227
        $this->prepareProxy();
228
229
        $this->proxy->setOptions([
230
            'display_empty_item' => true,
231
            'empty_item_label'   => '---',
232
        ]);
233
234
        $result = $this->proxy->getValueOptions();
235
        $this->assertArrayHasKey('', $result);
236
        $this->assertEquals($result[''], '---');
237
    }
238
239
    public function testLabelGeneratorUsedForGetValueOptions() : void
240
    {
241
        $this->prepareProxy();
242
243
        $this->proxy->setOptions([
244
            'label_generator' => static function ($targetEntity) {
245
                return $targetEntity->getEmail();
246
            },
247
        ]);
248
249
        $this->metadata->expects($this->never())
250
            ->method('hasField');
251
252
        $result = $this->proxy->getvalueOptions();
253
        $this->assertEquals($result[0]['label'], 'object one email');
254
        $this->assertEquals($result[1]['label'], 'object two email');
255
        $this->assertEquals($result[0]['value'], 1);
256
        $this->assertEquals($result[1]['value'], 2);
257
    }
258
259
    public function testExceptionThrownForNonCallableLabelGenerator() : void
260
    {
261
        $this->prepareProxy();
262
263
        $this->expectException(
264
            'TypeError'
265
        );
266
        $this->expectExceptionMessage(
267
            'Argument 1 passed to DoctrineModule\Form\Element\Proxy::setLabelGenerator() must be callable'
268
        );
269
270
        $this->proxy->setOptions(['label_generator' => 'I throw an invalid type error']);
271
    }
272
273
    public function testUsingOptionAttributesOfTypeString() : void
274
    {
275
        $this->prepareProxy();
276
277
        $this->proxy->setOptions([
278
            'option_attributes' => [
279
                'class' => 'foo',
280
                'lang' => 'en',
281
            ],
282
        ]);
283
284
        $options = $this->proxy->getValueOptions();
285
286
        $expectedAttributes = [
287
            'class' => 'foo',
288
            'lang' => 'en',
289
        ];
290
291
        $this->assertCount(2, $options);
292
293
        $this->assertArrayHasKey('attributes', $options[0]);
294
        $this->assertArrayHasKey('attributes', $options[1]);
295
296
        $this->assertEquals($expectedAttributes, $options[0]['attributes']);
297
        $this->assertEquals($expectedAttributes, $options[1]['attributes']);
298
    }
299
300
    public function testUsingOptionAttributesOfTypeCallableReturningString() : void
301
    {
302
        $this->prepareProxy();
303
304
        $this->proxy->setOptions([
305
            'option_attributes' => [
306
                'data-id' => static function ($object) {
307
                    return $object->getId();
308
                },
309
            ],
310
        ]);
311
312
        $options = $this->proxy->getValueOptions();
313
314
        $this->assertCount(2, $options);
315
316
        $this->assertArrayHasKey('attributes', $options[0]);
317
        $this->assertArrayHasKey('attributes', $options[1]);
318
319
        $this->assertEquals(['data-id' => 1], $options[0]['attributes']);
320
        $this->assertEquals(['data-id' => 2], $options[1]['attributes']);
321
    }
322
323
    public function testRuntimeExceptionOnWrongOptionAttributesValue() : void
324
    {
325
        $this->prepareProxy();
326
327
        $this->proxy->setOptions([
328
            'option_attributes' => [
329
                'data-id' => new stdClass(['id' => 1]),
330
            ],
331
        ]);
332
333
        $this->expectException('RuntimeException');
334
335
        $this->proxy->getValueOptions();
336
    }
337
338
    public function testCanWorkWithEmptyTables() : void
339
    {
340
        $this->prepareEmptyProxy();
341
342
        $result = $this->proxy->getValueOptions();
343
        $this->assertEquals([], $result);
344
    }
345
346
    public function testCanWorkWithEmptyDataReturnedAsArray() : void
347
    {
348
        $this->prepareEmptyProxy([]);
349
350
        $result = $this->proxy->getValueOptions();
351
        $this->assertEquals([], $result);
352
    }
353
354
    public function testExceptionThrownForNonTraversableResults() : void
355
    {
356
        $this->prepareEmptyProxy(new stdClass());
357
358
        $this->expectException(
359
            'DoctrineModule\Form\Element\Exception\InvalidRepositoryResultException'
360
        );
361
        $this->expectExceptionMessage(
362
            'return value must be an array or Traversable'
363
        );
364
365
        $this->proxy->getValueOptions();
366
    }
367
368
    public function testUsingFindMethod() : void
369
    {
370
        $this->prepareFilteredProxy();
371
372
        $this->proxy->getValueOptions();
373
    }
374
375
    /**
376
     * A \RuntimeException should be thrown when the optgroup_identifier option does not reflect an existing method
377
     * within the target object
378
     */
379
    public function testExceptionThrownWhenOptgroupIdentifiesNotCallable() : void
380
    {
381
        $this->prepareProxyWithOptgroupPreset();
382
383
        $this->proxy->setOptions(['optgroup_identifier' => 'NonExistantFunctionName']);
384
385
        $this->expectException('RuntimeException');
386
387
        $this->proxy->getValueOptions();
388
    }
389
390
    /**
391
     * Tests the following case:
392
     *
393
     * An optgroup identifier has been given.
394
     * Two entries have the optgroup value "Group One".
395
     * One entry has the optgroup value "Group Two".
396
     *
397
     * Entries should be grouped accordingly under the respective keys.
398
     */
399
    public function testValueOptionsGeneratedProperlyWithOptgroups() : void
400
    {
401
        $this->prepareProxyWithOptgroupPreset();
402
403
        $this->proxy->setOptions(['optgroup_identifier' => 'optgroup']);
404
405
        $valueOptions = $this->proxy->getValueOptions();
406
407
        $expectedOutput = [
408
            'Group One' => [
409
                'label' => 'Group One',
410
                'options' => [
411
                    0 => [
412
                        'label' => 'object one username',
413
                        'value' => 1,
414
                        'attributes' => [],
415
                    ],
416
                    1 => [
417
                        'label' => 'object two username',
418
                        'value' => 2,
419
                        'attributes' => [],
420
                    ],
421
                ],
422
            ],
423
            'Group Two' => [
424
                'label' => 'Group Two',
425
                'options' => [
426
                    0 => [
427
                        'label' => 'object three username',
428
                        'value' => 3,
429
                        'attributes' => [],
430
                    ],
431
                ],
432
            ],
433
        ];
434
435
        $this->assertEquals($expectedOutput, $valueOptions);
436
    }
437
438
    /**
439
     * Tests the following case:
440
     *
441
     * An optgroup identifier has been given.
442
     * Both entries do not have an optgroup value.
443
     * optgroup_default has been configured.
444
     *
445
     * Both entries should be grouped under the optgroup_default key.
446
     */
447
    public function testEmptyOptgroupValueBelongsToOptgroupDefaultIfConfigured() : void
448
    {
449
        $this->prepareProxy();
450
451
        $this->proxy->setOptions([
452
            'optgroup_identifier' => 'optgroup',
453
            'optgroup_default'    => 'Others',
454
        ]);
455
456
        $valueOptions = $this->proxy->getValueOptions();
457
458
        $expectedOutput = [
459
            'Others' => [
460
                'label' => 'Others',
461
                'options' => [
462
                    0 => [
463
                        'label' => 'object one username',
464
                        'value' => 1,
465
                        'attributes' => [],
466
                    ],
467
                    1 => [
468
                        'label' => 'object two username',
469
                        'value' => 2,
470
                        'attributes' => [],
471
                    ],
472
                ],
473
            ],
474
        ];
475
476
        $this->assertEquals($expectedOutput, $valueOptions);
477
    }
478
479
    /**
480
     * Tests the following case:
481
     *
482
     * An optgroup identifier has been given.
483
     * One entry has a valid value.
484
     * A second entry has a null value.
485
     * No optgroup_default has been configured.
486
     *
487
     * Entry one should be grouped, entry two shouldn't be.
488
     */
489
    public function testEmptyOptgroupValueBelongsToNoOptgroupIfNotConfigured() : void
490
    {
491
        $this->prepareProxyWithOptgroupPresetThatHasPartiallyEmptyOptgroupValues();
492
493
        $this->proxy->setOptions(['optgroup_identifier' => 'optgroup']);
494
495
        $valueOptions = $this->proxy->getValueOptions();
496
497
        $expectedOutput = [
498
            'Group One' => [
499
                'label' => 'Group One',
500
                'options' => [
501
                    0 => [
502
                        'label' => 'object one username',
503
                        'value' => 1,
504
                        'attributes' => [],
505
                    ],
506
                ],
507
            ],
508
            0 => [
509
                'label' => 'object two username',
510
                'value' => 2,
511
                'attributes' => [],
512
            ],
513
        ];
514
515
        $this->assertEquals($expectedOutput, $valueOptions);
516
    }
517
518
    protected function prepareProxy() : void
519
    {
520
        $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
521
        $objectOne   = new FormObject();
522
        $objectTwo   = new FormObject();
523
524
        $objectOne->setId(1)
525
            ->setUsername('object one username')
526
            ->setPassword('object one password')
527
            ->setEmail('object one email')
528
            ->setFirstname('object one firstname')
529
            ->setSurname('object one surname');
530
531
        $objectTwo->setId(2)
532
            ->setUsername('object two username')
533
            ->setPassword('object two password')
534
            ->setEmail('object two email')
535
            ->setFirstname('object two firstname')
536
            ->setSurname('object two surname');
537
538
        $result = new ArrayCollection([$objectOne, $objectTwo]);
539
540
        $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata');
541
        $metadata
542
            ->expects($this->any())
543
            ->method('getIdentifierValues')
544
            ->will(
545
                $this->returnCallback(
546
                    static function () use ($objectOne, $objectTwo) {
547
                        $input = func_get_args();
548
                        $input = array_shift($input);
549
550
                        if ($input === $objectOne) {
551
                            return ['id' => 1];
552
                        }
553
554
                        if ($input === $objectTwo) {
555
                            return ['id' => 2];
556
                        }
557
558
                        return [];
559
                    }
560
                )
561
            );
562
563
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
564
        $objectRepository->expects($this->any())
565
            ->method('findAll')
566
            ->will($this->returnValue($result));
567
568
        $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager');
569
        $objectManager->expects($this->any())
570
            ->method('getClassMetadata')
571
            ->with($this->equalTo($objectClass))
572
            ->will($this->returnValue($metadata));
573
574
        $objectManager
575
            ->expects($this->any())
576
            ->method('getRepository')
577
            ->with($this->equalTo($objectClass))
578
            ->will($this->returnValue($objectRepository));
579
580
        $this->proxy->setOptions([
581
            'object_manager' => $objectManager,
582
            'target_class'   => $objectClass,
583
        ]);
584
585
        $this->metadata = $metadata;
586
    }
587
588
    protected function prepareProxyWithOptgroupPreset() : void
589
    {
590
        $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
591
        $objectOne   = new FormObject();
592
        $objectTwo   = new FormObject();
593
        $objectThree = new FormObject();
594
595
        $objectOne->setId(1)
596
            ->setUsername('object one username')
597
            ->setPassword('object one password')
598
            ->setEmail('object one email')
599
            ->setFirstname('object one firstname')
600
            ->setSurname('object one surname')
601
            ->setOptgroup('Group One');
602
603
        $objectTwo->setId(2)
604
            ->setUsername('object two username')
605
            ->setPassword('object two password')
606
            ->setEmail('object two email')
607
            ->setFirstname('object two firstname')
608
            ->setSurname('object two surname')
609
            ->setOptgroup('Group One');
610
611
        $objectThree->setId(3)
612
            ->setUsername('object three username')
613
            ->setPassword('object three password')
614
            ->setEmail('object three email')
615
            ->setFirstname('object three firstname')
616
            ->setSurname('object three surname')
617
            ->setOptgroup('Group Two');
618
619
        $result = new ArrayCollection([$objectOne, $objectTwo, $objectThree]);
620
621
        $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata');
622
        $metadata
623
            ->expects($this->any())
624
            ->method('getIdentifierValues')
625
            ->will(
626
                $this->returnCallback(
627
                    static function () use ($objectOne, $objectTwo, $objectThree) {
628
                        $input = func_get_args();
629
                        $input = array_shift($input);
630
631
                        if ($input === $objectOne) {
632
                            return ['id' => 1];
633
                        }
634
635
                        if ($input === $objectTwo) {
636
                            return ['id' => 2];
637
                        }
638
639
                        if ($input === $objectThree) {
640
                            return ['id' => 3];
641
                        }
642
643
                        return [];
644
                    }
645
                )
646
            );
647
648
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
649
        $objectRepository->expects($this->any())
650
            ->method('findAll')
651
            ->will($this->returnValue($result));
652
653
        $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager');
654
        $objectManager->expects($this->any())
655
            ->method('getClassMetadata')
656
            ->with($this->equalTo($objectClass))
657
            ->will($this->returnValue($metadata));
658
659
        $objectManager
660
            ->expects($this->any())
661
            ->method('getRepository')
662
            ->with($this->equalTo($objectClass))
663
            ->will($this->returnValue($objectRepository));
664
665
        $this->proxy->setOptions([
666
            'object_manager' => $objectManager,
667
            'target_class'   => $objectClass,
668
        ]);
669
670
        $this->metadata = $metadata;
671
    }
672
673
    protected function prepareProxyWithOptgroupPresetThatHasPartiallyEmptyOptgroupValues() : void
674
    {
675
        $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
676
        $objectOne   = new FormObject();
677
        $objectTwo   = new FormObject();
678
679
        $objectOne->setId(1)
680
            ->setUsername('object one username')
681
            ->setPassword('object one password')
682
            ->setEmail('object one email')
683
            ->setFirstname('object one firstname')
684
            ->setSurname('object one surname')
685
            ->setOptgroup('Group One');
686
687
        $objectTwo->setId(2)
688
            ->setUsername('object two username')
689
            ->setPassword('object two password')
690
            ->setEmail('object two email')
691
            ->setFirstname('object two firstname')
692
            ->setSurname('object two surname');
693
694
        $result = new ArrayCollection([$objectOne, $objectTwo]);
695
696
        $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata');
697
        $metadata
698
            ->expects($this->any())
699
            ->method('getIdentifierValues')
700
            ->will(
701
                $this->returnCallback(
702
                    static function () use ($objectOne, $objectTwo) {
703
                        $input = func_get_args();
704
                        $input = array_shift($input);
705
706
                        if ($input === $objectOne) {
707
                            return ['id' => 1];
708
                        }
709
710
                        if ($input === $objectTwo) {
711
                            return ['id' => 2];
712
                        }
713
714
                        return [];
715
                    }
716
                )
717
            );
718
719
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
720
        $objectRepository->expects($this->any())
721
            ->method('findAll')
722
            ->will($this->returnValue($result));
723
724
        $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager');
725
        $objectManager->expects($this->any())
726
            ->method('getClassMetadata')
727
            ->with($this->equalTo($objectClass))
728
            ->will($this->returnValue($metadata));
729
730
        $objectManager
731
            ->expects($this->any())
732
            ->method('getRepository')
733
            ->with($this->equalTo($objectClass))
734
            ->will($this->returnValue($objectRepository));
735
736
        $this->proxy->setOptions([
737
            'object_manager' => $objectManager,
738
            'target_class'   => $objectClass,
739
        ]);
740
741
        $this->metadata = $metadata;
742
    }
743
744
    protected function prepareFilteredProxy() : void
745
    {
746
        $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
747
        $objectOne   = new FormObject();
748
        $objectTwo   = new FormObject();
749
750
        $objectOne->setId(1)
751
            ->setUsername('object one username')
752
            ->setPassword('object one password')
753
            ->setEmail('object one email')
754
            ->setFirstname('object one firstname')
755
            ->setSurname('object one surname');
756
757
        $objectTwo->setId(2)
758
            ->setUsername('object two username')
759
            ->setPassword('object two password')
760
            ->setEmail('object two email')
761
            ->setFirstname('object two firstname')
762
            ->setSurname('object two surname');
763
764
        $result = new ArrayCollection([$objectOne, $objectTwo]);
765
766
        $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata');
767
        $metadata
768
            ->expects($this->exactly(2))
769
            ->method('getIdentifierValues')
770
            ->will(
771
                $this->returnCallback(
772
                    static function () use ($objectOne, $objectTwo) {
773
                        $input = func_get_args();
774
                        $input = array_shift($input);
775
                        if ($input === $objectOne) {
776
                            return ['id' => 1];
777
                        }
778
779
                        if ($input === $objectTwo) {
780
                            return ['id' => 2];
781
                        }
782
783
                        return [];
784
                    }
785
                )
786
            );
787
788
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
789
        $objectRepository
790
            ->expects($this->once())
791
            ->method('findBy')
792
            ->will($this->returnValue($result));
793
794
        $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager');
795
        $objectManager
796
            ->expects($this->once())
797
            ->method('getClassMetadata')
798
            ->with($this->equalTo($objectClass))
799
            ->will($this->returnValue($metadata));
800
801
        $objectManager
802
            ->expects($this->once())
803
            ->method('getRepository')
804
            ->with($this->equalTo($objectClass))
805
            ->will($this->returnValue($objectRepository));
806
807
        $this->proxy->setOptions([
808
            'object_manager' => $objectManager,
809
            'target_class'   => $objectClass,
810
            'find_method' => [
811
                'name' => 'findBy',
812
                'params' => [
813
                    'criteria' => ['email' => 'object one email'],
814
                ],
815
            ],
816
        ]);
817
818
        $this->metadata = $metadata;
819
    }
820
821
    /**
822
     * @param mixed $result
823
     */
824
    public function prepareEmptyProxy($result = null) : void
825
    {
826
        if ($result === null) {
827
            $result = new ArrayCollection();
828
        }
829
830
        $objectClass      = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
831
        $metadata         = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata');
832
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
833
834
        $objectRepository
835
            ->expects($this->once())
836
            ->method('findAll')
837
            ->will($this->returnValue($result));
838
839
        $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager');
840
        $objectManager
841
            ->expects($this->once())
842
            ->method('getClassMetadata')
843
            ->with($this->equalTo($objectClass))
844
            ->will($this->returnValue($metadata));
845
846
        $objectManager
847
            ->expects($this->once())
848
            ->method('getRepository')
849
            ->with($this->equalTo($objectClass))
850
            ->will($this->returnValue($objectRepository));
851
852
        $this->proxy->setOptions([
853
            'object_manager' => $objectManager,
854
            'target_class'   => $objectClass,
855
        ]);
856
857
        $this->metadata = $metadata;
858
    }
859
}
860