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, 'someField', $parentAssociationMapping = []]) |
133
|
|
|
->shouldBeCalledTimes(1); |
134
|
|
|
|
135
|
|
|
$classMetadata->fieldMappings = [ |
136
|
|
|
'someField' => [ |
137
|
|
|
'type' => 'string', |
138
|
|
|
'declaredField' => 'someFieldDeclared', |
139
|
|
|
'fieldName' => 'fakeField', |
140
|
|
|
], |
141
|
|
|
]; |
142
|
|
|
$classMetadata->associationMappings = ['someField' => ['fieldName' => 'fakeField']]; |
143
|
|
|
$classMetadata->embeddedClasses = ['someFieldDeclared' => ['fieldName' => 'fakeField']]; |
144
|
|
|
|
145
|
|
|
$this->datagridBuilder->fixFieldDescription($this->admin->reveal(), $fieldDescription); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testAddFilterNoType(): void |
149
|
|
|
{ |
150
|
|
|
$datagrid = $this->prophesize(DatagridInterface::class); |
151
|
|
|
$guessType = $this->prophesize(TypeGuess::class); |
152
|
|
|
|
153
|
|
|
$fieldDescription = new FieldDescription(); |
154
|
|
|
$fieldDescription->setName('test'); |
155
|
|
|
|
156
|
|
|
$this->typeGuesser->guessType(Argument::cetera())->willReturn($guessType->reveal()); |
157
|
|
|
$guessType->getOptions()->willReturn(['name' => 'value']); |
158
|
|
|
|
159
|
|
|
$guessType->getType()->willReturn($typeGuessReturn = ModelAutocompleteFilter::class); |
160
|
|
|
|
161
|
|
|
$this->modelManager->hasMetadata(Argument::cetera())->willReturn(false)->shouldBeCalledTimes(1); |
162
|
|
|
|
163
|
|
|
$this->admin->getCode()->willReturn('someFakeCode'); |
164
|
|
|
|
165
|
|
|
$this->filterFactory->create(Argument::cetera())->willReturn(new ModelAutocompleteFilter()); |
166
|
|
|
|
167
|
|
|
$this->admin->getLabelTranslatorStrategy()->willReturn(new FormLabelTranslatorStrategy()); |
168
|
|
|
|
169
|
|
|
$datagrid->addFilter(Argument::type(ModelAutocompleteFilter::class)); |
170
|
|
|
|
171
|
|
|
$this->datagridBuilder->addFilter( |
172
|
|
|
$datagrid->reveal(), |
173
|
|
|
null, |
174
|
|
|
$fieldDescription, |
175
|
|
|
$this->admin->reveal() |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|