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
|
|
|
TemplateRegistry::TYPE_MANY_TO_MANY => '@SonataAdmin/CRUD/Association/show_many_to_many.html.twig', |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$this->admin = $this->createMock(AdminInterface::class); |
65
|
|
|
$this->modelManager = $this->createStub(ModelManager::class); |
66
|
|
|
|
67
|
|
|
$this->admin->method('getClass')->willReturn('FakeClass'); |
68
|
|
|
$this->admin->method('getModelManager')->willReturn($this->modelManager); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGetBaseList(): void |
72
|
|
|
{ |
73
|
|
|
$this->assertInstanceOf(FieldDescriptionCollection::class, $this->showBuilder->getBaseList()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testAddFieldNoType(): void |
77
|
|
|
{ |
78
|
|
|
$typeGuess = $this->createStub(TypeGuess::class); |
79
|
|
|
|
80
|
|
|
$fieldDescription = new FieldDescription(); |
81
|
|
|
$fieldDescription->setName('FakeName'); |
82
|
|
|
$fieldDescription->setMappingType(ClassMetadata::ONE); |
83
|
|
|
|
84
|
|
|
$this->admin->expects($this->once())->method('attachAdminClass'); |
85
|
|
|
$this->admin->expects($this->once())->method('addShowFieldDescription'); |
86
|
|
|
|
87
|
|
|
$typeGuess->method('getType')->willReturn('fakeType'); |
88
|
|
|
|
89
|
|
|
$this->guesser->method('guessType')->willReturn($typeGuess); |
90
|
|
|
|
91
|
|
|
$this->modelManager->method('hasMetadata')->willReturn(false); |
92
|
|
|
|
93
|
|
|
$this->showBuilder->addField( |
94
|
|
|
new FieldDescriptionCollection(), |
95
|
|
|
null, |
96
|
|
|
$fieldDescription, |
97
|
|
|
$this->admin |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testAddFieldWithType(): void |
102
|
|
|
{ |
103
|
|
|
$fieldDescription = new FieldDescription(); |
104
|
|
|
$fieldDescription->setName('FakeName'); |
105
|
|
|
|
106
|
|
|
$this->admin->expects($this->once())->method('addShowFieldDescription'); |
107
|
|
|
|
108
|
|
|
$this->modelManager->method('hasMetadata')->willReturn(false); |
109
|
|
|
|
110
|
|
|
$this->showBuilder->addField( |
111
|
|
|
new FieldDescriptionCollection(), |
112
|
|
|
'someType', |
113
|
|
|
$fieldDescription, |
114
|
|
|
$this->admin |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @dataProvider fixFieldDescriptionData |
120
|
|
|
*/ |
121
|
|
|
public function testFixFieldDescription(string $type, string $mappingType, string $template): void |
122
|
|
|
{ |
123
|
|
|
$classMetadata = $this->createStub(ClassMetadata::class); |
124
|
|
|
|
125
|
|
|
$fieldDescription = new FieldDescription(); |
126
|
|
|
$fieldDescription->setName('FakeName'); |
127
|
|
|
$fieldDescription->setType($type); |
128
|
|
|
$fieldDescription->setMappingType($mappingType); |
129
|
|
|
|
130
|
|
|
$this->admin->expects($this->once())->method('attachAdminClass'); |
131
|
|
|
|
132
|
|
|
$this->modelManager->method('hasMetadata')->willReturn(true); |
133
|
|
|
|
134
|
|
|
$this->modelManager->method('getParentMetadataForProperty') |
135
|
|
|
->willReturn([$classMetadata, 2, $parentAssociationMapping = []]); |
136
|
|
|
|
137
|
|
|
$classMetadata->fieldMappings = [2 => []]; |
138
|
|
|
|
139
|
|
|
$classMetadata->associationMappings = [2 => ['fieldName' => 'fakeField']]; |
140
|
|
|
|
141
|
|
|
$this->showBuilder->fixFieldDescription($this->admin, $fieldDescription); |
142
|
|
|
|
143
|
|
|
$this->assertSame($template, $fieldDescription->getTemplate()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function fixFieldDescriptionData(): iterable |
147
|
|
|
{ |
148
|
|
|
return [ |
149
|
|
|
'one' => [ |
150
|
|
|
TemplateRegistry::TYPE_MANY_TO_ONE, |
151
|
|
|
ClassMetadata::ONE, |
152
|
|
|
'@SonataAdmin/CRUD/Association/show_many_to_one.html.twig', |
153
|
|
|
], |
154
|
|
|
'many' => [ |
155
|
|
|
TemplateRegistry::TYPE_MANY_TO_MANY, |
156
|
|
|
ClassMetadata::MANY, |
157
|
|
|
'@SonataAdmin/CRUD/Association/show_many_to_many.html.twig', |
158
|
|
|
], |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testFixFieldDescriptionException(): void |
163
|
|
|
{ |
164
|
|
|
$this->expectException(\RuntimeException::class); |
165
|
|
|
$this->showBuilder->fixFieldDescription($this->admin, new FieldDescription()); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|