Completed
Pull Request — master (#812)
by
unknown
02:47
created

ShowBuilderTest::testGetBaseList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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