Completed
Pull Request — master (#5097)
by Marko
04:00 queued 16s
created

LegacyModelChoiceListTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 74
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 2
A testLoadFromEntity() 0 22 1
A testLoadFromCustomQuery() 0 18 1
A testLoadArrayOfChoices() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Form\ChoiceList;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
18
use Sonata\AdminBundle\Model\ModelManagerInterface;
19
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo;
20
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
21
22
class LegacyModelChoiceListTest extends TestCase
23
{
24
    private $modelManager = null;
25
26
    public function setUp(): void
27
    {
28
        if (!class_exists(SimpleChoiceList::class)) {
29
            $this->markTestSkipped('Test only available for <= SF2.8');
30
        }
31
32
        $this->modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class);
33
34
        $this->modelManager->expects($this->any())
35
            ->method('getIdentifierFieldNames')
36
            ->will($this->returnValue(['foo', 'bar']));
37
    }
38
39
    public function testLoadFromEntity(): void
40
    {
41
        // Get choices From Entity, count($this->identifier) > 1
42
        $fooA = new Foo();
43
        $fooA->setBar(1);
44
        $fooB = new Foo();
45
        $fooB->setBar(2);
46
47
        $result = [$fooA, $fooB];
48
49
        $this->modelManager->expects($this->once())
50
            ->method('findBy')
51
            ->will($this->returnValue($result));
52
53
        $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...
54
            $this->modelManager,
55
            \Sonata\AdminBundle\Tests\Fixtures\Entity\Foo::class,
56
            'bar'
57
        );
58
59
        $this->assertSame(array_keys($result), $modelChoice->getChoices());
60
    }
61
62
    public function testLoadFromCustomQuery(): void
63
    {
64
        // Get choices From Custom Query, count($this->identifier) > 1
65
        $result = [1, 2];
66
67
        $this->modelManager->expects($this->any())
68
            ->method('executeQuery')
69
            ->will($this->returnValue($result));
70
71
        $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...
72
            $this->modelManager,
73
            \Sonata\AdminBundle\Tests\Fixtures\Entity\Foo::class,
74
            null,
75
            'SELECT foo, baz from foo'
76
        );
77
78
        $this->assertSame(array_keys($result), $modelChoice->getChoices());
79
    }
80
81
    public function testLoadArrayOfChoices(): void
82
    {
83
        // Get choices from Array of choices, count($this->identifier) > 1
84
        $result = [1, 2];
85
        $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...
86
            $this->modelManager,
87
            \Sonata\AdminBundle\Tests\Fixtures\Entity\Foo::class,
88
            null,
89
            null,
90
            $result
91
        );
92
93
        $this->assertSame(array_keys($result), $modelChoice->getChoices());
94
    }
95
}
96