Completed
Push — master ( 6dd6fb...aed73a )
by
unknown
07:17
created

tests/Builder/FormContractorTest.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 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\CoreBundle\Form\Type\CollectionType as DeprecatedCollectionType;
27
use Sonata\DoctrineORMAdminBundle\Builder\FormContractor;
28
use Sonata\DoctrineORMAdminBundle\Model\ModelManager;
29
use Sonata\Form\Type\CollectionType;
30
use Symfony\Component\Form\FormBuilderInterface;
31
use Symfony\Component\Form\FormFactoryInterface;
32
33
/**
34
 * @author Sullivan Senechal <[email protected]>
35
 */
36
final class FormContractorTest extends TestCase
37
{
38
    /**
39
     * @var FormFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
40
     */
41
    private $formFactory;
42
43
    /**
44
     * @var FormContractor
45
     */
46
    private $formContractor;
47
48
    protected function setUp(): void
49
    {
50
        $this->formFactory = $this->createMock(FormFactoryInterface::class);
51
52
        $this->formContractor = new FormContractor($this->formFactory);
53
    }
54
55
    public function testGetFormBuilder(): void
56
    {
57
        $this->formFactory->expects($this->once())->method('createNamedBuilder')
0 ignored issues
show
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...
58
            ->willReturn($this->createMock(FormBuilderInterface::class));
59
60
        $this->assertInstanceOf(
61
            FormBuilderInterface::class,
62
            $this->formContractor->getFormBuilder('test', ['foo' => 'bar'])
63
        );
64
    }
65
66
    public function testDefaultOptionsForSonataFormTypes(): void
67
    {
68
        $admin = $this->createMock(AdminInterface::class);
69
        $modelManager = $this->createMock(ModelManagerInterface::class);
70
        $model = $this->createMock(\stdClass::class);
71
        $modelClass = 'FooEntity';
72
73
        $admin->method('getModelManager')->willReturn($modelManager);
74
        $admin->method('getClass')->willReturn($modelClass);
75
76
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
77
        $fieldDescription->method('getAdmin')->willReturn($admin);
78
        $fieldDescription->method('getTargetEntity')->willReturn($modelClass);
79
        $fieldDescription->method('getAssociationAdmin')->willReturn($admin);
80
        $admin->method('getNewInstance')->willReturn($model);
81
82
        $modelTypes = [];
83
        $classTypes = [
84
            'Sonata\AdminBundle\Form\Type\ModelType',
85
            'Sonata\AdminBundle\Form\Type\ModelListType',
86
            'Sonata\AdminBundle\Form\Type\ModelHiddenType',
87
            'Sonata\AdminBundle\Form\Type\ModelAutocompleteType',
88
        ];
89
90
        foreach ($classTypes as $classType) {
91
            array_push(
92
                $modelTypes,
93
                // add class type.
94
                $classType,
95
                // add instance of class type.
96
                \get_class(
97
                    $this->getMockBuilder($classType)
98
                        ->disableOriginalConstructor()
99
                        ->getMock()
100
                )
101
            );
102
        }
103
104
        // NEXT_MAJOR: Use only FQCNs when dropping support for form mapping
105
        $modelTypes = [
106
            'sonata_type_model',
107
            'sonata_type_model_list',
108
            'sonata_type_model_hidden',
109
            'sonata_type_model_autocomplete',
110
            ModelType::class,
111
            ModelListType::class,
112
            ModelHiddenType::class,
113
            ModelAutocompleteType::class,
114
        ];
115
        $adminTypes = [
116
            'sonata_type_admin',
117
            AdminType::class,
118
        ];
119
        $collectionTypes = [
120
            'sonata_type_collection',
121
            DeprecatedCollectionType::class,
122
        ];
123
124
        if (class_exists(CollectionType::class)) {
125
            $collectionTypes[] = CollectionType::class;
126
        }
127
128
        // model types
129
        foreach ($modelTypes as $formType) {
130
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
131
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
132
            $this->assertSame($modelClass, $options['class']);
133
            $this->assertSame($modelManager, $options['model_manager']);
134
        }
135
136
        // admin type
137
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadata::ONE_TO_ONE);
138
        foreach ($adminTypes as $formType) {
139
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
140
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
141
            $this->assertSame($modelClass, $options['data_class']);
142
            $this->assertFalse($options['btn_add']);
143
            $this->assertFalse($options['delete']);
144
            $this->assertSame($model, $options['empty_data']());
145
        }
146
147
        // collection type
148
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadata::ONE_TO_MANY);
149
        foreach ($collectionTypes as $formType) {
150
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
151
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
152
            $this->assertSame(AdminType::class, $options['type']);
153
            $this->assertTrue($options['modifiable']);
154
            $this->assertSame($fieldDescription, $options['type_options']['sonata_field_description']);
155
            $this->assertSame($modelClass, $options['type_options']['data_class']);
156
            $this->assertSame($model, $options['type_options']['empty_data']());
157
        }
158
    }
159
160
    public function testAdminClassAttachForNotMappedField(): void
161
    {
162
        // Given
163
        $modelManager = $this->createMock(ModelManager::class);
164
        $modelManager->method('hasMetadata')->willReturn(false);
165
166
        $admin = $this->createMock(AdminInterface::class);
167
        $admin->method('getModelManager')->willReturn($modelManager);
168
169
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
170
        $fieldDescription->method('getMappingType')->willReturn('simple');
171
        $fieldDescription->method('getType')->willReturn('sonata_type_model_list');
172
        $fieldDescription->method('getOption')->with($this->logicalOr(
173
            $this->equalTo('edit'),
174
            $this->equalTo('admin_code')
175
        ))->willReturn('sonata.admin.code');
176
177
        // Then
178
        $admin
179
            ->expects($this->once())
180
            ->method('attachAdminClass')
181
            ->with($fieldDescription)
182
        ;
183
184
        // When
185
        $this->formContractor->fixFieldDescription($admin, $fieldDescription);
186
    }
187
}
188