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
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testGetBaseList(): void |
61
|
|
|
{ |
62
|
|
|
$this->assertInstanceOf(FieldDescriptionCollection::class, $this->showBuilder->getBaseList()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testAddFieldNoType(): void |
66
|
|
|
{ |
67
|
|
|
$typeGuess = $this->prophesize(TypeGuess::class); |
68
|
|
|
|
69
|
|
|
$fieldDescription = new FieldDescription(); |
70
|
|
|
$fieldDescription->setName('FakeName'); |
71
|
|
|
$fieldDescription->setMappingType(ClassMetadata::MANY_TO_ONE); |
72
|
|
|
|
73
|
|
|
$this->admin->attachAdminClass(Argument::cetera())->shouldBeCalled(); |
74
|
|
|
$this->admin->addShowFieldDescription(Argument::cetera())->shouldBeCalled(); |
75
|
|
|
|
76
|
|
|
$typeGuess->getType()->willReturn($typeGuessReturn = 'fakeType'); |
77
|
|
|
|
78
|
|
|
$this->guesser->guessType(Argument::any(), Argument::any(), $this->modelManager)->willReturn($typeGuess); |
79
|
|
|
|
80
|
|
|
$this->modelManager->hasMetadata(Argument::any())->willReturn(false); |
81
|
|
|
|
82
|
|
|
$this->showBuilder->addField( |
83
|
|
|
new FieldDescriptionCollection(), |
84
|
|
|
null, |
85
|
|
|
$fieldDescription, |
86
|
|
|
$this->admin->reveal() |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testAddFieldWithType(): void |
91
|
|
|
{ |
92
|
|
|
$fieldDescription = new FieldDescription(); |
93
|
|
|
$fieldDescription->setName('FakeName'); |
94
|
|
|
|
95
|
|
|
$this->admin->addShowFieldDescription(Argument::cetera())->shouldBeCalled(); |
96
|
|
|
|
97
|
|
|
$this->modelManager->hasMetadata(Argument::any())->willReturn(false); |
98
|
|
|
|
99
|
|
|
$this->showBuilder->addField( |
100
|
|
|
new FieldDescriptionCollection(), |
101
|
|
|
'someType', |
102
|
|
|
$fieldDescription, |
103
|
|
|
$this->admin->reveal() |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @dataProvider fixFieldDescriptionData |
109
|
|
|
* @dataProvider fixFieldDescriptionDeprecatedData |
110
|
|
|
*/ |
111
|
|
|
public function testFixFieldDescription(string $type, int $mappingType, string $template): void |
112
|
|
|
{ |
113
|
|
|
$classMetadata = $this->prophesize(ClassMetadata::class); |
114
|
|
|
|
115
|
|
|
$fieldDescription = new FieldDescription(); |
116
|
|
|
$fieldDescription->setName('FakeName'); |
117
|
|
|
$fieldDescription->setType($type); |
118
|
|
|
$fieldDescription->setMappingType($mappingType); |
119
|
|
|
|
120
|
|
|
$this->admin->attachAdminClass(Argument::cetera())->shouldBeCalled(); |
121
|
|
|
|
122
|
|
|
$this->modelManager->hasMetadata(Argument::any())->willReturn(true); |
123
|
|
|
|
124
|
|
|
$this->modelManager->getParentMetadataForProperty(Argument::cetera()) |
125
|
|
|
->willReturn([$classMetadata, 2, $parentAssociationMapping = []]); |
126
|
|
|
|
127
|
|
|
$classMetadata->fieldMappings = [2 => []]; |
128
|
|
|
|
129
|
|
|
$classMetadata->associationMappings = [2 => ['fieldName' => 'fakeField']]; |
130
|
|
|
|
131
|
|
|
$this->showBuilder->fixFieldDescription($this->admin->reveal(), $fieldDescription); |
132
|
|
|
|
133
|
|
|
$this->assertSame($template, $fieldDescription->getTemplate()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function fixFieldDescriptionData(): iterable |
137
|
|
|
{ |
138
|
|
|
return [ |
139
|
|
|
'one-to-one' => [ |
140
|
|
|
TemplateRegistry::TYPE_ONE_TO_ONE, |
141
|
|
|
ClassMetadata::ONE_TO_ONE, |
142
|
|
|
'@SonataAdmin/CRUD/Association/show_one_to_one.html.twig', |
143
|
|
|
], |
144
|
|
|
'many-to-one' => [ |
145
|
|
|
TemplateRegistry::TYPE_MANY_TO_ONE, |
146
|
|
|
ClassMetadata::MANY_TO_ONE, |
147
|
|
|
'@SonataAdmin/CRUD/Association/show_many_to_one.html.twig', |
148
|
|
|
], |
149
|
|
|
'one-to-many' => [ |
150
|
|
|
TemplateRegistry::TYPE_ONE_TO_MANY, |
151
|
|
|
ClassMetadata::ONE_TO_MANY, |
152
|
|
|
'@SonataAdmin/CRUD/Association/show_one_to_many.html.twig', |
153
|
|
|
], |
154
|
|
|
'many-to-many' => [ |
155
|
|
|
TemplateRegistry::TYPE_MANY_TO_MANY, |
156
|
|
|
ClassMetadata::MANY_TO_MANY, |
157
|
|
|
'@SonataAdmin/CRUD/Association/show_many_to_many.html.twig', |
158
|
|
|
], |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* NEXT_MAJOR: Remove this dataprovider. |
164
|
|
|
*/ |
165
|
|
|
public function fixFieldDescriptionDeprecatedData(): iterable |
166
|
|
|
{ |
167
|
|
|
return [ |
168
|
|
|
'deprecated-one-to-one' => [ |
169
|
|
|
'orm_one_to_one', |
170
|
|
|
ClassMetadata::ONE_TO_ONE, |
171
|
|
|
'@SonataAdmin/CRUD/Association/show_one_to_one.html.twig', |
172
|
|
|
], |
173
|
|
|
'deprecated-many-to-one' => [ |
174
|
|
|
'orm_many_to_one', |
175
|
|
|
ClassMetadata::MANY_TO_ONE, |
176
|
|
|
'@SonataAdmin/CRUD/Association/show_many_to_one.html.twig', |
177
|
|
|
], |
178
|
|
|
'deprecated-one-to-many' => [ |
179
|
|
|
'orm_one_to_many', |
180
|
|
|
ClassMetadata::ONE_TO_MANY, |
181
|
|
|
'@SonataAdmin/CRUD/Association/show_one_to_many.html.twig', |
182
|
|
|
], |
183
|
|
|
'deprecated-many-to-many' => [ |
184
|
|
|
'orm_many_to_many', |
185
|
|
|
ClassMetadata::MANY_TO_MANY, |
186
|
|
|
'@SonataAdmin/CRUD/Association/show_many_to_many.html.twig', |
187
|
|
|
], |
188
|
|
|
]; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testFixFieldDescriptionException(): void |
192
|
|
|
{ |
193
|
|
|
$this->expectException(\RuntimeException::class); |
194
|
|
|
$this->showBuilder->fixFieldDescription($this->admin->reveal(), new FieldDescription()); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|