Completed
Push — 3.x ( b382c8...064670 )
by Jordi Sala
01:30
created

testFixFieldDescriptionException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
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\TestCase;
18
use Prophecy\Argument;
19
use Sonata\AdminBundle\Admin\AbstractAdmin;
20
use Sonata\AdminBundle\Admin\AdminInterface;
21
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
22
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription;
23
use Sonata\DoctrineMongoDBAdminBundle\Builder\ListBuilder;
24
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager;
25
use Symfony\Component\Form\Guess\Guess;
26
use Symfony\Component\Form\Guess\TypeGuess;
27
28
/**
29
 * @author Andrew Mor-Yaroslavtsev <[email protected]>
30
 */
31
class ListBuilderTest extends TestCase
32
{
33
    /**
34
     * @var TypeGuesserInterface|\Prophecy\Prophecy\ObjectProphecy
35
     */
36
    protected $typeGuesser;
37
38
    /**
39
     * @var ListBuilder
40
     */
41
    protected $listBuilder;
42
43
    /**
44
     * @var AdminInterface|\Prophecy\Prophecy\ObjectProphecy
45
     */
46
    protected $admin;
47
48
    /**
49
     * @var ModelManager|\Prophecy\Prophecy\ObjectProphecy
50
     */
51
    protected $modelManager;
52
53
    protected function setUp(): void
54
    {
55
        $this->typeGuesser = $this->prophesize(TypeGuesserInterface::class);
56
57
        $this->modelManager = $this->prophesize(ModelManager::class);
58
        $this->modelManager->hasMetadata(Argument::any())->willReturn(false);
59
60
        $this->admin = $this->prophesize(AbstractAdmin::class);
61
        $this->admin->getClass()->willReturn('Foo');
62
        $this->admin->getModelManager()->willReturn($this->modelManager);
63
        $this->admin->addListFieldDescription(Argument::any(), Argument::any())
64
            ->willReturn();
65
66
        $this->listBuilder = new ListBuilder($this->typeGuesser->reveal());
67
    }
68
69
    public function testAddListActionField(): void
70
    {
71
        $fieldDescription = new FieldDescription();
72
        $fieldDescription->setName('foo');
73
        $list = $this->listBuilder->getBaseList();
74
        $this->listBuilder
75
            ->addField($list, 'actions', $fieldDescription, $this->admin->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
77
        $this->assertSame(
78
            '@SonataAdmin/CRUD/list__action.html.twig',
79
            $list->get('foo')->getTemplate(),
80
            'Custom list action field has a default list action template assigned'
81
        );
82
    }
83
84
    public function testCorrectFixedActionsFieldType(): void
85
    {
86
        $this->typeGuesser->guessType(
87
            Argument::any(),
88
            Argument::any(),
89
            Argument::any()
90
        )->willReturn(
91
            new TypeGuess('actions', [], Guess::LOW_CONFIDENCE)
92
        );
93
94
        $fieldDescription = new FieldDescription();
95
        $fieldDescription->setName('_action');
96
        $list = $this->listBuilder->getBaseList();
97
        $this->listBuilder->addField($list, null, $fieldDescription, $this->admin->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
99
        $this->assertSame(
100
            'actions',
101
            $list->get('_action')->getType(),
102
            'Standard list _action field has "actions" type'
103
        );
104
    }
105
106
    /**
107
     * @dataProvider fixFieldDescriptionData
108
     */
109
    public function testFixFieldDescription(string $type, string $template): void
110
    {
111
        $classMetadata = $this->prophesize(ClassMetadata::class);
112
        $this->modelManager->hasMetadata(Argument::any())->willReturn(true);
113
        $fieldDescription = new FieldDescription();
114
        $fieldDescription->setName('test');
115
        $fieldDescription->setOption('sortable', true);
116
        $fieldDescription->setType('fakeType');
117
        $fieldDescription->setMappingType($type);
118
119
        $this->admin->attachAdminClass(Argument::any())->shouldBeCalledTimes(1);
120
121
        $classMetadata->fieldMappings = [2 => [1 => 'test', 'type' => 'string']];
122
        $this->modelManager->getParentMetadataForProperty(Argument::cetera())
0 ignored issues
show
Bug introduced by
The call to getParentMetadataForProperty() misses a required argument $propertyFullName.

This check looks for function calls that miss required arguments.

Loading history...
123
            ->willReturn([$classMetadata, 2, $parentAssociationMapping = []]);
124
125
        $this->listBuilder->fixFieldDescription($this->admin->reveal(), $fieldDescription);
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
127
        $this->assertSame($template, $fieldDescription->getTemplate());
128
    }
129
130
    public function fixFieldDescriptionData(): array
131
    {
132
        return [
133
            'one-to-one' => [
134
                ClassMetadata::ONE,
135
                '@SonataAdmin/CRUD/Association/list_many_to_one.html.twig',
136
            ],
137
            'many-to-one' => [
138
                ClassMetadata::MANY,
139
                '@SonataAdmin/CRUD/Association/list_many_to_many.html.twig',
140
            ],
141
        ];
142
    }
143
144
    public function testFixFieldDescriptionException(): void
145
    {
146
        $this->expectException(\RuntimeException::class);
147
        $this->listBuilder->fixFieldDescription($this->admin->reveal(), new FieldDescription());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
    }
149
}
150