Completed
Pull Request — 3.x (#380)
by
unknown
01:21
created

ShowBuilderTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\DoctrineMongoDBAdminBundle\Tests\Builder;
15
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
17
use PHPUnit\Framework\MockObject\MockObject;
18
use PHPUnit\Framework\MockObject\Stub;
19
use PHPUnit\Framework\TestCase;
20
use Sonata\AdminBundle\Admin\AdminInterface;
21
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
22
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
23
use Sonata\AdminBundle\Templating\TemplateRegistry;
24
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription;
25
use Sonata\DoctrineMongoDBAdminBundle\Builder\ShowBuilder;
26
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager;
27
use Symfony\Component\Form\Guess\TypeGuess;
28
29
/**
30
 * @author Marko Kunic <[email protected]>
31
 */
32
final class ShowBuilderTest extends TestCase
33
{
34
    /**
35
     * @var Stub&TypeGuesserInterface
36
     */
37
    private $guesser;
38
39
    /**
40
     * @var ShowBuilder
41
     */
42
    private $showBuilder;
43
44
    /**
45
     * @var MockObject&AdminInterface
46
     */
47
    private $admin;
48
49
    /**
50
     * @var Stub&ModelManager
51
     */
52
    private $modelManager;
53
54
    protected function setUp(): void
55
    {
56
        $this->guesser = $this->createStub(TypeGuesserInterface::class);
57
58
        $this->showBuilder = new ShowBuilder(
59
            $this->guesser,
60
            [
61
                'fakeTemplate' => 'fake',
62
                TemplateRegistry::TYPE_MANY_TO_ONE => '@SonataAdmin/CRUD/Association/show_many_to_one.html.twig',
63
                TemplateRegistry::TYPE_MANY_TO_MANY => '@SonataAdmin/CRUD/Association/show_many_to_many.html.twig',
64
            ]
65
        );
66
67
        $this->admin = $this->createMock(AdminInterface::class);
68
        $this->modelManager = $this->createStub(ModelManager::class);
69
70
        $this->admin->method('getClass')->willReturn('FakeClass');
71
        $this->admin->method('getModelManager')->willReturn($this->modelManager);
72
    }
73
74
    public function testGetBaseList(): void
75
    {
76
        $this->assertInstanceOf(FieldDescriptionCollection::class, $this->showBuilder->getBaseList());
77
    }
78
79
    public function testAddFieldNoType(): void
80
    {
81
        $typeGuess = $this->createStub(TypeGuess::class);
82
83
        $fieldDescription = new FieldDescription();
84
        $fieldDescription->setName('FakeName');
85
        $fieldDescription->setMappingType(ClassMetadata::ONE);
86
87
        $this->admin->expects($this->once())->method('attachAdminClass');
88
        $this->admin->expects($this->once())->method('addShowFieldDescription');
89
90
        $typeGuess->method('getType')->willReturn('fakeType');
91
92
        $this->guesser->method('guessType')->willReturn($typeGuess);
93
94
        $this->modelManager->method('hasMetadata')->willReturn(false);
95
96
        $this->showBuilder->addField(
97
            new FieldDescriptionCollection(),
98
            null,
99
            $fieldDescription,
100
            $this->admin
101
        );
102
    }
103
104
    public function testAddFieldWithType(): void
105
    {
106
        $fieldDescription = new FieldDescription();
107
        $fieldDescription->setName('FakeName');
108
109
        $this->admin->expects($this->once())->method('addShowFieldDescription');
110
111
        $this->modelManager->method('hasMetadata')->willReturn(false);
112
113
        $this->showBuilder->addField(
114
            new FieldDescriptionCollection(),
115
            'someType',
116
            $fieldDescription,
117
            $this->admin
118
        );
119
    }
120
121
    /**
122
     * @dataProvider fixFieldDescriptionData
123
     */
124
    public function testFixFieldDescription(string $type, string $mappingType, string $template): void
125
    {
126
        $classMetadata = $this->createStub(ClassMetadata::class);
127
128
        $fieldDescription = new FieldDescription();
129
        $fieldDescription->setName('FakeName');
130
        $fieldDescription->setType($type);
131
        $fieldDescription->setMappingType($mappingType);
132
133
        $this->admin->expects($this->once())->method('attachAdminClass');
134
135
        $this->modelManager->method('hasMetadata')->willReturn(true);
136
137
        $this->modelManager->method('getParentMetadataForProperty')
138
            ->willReturn([$classMetadata, 2, $parentAssociationMapping = []]);
139
140
        $classMetadata->fieldMappings = [2 => []];
141
142
        $classMetadata->associationMappings = [2 => ['fieldName' => 'fakeField']];
143
144
        $this->showBuilder->fixFieldDescription($this->admin, $fieldDescription);
145
146
        $this->assertSame($template, $fieldDescription->getTemplate());
147
    }
148
149
    public function fixFieldDescriptionData(): iterable
150
    {
151
        return [
152
            'one' => [
153
                TemplateRegistry::TYPE_MANY_TO_ONE,
154
                ClassMetadata::ONE,
155
                '@SonataAdmin/CRUD/Association/show_many_to_one.html.twig',
156
            ],
157
            'many' => [
158
                TemplateRegistry::TYPE_MANY_TO_MANY,
159
                ClassMetadata::MANY,
160
                '@SonataAdmin/CRUD/Association/show_many_to_many.html.twig',
161
            ],
162
        ];
163
    }
164
165
    public function testFixFieldDescriptionException(): void
166
    {
167
        $this->expectException(\RuntimeException::class);
168
        $this->showBuilder->fixFieldDescription($this->admin, new FieldDescription());
169
    }
170
}
171