FormContractorTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 116
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testGetFormBuilder() 0 10 1
B testDefaultOptionsForSonataFormTypes() 0 57 4
A testAdminClassAttachForNotMappedField() 0 27 1
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\CoreBundle\Form\Type\CollectionType as DeprecatedCollectionType;
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 Pascal Hofmann
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
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
73
        $fieldDescription->method('getAdmin')->willReturn($admin);
74
        $fieldDescription->method('getTargetEntity')->willReturn($modelClass);
75
        $fieldDescription->method('getAssociationAdmin')->willReturn($admin);
76
77
        $modelTypes = [
78
            ModelType::class,
79
            ModelListType::class,
80
            ModelHiddenType::class,
81
            ModelAutocompleteType::class,
82
        ];
83
        $adminTypes = [
84
            AdminType::class,
85
        ];
86
        $collectionTypes = [
87
            DeprecatedCollectionType::class,
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('one');
132
        $fieldDescription->method('getType')->willReturn('sonata_type_model_list');
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