Completed
Push — master ( 369610...186bbc )
by
unknown
04:30 queued 11s
created

tests/Builder/FormContractorTest.php (1 issue)

Labels
Severity

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Builder;
15
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
17
use PHPUnit\Framework\TestCase;
18
use Sonata\DoctrineMongoDBAdminBundle\Builder\FormContractor;
19
use Symfony\Component\Form\FormFactoryInterface;
20
21
class FormContractorTest extends TestCase
22
{
23
    /**
24
     * @var FormFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
25
     */
26
    private $formFactory;
27
28
    /**
29
     * @var FormContractor
30
     */
31
    private $formContractor;
32
33
    protected function setUp(): void
34
    {
35
        $this->formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
36
        $this->formContractor = new FormContractor($this->formFactory);
37
    }
38
39
    public function testGetFormBuilder(): void
40
    {
41
        $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...
42
            ->willReturn($this->createMock('Symfony\Component\Form\FormBuilderInterface'));
43
        $this->assertInstanceOf(
44
            'Symfony\Component\Form\FormBuilderInterface',
45
            $this->formContractor->getFormBuilder('test', ['foo' => 'bar'])
46
        );
47
    }
48
49
    public function testDefaultOptionsForSonataFormTypes(): void
50
    {
51
        $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
52
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
53
        $modelClass = 'FooEntity';
54
        $admin->method('getModelManager')->willReturn($modelManager);
55
        $admin->method('getClass')->willReturn($modelClass);
56
        $fieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
57
        $fieldDescription->method('getAdmin')->willReturn($admin);
58
        $fieldDescription->method('getTargetEntity')->willReturn($modelClass);
59
        $fieldDescription->method('getAssociationAdmin')->willReturn($admin);
60
61
        $modelTypes = [
62
            'sonata_type_model',
63
            'sonata_type_model_list',
64
        ];
65
        $adminTypes = ['sonata_type_admin'];
66
        $collectionTypes = ['sonata_type_collection'];
67
        // NEXT_MAJOR: Use only FQCNs when dropping support for Symfony <2.8
68
        if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
69
            $classTypes = [
70
                'Sonata\AdminBundle\Form\Type\ModelType',
71
                'Sonata\AdminBundle\Form\Type\ModelListType',
72
            ];
73
            foreach ($classTypes as $classType) {
74
                array_push(
75
                    $modelTypes,
76
                    // add class type.
77
                    $classType,
78
                    // add instance of class type.
79
                    get_class($this->createMock($classType))
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->assertTrue($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(): void
112
    {
113
        // Given
114
        $modelManager = $this->createMock('Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager');
115
        $modelManager->method('hasMetadata')->willReturn(false);
116
        $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
117
        $admin->method('getModelManager')->willReturn($modelManager);
118
        $fieldDescription = $this->createMock('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