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\MockObject\MockObject; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
20
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
21
|
|
|
use Sonata\AdminBundle\Form\Type\AdminType; |
22
|
|
|
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType; |
23
|
|
|
use Sonata\AdminBundle\Form\Type\ModelHiddenType; |
24
|
|
|
use Sonata\AdminBundle\Form\Type\ModelListType; |
25
|
|
|
use Sonata\AdminBundle\Form\Type\ModelType; |
26
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
27
|
|
|
use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription; |
28
|
|
|
use Sonata\DoctrineORMAdminBundle\Builder\FormContractor; |
29
|
|
|
use Sonata\DoctrineORMAdminBundle\Model\ModelManager; |
30
|
|
|
use Sonata\Form\Type\CollectionType; |
31
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
32
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @author Sullivan Senechal <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
final class FormContractorTest extends TestCase |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var FormFactoryInterface|MockObject |
41
|
|
|
*/ |
42
|
|
|
private $formFactory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var FormContractor |
46
|
|
|
*/ |
47
|
|
|
private $formContractor; |
48
|
|
|
|
49
|
|
|
protected function setUp(): void |
50
|
|
|
{ |
51
|
|
|
$this->formFactory = $this->createMock(FormFactoryInterface::class); |
52
|
|
|
|
53
|
|
|
$this->formContractor = new FormContractor($this->formFactory); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetFormBuilder(): void |
57
|
|
|
{ |
58
|
|
|
$this->formFactory->expects($this->once())->method('createNamedBuilder') |
|
|
|
|
59
|
|
|
->willReturn($this->createMock(FormBuilderInterface::class)); |
60
|
|
|
|
61
|
|
|
$this->assertInstanceOf( |
62
|
|
|
FormBuilderInterface::class, |
63
|
|
|
$this->formContractor->getFormBuilder('test', ['foo' => 'bar']) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testDefaultOptionsForSonataFormTypes(): void |
68
|
|
|
{ |
69
|
|
|
$admin = $this->createMock(AdminInterface::class); |
70
|
|
|
$modelManager = $this->createMock(ModelManagerInterface::class); |
71
|
|
|
$model = $this->createMock(\stdClass::class); |
72
|
|
|
$modelClass = 'FooEntity'; |
73
|
|
|
|
74
|
|
|
$admin->method('getModelManager')->willReturn($modelManager); |
75
|
|
|
$admin->method('getClass')->willReturn($modelClass); |
76
|
|
|
|
77
|
|
|
// NEXT_MAJOR: Mock `FieldDescriptionInterface` instead and replace `getTargetEntity()` with `getTargetModel(). |
78
|
|
|
$fieldDescription = $this->createMock(FieldDescription::class); |
79
|
|
|
$fieldDescription->method('getAdmin')->willReturn($admin); |
80
|
|
|
$fieldDescription->method('getTargetModel')->willReturn($modelClass); |
81
|
|
|
$fieldDescription->method('getAssociationAdmin')->willReturn($admin); |
82
|
|
|
$admin->method('getNewInstance')->willReturn($model); |
83
|
|
|
|
84
|
|
|
$modelTypes = [ |
85
|
|
|
ModelType::class, |
86
|
|
|
ModelListType::class, |
87
|
|
|
ModelHiddenType::class, |
88
|
|
|
ModelAutocompleteType::class, |
89
|
|
|
]; |
90
|
|
|
$adminTypes = [ |
91
|
|
|
AdminType::class, |
92
|
|
|
]; |
93
|
|
|
$collectionTypes = [ |
94
|
|
|
CollectionType::class, |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
// model types |
98
|
|
|
foreach ($modelTypes as $formType) { |
99
|
|
|
$options = $this->formContractor->getDefaultOptions($formType, $fieldDescription); |
100
|
|
|
$this->assertSame($fieldDescription, $options['sonata_field_description']); |
101
|
|
|
$this->assertSame($modelClass, $options['class']); |
102
|
|
|
$this->assertSame($modelManager, $options['model_manager']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// admin type |
106
|
|
|
$fieldDescription->method('getMappingType')->willReturn(ClassMetadata::ONE_TO_ONE); |
107
|
|
|
foreach ($adminTypes as $formType) { |
108
|
|
|
$options = $this->formContractor->getDefaultOptions($formType, $fieldDescription); |
109
|
|
|
$this->assertSame($fieldDescription, $options['sonata_field_description']); |
110
|
|
|
$this->assertSame($modelClass, $options['data_class']); |
111
|
|
|
$this->assertFalse($options['btn_add']); |
112
|
|
|
$this->assertFalse($options['delete']); |
113
|
|
|
$this->assertSame($model, $options['empty_data']()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// collection type |
117
|
|
|
$fieldDescription->method('getMappingType')->willReturn(ClassMetadata::ONE_TO_MANY); |
118
|
|
|
foreach ($collectionTypes as $formType) { |
119
|
|
|
$options = $this->formContractor->getDefaultOptions($formType, $fieldDescription); |
120
|
|
|
$this->assertSame($fieldDescription, $options['sonata_field_description']); |
121
|
|
|
$this->assertSame(AdminType::class, $options['type']); |
122
|
|
|
$this->assertTrue($options['modifiable']); |
123
|
|
|
$this->assertSame($fieldDescription, $options['type_options']['sonata_field_description']); |
124
|
|
|
$this->assertSame($modelClass, $options['type_options']['data_class']); |
125
|
|
|
$this->assertSame($model, $options['type_options']['empty_data']()); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testAdminClassAttachForNotMappedField(): void |
130
|
|
|
{ |
131
|
|
|
// Given |
132
|
|
|
$modelManager = $this->createMock(ModelManager::class); |
133
|
|
|
$modelManager->method('hasMetadata')->willReturn(false); |
134
|
|
|
|
135
|
|
|
$admin = $this->createMock(AdminInterface::class); |
136
|
|
|
$admin->method('getModelManager')->willReturn($modelManager); |
137
|
|
|
|
138
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
139
|
|
|
$fieldDescription->method('getMappingType')->willReturn('simple'); |
140
|
|
|
$fieldDescription->method('getType')->willReturn(ModelListType::class); |
141
|
|
|
$fieldDescription->method('getOption')->with($this->logicalOr( |
142
|
|
|
$this->equalTo('edit'), |
143
|
|
|
$this->equalTo('admin_code') |
144
|
|
|
))->willReturn('sonata.admin.code'); |
145
|
|
|
|
146
|
|
|
// Then |
147
|
|
|
$admin |
148
|
|
|
->expects($this->once()) |
149
|
|
|
->method('attachAdminClass') |
150
|
|
|
->with($fieldDescription) |
151
|
|
|
; |
152
|
|
|
|
153
|
|
|
// When |
154
|
|
|
$this->formContractor->fixFieldDescription($admin, $fieldDescription); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.