Completed
Push — 3.x ( 486a75...bbeb6a )
by Jordi Sala
9s
created

testAdminClassAttachForNotMappedField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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