Completed
Pull Request — 3.x (#387)
by
unknown
01:23
created

testFixFieldDescriptionForAssociationMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Form\Type\CollectionType;
30
use Symfony\Component\Form\FormBuilderInterface;
31
use Symfony\Component\Form\FormFactoryInterface;
32
33
class FormContractorTest extends TestCase
34
{
35
    /**
36
     * @var FormFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
37
     */
38
    private $formFactory;
39
40
    /**
41
     * @var FormContractor
42
     */
43
    private $formContractor;
44
45
    protected function setUp(): void
46
    {
47
        $this->formFactory = $this->createMock(FormFactoryInterface::class);
48
49
        $this->formContractor = new FormContractor($this->formFactory);
50
    }
51
52
    public function testGetFormBuilder(): void
53
    {
54
        $this->formFactory->expects($this->once())->method('createNamedBuilder')
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...m\FormFactoryInterface>.

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.

Loading history...
55
            ->willReturn($this->createMock(FormBuilderInterface::class));
56
57
        $this->assertInstanceOf(
58
            FormBuilderInterface::class,
59
            $this->formContractor->getFormBuilder('test', ['foo' => 'bar'])
60
        );
61
    }
62
63
    public function testDefaultOptionsForSonataFormTypes(): void
64
    {
65
        $admin = $this->createMock(AdminInterface::class);
66
        $modelManager = $this->createMock(ModelManagerInterface::class);
67
        $modelClass = 'FooEntity';
68
69
        $admin->method('getModelManager')->willReturn($modelManager);
70
        $admin->method('getClass')->willReturn($modelClass);
71
72
        // NEXT_MAJOR: Mock `FieldDescriptionInterface` instead and replace `getTargetEntity()` with `getTargetModel().
73
        $fieldDescription = $this->createMock(FieldDescription::class);
74
        $fieldDescription->method('getAdmin')->willReturn($admin);
75
        $fieldDescription->method('getTargetModel')->willReturn($modelClass);
76
        $fieldDescription->method('getAssociationAdmin')->willReturn($admin);
77
78
        $modelTypes = [
79
            ModelType::class,
80
            ModelListType::class,
81
            ModelHiddenType::class,
82
            ModelAutocompleteType::class,
83
        ];
84
        $adminTypes = [
85
            AdminType::class,
86
        ];
87
        $collectionTypes = [
88
            CollectionType::class,
89
        ];
90
91
        // model types
92
        foreach ($modelTypes as $formType) {
93
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
94
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
95
            $this->assertSame($modelClass, $options['class']);
96
            $this->assertSame($modelManager, $options['model_manager']);
97
        }
98
99
        // admin type
100
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadata::ONE);
101
        foreach ($adminTypes as $formType) {
102
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
103
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
104
            $this->assertSame($modelClass, $options['data_class']);
105
            $this->assertFalse($options['btn_add']);
106
            $this->assertFalse($options['delete']);
107
        }
108
109
        // collection type
110
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadata::MANY);
111
        foreach ($collectionTypes as $index => $formType) {
112
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
113
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
114
            $this->assertSame(AdminType::class, $options['type']);
115
            $this->assertTrue($options['modifiable']);
116
            $this->assertSame($fieldDescription, $options['type_options']['sonata_field_description']);
117
            $this->assertSame($modelClass, $options['type_options']['data_class']);
118
        }
119
    }
120
121
    public function testAdminClassAttachForNotMappedField(): void
122
    {
123
        // Given
124
        $modelManager = $this->createMock(ModelManager::class);
125
        $modelManager->method('hasMetadata')->willReturn(false);
126
127
        $admin = $this->createMock(AdminInterface::class);
128
        $admin->method('getModelManager')->willReturn($modelManager);
129
130
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
131
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadata::ONE);
132
        $fieldDescription->method('getType')->willReturn(ModelListType::class);
133
        $fieldDescription->method('getOption')->with($this->logicalOr(
134
            $this->equalTo('edit'),
135
            $this->equalTo('admin_code')
136
        ))->willReturn('sonata.admin.code');
137
138
        // Then
139
        $admin
140
            ->expects($this->once())
141
            ->method('attachAdminClass')
142
            ->with($fieldDescription)
143
        ;
144
145
        // When
146
        $this->formContractor->fixFieldDescription($admin, $fieldDescription);
147
    }
148
149
    public function testFixFieldDescriptionForFieldMapping(): void
150
    {
151
        $fieldMappings = [
152
            'type' => 'string',
153
        ];
154
        $classMetadata = $this->createMock(ClassMetadata::class);
155
        $classMetadata->fieldMappings = ['test' => $fieldMappings];
156
157
        $modelManager = $this->createMock(ModelManager::class);
158
        $modelManager->method('hasMetadata')->willReturn(true);
159
        $modelManager->method('getMetadata')->willReturn($classMetadata);
160
161
        $admin = $this->createMock(AdminInterface::class);
162
        $admin->method('getModelManager')->willReturn($modelManager);
163
164
        $fieldDescription = new FieldDescription();
165
        $fieldDescription->setMappingType(ClassMetadata::ONE);
166
        $fieldDescription->setName('test');
167
168
        $this->formContractor->fixFieldDescription($admin, $fieldDescription);
169
170
        $this->assertSame($fieldMappings, $fieldDescription->getFieldMapping());
171
    }
172
173
    public function testFixFieldDescriptionForAssociationMapping(): void
174
    {
175
        $associationMappings = [
176
            'fieldName' => 'test',
177
            'type' => 'string',
178
        ];
179
        $classMetadata = $this->createMock(ClassMetadata::class);
180
        $classMetadata->associationMappings = ['test' => $associationMappings];
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('test');
192
193
        $this->formContractor->fixFieldDescription($admin, $fieldDescription);
194
195
        $this->assertSame($associationMappings, $fieldDescription->getAssociationMapping());
196
    }
197
}
198