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