Completed
Push — master ( f58ee6...d5419f )
by Jordi Sala
01:39
created

tests/Builder/FormContractorTest.php (1 issue)

calls to non-existent methods.

Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 PHPUnit\Framework\TestCase;
16
use Sonata\DoctrineMongoDBAdminBundle\Builder\FormContractor;
17
use Symfony\Component\Form\FormFactoryInterface;
18
19
class FormContractorTest extends 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->createMock('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
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->createMock('Symfony\Component\Form\FormBuilderInterface'));
41
        $this->assertInstanceOf(
42
            'Symfony\Component\Form\FormBuilderInterface',
43
            $this->formContractor->getFormBuilder('test', ['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
59
        $modelTypes = [
60
            'sonata_type_model',
61
            'sonata_type_model_list',
62
        ];
63
        $adminTypes = ['sonata_type_admin'];
64
        $collectionTypes = ['sonata_type_collection'];
65
        // NEXT_MAJOR: Use only FQCNs when dropping support for Symfony <2.8
66
        if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
67
            $classTypes = [
68
                'Sonata\AdminBundle\Form\Type\ModelType',
69
                'Sonata\AdminBundle\Form\Type\ModelListType',
70
            ];
71
            foreach ($classTypes as $classType) {
72
                array_push(
73
                    $modelTypes,
74
                    // add class type.
75
                    $classType,
76
                    // add instance of class type.
77
                    get_class($this->createMock($classType))
78
                );
79
            }
80
            $adminTypes[] = 'Sonata\AdminBundle\Form\Type\AdminType';
81
            $collectionTypes[] = 'Sonata\CoreBundle\Form\Type\CollectionType';
82
        }
83
        // model types
84
        foreach ($modelTypes as $formType) {
85
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
86
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
87
            $this->assertSame($modelClass, $options['class']);
88
            $this->assertSame($modelManager, $options['model_manager']);
89
        }
90
        // admin type
91
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadataInfo::ONE);
92
        foreach ($adminTypes as $formType) {
93
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
94
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
95
            $this->assertSame($modelClass, $options['data_class']);
96
        }
97
        // collection type
98
        $fieldDescription->method('getMappingType')->willReturn(ClassMetadataInfo::MANY);
99
        foreach ($collectionTypes as $index => $formType) {
100
            $options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
101
            $this->assertSame($fieldDescription, $options['sonata_field_description']);
102
            $this->assertSame($adminTypes[$index], $options['type']);
103
            $this->assertTrue($options['modifiable']);
104
            $this->assertSame($fieldDescription, $options['type_options']['sonata_field_description']);
105
            $this->assertSame($modelClass, $options['type_options']['data_class']);
106
        }
107
    }
108
109
    public function testAdminClassAttachForNotMappedField()
110
    {
111
        // Given
112
        $modelManager = $this->createMock('Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager');
113
        $modelManager->method('hasMetadata')->willReturn(false);
114
        $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
115
        $admin->method('getModelManager')->willReturn($modelManager);
116
        $fieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
117
        $fieldDescription->method('getMappingType')->willReturn('one');
118
        $fieldDescription->method('getType')->willReturn('sonata_type_model_list');
119
        $fieldDescription->method('getOption')->with($this->logicalOr(
120
            $this->equalTo('edit'),
121
            $this->equalTo('admin_code')
122
        ))->willReturn('sonata.admin.code');
123
        // Then
124
        $admin
125
            ->expects($this->once())
126
            ->method('attachAdminClass')
127
            ->with($fieldDescription)
128
        ;
129
        // When
130
        $this->formContractor->fixFieldDescription($admin, $fieldDescription);
131
    }
132
}
133