Completed
Pull Request — 3.x (#196)
by
unknown
01:51
created

FormContractorTest::testGetFormBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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