|
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\MockObject\MockObject; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
|
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\Filter\FilterFactoryInterface; |
|
26
|
|
|
use Sonata\AdminBundle\Guesser\TypeGuesserInterface; |
|
27
|
|
|
use Sonata\AdminBundle\Translator\FormLabelTranslatorStrategy; |
|
28
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription; |
|
29
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Builder\DatagridBuilder; |
|
30
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Filter\ModelFilter; |
|
31
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager; |
|
32
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Tests\Fixtures\Document\SimpleAnnotationDocument; |
|
33
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
34
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
35
|
|
|
use Symfony\Component\Form\Guess\Guess; |
|
36
|
|
|
use Symfony\Component\Form\Guess\TypeGuess; |
|
37
|
|
|
|
|
38
|
|
|
final class DatagridBuilderTest extends TestCase |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var DatagridBuilder |
|
42
|
|
|
*/ |
|
43
|
|
|
private $datagridBuilder; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var MockObject&TypeGuesserInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
private $typeGuesser; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var MockObject&FormFactoryInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
private $formFactory; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var MockObject&FilterFactoryInterface |
|
57
|
|
|
*/ |
|
58
|
|
|
private $filterFactory; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var MockObject&AdminInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
private $admin; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var MockObject&ModelManager |
|
67
|
|
|
*/ |
|
68
|
|
|
private $modelManager; |
|
69
|
|
|
|
|
70
|
|
|
protected function setUp(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->formFactory = $this->createStub(FormFactoryInterface::class); |
|
73
|
|
|
$this->filterFactory = $this->createMock(FilterFactoryInterface::class); |
|
74
|
|
|
$this->typeGuesser = $this->createStub(TypeGuesserInterface::class); |
|
75
|
|
|
|
|
76
|
|
|
$this->datagridBuilder = new DatagridBuilder( |
|
77
|
|
|
$this->formFactory, |
|
78
|
|
|
$this->filterFactory, |
|
79
|
|
|
$this->typeGuesser |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$this->admin = $this->createMock(AdminInterface::class); |
|
83
|
|
|
$this->modelManager = $this->createMock(ModelManager::class); |
|
84
|
|
|
|
|
85
|
|
|
$this->admin |
|
86
|
|
|
->method('getClass') |
|
87
|
|
|
->willReturn('FakeClass'); |
|
88
|
|
|
$this->admin |
|
89
|
|
|
->method('getModelManager') |
|
90
|
|
|
->willReturn($this->modelManager); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testGetBaseDatagrid(): void |
|
94
|
|
|
{ |
|
95
|
|
|
$proxyQuery = $this->createStub(ProxyQueryInterface::class); |
|
96
|
|
|
$fieldDescription = $this->createStub(FieldDescriptionCollection::class); |
|
97
|
|
|
$formBuilder = $this->createStub(FormBuilderInterface::class); |
|
98
|
|
|
|
|
99
|
|
|
$this->admin->method('createQuery')->willReturn($proxyQuery); |
|
100
|
|
|
$this->admin->method('getList')->willReturn($fieldDescription); |
|
101
|
|
|
|
|
102
|
|
|
$this->modelManager->method('getIdentifierFieldNames')->willReturn(['id']); |
|
103
|
|
|
|
|
104
|
|
|
$this->formFactory->method('createNamedBuilder')->willReturn($formBuilder); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertInstanceOf( |
|
107
|
|
|
Datagrid::class, |
|
108
|
|
|
$datagrid = $this->datagridBuilder->getBaseDatagrid($this->admin) |
|
109
|
|
|
); |
|
110
|
|
|
$this->assertInstanceOf(Pager::class, $datagrid->getPager()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testFixFieldDescription(): void |
|
114
|
|
|
{ |
|
115
|
|
|
$classMetadata = new ClassMetadata(SimpleAnnotationDocument::class); |
|
116
|
|
|
$classMetadata->mapField([ |
|
117
|
|
|
'fieldName' => 'name', |
|
118
|
|
|
'type' => 'string', |
|
119
|
|
|
]); |
|
120
|
|
|
|
|
121
|
|
|
$fieldDescription = new FieldDescription(); |
|
122
|
|
|
$fieldDescription->setName('name'); |
|
123
|
|
|
|
|
124
|
|
|
$this->modelManager->method('hasMetadata')->willReturn(true); |
|
125
|
|
|
|
|
126
|
|
|
$this->modelManager |
|
127
|
|
|
->expects($this->once()) |
|
128
|
|
|
->method('getParentMetadataForProperty') |
|
129
|
|
|
->willReturn([$classMetadata, 'name', $parentAssociationMapping = []]); |
|
130
|
|
|
|
|
131
|
|
|
$this->datagridBuilder->fixFieldDescription($this->admin, $fieldDescription); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertSame($classMetadata->fieldMappings['name'], $fieldDescription->getOption('field_mapping')); |
|
134
|
|
|
$this->assertTrue($fieldDescription->getOption('global_search')); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function testFixFieldDescriptionWithAssociationMapping(): void |
|
138
|
|
|
{ |
|
139
|
|
|
$classMetadata = new ClassMetadata(SimpleAnnotationDocument::class); |
|
140
|
|
|
$classMetadata->mapOneEmbedded([ |
|
141
|
|
|
'fieldName' => 'associatedDocument', |
|
142
|
|
|
]); |
|
143
|
|
|
|
|
144
|
|
|
$fieldDescription = new FieldDescription(); |
|
145
|
|
|
$fieldDescription->setName('associatedDocument'); |
|
146
|
|
|
$fieldDescription->setMappingType(ClassMetadata::ONE); |
|
147
|
|
|
|
|
148
|
|
|
$this->admin |
|
149
|
|
|
->expects($this->once()) |
|
150
|
|
|
->method('attachAdminClass'); |
|
151
|
|
|
|
|
152
|
|
|
$this->modelManager->method('hasMetadata')->willReturn(true); |
|
153
|
|
|
|
|
154
|
|
|
$this->modelManager |
|
155
|
|
|
->expects($this->once()) |
|
156
|
|
|
->method('getParentMetadataForProperty') |
|
157
|
|
|
->willReturn([$classMetadata, 'associatedDocument', $parentAssociationMapping = []]); |
|
158
|
|
|
|
|
159
|
|
|
$this->datagridBuilder->fixFieldDescription($this->admin, $fieldDescription); |
|
160
|
|
|
|
|
161
|
|
|
$this->assertSame($classMetadata->associationMappings['associatedDocument'], $fieldDescription->getOption('association_mapping')); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function testAddFilterNoType(): void |
|
165
|
|
|
{ |
|
166
|
|
|
$this->admin |
|
167
|
|
|
->expects($this->once()) |
|
168
|
|
|
->method('addFilterFieldDescription'); |
|
169
|
|
|
|
|
170
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
|
171
|
|
|
$guessType = new TypeGuess(ModelFilter::class, [ |
|
172
|
|
|
'guess_option' => 'guess_value', |
|
173
|
|
|
'guess_array_option' => [ |
|
174
|
|
|
'guess_array_value', |
|
175
|
|
|
], |
|
176
|
|
|
], Guess::VERY_HIGH_CONFIDENCE); |
|
177
|
|
|
|
|
178
|
|
|
$fieldDescription = new FieldDescription(); |
|
179
|
|
|
$fieldDescription->setName('test'); |
|
180
|
|
|
|
|
181
|
|
|
$this->typeGuesser->method('guessType')->willReturn($guessType); |
|
182
|
|
|
|
|
183
|
|
|
$this->modelManager |
|
184
|
|
|
->expects($this->once()) |
|
185
|
|
|
->method('hasMetadata')->willReturn(false); |
|
186
|
|
|
|
|
187
|
|
|
$this->admin->method('getCode')->willReturn('someFakeCode'); |
|
188
|
|
|
|
|
189
|
|
|
$this->filterFactory->method('create')->willReturn(new ModelFilter()); |
|
190
|
|
|
|
|
191
|
|
|
$this->admin->method('getLabelTranslatorStrategy')->willReturn(new FormLabelTranslatorStrategy()); |
|
192
|
|
|
|
|
193
|
|
|
$datagrid |
|
194
|
|
|
->expects($this->once()) |
|
195
|
|
|
->method('addFilter') |
|
196
|
|
|
->with($this->isInstanceOf(ModelFilter::class)); |
|
197
|
|
|
|
|
198
|
|
|
$this->filterFactory |
|
199
|
|
|
->expects($this->once()) |
|
200
|
|
|
->method('create') |
|
201
|
|
|
->with('test', ModelFilter::class); |
|
202
|
|
|
|
|
203
|
|
|
$this->datagridBuilder->addFilter( |
|
204
|
|
|
$datagrid, |
|
205
|
|
|
null, |
|
206
|
|
|
$fieldDescription, |
|
207
|
|
|
$this->admin |
|
208
|
|
|
); |
|
209
|
|
|
|
|
210
|
|
|
$this->assertSame('guess_value', $fieldDescription->getOption('guess_option')); |
|
211
|
|
|
$this->assertSame(['guess_array_value'], $fieldDescription->getOption('guess_array_option')); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function testAddFilterWithType(): void |
|
215
|
|
|
{ |
|
216
|
|
|
$this->admin |
|
217
|
|
|
->expects($this->once()) |
|
218
|
|
|
->method('addFilterFieldDescription'); |
|
219
|
|
|
|
|
220
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
|
221
|
|
|
|
|
222
|
|
|
$fieldDescription = new FieldDescription(); |
|
223
|
|
|
$fieldDescription->setName('test'); |
|
224
|
|
|
|
|
225
|
|
|
$this->filterFactory->method('create')->willReturn(new ModelFilter()); |
|
226
|
|
|
|
|
227
|
|
|
$this->admin->method('getLabelTranslatorStrategy')->willReturn(new FormLabelTranslatorStrategy()); |
|
228
|
|
|
|
|
229
|
|
|
$datagrid |
|
230
|
|
|
->expects($this->once()) |
|
231
|
|
|
->method('addFilter') |
|
232
|
|
|
->with($this->isInstanceOf(ModelFilter::class)); |
|
233
|
|
|
|
|
234
|
|
|
$this->datagridBuilder->addFilter( |
|
235
|
|
|
$datagrid, |
|
236
|
|
|
ModelFilter::class, |
|
237
|
|
|
$fieldDescription, |
|
238
|
|
|
$this->admin |
|
239
|
|
|
); |
|
240
|
|
|
|
|
241
|
|
|
$this->assertSame(ModelFilter::class, $fieldDescription->getType()); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|