Completed
Push — 3.x ( b582dd...8b3829 )
by Jordi Sala
01:51
created

testFixFieldDescriptionException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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