Completed
Push — master ( 5b5dc4...6c5dbb )
by
unknown
9s
created

testFixFieldDescriptionException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\Guesser\TypeGuesserInterface;
21
use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription;
22
use Sonata\DoctrineORMAdminBundle\Builder\ListBuilder;
23
use Sonata\DoctrineORMAdminBundle\Model\ModelManager;
24
use Symfony\Component\Form\Guess\Guess;
25
use Symfony\Component\Form\Guess\TypeGuess;
26
27
/**
28
 * @author Andrew Mor-Yaroslavtsev <[email protected]>
29
 * @author Marko Kunic <[email protected]>
30
 */
31
class ListBuilderTest extends TestCase
32
{
33
    private $typeGuesser;
34
    private $listBuilder;
35
    private $admin;
36
    private $modelManager;
37
38
    protected function setUp(): void
39
    {
40
        $this->typeGuesser = $this->prophesize(TypeGuesserInterface::class);
41
42
        $this->modelManager = $this->prophesize(ModelManager::class);
43
44
        $this->admin = $this->prophesize(AdminInterface::class);
45
        $this->admin->getClass()->willReturn('Foo');
46
        $this->admin->getModelManager()->willReturn($this->modelManager);
47
        $this->admin->addListFieldDescription(Argument::cetera())->willReturn();
48
49
        $this->listBuilder = new ListBuilder($this->typeGuesser->reveal());
50
    }
51
52
    public function testAddListActionField(): void
53
    {
54
        $fieldDescription = new FieldDescription();
55
        $fieldDescription->setName('foo');
56
        $list = $this->listBuilder->getBaseList();
57
        $this->listBuilder
58
            ->addField($list, 'actions', $fieldDescription, $this->admin->reveal());
59
60
        $this->assertSame(
61
            '@SonataAdmin/CRUD/list__action.html.twig',
62
            $list->get('foo')->getTemplate(),
63
            'Custom list action field has a default list action template assigned'
64
        );
65
    }
66
67
    public function testCorrectFixedActionsFieldType(): void
68
    {
69
        $this->typeGuesser->guessType(Argument::cetera())
70
            ->willReturn(new TypeGuess('_action', [], Guess::LOW_CONFIDENCE));
71
72
        $fieldDescription = new FieldDescription();
73
        $fieldDescription->setName('_action');
74
        $fieldDescription->setOption('actions', ['test' => []]);
75
        $list = $this->listBuilder->getBaseList();
76
        $this->listBuilder->addField($list, null, $fieldDescription, $this->admin->reveal());
77
78
        $this->assertSame(
79
            'actions',
80
            $list->get('_action')->getType(),
81
            'Standard list _action field has "actions" type'
82
        );
83
84
        $this->assertSame(
85
            '@SonataAdmin/CRUD/list__action_test.html.twig',
86
            $fieldDescription->getOption('actions')['test']['template']
87
        );
88
    }
89
90
    /**
91
     * @dataProvider fixFieldDescriptionData
92
     */
93
    public function testFixFieldDescription($type, $template): void
94
    {
95
        $classMetadata = $this->prophesize(ClassMetadata::class);
96
        $this->modelManager->hasMetadata(Argument::any())->willReturn(true);
97
        $fieldDescription = new FieldDescription();
98
        $fieldDescription->setName('test');
99
        $fieldDescription->setOption('sortable', true);
100
        $fieldDescription->setType('fakeType');
101
        $fieldDescription->setMappingType($type);
102
103
        $this->admin->attachAdminClass(Argument::any())->shouldBeCalledTimes(1);
104
105
        $classMetadata->fieldMappings = [2 => [1 => 'test', 'type' => 'string']];
106
        $this->modelManager->getParentMetadataForProperty(Argument::cetera())
107
            ->willReturn([$classMetadata, 2, $parentAssociationMapping = []]);
108
109
        $classMetadata->associationMappings = [2 => ['fieldName' => 'fake']];
110
111
        $this->listBuilder->fixFieldDescription($this->admin->reveal(), $fieldDescription);
112
113
        $this->assertSame($template, $fieldDescription->getTemplate());
114
    }
115
116
    public function fixFieldDescriptionData(): array
117
    {
118
        return [
119
            'one-to-one' => [
120
                ClassMetadata::ONE_TO_ONE,
121
                '@SonataAdmin/CRUD/Association/list_one_to_one.html.twig',
122
            ],
123
            'many-to-one' => [
124
                ClassMetadata::MANY_TO_ONE,
125
                '@SonataAdmin/CRUD/Association/list_many_to_one.html.twig',
126
            ],
127
            'one-to-many' => [
128
                ClassMetadata::ONE_TO_MANY,
129
                '@SonataAdmin/CRUD/Association/list_one_to_many.html.twig',
130
            ],
131
            'many-to-many' => [
132
                ClassMetadata::MANY_TO_MANY,
133
                '@SonataAdmin/CRUD/Association/list_many_to_many.html.twig',
134
            ],
135
        ];
136
    }
137
138
    public function testFixFieldDescriptionException(): void
139
    {
140
        $this->expectException(\RuntimeException::class);
141
        $this->listBuilder->fixFieldDescription($this->admin->reveal(), new FieldDescription());
142
    }
143
}
144