Passed
Push — master ( 4fd14e...da3759 )
by Gerrit
02:04
created

shouldCreateRequestArgumentFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
/**
3
 * Copyright (C) 2017  Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\SymfonyGenerics\Tests\Unit\Arguments\ArgumentFactory;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\RequestArgumentFactory;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
use InvalidArgumentException;
17
use Addiks\SymfonyGenerics\Arguments\RequestArgument;
18
19
final class RequestArgumentFactoryTest extends TestCase
20
{
21
22
    /**
23
     * @var RequestArgumentFactory
24
     */
25
    private $factory;
26
27
    /**
28
     * @var RequestStack
29
     */
30
    private $requestStack;
31
32
    public function setUp()
33
    {
34
        $this->requestStack = $this->createMock(RequestStack::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...on\RequestStack::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...oundation\RequestStack> of property $requestStack.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
36
        $this->factory = new RequestArgumentFactory($this->requestStack);
0 ignored issues
show
Documentation introduced by
$this->requestStack is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...oundation\RequestStack>.

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...
37
    }
38
39
    /**
40
     * @test
41
     * @dataProvider dataProviderForShouldKnowIfUnderstandAString
42
     */
43
    public function shouldKnowIfUnderstandAString(bool $expectedResult, string $source)
44
    {
45
        /** @var bool $actualResult */
46
        $actualResult = $this->factory->understandsString($source);
47
48
        $this->assertEquals($expectedResult, $actualResult);
49
    }
50
51
    public function dataProviderForShouldKnowIfUnderstandAString()
52
    {
53
        return array(
54
            [true, "\$foo"],
55
            [true, "\$f"],
56
            [false, "\$ "],
57
            [false, "\$-"],
58
            [false, "\$"],
59
            [false, "foo"],
60
            [false, "f"],
61
            [false, ""],
62
            [false, "-"],
63
            [false, "_"],
64
        );
65
    }
66
67
    /**
68
     * @test
69
     * @dataProvider dataProviderForShouldKnowIfUnderstandAnArray
70
     */
71
    public function shouldKnowIfUnderstandAnArray(bool $expectedResult, array $source)
72
    {
73
        /** @var bool $actualResult */
74
        $actualResult = $this->factory->understandsArray($source);
75
76
        $this->assertEquals($expectedResult, $actualResult);
77
    }
78
79
    public function dataProviderForShouldKnowIfUnderstandAnArray()
80
    {
81
        return array(
82
            [true, ['key' => 'foo', 'type' => 'request']],
83
            [true, ['key' => 'bar', 'type' => 'request']],
84
            [true, ['key' => 'a', 'type' => 'request']],
85
            [true, ['key' => '', 'type' => 'request']],
86
            [false, ['key' => '', 'type' => 'foo']],
87
            [false, ['key' => '']],
88
            [false, ['type' => 'request']],
89
        );
90
    }
91
92
    /**
93
     * @test
94
     * @dataProvider dataProviderForShouldCreateRequestArgumentFromString
95
     */
96
    public function shouldCreateRequestArgumentFromString($expectedResult, string $source, bool $shouldRejectCreation)
97
    {
98
        if ($shouldRejectCreation) {
99
            $this->expectException(InvalidArgumentException::class);
100
        }
101
102
        /** @var mixed $actualResult */
103
        $actualResult = $this->factory->createArgumentFromString($source);
104
105
        $this->assertEquals($expectedResult, $actualResult);
106
    }
107
108
    public function dataProviderForShouldCreateRequestArgumentFromString()
109
    {
110
        $this->setUp();
111
112
        return array(
113
            [new RequestArgument($this->requestStack, "someKey"), '$someKey', false],
114
            [new RequestArgument($this->requestStack, "a"), '$a', false],
115
            [null, '$', true],
116
            [null, 'a', true],
117
            [null, '', true],
118
        );
119
    }
120
121
    /**
122
     * @test
123
     * @dataProvider dataProviderForShouldCreateRequestArgumentFromArray
124
     */
125
    public function shouldCreateRequestArgumentFromArray($expectedResult, array $source, bool $shouldRejectCreation)
126
    {
127
        if ($shouldRejectCreation) {
128
            $this->expectException(InvalidArgumentException::class);
129
        }
130
131
        /** @var mixed $actualResult */
132
        $actualResult = $this->factory->createArgumentFromArray($source);
133
134
        $this->assertEquals($expectedResult, $actualResult);
135
    }
136
137
    public function dataProviderForShouldCreateRequestArgumentFromArray()
138
    {
139
        $this->setUp();
140
141
        return array(
142
            [new RequestArgument($this->requestStack, "someKey"), ['key' => 'someKey'], false],
143
            [new RequestArgument($this->requestStack, "a"), ['key' => 'a'], false],
144
            [null, [], true],
145
        );
146
    }
147
148
}
149