Completed
Push — master ( 460827...4b7807 )
by Christian
12:37
created

tests/Form/Type/MediaTypeTest.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\MediaBundle\Tests\Form\Type;
15
16
use Sonata\MediaBundle\Form\Type\MediaType;
17
use Sonata\MediaBundle\Provider\Pool;
18
use Symfony\Component\Form\Forms;
19
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
20
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
21
22
/**
23
 * @author Virgile Vivier <[email protected]>
24
 * @author Christian Gripp <[email protected]>
25
 */
26
class MediaTypeTest extends AbstractTypeTest
27
{
28
    protected $mediaPool;
29
30
    /**
31
     * @var MediaType
32
     */
33
    protected $mediaType;
34
35
    protected function setUp(): void
36
    {
37
        parent::setUp();
38
39
        $this->mediaPool = $this->createMock(Pool::class);
40
        $this->mediaType = new MediaType($this->mediaPool, 'testClass');
0 ignored issues
show
$this->mediaPool is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\MediaBundle\Provider\Pool>.

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...
41
42
        $this->factory = Forms::createFormFactoryBuilder()
43
            ->addType($this->mediaType)
44
            ->addExtensions($this->getExtensions())
45
            ->getFormFactory();
46
    }
47
48
    public function testMissingFormOptions(): void
49
    {
50
        $this->mediaPool->expects($this->any())->method('getProviderList')->willReturn([
51
            'provider_a' => 'provider_a',
52
            'provider_b' => 'provider_b',
53
        ]);
54
        $this->mediaPool->expects($this->any())->method('getContexts')->willReturn([
55
            'video' => [],
56
            'pic' => [],
57
        ]);
58
59
        $this->expectException(MissingOptionsException::class);
60
        $this->expectExceptionMessage(
61
            'The required options "context", "provider" are missing.'
62
        );
63
64
        $this->factory->create($this->getFormType(), null);
65
    }
66
67
    public function testMissingFormContextOption(): void
68
    {
69
        $this->mediaPool->expects($this->any())->method('getProviderList')->willReturn([
70
            'provider_a' => 'provider_a',
71
            'provider_b' => 'provider_b',
72
        ]);
73
        $this->mediaPool->expects($this->any())->method('getContexts')->willReturn([
74
            'video' => [],
75
            'pic' => [],
76
        ]);
77
78
        $this->expectException(MissingOptionsException::class);
79
80
        $this->factory->create($this->getFormType(), null, [
81
            'provider' => 'provider_a',
82
        ]);
83
    }
84
85
    public function testMissingFormProviderOption(): void
86
    {
87
        $this->mediaPool->expects($this->any())->method('getProviderList')->willReturn([
88
            'provider_a' => 'provider_a',
89
            'provider_b' => 'provider_b',
90
        ]);
91
        $this->mediaPool->expects($this->any())->method('getContexts')->willReturn([
92
            'video' => [],
93
            'pic' => [],
94
        ]);
95
96
        $this->expectException(MissingOptionsException::class);
97
98
        $this->factory->create($this->getFormType(), null, [
99
            'context' => 'pic',
100
        ]);
101
    }
102
103
    public function testInvalidFormProviderOption(): void
104
    {
105
        $this->mediaPool->expects($this->any())->method('getProviderList')->willReturn([
106
            'provider_a' => 'provider_a',
107
            'provider_b' => 'provider_b',
108
        ]);
109
        $this->mediaPool->expects($this->any())->method('getContexts')->willReturn([
110
            'video' => [],
111
            'pic' => [],
112
        ]);
113
114
        $this->expectException(InvalidOptionsException::class);
115
        $this->expectExceptionMessage(
116
            'The option "provider" with value "provider_c" is invalid. Accepted values are: "provider_a", "provider_b".'
117
        );
118
119
        $this->factory->create($this->getFormType(), null, [
120
            'provider' => 'provider_c',
121
            'context' => 'pic',
122
        ]);
123
    }
124
125
    public function testInvalidFormContextOption(): void
126
    {
127
        $this->mediaPool->expects($this->any())->method('getProviderList')->willReturn([
128
            'provider_a' => 'provider_a',
129
            'provider_b' => 'provider_b',
130
        ]);
131
        $this->mediaPool->expects($this->any())->method('getContexts')->willReturn([
132
            'video' => [],
133
            'pic' => [],
134
        ]);
135
136
        $this->expectException(InvalidOptionsException::class);
137
        $this->expectExceptionMessage(
138
            'The option "context" with value "photo" is invalid. Accepted values are: "video", "pic".'
139
        );
140
141
        $this->factory->create($this->getFormType(), null, [
142
            'provider' => 'provider_b',
143
            'context' => 'photo',
144
        ]);
145
    }
146
147
    protected function getTestedInstance()
148
    {
149
        return new MediaType($this->mediaPool, 'testclass');
150
    }
151
152
    private function getFormType(): string
153
    {
154
        return MediaType::class;
155
    }
156
}
157