Completed
Pull Request — 3.x (#276)
by Jordi Sala
04:12
created

testAdminClassAttachForNotMappedField()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 27
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Builder;
13
14
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
15
use PHPUnit\Framework\TestCase;
16
use Sonata\AdminBundle\Admin\AdminInterface;
17
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
18
use Sonata\AdminBundle\Form\Type\AdminType;
19
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
20
use Sonata\AdminBundle\Form\Type\ModelHiddenType;
21
use Sonata\AdminBundle\Form\Type\ModelListType;
22
use Sonata\AdminBundle\Form\Type\ModelType;
23
use Sonata\AdminBundle\Model\ModelManagerInterface;
24
use Sonata\CoreBundle\Form\Type\CollectionType;
25
use Sonata\DoctrineMongoDBAdminBundle\Builder\FormContractor;
26
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager;
27
use Symfony\Component\Form\FormBuilderInterface;
28
use Symfony\Component\Form\FormFactoryInterface;
29
30
class FormContractorTest extends TestCase
31
{
32
    /**
33
     * @var FormFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
34
     */
35
    private $formFactory;
36
37
    /**
38
     * @var FormContractor
39
     */
40
    private $formContractor;
41
42
    protected function setUp()
43
    {
44
        $this->formFactory = $this->createMock(FormFactoryInterface::class);
45
46
        $this->formContractor = new FormContractor($this->formFactory);
47
    }
48
49
    public function testGetFormBuilder()
50
    {
51
        $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...
52
            ->willReturn($this->createMock(FormBuilderInterface::class));
53
54
        $this->assertInstanceOf(
55
            FormBuilderInterface::class,
56
            $this->formContractor->getFormBuilder('test', ['foo' => 'bar'])
57
        );
58
    }
59
60
    public function testDefaultOptionsForSonataFormTypes()
61
    {
62
        $admin = $this->createMock(AdminInterface::class);
63
        $modelManager = $this->createMock(ModelManagerInterface::class);
64
        $modelClass = 'FooEntity';
65
66
        $admin->method('getModelManager')->willReturn($modelManager);
67
        $admin->method('getClass')->willReturn($modelClass);
68
69
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
70
        $fieldDescription->method('getAdmin')->willReturn($admin);
71
        $fieldDescription->method('getTargetEntity')->willReturn($modelClass);
72
        $fieldDescription->method('getAssociationAdmin')->willReturn($admin);
73
74
        // NEXT_MAJOR: Use only FQCNs when dropping support for Symfony 2.8
75
        $modelTypes = [
76
            'sonata_type_model',
77
            'sonata_type_model_list',
78
            'sonata_type_model_hidden',
79
            'sonata_type_model_autocomplete',
80
            ModelType::class,
81
            ModelListType::class,
82
            ModelHiddenType::class,
83
            ModelAutocompleteType::class,
84
        ];
85
        $adminTypes = [
86
            'sonata_type_admin',
87
            AdminType::class,
88
        ];
89
        $collectionTypes = [
90
            'sonata_type_collection',
91
            CollectionType::class,
92
        ];
93
94
        // model types
95
        foreach ($modelTypes as $formType) {
96
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
97
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
98
            $this->assertSame($modelClass, $options['class']);
99
            $this->assertSame($modelManager, $options['model_manager']);
100
        }
101
102
        // admin type
103
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadataInfo::ONE);
104
        foreach ($adminTypes as $formType) {
105
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
106
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
107
            $this->assertSame($modelClass, $options['data_class']);
108
            $this->assertFalse($options['btn_add']);
109
            $this->assertFalse($options['delete']);
110
        }
111
112
        // collection type
113
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadataInfo::MANY);
114
        foreach ($collectionTypes as $index => $formType) {
115
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
116
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
117
            $this->assertSame(AdminType::class, $options['type']);
118
            $this->assertTrue($options['modifiable']);
119
            $this->assertSame($fieldDescription, $options['type_options']['sonata_field_description']);
120
            $this->assertSame($modelClass, $options['type_options']['data_class']);
121
        }
122
    }
123
124
    public function testAdminClassAttachForNotMappedField()
125
    {
126
        // Given
127
        $modelManager = $this->createMock(ModelManager::class);
128
        $modelManager->method('hasMetadata')->willReturn(false);
129
130
        $admin = $this->createMock(AdminInterface::class);
131
        $admin->method('getModelManager')->willReturn($modelManager);
132
133
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
134
        $fieldDescription->method('getMappingType')->willReturn('one');
135
        $fieldDescription->method('getType')->willReturn('sonata_type_model_list');
136
        $fieldDescription->method('getOption')->with($this->logicalOr(
137
            $this->equalTo('edit'),
138
            $this->equalTo('admin_code')
139
        ))->willReturn('sonata.admin.code');
140
141
        // Then
142
        $admin
143
            ->expects($this->once())
144
            ->method('attachAdminClass')
145
            ->with($fieldDescription)
146
        ;
147
148
        // When
149
        $this->formContractor->fixFieldDescription($admin, $fieldDescription);
150
    }
151
}
152