Completed
Pull Request — 3.x (#178)
by
unknown
03:41 queued 33s
created

FormContractorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
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\ClassMetadata;
15
use Sonata\DoctrineMongoDBAdminBundle\Builder\FormContractor;
16
17
/**
18
 * @author ju1ius <[email protected]>
19
 */
20
class FormContractorTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @var FormFactoryInterface
24
     */
25
    private $formFactory;
26
27
    /**
28
     * @var FormContractor
29
     */
30
    private $formContractor;
31
32
    protected function setUp()
33
    {
34
        $this->formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
35
36
        $this->formContractor = new FormContractor($this->formFactory);
37
    }
38
39
    public function testDefaultOptionsForSonataFormTypes()
40
    {
41
        $admin = $this->getMockBuilder('Sonata\AdminBundle\Admin\AdminInterface')->getMock();
42
        $modelManager = $this->getMockBuilder('Sonata\AdminBundle\Model\ModelManagerInterface')->getMock();
43
        $modelClass = 'FooEntity';
44
45
        $admin->method('getModelManager')->willReturn($modelManager);
46
        $admin->method('getClass')->willReturn($modelClass);
47
48
        $fieldDescription = $this->getMockBuilder('Sonata\AdminBundle\Admin\FieldDescriptionInterface')->getMock();
49
        $fieldDescription->method('getAdmin')->willReturn($admin);
50
        $fieldDescription->method('getTargetEntity')->willReturn($modelClass);
51
        $fieldDescription->method('getAssociationAdmin')->willReturn($admin);
52
53
        $modelTypes = array(
54
            'sonata_type_model',
55
            'sonata_type_model_list',
56
            'sonata_type_model_hidden',
57
            'sonata_type_model_autocomplete',
58
        );
59
        $adminTypes = array('sonata_type_admin');
60
        $collectionTypes = array('sonata_type_collection');
61
        if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
62
            array_push(
63
                $modelTypes,
64
                'Sonata\AdminBundle\Form\Type\ModelType',
65
                'Sonata\AdminBundle\Form\Type\ModelTypeList',
66
                'Sonata\AdminBundle\Form\Type\ModelHiddenType',
67
                'Sonata\AdminBundle\Form\Type\ModelAutocompleteType'
68
            );
69
            $adminTypes[] = 'Sonata\AdminBundle\Form\Type\AdminType';
70
            $collectionTypes[] = 'Sonata\CoreBundle\Form\Type\CollectionType';
71
        }
72
73
        // model types
74
        foreach ($modelTypes as $formType) {
75
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
76
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
77
            $this->assertSame($modelClass, $options['class']);
78
            $this->assertSame($modelManager, $options['model_manager']);
79
        }
80
81
        // admin type
82
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadata::ONE);
83
        foreach ($adminTypes as $formType) {
84
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
85
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
86
            $this->assertSame($modelClass, $options['data_class']);
87
            $this->assertSame(false, $options['btn_add']);
88
            $this->assertSame(false, $options['delete']);
89
        }
90
91
        // collection type
92
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadata::MANY);
93
        foreach ($collectionTypes as $formType) {
94
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
95
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
96
            $this->assertSame('sonata_type_admin', $options['type']);
97
            $this->assertSame(true, $options['modifiable']);
98
            $this->assertSame($fieldDescription, $options['type_options']['sonata_field_description']);
99
            $this->assertSame($modelClass, $options['type_options']['data_class']);
100
        }
101
    }
102
}
103