Completed
Pull Request — 3.x (#807)
by
unknown
01:48
created

ShowBuilderTest::testFixFieldDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 49
rs 9.2258
cc 1
eloc 33
nc 1
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\DoctrineORMAdminBundle\Tests\Builder;
13
14
use Doctrine\ORM\Mapping\ClassMetadata;
15
use PHPUnit\Framework\TestCase;
16
use Sonata\AdminBundle\Admin\AdminInterface;
17
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
18
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
19
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
20
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
21
use Sonata\DoctrineORMAdminBundle\Builder\ShowBuilder;
22
use Sonata\DoctrineORMAdminBundle\Model\ModelManager;
23
use Symfony\Component\Form\Guess\TypeGuess;
24
25
/**
26
 * @author Marko Kunic <[email protected]>
27
 */
28
class ShowBuilderTest extends TestCase
29
{
30
    /**
31
     * @var TypeGuesserInterface
32
     */
33
    private $guesser;
34
35
    /**
36
     * @var ShowBuilderInterface
37
     */
38
    private $showBuilder;
39
40
    public function setUp()
41
    {
42
        $this->guesser = $this->prophesize(TypeGuesserInterface::class);
43
44
        $this->showBuilder = new ShowBuilder(
45
            $this->guesser->reveal(),
46
            ['fakeTemplate' => 'fake']
47
        );
48
    }
49
50
    public function testGetBaseList()
51
    {
52
        $this->assertInstanceOf(FieldDescriptionCollection::class, $this->showBuilder->getBaseList());
53
    }
54
55
    public function testAddFieldNoType()
56
    {
57
        $fieldDescriptionCollection = $this->prophesize(FieldDescriptionCollection::class);
58
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
59
        $admin = $this->prophesize(AdminInterface::class);
60
        $typeGuess = $this->prophesize(TypeGuess::class);
0 ignored issues
show
Unused Code introduced by
$typeGuess is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
61
        $modelManager = $this->prophesize(ModelManager::class);
62
        $typeGuess = $this->prophesize(TypeGuess::class);
63
64
        $admin->getClass()->willReturn($adminClassReturn = 'FakeClass')->shouldBeCalledTimes(2);
65
66
        $fieldDescription->getName()->willReturn($fieldDescriptionNameReturn = 'FakeName')
67
            ->shouldBeCalledTimes(4);
68
69
        $admin->getModelManager()->willReturn($modelManager->reveal())->shouldBeCalledTimes(2);
70
71
        $typeGuess->getType()->willReturn($typeGuessReturn = 'fakeType')->shouldBeCalledTimes(1);
72
73
        $this->guesser->guessType($adminClassReturn, $fieldDescriptionNameReturn, $modelManager)
74
            ->willReturn($typeGuess)
75
            ->shouldBeCalledTimes(1);
76
77
        $fieldDescription->setType($typeGuessReturn)->shouldBeCalledTimes(1);
78
79
        $fieldDescription->setAdmin($admin)->shouldBeCalledTimes(1);
80
81
        $modelManager->hasMetadata($adminClassReturn)->willReturn(false)->shouldBeCalledTimes(1);
82
83
        $fieldDescription->getType()->willReturn(true)->shouldBeCalledTimes(1);
84
85
        $fieldDescription->setOption('code', $fieldDescriptionNameReturn)->shouldBeCalledTimes(1);
86
        $fieldDescription->setOption('label', $fieldDescriptionNameReturn)->shouldBeCalledTimes(1);
87
88
        $fieldDescription->getOption('code', $fieldDescriptionNameReturn)
89
            ->willReturn($fieldDescriptionNameReturn)
90
            ->shouldBeCalledTimes(1);
91
92
        $fieldDescription->getOption('label', $fieldDescriptionNameReturn)
93
            ->willReturn($fieldDescriptionNameReturn)
94
            ->shouldBeCalledTimes(1);
95
96
        $fieldDescription->getTemplate()->willReturn(true)->shouldBeCalledTimes(1);
97
98
        $fieldDescription->getMappingType()->willReturn(ClassMetadata::MANY_TO_ONE)->shouldBeCalledTimes(1);
99
100
        $admin->attachAdminClass($fieldDescription)->shouldBeCalledTimes(1);
101
102
        $admin->addShowFieldDescription($fieldDescriptionNameReturn, $fieldDescription)
103
            ->shouldBeCalledTimes(1);
104
105
        $fieldDescriptionCollection->add($fieldDescription)->shouldBeCalledTimes(1);
106
107
        $this->showBuilder->addField(
108
            $fieldDescriptionCollection->reveal(),
109
            null,
110
            $fieldDescription->reveal(),
111
            $admin->reveal()
112
        );
113
    }
114
115
    public function testAddFieldWithType()
116
    {
117
        $fieldDescriptionCollection = $this->prophesize(FieldDescriptionCollection::class);
118
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
119
        $admin = $this->prophesize(AdminInterface::class);
120
        $modelManager = $this->prophesize(ModelManager::class);
121
122
        $admin->getClass()->willReturn($adminClassReturn = 'FakeClass')->shouldBeCalledTimes(1);
123
124
        $fieldDescription->getName()->willReturn($fieldDescriptionNameReturn = 'FakeName')
125
            ->shouldBeCalledTimes(1);
126
127
        $admin->getModelManager()->willReturn($modelManager->reveal())->shouldBeCalledTimes(1);
128
129
        $this->guesser->guessType()->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to guessType() misses some required arguments starting with $class.
Loading history...
130
131
        $fieldDescription->setType($type = 'someType')->shouldBeCalledTimes(1);
132
        $fieldDescription->setAdmin($admin)->shouldBeCalledTimes(1);
133
134
        $fieldDescription->setAdmin($admin)->shouldBeCalledTimes(1);
135
136
        $modelManager->hasMetadata($adminClassReturn)->willReturn(false)->shouldBeCalledTimes(1);
137
138
        $fieldDescription->getType()->willReturn(false)->shouldBeCalledTimes(1);
139
140
        $this->expectException(\RuntimeException::class);
141
142
        $this->showBuilder->addField(
143
            $fieldDescriptionCollection->reveal(),
144
            $type,
145
            $fieldDescription->reveal(),
146
            $admin->reveal()
147
        );
148
    }
149
150
    public function testFixFieldDescription()
151
    {
152
        $fieldDescription = $this->prophesize(FieldDescriptionInterface::class);
153
        $admin = $this->prophesize(AdminInterface::class);
154
        $modelManager = $this->prophesize(ModelManager::class);
155
        $classMetadata = $this->prophesize(ClassMetadata::class);
156
157
        $fieldDescription->setAdmin($admin)->shouldBeCalledTimes(1);
158
159
        $admin->getClass()->willReturn($adminClassReturn = 'FakeClass')->shouldBeCalledTimes(2);
160
        $modelManager->hasMetadata($adminClassReturn)->willReturn(true)->shouldBeCalledTimes(1);
161
        $admin->getModelManager()->willReturn($modelManager->reveal())->shouldBeCalledTimes(2);
162
163
164
        $fieldDescription->getName()->willReturn($fieldDescriptionNameReturn = 'FakeName')
165
            ->shouldBeCalledTimes(3);
166
        $modelManager->getParentMetadataForProperty($adminClassReturn, $fieldDescriptionNameReturn)
167
            ->willReturn([$classMetadata, 2, $parentAssociationMapping = []])
168
            ->shouldBeCalledTimes(1);
169
170
        $fieldDescription->setParentAssociationMappings($parentAssociationMapping)->shouldBeCalledTimes(1);
171
172
        $classMetadata->fieldMappings = [2 => $fieldMapping = 'test'];
173
        $fieldDescription->setFieldMapping($fieldMapping)->shouldBeCalledTimes(1);
174
175
        $classMetadata->associationMappings = [2 => $associationMapping = 'test'];
176
        $fieldDescription->setAssociationMapping($associationMapping)->shouldBeCalledTimes(1);
177
178
        $fieldDescription->getType()->willReturn('test')->shouldBeCalledTimes(2);
179
180
        $fieldDescription->setOption('code', $fieldDescriptionNameReturn)->shouldBeCalledTimes(1);
181
        $fieldDescription->setOption('label', $fieldDescriptionNameReturn)->shouldBeCalledTimes(1);
182
        $fieldDescription->getOption('code', $fieldDescriptionNameReturn)
183
            ->willReturn($fieldDescriptionNameReturn)->shouldBeCalledTimes(1);
184
        $fieldDescription->getOption('label', $fieldDescriptionNameReturn)
185
            ->willReturn($fieldDescriptionNameReturn)->shouldBeCalledTimes(1);
186
187
        $fieldDescription->getTemplate()->willReturn(false)->shouldBeCalledTimes(2);
188
        $fieldDescription->setTemplate(null)->shouldBeCalledTimes(1);
189
190
        $fieldDescription->getMappingType()->willReturn(ClassMetadata::ONE_TO_ONE)->shouldBeCalledTimes(2);
191
192
        $fieldDescription->setTemplate('@SonataAdmin/CRUD/Association/show_one_to_one.html.twig')
193
            ->shouldBeCalledTimes(1);
194
195
        $admin->attachAdminClass($fieldDescription)->shouldBeCalledTimes(1);
196
197
        $this->showBuilder->fixFieldDescription($admin->reveal(), $fieldDescription->reveal());
198
    }
199
}
200