Completed
Pull Request — 3.x (#4772)
by Grégoire
03:39
created

testTransform()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 17
nc 1
nop 3
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\DataTransformer;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use PHPUnit\Framework\TestCase;
16
use Sonata\AdminBundle\Form\DataTransformer\LegacyModelsToArrayTransformer;
17
use Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity;
18
19
/**
20
 * @author Andrej Hudec <[email protected]>
21
 */
22
class ModelsToArrayTransformerLegacyTest extends TestCase
23
{
24
    private $choiceList;
25
    private $modelManager;
26
27
    /**
28
     * @group legacy
29
     */
30
    public function setUp()
31
    {
32
        if (interface_exists('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')) { // SF2.7+
33
            $this->markTestSkipped('Test only available for < SF2.7');
34
        }
35
36
        $this->choiceList = $this->getMockBuilder('Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList')
37
            ->disableOriginalConstructor()
38
            ->getMock();
39
40
        $this->modelManager = $this->getMockForAbstractClass('Sonata\AdminBundle\Model\ModelManagerInterface');
41
42
        // php 5.3 BC
43
        $modelManager = $this->modelManager;
44
45
        $this->choiceList->expects($this->any())
46
            ->method('getModelManager')
47
            ->will($this->returnCallback(function () use ($modelManager) {
48
                return $modelManager;
49
            }));
50
    }
51
52
    /**
53
     * @dataProvider getTransformTests
54
     */
55
    public function testTransform($expected, $collection, $identifiers)
56
    {
57
        $transformer = new LegacyModelsToArrayTransformer($this->choiceList);
58
59
        $this->choiceList->expects($this->any())
60
            ->method('getIdentifierValues')
61
            ->will($this->returnCallback(function ($entity) use ($identifiers) {
62
                if ($entity instanceof FooEntity) {
63
                    return $identifiers;
64
                }
65
66
                return [];
67
            }));
68
69
        $this->choiceList->expects($this->any())
70
            ->method('getIdentifier')
71
            ->will($this->returnCallback(function () use ($identifiers) {
72
                return $identifiers;
73
            }));
74
75
        $this->choiceList->expects($this->any())
76
            ->method('getEntities')
77
            ->will($this->returnCallback(function () {
78
                return ['bcd' => new FooEntity(['bcd']), 'efg' => new FooEntity(['efg']), 'abc' => new FooEntity(['abc'])];
79
            }));
80
81
        $this->assertSame($expected, $transformer->transform($collection));
82
    }
83
84
    public function getTransformTests()
85
    {
86
        return [
87
            [[], null, []],
88
            [[], [], []],
89
            [['id'], [new FooEntity()], ['id']],
90
            [['id', 'id'], [new FooEntity(), new FooEntity()], ['id']],
91
            [['abc', 'bcd', 'efg'], [new FooEntity(['abc']), new FooEntity(['bcd']), new FooEntity(['efg'])], ['id1', 'id2']],
92
        ];
93
    }
94
95
    public function testReverseTransformWithException1()
96
    {
97
        $this->expectException('Symfony\Component\Form\Exception\UnexpectedTypeException', 'Expected argument of type "\ArrayAccess", "NULL" given');
98
99
        $transformer = new LegacyModelsToArrayTransformer($this->choiceList);
100
101
        $this->modelManager->expects($this->any())
102
            ->method('getModelCollectionInstance')
103
            ->will($this->returnValue(null));
104
105
        $transformer->reverseTransform([]);
106
    }
107
108
    public function testReverseTransformWithException2()
109
    {
110
        $this->expectException('Symfony\Component\Form\Exception\UnexpectedTypeException', 'Expected argument of type "array", "integer" given');
111
112
        $transformer = new LegacyModelsToArrayTransformer($this->choiceList);
113
114
        $this->modelManager->expects($this->any())
115
            ->method('getModelCollectionInstance')
116
            ->will($this->returnValue(new ArrayCollection()));
117
118
        $transformer->reverseTransform(123);
119
    }
120
121
    /**
122
     * @dataProvider getReverseTransformEmptyTests
123
     */
124
    public function testReverseTransformEmpty($keys)
125
    {
126
        $transformer = new LegacyModelsToArrayTransformer($this->choiceList);
127
128
        $this->modelManager->expects($this->any())
129
            ->method('getModelCollectionInstance')
130
            ->will($this->returnValue(new ArrayCollection()));
131
132
        $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $transformer->reverseTransform($keys));
133
    }
134
135
    public function getReverseTransformEmptyTests()
136
    {
137
        return [
138
            [null],
139
            [''],
140
        ];
141
    }
142
143
    public function testReverseTransform()
144
    {
145
        $transformer = new LegacyModelsToArrayTransformer($this->choiceList);
146
147
        $this->modelManager->expects($this->any())
148
            ->method('getModelCollectionInstance')
149
            ->will($this->returnValue(new ArrayCollection()));
150
151
        $entity1 = new FooEntity(['foo']);
152
        $entity2 = new FooEntity(['bar']);
153
        $entity3 = new FooEntity(['baz']);
154
155
        $this->choiceList->expects($this->any())
156
            ->method('getEntity')
157
            ->will($this->returnCallback(function ($key) use ($entity1, $entity2, $entity3) {
158
                switch ($key) {
159
                    case 'foo':
160
                        return $entity1;
161
162
                    case 'bar':
163
                        return $entity2;
164
165
                    case 'baz':
166
                        return $entity3;
167
                }
168
169
                return;
170
            }));
171
172
        $collection = $transformer->reverseTransform(['foo', 'bar']);
173
        $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $collection);
174
        $this->assertSame([$entity1, $entity2], $collection->getValues());
175
        $this->assertCount(2, $collection);
176
    }
177
178
    public function testReverseTransformWithNonexistentEntityKey()
179
    {
180
        $this->expectException('Symfony\Component\Form\Exception\TransformationFailedException', 'The entities with keys "nonexistent" could not be found');
181
182
        $transformer = new LegacyModelsToArrayTransformer($this->choiceList);
183
184
        $this->modelManager->expects($this->any())
185
            ->method('getModelCollectionInstance')
186
            ->will($this->returnValue(new ArrayCollection()));
187
188
        $this->choiceList->expects($this->any())
189
            ->method('getEntity')
190
            ->will($this->returnValue(false));
191
192
        $transformer->reverseTransform(['nonexistent']);
193
    }
194
}
195