Completed
Push — 3.x ( 86c9c1...af1625 )
by Grégoire
02:11
created

DatagridBuilderTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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