Completed
Push — 3.x ( a2ba13...55d1f1 )
by Grégoire
17:21
created

FormContractorTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 8
c 3
b 0
f 2
lcom 1
cbo 1
dl 0
loc 125
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testGetFormBuilder() 0 10 1
B testDefaultOptionsForSonataFormTypes() 0 64 5
B testAdminClassAttachForNotMappedField() 0 29 1
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\DoctrineORMAdminBundle\Tests\Builder;
13
14
use Doctrine\ORM\Mapping\ClassMetadata;
15
use Sonata\DoctrineORMAdminBundle\Builder\FormContractor;
16
use Symfony\Component\Form\FormFactoryInterface;
17
18
/**
19
 * @author Sullivan Senechal <[email protected]>
20
 */
21
final class FormContractorTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var FormFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
25
     */
26
    private $formFactory;
27
28
    /**
29
     * @var FormContractor
30
     */
31
    private $formContractor;
32
33
    protected function setUp()
34
    {
35
        $this->formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
36
37
        $this->formContractor = new FormContractor($this->formFactory);
38
    }
39
40
    public function testGetFormBuilder()
41
    {
42
        $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...
43
            ->willReturn($this->getMock('Symfony\Component\Form\FormBuilderInterface'));
44
45
        $this->assertInstanceOf(
46
            'Symfony\Component\Form\FormBuilderInterface',
47
            $this->formContractor->getFormBuilder('test', array('foo' => 'bar'))
48
        );
49
    }
50
51
    public function testDefaultOptionsForSonataFormTypes()
52
    {
53
        $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
54
        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
55
        $modelClass = 'FooEntity';
56
57
        $admin->method('getModelManager')->willReturn($modelManager);
58
        $admin->method('getClass')->willReturn($modelClass);
59
60
        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
61
        $fieldDescription->method('getAdmin')->willReturn($admin);
62
        $fieldDescription->method('getTargetEntity')->willReturn($modelClass);
63
        $fieldDescription->method('getAssociationAdmin')->willReturn($admin);
64
65
        $modelTypes = array(
66
            'sonata_type_model',
67
            'sonata_type_model_list',
68
            'sonata_type_model_hidden',
69
            'sonata_type_model_autocomplete',
70
        );
71
        $adminTypes = array('sonata_type_admin');
72
        $collectionTypes = array('sonata_type_collection');
73
        // NEXT_MAJOR: Use only FQCNs when dropping support for Symfony <2.8
74
        if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
75
            array_push(
76
                $modelTypes,
77
                'Sonata\AdminBundle\Form\Type\ModelType',
78
                'Sonata\AdminBundle\Form\Type\ModelTypeList',
79
                'Sonata\AdminBundle\Form\Type\ModelHiddenType',
80
                'Sonata\AdminBundle\Form\Type\ModelAutocompleteType'
81
            );
82
            $adminTypes[] = 'Sonata\AdminBundle\Form\Type\AdminType';
83
            $collectionTypes[] = 'Sonata\CoreBundle\Form\Type\CollectionType';
84
        }
85
86
        // model types
87
        foreach ($modelTypes as $formType) {
88
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
89
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
90
            $this->assertSame($modelClass, $options['class']);
91
            $this->assertSame($modelManager, $options['model_manager']);
92
        }
93
94
        // admin type
95
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadata::ONE_TO_ONE);
96
        foreach ($adminTypes as $formType) {
97
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
98
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
99
            $this->assertSame($modelClass, $options['data_class']);
100
            $this->assertSame(false, $options['btn_add']);
101
            $this->assertSame(false, $options['delete']);
102
        }
103
104
        // collection type
105
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadata::ONE_TO_MANY);
106
        foreach ($collectionTypes as $formType) {
107
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
108
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
109
            $this->assertSame('sonata_type_admin', $options['type']);
110
            $this->assertSame(true, $options['modifiable']);
111
            $this->assertSame($fieldDescription, $options['type_options']['sonata_field_description']);
112
            $this->assertSame($modelClass, $options['type_options']['data_class']);
113
        }
114
    }
115
116
    public function testAdminClassAttachForNotMappedField()
117
    {
118
        // Given
119
        $modelManager = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Model\ModelManager')
120
            ->disableOriginalConstructor()
121
            ->getMock();
122
        $modelManager->method('hasMetadata')->willReturn(false);
123
124
        $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
125
        $admin->method('getModelManager')->willReturn($modelManager);
126
127
        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
128
        $fieldDescription->method('getMappingType')->willReturn('simple');
129
        $fieldDescription->method('getType')->willReturn('sonata_type_model_list');
130
        $fieldDescription->method('getOption')->with($this->logicalOr(
131
            $this->equalTo('edit'),
132
            $this->equalTo('admin_code')
133
        ))->willReturn('sonata.admin.code');
134
135
        // Then
136
        $admin
137
            ->expects($this->once())
138
            ->method('attachAdminClass')
139
            ->with($fieldDescription)
140
        ;
141
142
        // When
143
        $this->formContractor->fixFieldDescription($admin, $fieldDescription);
144
    }
145
}
146