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\MockObject\Stub; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
21
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionCollection; |
22
|
|
|
use Sonata\AdminBundle\Guesser\TypeGuesserInterface; |
23
|
|
|
use Sonata\AdminBundle\Templating\TemplateRegistry; |
24
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription; |
25
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Builder\ShowBuilder; |
26
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager; |
27
|
|
|
use Symfony\Component\Form\Guess\TypeGuess; |
28
|
|
|
|
29
|
|
|
final class ShowBuilderTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var Stub&TypeGuesserInterface |
33
|
|
|
*/ |
34
|
|
|
private $guesser; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ShowBuilder |
38
|
|
|
*/ |
39
|
|
|
private $showBuilder; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var MockObject&AdminInterface |
43
|
|
|
*/ |
44
|
|
|
private $admin; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Stub&ModelManager |
48
|
|
|
*/ |
49
|
|
|
private $modelManager; |
50
|
|
|
|
51
|
|
|
protected function setUp(): void |
52
|
|
|
{ |
53
|
|
|
$this->guesser = $this->createStub(TypeGuesserInterface::class); |
54
|
|
|
|
55
|
|
|
$this->showBuilder = new ShowBuilder( |
56
|
|
|
$this->guesser, |
57
|
|
|
[ |
58
|
|
|
'fakeTemplate' => 'fake', |
59
|
|
|
TemplateRegistry::TYPE_MANY_TO_ONE => '@SonataAdmin/CRUD/Association/show_many_to_one.html.twig', |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$this->admin = $this->createMock(AdminInterface::class); |
64
|
|
|
$this->modelManager = $this->createStub(ModelManager::class); |
65
|
|
|
|
66
|
|
|
$this->admin->method('getClass')->willReturn('FakeClass'); |
67
|
|
|
$this->admin->method('getModelManager')->willReturn($this->modelManager); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testGetBaseList(): void |
71
|
|
|
{ |
72
|
|
|
$this->assertInstanceOf(FieldDescriptionCollection::class, $this->showBuilder->getBaseList()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testAddFieldNoType(): void |
76
|
|
|
{ |
77
|
|
|
$typeGuess = $this->createStub(TypeGuess::class); |
78
|
|
|
|
79
|
|
|
$fieldDescription = new FieldDescription(); |
80
|
|
|
$fieldDescription->setName('FakeName'); |
81
|
|
|
$fieldDescription->setMappingType(ClassMetadata::ONE); |
82
|
|
|
|
83
|
|
|
$this->admin->expects($this->once())->method('attachAdminClass'); |
84
|
|
|
$this->admin->expects($this->once())->method('addShowFieldDescription'); |
85
|
|
|
|
86
|
|
|
$typeGuess->method('getType')->willReturn('fakeType'); |
87
|
|
|
|
88
|
|
|
$this->guesser->method('guessType')->willReturn($typeGuess); |
89
|
|
|
|
90
|
|
|
$this->modelManager->method('hasMetadata')->willReturn(false); |
91
|
|
|
|
92
|
|
|
$this->showBuilder->addField( |
93
|
|
|
new FieldDescriptionCollection(), |
94
|
|
|
null, |
95
|
|
|
$fieldDescription, |
96
|
|
|
$this->admin |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$this->assertSame('fakeType', $fieldDescription->getType()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testAddFieldWithType(): void |
103
|
|
|
{ |
104
|
|
|
$fieldDescription = new FieldDescription(); |
105
|
|
|
$fieldDescription->setName('FakeName'); |
106
|
|
|
|
107
|
|
|
$this->admin->expects($this->once())->method('addShowFieldDescription'); |
108
|
|
|
|
109
|
|
|
$this->modelManager->method('hasMetadata')->willReturn(false); |
110
|
|
|
|
111
|
|
|
$this->showBuilder->addField( |
112
|
|
|
new FieldDescriptionCollection(), |
113
|
|
|
'someType', |
114
|
|
|
$fieldDescription, |
115
|
|
|
$this->admin |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$this->assertSame('someType', $fieldDescription->getType()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @dataProvider fixFieldDescriptionData |
123
|
|
|
*/ |
124
|
|
|
public function testFixFieldDescription(string $type, string $mappingType, string $template): void |
125
|
|
|
{ |
126
|
|
|
$classMetadata = $this->createStub(ClassMetadata::class); |
127
|
|
|
|
128
|
|
|
$fieldDescription = new FieldDescription(); |
129
|
|
|
$fieldDescription->setName('FakeName'); |
130
|
|
|
$fieldDescription->setType($type); |
131
|
|
|
$fieldDescription->setMappingType($mappingType); |
132
|
|
|
|
133
|
|
|
$this->admin->expects($this->once())->method('attachAdminClass'); |
134
|
|
|
|
135
|
|
|
$this->modelManager->method('hasMetadata')->willReturn(true); |
136
|
|
|
|
137
|
|
|
$this->modelManager->method('getParentMetadataForProperty') |
138
|
|
|
->willReturn([$classMetadata, 'fakeName', $parentAssociationMapping = []]); |
139
|
|
|
|
140
|
|
|
$classMetadata->fieldMappings = ['fakeName' => []]; |
141
|
|
|
|
142
|
|
|
$classMetadata->associationMappings = ['fakeName' => ['fieldName' => 'fakeField', 'type' => $type]]; |
143
|
|
|
|
144
|
|
|
$this->showBuilder->fixFieldDescription($this->admin, $fieldDescription); |
145
|
|
|
|
146
|
|
|
$this->assertSame($template, $fieldDescription->getTemplate()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function fixFieldDescriptionData(): iterable |
150
|
|
|
{ |
151
|
|
|
return [ |
152
|
|
|
'one' => [ |
153
|
|
|
TemplateRegistry::TYPE_MANY_TO_ONE, |
154
|
|
|
ClassMetadata::ONE, |
155
|
|
|
'@SonataAdmin/CRUD/Association/show_many_to_one.html.twig', |
156
|
|
|
], |
157
|
|
|
'many' => [ |
158
|
|
|
TemplateRegistry::TYPE_MANY_TO_MANY, |
159
|
|
|
ClassMetadata::MANY, |
160
|
|
|
'@SonataAdmin/CRUD/Association/show_many_to_many.html.twig', |
161
|
|
|
], |
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @dataProvider fixFieldDescriptionTypes |
167
|
|
|
*/ |
168
|
|
|
public function testFixFieldDescriptionFixesType(string $expectedType, string $type): void |
169
|
|
|
{ |
170
|
|
|
$fieldDescription = new FieldDescription(); |
171
|
|
|
$fieldDescription->setName('FakeName'); |
172
|
|
|
$fieldDescription->setType($type); |
173
|
|
|
|
174
|
|
|
$this->showBuilder->fixFieldDescription($this->admin, $fieldDescription); |
175
|
|
|
|
176
|
|
|
$this->assertSame($expectedType, $fieldDescription->getType()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function fixFieldDescriptionTypes(): iterable |
180
|
|
|
{ |
181
|
|
|
return [ |
182
|
|
|
['string', 'id'], |
183
|
|
|
['integer', 'int'], |
184
|
|
|
]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testFixFieldDescriptionException(): void |
188
|
|
|
{ |
189
|
|
|
$this->expectException(\RuntimeException::class); |
190
|
|
|
$this->showBuilder->fixFieldDescription($this->admin, new FieldDescription()); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|