Completed
Push — 3.x ( 3d5935...36902d )
by Jordi Sala
02:59
created

tests/Form/Extension/ChoiceTypeExtensionTest.php (1 issue)

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\AdminBundle\Tests\Form\Extension;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Form\Extension\ChoiceTypeExtension;
18
use Sonata\AdminBundle\Tests\Fixtures\TestExtension;
19
use Sonata\CoreBundle\Form\Extension\DependencyInjectionExtension;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
22
use Symfony\Component\Form\FormFactoryInterface;
23
use Symfony\Component\Form\Forms;
24
25
class ChoiceTypeExtensionTest extends TestCase
26
{
27
    /**
28
     * @var FormFactoryInterface
29
     */
30
    private $factory;
31
32
    protected function setUp(): void
33
    {
34
        if (class_exists(DependencyInjectionExtension::class)) {
35
            $container = $this->getMockForAbstractClass(ContainerInterface::class);
36
            $container->method('has')->willReturn(true);
37
            $container->method('get')
38
                ->with($this->equalTo('sonata.admin.form.choice_extension'))
39
                ->willReturn(new ChoiceTypeExtension());
40
41
            $typeServiceIds = [];
42
            $typeExtensionServiceIds = [];
43
            $guesserServiceIds = [];
44
            $mappingTypes = [
45
                'choice' => ChoiceType::class,
46
            ];
47
            $extensionTypes = [
48
                'choice' => [
49
                    'sonata.admin.form.choice_extension',
50
                ],
51
            ];
52
53
            $extension = new DependencyInjectionExtension(
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\CoreBundle\Form\E...dencyInjectionExtension has been deprecated with message: since sonata-project/core-bundle 3.7, to be removed in 4.0, the form mapping feature should be disabled.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
54
                $container,
55
                $typeServiceIds,
56
                $typeExtensionServiceIds,
57
                $guesserServiceIds,
58
                $mappingTypes,
59
                $extensionTypes
60
            );
61
        } else {
62
            $extension = new TestExtension(null);
63
            $extension->addTypeExtension(new ChoiceTypeExtension());
64
        }
65
66
        $this->factory = Forms::createFormFactoryBuilder()
67
            ->addExtension($extension)
68
            ->getFormFactory();
69
    }
70
71
    public function testExtendedType(): void
72
    {
73
        $extension = new ChoiceTypeExtension();
74
75
        $this->assertSame(
76
            ChoiceType::class,
77
            $extension->getExtendedType()
78
        );
79
80
        $this->assertSame(
81
            [ChoiceType::class],
82
            ChoiceTypeExtension::getExtendedTypes()
83
        );
84
    }
85
86
    public function testDefaultOptionsWithSortable(): void
87
    {
88
        $view = $this->factory
89
            ->create(ChoiceType::class, null, [
90
                'sortable' => true,
91
            ])
92
            ->createView();
93
94
        $this->assertTrue(isset($view->vars['sortable']));
95
        $this->assertTrue($view->vars['sortable']);
96
    }
97
98
    public function testDefaultOptionsWithoutSortable(): void
99
    {
100
        $view = $this->factory
101
            ->create(ChoiceType::class, null, [])
102
            ->createView();
103
104
        $this->assertTrue(isset($view->vars['sortable']));
105
        $this->assertFalse($view->vars['sortable']);
106
    }
107
}
108