Completed
Push — 3.x ( dfd511...9d8762 )
by Jordi Sala
21:16 queued 15:43
created

LegacyModelChoiceListTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
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 Sonata\AdminBundle\Tests\Form\ChoiceList;
13
14
use PHPUnit\Framework\TestCase;
15
use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
16
use Sonata\AdminBundle\Model\ModelManagerInterface;
17
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo;
18
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
19
20
class LegacyModelChoiceListTest extends TestCase
21
{
22
    private $modelManager = null;
23
24
    public function setUp()
25
    {
26
        if (!class_exists(SimpleChoiceList::class)) {
27
            $this->markTestSkipped('Test only available for <= SF2.8');
28
        }
29
30
        $this->modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class);
31
32
        $this->modelManager->expects($this->any())
33
            ->method('getIdentifierFieldNames')
34
            ->will($this->returnValue(['foo', 'bar']));
35
    }
36
37
    public function testLoadFromEntity()
38
    {
39
        // Get choices From Entity, count($this->identifier) > 1
40
        $fooA = new Foo();
41
        $fooA->setBar(1);
42
        $fooB = new Foo();
43
        $fooB->setBar(2);
44
45
        $result = [$fooA, $fooB];
46
47
        $this->modelManager->expects($this->once())
48
            ->method('findBy')
49
            ->will($this->returnValue($result));
50
51
        $modelChoice = new ModelChoiceList(
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList has been deprecated with message: Since 3.24, to be removed in 4.0. Use Sonata\AdminBundle\ModelChoiceLoader instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
52
            $this->modelManager,
53
            \Sonata\AdminBundle\Tests\Fixtures\Entity\Foo::class,
54
            'bar'
55
        );
56
57
        $this->assertSame(array_keys($result), $modelChoice->getChoices());
58
    }
59
60
    public function testLoadFromCustomQuery()
61
    {
62
        // Get choices From Custom Query, count($this->identifier) > 1
63
        $result = [1, 2];
64
65
        $this->modelManager->expects($this->any())
66
            ->method('executeQuery')
67
            ->will($this->returnValue($result));
68
69
        $modelChoice = new ModelChoiceList(
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList has been deprecated with message: Since 3.24, to be removed in 4.0. Use Sonata\AdminBundle\ModelChoiceLoader instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
70
            $this->modelManager,
71
            \Sonata\AdminBundle\Tests\Fixtures\Entity\Foo::class,
72
            null,
73
            'SELECT foo, baz from foo'
74
        );
75
76
        $this->assertSame(array_keys($result), $modelChoice->getChoices());
77
    }
78
79
    public function testLoadArrayOfChoices()
80
    {
81
        // Get choices from Array of choices, count($this->identifier) > 1
82
        $result = [1, 2];
83
        $modelChoice = new ModelChoiceList(
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList has been deprecated with message: Since 3.24, to be removed in 4.0. Use Sonata\AdminBundle\ModelChoiceLoader instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
84
            $this->modelManager,
85
            \Sonata\AdminBundle\Tests\Fixtures\Entity\Foo::class,
86
            null,
87
            null,
88
            $result
89
        );
90
91
        $this->assertSame(array_keys($result), $modelChoice->getChoices());
92
    }
93
}
94