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\AdminInterface; |
19
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionCollection; |
20
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
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\Model\ModelManagerInterface; |
29
|
|
|
use Sonata\AdminBundle\Translator\FormLabelTranslatorStrategy; |
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
|
|
|
protected $datagridBuilder; |
47
|
|
|
private $typeGuesser; |
48
|
|
|
private $formFactory; |
49
|
|
|
private $filterFactory; |
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
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @dataProvider getBaseDatagridData |
66
|
|
|
*/ |
67
|
|
|
public function testGetBaseDatagrid($pagerType, $pager) |
68
|
|
|
{ |
69
|
|
|
$admin = $this->prophesize(AbstractAdmin::class); |
70
|
|
|
$proxyQuery = $this->prophesize(ProxyQueryInterface::class); |
71
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionCollection::class); |
72
|
|
|
$modelManager = $this->prophesize(ModelManagerInterface::class); |
73
|
|
|
$formBuilder = $this->prophesize(FormBuilderInterface::class); |
74
|
|
|
|
75
|
|
|
$admin->getPagerType()->willReturn($pagerType); |
76
|
|
|
$admin->getClass()->willReturn($class = 'Foo\Bar'); |
77
|
|
|
$admin->createQuery()->willReturn($proxyQuery->reveal()); |
78
|
|
|
$admin->getList()->willReturn($fieldDescription->reveal()); |
79
|
|
|
|
80
|
|
|
$modelManager->getIdentifierFieldNames($class)->willReturn(['id']); |
81
|
|
|
$admin->getModelManager()->willReturn($modelManager->reveal()); |
82
|
|
|
|
83
|
|
|
$this->formFactory->createNamedBuilder(Argument::cetera())->willReturn($formBuilder->reveal()); |
84
|
|
|
|
85
|
|
|
$this->assertInstanceOf(Datagrid::class, $datagrid = $this->datagridBuilder->getBaseDatagrid($admin->reveal())); |
86
|
|
|
$this->assertInstanceOf($pager, $datagrid->getPager()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getBaseDatagridData() |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
|
|
[ |
93
|
|
|
Pager::TYPE_SIMPLE, |
94
|
|
|
SimplePager::class, |
95
|
|
|
], |
96
|
|
|
[ |
97
|
|
|
Pager::TYPE_DEFAULT, |
98
|
|
|
Pager::class, |
99
|
|
|
], |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testGetBaseDatagridBadPagerTye() |
104
|
|
|
{ |
105
|
|
|
$admin = $this->prophesize(AbstractAdmin::class); |
106
|
|
|
$admin->getPagerType()->willReturn('fake'); |
107
|
|
|
|
108
|
|
|
$this->expectException(\RuntimeException::class); |
109
|
|
|
|
110
|
|
|
$this->datagridBuilder->getBaseDatagrid($admin->reveal()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testFixFieldDescription() |
114
|
|
|
{ |
115
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
116
|
|
|
$admin = $this->prophesize(AdminInterface::class); |
117
|
|
|
$modelManager = $this->prophesize(ModelManager::class); |
118
|
|
|
$classMetadata = $this->prophesize(ClassMetadata::class); |
119
|
|
|
|
120
|
|
|
$fieldDescription->setAdmin($admin)->shouldBeCalledTimes(1); |
121
|
|
|
|
122
|
|
|
$admin->getClass()->willReturn($adminClassReturn = 'FakeClass')->shouldBeCalledTimes(2); |
123
|
|
|
$modelManager->hasMetadata($adminClassReturn)->willReturn(true)->shouldBeCalledTimes(1); |
124
|
|
|
$admin->getModelManager()->willReturn($modelManager->reveal())->shouldBeCalledTimes(2); |
125
|
|
|
|
126
|
|
|
$fieldDescription->getName()->willReturn($fieldDescriptionNameReturn = 'FakeName') |
127
|
|
|
->shouldBeCalledTimes(3); |
128
|
|
|
$modelManager->getParentMetadataForProperty($adminClassReturn, $fieldDescriptionNameReturn) |
129
|
|
|
->willReturn([$classMetadata, 2, $parentAssociationMapping = []]) |
130
|
|
|
->shouldBeCalledTimes(1); |
131
|
|
|
|
132
|
|
|
$classMetadata->fieldMappings = [2 => [1 => 'test', 'type' => 'string']]; |
133
|
|
|
|
134
|
|
|
$fieldDescription->setOption('field_mapping', Argument::cetera())->shouldBeCalledTimes(1); |
135
|
|
|
$fieldDescription->getOption('field_mapping', Argument::cetera())->shouldBeCalledTimes(1); |
136
|
|
|
|
137
|
|
|
$fieldDescription->setOption('global_search', true)->shouldBeCalledTimes(1); |
138
|
|
|
$fieldDescription->getOption('global_search', true)->willReturn(true) |
139
|
|
|
->shouldBeCalledTimes(1); |
140
|
|
|
|
141
|
|
|
$classMetadata->associationMappings = [2 => $associationMapping = 'test']; |
142
|
|
|
|
143
|
|
|
$fieldDescription->setOption('association_mapping', $associationMapping)->shouldBeCalledTimes(1); |
144
|
|
|
$fieldDescription->getOption('association_mapping', $associationMapping) |
145
|
|
|
->willReturn($associationMapping) |
146
|
|
|
->shouldBeCalledTimes(1); |
147
|
|
|
|
148
|
|
|
$fieldDescription->setOption('parent_association_mappings', $parentAssociationMapping) |
149
|
|
|
->shouldBeCalledTimes(1); |
150
|
|
|
$fieldDescription->getOption('parent_association_mappings', $parentAssociationMapping) |
151
|
|
|
->willReturn($parentAssociationMapping) |
152
|
|
|
->shouldBeCalledTimes(1); |
153
|
|
|
|
154
|
|
|
$fieldDescription->setOption('code', $fieldDescriptionNameReturn)->shouldBeCalledTimes(1); |
155
|
|
|
$fieldDescription->setOption('name', $fieldDescriptionNameReturn)->shouldBeCalledTimes(1); |
156
|
|
|
$fieldDescription->getOption('code', $fieldDescriptionNameReturn) |
157
|
|
|
->willReturn($fieldDescriptionNameReturn)->shouldBeCalledTimes(1); |
158
|
|
|
$fieldDescription->getOption('name', $fieldDescriptionNameReturn) |
159
|
|
|
->willReturn($fieldDescriptionNameReturn)->shouldBeCalledTimes(1); |
160
|
|
|
|
161
|
|
|
$fieldDescription->getMappingType()->willReturn(ClassMetadata::ONE_TO_MANY)->shouldBeCalledTimes(1); |
162
|
|
|
|
163
|
|
|
$admin->attachAdminClass($fieldDescription)->shouldBeCalledTimes(1); |
164
|
|
|
|
165
|
|
|
$this->datagridBuilder->fixFieldDescription($admin->reveal(), $fieldDescription->reveal()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testAddFilterNoType() |
169
|
|
|
{ |
170
|
|
|
$datagrid = $this->prophesize(DatagridInterface::class); |
171
|
|
|
$fieldDescription = $this->prophesize(FieldDescriptionInterface::class); |
172
|
|
|
$admin = $this->prophesize(AdminInterface::class); |
173
|
|
|
$modelManager = $this->prophesize(ModelManager::class); |
174
|
|
|
$guessType = $this->prophesize(TypeGuess::class); |
175
|
|
|
|
176
|
|
|
$admin->getClass()->shouldBeCalledTimes(2); |
177
|
|
|
$admin->getModelManager()->willReturn($modelManager->reveal())->shouldBeCalledTimes(3); |
178
|
|
|
|
179
|
|
|
$this->typeGuesser->guessType(Argument::cetera())->willReturn($guessType->reveal()); |
180
|
|
|
$guessType->getOptions()->willReturn(['name' => 'value']); |
181
|
|
|
|
182
|
|
|
$fieldDescription->setOption('name', 'value')->shouldBeCalledTimes(1); |
183
|
|
|
$fieldDescription->getOption('name', 'value')->willReturn('value')->shouldBeCalledTimes(1); |
184
|
|
|
$guessType->getType()->willReturn($typeGuessReturn = ModelAutocompleteFilter::class) |
185
|
|
|
->shouldBeCalledTimes(1); |
186
|
|
|
|
187
|
|
|
$fieldDescription->setType($typeGuessReturn)->shouldBeCalledTimes(1); |
188
|
|
|
|
189
|
|
|
$fieldDescription->getName()->willReturn($name = 'FakeName'); |
190
|
|
|
|
191
|
|
|
$fieldDescription->setAdmin($admin)->shouldBeCalledTimes(1); |
192
|
|
|
|
193
|
|
|
$modelManager->hasMetadata(Argument::cetera())->willReturn(false)->shouldBeCalledTimes(1); |
194
|
|
|
$admin->getModelManager()->willReturn($modelManager->reveal())->shouldBeCalledTimes(3); |
195
|
|
|
|
196
|
|
|
$fieldDescription->setOption('code', $name)->shouldBeCalledTimes(1); |
197
|
|
|
$fieldDescription->setOption('name', $name)->shouldBeCalledTimes(1); |
198
|
|
|
$fieldDescription->getOption('code', $name) |
199
|
|
|
->willReturn($name)->shouldBeCalledTimes(1); |
200
|
|
|
$fieldDescription->getOption('name', $name) |
201
|
|
|
->willReturn($name)->shouldBeCalledTimes(1); |
202
|
|
|
|
203
|
|
|
$fieldDescription->getMappingType()->willReturn('fake')->shouldBeCalledTimes(1); |
204
|
|
|
|
205
|
|
|
$admin->addFilterFieldDescription($name, $fieldDescription)->shouldBeCalledTimes(1); |
206
|
|
|
|
207
|
|
|
$fieldDescription->mergeOption(Argument::cetera())->shouldBeCalledTimes(2); |
208
|
|
|
|
209
|
|
|
$fieldDescription->getTargetEntity()->shouldBeCalledTimes(1); |
210
|
|
|
$fieldDescription->getAdmin()->willReturn($admin->reveal())->shouldBeCalledTimes(1); |
211
|
|
|
$admin->getCode()->willReturn('someFakeCode'); |
212
|
|
|
|
213
|
|
|
$fieldDescription->getOptions()->willReturn([])->shouldBeCalledTimes(1); |
214
|
|
|
$this->filterFactory->create(Argument::cetera())->willReturn(new ModelAutocompleteFilter()); |
215
|
|
|
|
216
|
|
|
$admin->getLabelTranslatorStrategy()->willReturn(new FormLabelTranslatorStrategy()); |
217
|
|
|
|
218
|
|
|
$datagrid->addFilter(Argument::type(ModelAutocompleteFilter::class)); |
219
|
|
|
|
220
|
|
|
$this->datagridBuilder->addFilter( |
221
|
|
|
$datagrid->reveal(), |
222
|
|
|
null, |
223
|
|
|
$fieldDescription->reveal(), |
224
|
|
|
$admin->reveal() |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|