Completed
Push — master ( 46c3a0...a94e83 )
by Grégoire
13s queued 11s
created

Form/Widget/FormSonataFilterChoiceWidgetTest.php (2 issues)

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\Widget;
15
16
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
17
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SymfonyChoiceType;
18
use Symfony\Component\Form\Extension\Core\Type\RangeType;
19
use Symfony\Component\Form\FormTypeGuesserInterface;
20
use Symfony\Component\Form\Tests\Fixtures\TestExtension;
21
use Symfony\Component\Translation\TranslatorInterface;
22
23
class FormSonataFilterChoiceWidgetTest extends BaseWidgetTest
24
{
25
    protected $type = 'filter';
26
27
    public function setUp(): void
28
    {
29
        parent::setUp();
30
    }
31
32
    public function testDefaultValueRendering(): void
33
    {
34
        $choice = $this->factory->create(
35
            $this->getParentClass(),
36
            null,
37
            $this->getDefaultOption()
38
        );
39
40
        $html = $this->cleanHtmlWhitespace($this->renderWidget($choice->createView()));
41
        $html = $this->cleanHtmlAttributeWhitespace($html);
42
43
        $this->assertContains(
44
            '<option value="1">[trans]label_type_contains[/trans]</option>',
45
            $html
46
        );
47
48
        $this->assertContains(
49
            '<option value="2">[trans]label_type_not_contains[/trans]</option>',
50
            $html
51
        );
52
53
        $this->assertContains(
54
            '<option value="3">[trans]label_type_equals[/trans]</option></select>',
55
            $html
56
        );
57
    }
58
59
    protected function getParentClass()
60
    {
61
        if (class_exists(RangeType::class)) {
62
            return ChoiceType::class;
63
        }
64
65
        return 'sonata_type_filter_choice';
66
    }
67
68
    protected function getChoiceClass()
69
    {
70
        if (class_exists(RangeType::class)) {
71
            return SymfonyChoiceType::class;
72
        }
73
74
        return 'choice';
75
    }
76
77
    protected function getExtensions()
78
    {
79
        $mock = $this->getMockBuilder(TranslatorInterface::class)->getMock();
80
81
        $mock->method('trans')
82
            ->willReturnCallback(static function ($arg) {
83
                return $arg;
84
            }
85
        );
86
87
        $extensions = parent::getExtensions();
88
        $guesser = $this->getMockForAbstractClass(FormTypeGuesserInterface::class);
89
        $extension = new TestExtension($guesser);
0 ignored issues
show
$guesser is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...rmTypeGuesserInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
        $type = new ChoiceType($mock);
0 ignored issues
show
$mock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...on\TranslatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
        $extension->addType($type);
92
93
        if (!$extension->hasType($this->getParentClass())) {
94
            $reflection = new \ReflectionClass($extension);
95
            $property = $reflection->getProperty('types');
96
            $property->setAccessible(true);
97
            $property->setValue($extension, [\get_class($type) => current($property->getValue($extension))]);
98
        }
99
100
        $extensions[] = $extension;
101
102
        return $extensions;
103
    }
104
105
    protected function getDefaultOption()
106
    {
107
        return ['field_type' => $this->getChoiceClass(),
108
             'field_options' => [],
109
             'operator_type' => $this->getChoiceClass(),
110
             'operator_options' => [],
111
        ];
112
    }
113
}
114