Completed
Push — master ( b87479...e865f1 )
by
unknown
8s
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\DoctrineMongoDBAdminBundle\Tests\Builder;
15
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
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;
27
use Sonata\DoctrineMongoDBAdminBundle\Builder\FormContractor;
28
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager;
29
use Symfony\Component\Form\FormBuilderInterface;
30
use Symfony\Component\Form\FormFactoryInterface;
31
32
class FormContractorTest extends TestCase
33
{
34
    /**
35
     * @var FormFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
36
     */
37
    private $formFactory;
38
39
    /**
40
     * @var FormContractor
41
     */
42
    private $formContractor;
43
44
    protected function setUp(): void
45
    {
46
        $this->formFactory = $this->createMock(FormFactoryInterface::class);
47
48
        $this->formContractor = new FormContractor($this->formFactory);
49
    }
50
51
    public function testGetFormBuilder(): void
52
    {
53
        $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...
54
            ->willReturn($this->createMock(FormBuilderInterface::class));
55
56
        $this->assertInstanceOf(
57
            FormBuilderInterface::class,
58
            $this->formContractor->getFormBuilder('test', ['foo' => 'bar'])
59
        );
60
    }
61
62
    public function testDefaultOptionsForSonataFormTypes(): void
63
    {
64
        $admin = $this->createMock(AdminInterface::class);
65
        $modelManager = $this->createMock(ModelManagerInterface::class);
66
        $modelClass = 'FooEntity';
67
68
        $admin->method('getModelManager')->willReturn($modelManager);
69
        $admin->method('getClass')->willReturn($modelClass);
70
71
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
72
        $fieldDescription->method('getAdmin')->willReturn($admin);
73
        $fieldDescription->method('getTargetEntity')->willReturn($modelClass);
74
        $fieldDescription->method('getAssociationAdmin')->willReturn($admin);
75
76
        // NEXT_MAJOR: Use only FQCNs when dropping support for Symfony 2.8
77
        $modelTypes = [
78
            'sonata_type_model',
79
            'sonata_type_model_list',
80
            'sonata_type_model_hidden',
81
            'sonata_type_model_autocomplete',
82
            ModelType::class,
83
            ModelListType::class,
84
            ModelHiddenType::class,
85
            ModelAutocompleteType::class,
86
        ];
87
        $adminTypes = [
88
            'sonata_type_admin',
89
            AdminType::class,
90
        ];
91
        $collectionTypes = [
92
            'sonata_type_collection',
93
            CollectionType::class,
94
        ];
95
96
        // model types
97
        foreach ($modelTypes as $formType) {
98
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
99
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
100
            $this->assertSame($modelClass, $options['class']);
101
            $this->assertSame($modelManager, $options['model_manager']);
102
        }
103
104
        // admin type
105
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadataInfo::ONE);
106
        foreach ($adminTypes as $formType) {
107
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
108
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
109
            $this->assertSame($modelClass, $options['data_class']);
110
            $this->assertFalse($options['btn_add']);
111
            $this->assertFalse($options['delete']);
112
        }
113
114
        // collection type
115
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadataInfo::MANY);
116
        foreach ($collectionTypes as $index => $formType) {
117
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
118
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
119
            $this->assertSame(AdminType::class, $options['type']);
120
            $this->assertTrue($options['modifiable']);
121
            $this->assertSame($fieldDescription, $options['type_options']['sonata_field_description']);
122
            $this->assertSame($modelClass, $options['type_options']['data_class']);
123
        }
124
    }
125
126
    public function testAdminClassAttachForNotMappedField(): void
127
    {
128
        // Given
129
        $modelManager = $this->createMock(ModelManager::class);
130
        $modelManager->method('hasMetadata')->willReturn(false);
131
132
        $admin = $this->createMock(AdminInterface::class);
133
        $admin->method('getModelManager')->willReturn($modelManager);
134
135
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
136
        $fieldDescription->method('getMappingType')->willReturn('one');
137
        $fieldDescription->method('getType')->willReturn('sonata_type_model_list');
138
        $fieldDescription->method('getOption')->with($this->logicalOr(
139
            $this->equalTo('edit'),
140
            $this->equalTo('admin_code')
141
        ))->willReturn('sonata.admin.code');
142
143
        // Then
144
        $admin
145
            ->expects($this->once())
146
            ->method('attachAdminClass')
147
            ->with($fieldDescription)
148
        ;
149
150
        // When
151
        $this->formContractor->fixFieldDescription($admin, $fieldDescription);
152
    }
153
}
154