|
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 Sonata\AdminBundle\Admin\AdminInterface; |
|
19
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
|
20
|
|
|
use Sonata\AdminBundle\Form\Type\AdminType; |
|
21
|
|
|
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType; |
|
22
|
|
|
use Sonata\AdminBundle\Form\Type\ModelHiddenType; |
|
23
|
|
|
use Sonata\AdminBundle\Form\Type\ModelListType; |
|
24
|
|
|
use Sonata\AdminBundle\Form\Type\ModelType; |
|
25
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
|
26
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription; |
|
27
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Builder\FormContractor; |
|
28
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager; |
|
29
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Tests\Fixtures\Document\SimpleAnnotationDocument; |
|
30
|
|
|
use Sonata\Form\Type\CollectionType; |
|
31
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
32
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
33
|
|
|
|
|
34
|
|
|
class FormContractorTest extends TestCase |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var FormFactoryInterface|\PHPUnit_Framework_MockObject_MockObject |
|
38
|
|
|
*/ |
|
39
|
|
|
private $formFactory; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var FormContractor |
|
43
|
|
|
*/ |
|
44
|
|
|
private $formContractor; |
|
45
|
|
|
|
|
46
|
|
|
protected function setUp(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->formFactory = $this->createMock(FormFactoryInterface::class); |
|
49
|
|
|
|
|
50
|
|
|
$this->formContractor = new FormContractor($this->formFactory); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testGetFormBuilder(): void |
|
54
|
|
|
{ |
|
55
|
|
|
$this->formFactory->expects($this->once())->method('createNamedBuilder') |
|
|
|
|
|
|
56
|
|
|
->willReturn($this->createMock(FormBuilderInterface::class)); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertInstanceOf( |
|
59
|
|
|
FormBuilderInterface::class, |
|
60
|
|
|
$this->formContractor->getFormBuilder('test', ['foo' => 'bar']) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testDefaultOptionsForSonataFormTypes(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$admin = $this->createMock(AdminInterface::class); |
|
67
|
|
|
$modelManager = $this->createMock(ModelManagerInterface::class); |
|
68
|
|
|
$modelClass = 'FooEntity'; |
|
69
|
|
|
|
|
70
|
|
|
$admin->method('getModelManager')->willReturn($modelManager); |
|
71
|
|
|
$admin->method('getClass')->willReturn($modelClass); |
|
72
|
|
|
|
|
73
|
|
|
// NEXT_MAJOR: Mock `FieldDescriptionInterface` instead and replace `getTargetEntity()` with `getTargetModel(). |
|
74
|
|
|
$fieldDescription = $this->createMock(FieldDescription::class); |
|
75
|
|
|
$fieldDescription->method('getAdmin')->willReturn($admin); |
|
76
|
|
|
$fieldDescription->method('getTargetModel')->willReturn($modelClass); |
|
77
|
|
|
$fieldDescription->method('getAssociationAdmin')->willReturn($admin); |
|
78
|
|
|
|
|
79
|
|
|
$modelTypes = [ |
|
80
|
|
|
ModelType::class, |
|
81
|
|
|
ModelListType::class, |
|
82
|
|
|
ModelHiddenType::class, |
|
83
|
|
|
ModelAutocompleteType::class, |
|
84
|
|
|
]; |
|
85
|
|
|
$adminTypes = [ |
|
86
|
|
|
AdminType::class, |
|
87
|
|
|
]; |
|
88
|
|
|
$collectionTypes = [ |
|
89
|
|
|
CollectionType::class, |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
|
|
// model types |
|
93
|
|
|
foreach ($modelTypes as $formType) { |
|
94
|
|
|
$options = $this->formContractor->getDefaultOptions($formType, $fieldDescription); |
|
95
|
|
|
$this->assertSame($fieldDescription, $options['sonata_field_description']); |
|
96
|
|
|
$this->assertSame($modelClass, $options['class']); |
|
97
|
|
|
$this->assertSame($modelManager, $options['model_manager']); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// admin type |
|
101
|
|
|
$fieldDescription->method('getMappingType')->willReturn(ClassMetadata::ONE); |
|
102
|
|
|
foreach ($adminTypes as $formType) { |
|
103
|
|
|
$options = $this->formContractor->getDefaultOptions($formType, $fieldDescription); |
|
104
|
|
|
$this->assertSame($fieldDescription, $options['sonata_field_description']); |
|
105
|
|
|
$this->assertSame($modelClass, $options['data_class']); |
|
106
|
|
|
$this->assertFalse($options['btn_add']); |
|
107
|
|
|
$this->assertFalse($options['delete']); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// collection type |
|
111
|
|
|
$fieldDescription->method('getMappingType')->willReturn(ClassMetadata::MANY); |
|
112
|
|
|
foreach ($collectionTypes as $index => $formType) { |
|
113
|
|
|
$options = $this->formContractor->getDefaultOptions($formType, $fieldDescription); |
|
114
|
|
|
$this->assertSame($fieldDescription, $options['sonata_field_description']); |
|
115
|
|
|
$this->assertSame(AdminType::class, $options['type']); |
|
116
|
|
|
$this->assertTrue($options['modifiable']); |
|
117
|
|
|
$this->assertSame($fieldDescription, $options['type_options']['sonata_field_description']); |
|
118
|
|
|
$this->assertSame($modelClass, $options['type_options']['data_class']); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testAdminClassAttachForNotMappedField(): void |
|
123
|
|
|
{ |
|
124
|
|
|
// Given |
|
125
|
|
|
$modelManager = $this->createMock(ModelManager::class); |
|
126
|
|
|
$modelManager->method('hasMetadata')->willReturn(false); |
|
127
|
|
|
|
|
128
|
|
|
$admin = $this->createMock(AdminInterface::class); |
|
129
|
|
|
$admin->method('getModelManager')->willReturn($modelManager); |
|
130
|
|
|
|
|
131
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
|
132
|
|
|
$fieldDescription->method('getMappingType')->willReturn(ClassMetadata::ONE); |
|
133
|
|
|
$fieldDescription->method('getType')->willReturn(ModelListType::class); |
|
134
|
|
|
$fieldDescription->method('getOption')->with($this->logicalOr( |
|
135
|
|
|
$this->equalTo('edit'), |
|
136
|
|
|
$this->equalTo('admin_code') |
|
137
|
|
|
))->willReturn('sonata.admin.code'); |
|
138
|
|
|
|
|
139
|
|
|
// Then |
|
140
|
|
|
$admin |
|
141
|
|
|
->expects($this->once()) |
|
142
|
|
|
->method('attachAdminClass') |
|
143
|
|
|
->with($fieldDescription) |
|
144
|
|
|
; |
|
145
|
|
|
|
|
146
|
|
|
// When |
|
147
|
|
|
$this->formContractor->fixFieldDescription($admin, $fieldDescription); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testFixFieldDescriptionForFieldMapping(): void |
|
151
|
|
|
{ |
|
152
|
|
|
$classMetadata = new ClassMetadata(SimpleAnnotationDocument::class); |
|
153
|
|
|
$classMetadata->mapField([ |
|
154
|
|
|
'fieldName' => 'name', |
|
155
|
|
|
'type' => 'string', |
|
156
|
|
|
]); |
|
157
|
|
|
|
|
158
|
|
|
$modelManager = $this->createMock(ModelManager::class); |
|
159
|
|
|
$modelManager->method('hasMetadata')->willReturn(true); |
|
160
|
|
|
$modelManager->method('getMetadata')->willReturn($classMetadata); |
|
161
|
|
|
|
|
162
|
|
|
$admin = $this->createMock(AdminInterface::class); |
|
163
|
|
|
$admin->method('getModelManager')->willReturn($modelManager); |
|
164
|
|
|
|
|
165
|
|
|
$fieldDescription = new FieldDescription(); |
|
166
|
|
|
$fieldDescription->setMappingType(ClassMetadata::ONE); |
|
167
|
|
|
$fieldDescription->setName('name'); |
|
168
|
|
|
|
|
169
|
|
|
$this->formContractor->fixFieldDescription($admin, $fieldDescription); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertSame($classMetadata->fieldMappings['name'], $fieldDescription->getFieldMapping()); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function testFixFieldDescriptionForAssociationMapping(): void |
|
175
|
|
|
{ |
|
176
|
|
|
$classMetadata = new ClassMetadata(SimpleAnnotationDocument::class); |
|
177
|
|
|
$classMetadata->mapOneEmbedded([ |
|
178
|
|
|
'fieldName' => 'name', |
|
179
|
|
|
'type' => 'string', |
|
180
|
|
|
]); |
|
181
|
|
|
|
|
182
|
|
|
$modelManager = $this->createMock(ModelManager::class); |
|
183
|
|
|
$modelManager->method('hasMetadata')->willReturn(true); |
|
184
|
|
|
$modelManager->method('getMetadata')->willReturn($classMetadata); |
|
185
|
|
|
|
|
186
|
|
|
$admin = $this->createMock(AdminInterface::class); |
|
187
|
|
|
$admin->method('getModelManager')->willReturn($modelManager); |
|
188
|
|
|
|
|
189
|
|
|
$fieldDescription = new FieldDescription(); |
|
190
|
|
|
$fieldDescription->setMappingType(ClassMetadata::ONE); |
|
191
|
|
|
$fieldDescription->setName('name'); |
|
192
|
|
|
|
|
193
|
|
|
$this->formContractor->fixFieldDescription($admin, $fieldDescription); |
|
194
|
|
|
|
|
195
|
|
|
$this->assertSame($classMetadata->associationMappings['name'], $fieldDescription->getAssociationMapping()); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
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.