Completed
Push — 3.x ( 36fa90...980c10 )
by Christian
15s
created

FormContractorTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 119
rs 10

4 Methods

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