|
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\TestCase; |
|
18
|
|
|
use Prophecy\Argument; |
|
19
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
|
20
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
|
21
|
|
|
use Sonata\AdminBundle\Guesser\TypeGuesserInterface; |
|
22
|
|
|
use Sonata\AdminBundle\Templating\TemplateRegistry; |
|
23
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription; |
|
24
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Builder\ListBuilder; |
|
25
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager; |
|
26
|
|
|
use Symfony\Component\Form\Guess\Guess; |
|
27
|
|
|
use Symfony\Component\Form\Guess\TypeGuess; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @author Andrew Mor-Yaroslavtsev <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class ListBuilderTest extends TestCase |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var TypeGuesserInterface|\Prophecy\Prophecy\ObjectProphecy |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $typeGuesser; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ListBuilder |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $listBuilder; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var AdminInterface|\Prophecy\Prophecy\ObjectProphecy |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $admin; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var ModelManager|\Prophecy\Prophecy\ObjectProphecy |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $modelManager; |
|
53
|
|
|
|
|
54
|
|
|
protected function setUp(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->typeGuesser = $this->prophesize(TypeGuesserInterface::class); |
|
57
|
|
|
|
|
58
|
|
|
$this->modelManager = $this->prophesize(ModelManager::class); |
|
59
|
|
|
$this->modelManager->hasMetadata(Argument::any())->willReturn(false); |
|
60
|
|
|
|
|
61
|
|
|
$this->admin = $this->prophesize(AbstractAdmin::class); |
|
62
|
|
|
$this->admin->getClass()->willReturn('Foo'); |
|
63
|
|
|
$this->admin->getModelManager()->willReturn($this->modelManager); |
|
64
|
|
|
$this->admin->addListFieldDescription(Argument::any(), Argument::any()) |
|
65
|
|
|
->willReturn(); |
|
66
|
|
|
|
|
67
|
|
|
$this->listBuilder = new ListBuilder($this->typeGuesser->reveal(), [ |
|
68
|
|
|
'fakeTemplate' => 'fake', |
|
69
|
|
|
TemplateRegistry::TYPE_STRING => '@SonataAdmin/CRUD/list_string.html.twig', |
|
70
|
|
|
]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testAddListActionField(): void |
|
74
|
|
|
{ |
|
75
|
|
|
$fieldDescription = new FieldDescription(); |
|
76
|
|
|
$fieldDescription->setName('foo'); |
|
77
|
|
|
$list = $this->listBuilder->getBaseList(); |
|
78
|
|
|
$this->listBuilder |
|
79
|
|
|
->addField($list, 'actions', $fieldDescription, $this->admin->reveal()); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$this->assertSame( |
|
82
|
|
|
'@SonataAdmin/CRUD/list__action.html.twig', |
|
83
|
|
|
$list->get('foo')->getTemplate(), |
|
84
|
|
|
'Custom list action field has a default list action template assigned' |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testCorrectFixedActionsFieldType(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$this->typeGuesser->guessType( |
|
91
|
|
|
Argument::any(), |
|
92
|
|
|
Argument::any(), |
|
93
|
|
|
Argument::any() |
|
94
|
|
|
)->willReturn( |
|
95
|
|
|
new TypeGuess('actions', [], Guess::LOW_CONFIDENCE) |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
$fieldDescription = new FieldDescription(); |
|
99
|
|
|
$fieldDescription->setName('_action'); |
|
100
|
|
|
$list = $this->listBuilder->getBaseList(); |
|
101
|
|
|
$this->listBuilder->addField($list, null, $fieldDescription, $this->admin->reveal()); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
$this->assertSame( |
|
104
|
|
|
'actions', |
|
105
|
|
|
$list->get('_action')->getType(), |
|
106
|
|
|
'Standard list _action field has "actions" type' |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testFixFieldDescriptionWithFieldMapping(): void |
|
111
|
|
|
{ |
|
112
|
|
|
$classMetadata = $this->prophesize(ClassMetadata::class); |
|
113
|
|
|
$this->modelManager->hasMetadata(Argument::any())->willReturn(true); |
|
114
|
|
|
$fieldDescription = new FieldDescription(); |
|
115
|
|
|
$fieldDescription->setName('test'); |
|
116
|
|
|
$fieldDescription->setOption('sortable', true); |
|
117
|
|
|
$fieldDescription->setType('string'); |
|
118
|
|
|
|
|
119
|
|
|
$classMetadata->fieldMappings = ['test' => ['type' => 'string']]; |
|
120
|
|
|
$this->modelManager->getParentMetadataForProperty(Argument::cetera()) |
|
|
|
|
|
|
121
|
|
|
->willReturn([$classMetadata, 'test', $parentAssociationMapping = []]); |
|
122
|
|
|
|
|
123
|
|
|
$this->listBuilder->fixFieldDescription($this->admin->reveal(), $fieldDescription); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
$this->assertSame('@SonataAdmin/CRUD/list_string.html.twig', $fieldDescription->getTemplate()); |
|
126
|
|
|
$this->assertSame(['type' => 'string'], $fieldDescription->getFieldMapping()); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @dataProvider fixFieldDescriptionData |
|
131
|
|
|
*/ |
|
132
|
|
|
public function testFixFieldDescriptionWithAssociationMapping(string $type, string $template): void |
|
133
|
|
|
{ |
|
134
|
|
|
$classMetadata = $this->prophesize(ClassMetadata::class); |
|
135
|
|
|
$this->modelManager->hasMetadata(Argument::any())->willReturn(true); |
|
136
|
|
|
$fieldDescription = new FieldDescription(); |
|
137
|
|
|
$fieldDescription->setName('test'); |
|
138
|
|
|
$fieldDescription->setOption('sortable', true); |
|
139
|
|
|
$fieldDescription->setType($type); |
|
140
|
|
|
$fieldDescription->setMappingType($type); |
|
141
|
|
|
|
|
142
|
|
|
$this->admin->attachAdminClass(Argument::any())->shouldBeCalledTimes(1); |
|
143
|
|
|
|
|
144
|
|
|
$associationMapping = [ |
|
145
|
|
|
'fieldName' => 'associatedDocument', |
|
146
|
|
|
'name' => 'associatedDocument', |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
|
|
$classMetadata->associationMappings = [ |
|
150
|
|
|
'test' => $associationMapping, |
|
151
|
|
|
]; |
|
152
|
|
|
|
|
153
|
|
|
$this->modelManager->getParentMetadataForProperty(Argument::cetera()) |
|
|
|
|
|
|
154
|
|
|
->willReturn([$classMetadata, 'test', $parentAssociationMapping = []]); |
|
155
|
|
|
|
|
156
|
|
|
$this->listBuilder->fixFieldDescription($this->admin->reveal(), $fieldDescription); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
$this->assertSame($template, $fieldDescription->getTemplate()); |
|
159
|
|
|
$this->assertSame($associationMapping, $fieldDescription->getAssociationMapping()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function fixFieldDescriptionData(): array |
|
163
|
|
|
{ |
|
164
|
|
|
return [ |
|
165
|
|
|
'one-to-one' => [ |
|
166
|
|
|
ClassMetadata::ONE, |
|
167
|
|
|
'@SonataAdmin/CRUD/Association/list_many_to_one.html.twig', |
|
168
|
|
|
], |
|
169
|
|
|
'many-to-one' => [ |
|
170
|
|
|
ClassMetadata::MANY, |
|
171
|
|
|
'@SonataAdmin/CRUD/Association/list_many_to_many.html.twig', |
|
172
|
|
|
], |
|
173
|
|
|
]; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @dataProvider fixFieldDescriptionTypes |
|
178
|
|
|
*/ |
|
179
|
|
|
public function testFixFieldDescriptionFixesType(string $expectedType, string $type): void |
|
180
|
|
|
{ |
|
181
|
|
|
$this->modelManager->hasMetadata(Argument::any())->willReturn(false); |
|
182
|
|
|
$fieldDescription = new FieldDescription(); |
|
183
|
|
|
$fieldDescription->setName('test'); |
|
184
|
|
|
$fieldDescription->setType($type); |
|
185
|
|
|
|
|
186
|
|
|
$this->listBuilder->fixFieldDescription($this->admin->reveal(), $fieldDescription); |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
$this->assertSame($expectedType, $fieldDescription->getType()); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function fixFieldDescriptionTypes(): array |
|
192
|
|
|
{ |
|
193
|
|
|
return [ |
|
194
|
|
|
['string', 'id'], |
|
195
|
|
|
['integer', 'int'], |
|
196
|
|
|
]; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function testFixFieldDescriptionException(): void |
|
200
|
|
|
{ |
|
201
|
|
|
$this->expectException(\RuntimeException::class); |
|
202
|
|
|
$this->listBuilder->fixFieldDescription($this->admin->reveal(), new FieldDescription()); |
|
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.