Completed
Pull Request — master (#812)
by
unknown
05:20 queued 01:45
created

DatagridBuilderTest::testFixFieldDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
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\AbstractAdmin;
20
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
21
use Sonata\AdminBundle\Datagrid\Datagrid;
22
use Sonata\AdminBundle\Datagrid\DatagridInterface;
23
use Sonata\AdminBundle\Datagrid\Pager;
24
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
25
use Sonata\AdminBundle\Datagrid\SimplePager;
26
use Sonata\AdminBundle\Filter\FilterFactoryInterface;
27
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
28
use Sonata\AdminBundle\Translator\FormLabelTranslatorStrategy;
29
use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription;
30
use Sonata\DoctrineORMAdminBundle\Builder\DatagridBuilder;
31
use Sonata\DoctrineORMAdminBundle\Filter\ModelAutocompleteFilter;
32
use Sonata\DoctrineORMAdminBundle\Model\ModelManager;
33
use Symfony\Component\Form\FormBuilderInterface;
34
use Symfony\Component\Form\FormFactoryInterface;
35
use Symfony\Component\Form\Guess\TypeGuess;
36
37
/**
38
 * @author Sullivan Senechal <[email protected]>
39
 * @author Marko Kunic <[email protected]>
40
 */
41
final class DatagridBuilderTest extends TestCase
42
{
43
    /**
44
     * @var DatagridBuilder
45
     */
46
    private $datagridBuilder;
47
    private $typeGuesser;
48
    private $formFactory;
49
    private $filterFactory;
50
    private $admin;
51
    private $modelManager;
52
53
    protected function setUp(): void
54
    {
55
        $this->formFactory = $this->prophesize(FormFactoryInterface::class);
56
        $this->filterFactory = $this->prophesize(FilterFactoryInterface::class);
57
        $this->typeGuesser = $this->prophesize(TypeGuesserInterface::class);
58
59
        $this->datagridBuilder = new DatagridBuilder(
60
            $this->formFactory->reveal(),
61
            $this->filterFactory->reveal(),
62
            $this->typeGuesser->reveal()
63
        );
64
65
        $this->admin = $this->prophesize(AbstractAdmin::class);
66
        $this->modelManager = $this->prophesize(ModelManager::class);
67
68
        $this->admin->getClass()->willReturn('FakeClass');
69
        $this->admin->getModelManager()->willReturn($this->modelManager->reveal());
70
        $this->admin->attachAdminClass(Argument::cetera())->willReturn();
71
        $this->admin->addFilterFieldDescription(Argument::cetera())->willReturn();
72
    }
73
74
    /**
75
     * @dataProvider getBaseDatagridData
76
     */
77
    public function testGetBaseDatagrid($pagerType, $pager): void
78
    {
79
        $proxyQuery = $this->prophesize(ProxyQueryInterface::class);
80
        $fieldDescription = $this->prophesize(FieldDescriptionCollection::class);
81
        $formBuilder = $this->prophesize(FormBuilderInterface::class);
82
83
        $this->admin->getPagerType()->willReturn($pagerType);
84
        $this->admin->createQuery()->willReturn($proxyQuery->reveal());
85
        $this->admin->getList()->willReturn($fieldDescription->reveal());
86
87
        $this->modelManager->getIdentifierFieldNames(Argument::any())->willReturn(['id']);
88
89
        $this->formFactory->createNamedBuilder(Argument::cetera())->willReturn($formBuilder->reveal());
90
91
        $this->assertInstanceOf(
92
            Datagrid::class,
93
            $datagrid = $this->datagridBuilder->getBaseDatagrid($this->admin->reveal())
94
        );
95
        $this->assertInstanceOf($pager, $datagrid->getPager());
96
    }
97
98
    public function getBaseDatagridData(): array
99
    {
100
        return [
101
            'simple' => [
102
                Pager::TYPE_SIMPLE,
103
                SimplePager::class,
104
            ],
105
            'default' => [
106
                Pager::TYPE_DEFAULT,
107
                Pager::class,
108
            ],
109
        ];
110
    }
111
112
    public function testGetBaseDatagridBadPagerType(): void
113
    {
114
        $this->admin->getPagerType()->willReturn('fake');
115
116
        $this->expectException(\RuntimeException::class);
117
118
        $this->datagridBuilder->getBaseDatagrid($this->admin->reveal());
119
    }
120
121
    public function testFixFieldDescription(): void
122
    {
123
        $classMetadata = $this->prophesize(ClassMetadata::class);
124
125
        $fieldDescription = new FieldDescription();
126
        $fieldDescription->setName('test');
127
        $fieldDescription->setMappingType(ClassMetadata::ONE_TO_MANY);
128
129
        $this->modelManager->hasMetadata(Argument::any())->willReturn(true);
130
131
        $this->modelManager->getParentMetadataForProperty(Argument::cetera())
132
            ->willReturn([$classMetadata, 2, $parentAssociationMapping = []])
133
            ->shouldBeCalledTimes(1);
134
135
        $classMetadata->fieldMappings = [2 => [1 => 'test', 'type' => 'string']];
136
        $classMetadata->associationMappings = [2 => ['fieldName' => 'fakeField']];
137
138
        $this->datagridBuilder->fixFieldDescription($this->admin->reveal(), $fieldDescription);
139
    }
140
141
    public function testAddFilterNoType(): void
142
    {
143
        $datagrid = $this->prophesize(DatagridInterface::class);
144
        $guessType = $this->prophesize(TypeGuess::class);
145
146
        $fieldDescription = new FieldDescription();
147
        $fieldDescription->setName('test');
148
149
        $this->typeGuesser->guessType(Argument::cetera())->willReturn($guessType->reveal());
150
        $guessType->getOptions()->willReturn(['name' => 'value']);
151
152
        $guessType->getType()->willReturn($typeGuessReturn = ModelAutocompleteFilter::class);
153
154
        $this->modelManager->hasMetadata(Argument::cetera())->willReturn(false)->shouldBeCalledTimes(1);
155
156
        $this->admin->getCode()->willReturn('someFakeCode');
157
158
        $this->filterFactory->create(Argument::cetera())->willReturn(new ModelAutocompleteFilter());
159
160
        $this->admin->getLabelTranslatorStrategy()->willReturn(new FormLabelTranslatorStrategy());
161
162
        $datagrid->addFilter(Argument::type(ModelAutocompleteFilter::class));
163
164
        $this->datagridBuilder->addFilter(
165
            $datagrid->reveal(),
166
            null,
167
            $fieldDescription,
168
            $this->admin->reveal()
169
        );
170
    }
171
}
172