Passed
Push — master ( 918acc...2e1e94 )
by Gerrit
01:58
created

ArgumentCallFactoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\ArgumentCallFactory;
15
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory;
16
use InvalidArgumentException;
17
use Addiks\SymfonyGenerics\Arguments\ArgumentCall;
18
use Addiks\SymfonyGenerics\Arguments\Argument;
19
20
final class ArgumentCallFactoryTest extends TestCase
21
{
22
23
    /**
24
     * @var ArgumentCallFactory
25
     */
26
    private $factory;
27
28
    /**
29
     * @var ArgumentFactory
30
     */
31
    private $argumentFactory;
32
33
    public function setUp()
34
    {
35
        $this->argumentFactory = $this->createMock(ArgumentFactory::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...ArgumentFactory::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...actory\ArgumentFactory> of property $argumentFactory.

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...
36
37
        $this->factory = new ArgumentCallFactory($this->argumentFactory);
0 ignored issues
show
Documentation introduced by
$this->argumentFactory is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
38
    }
39
40
    /**
41
     * @test
42
     * @dataProvider dataProviderForShouldKnowIfUnderstandString
43
     */
44
    public function shouldKnowIfUnderstandString(bool $expectedResult, string $source)
45
    {
46
        /** @var bool $actualResult */
47
        $actualResult = $this->factory->understandsString($source);
48
49
        $this->assertEquals($expectedResult, $actualResult);
50
    }
51
52
    public function dataProviderForShouldKnowIfUnderstandString()
53
    {
54
        return array(
55
            [true, 'a::b'],
56
            [true, 'foo::bar'],
57
            [true, 'foo::bar(baz)'],
58
            [false, '::b'],
59
            [false, 'a::'],
60
            [false, '::'],
61
            [false, ''],
62
        );
63
    }
64
65
    /**
66
     * @test
67
     * @dataProvider dataProviderForShouldKnowIfUnderstandArray
68
     */
69
    public function shouldKnowIfUnderstandArray(bool $expectedResult, array $source)
70
    {
71
        /** @var bool $actualResult */
72
        $actualResult = $this->factory->understandsArray($source);
73
74
        $this->assertEquals($expectedResult, $actualResult);
75
    }
76
77
    public function dataProviderForShouldKnowIfUnderstandArray()
78
    {
79
        return array(
80
            [true,  ['callee' => 'foo', 'method' => 'bar']],
81
            [true,  ['callee' => 'foo', 'method' => 'bar', 'arguments' => []]],
82
            [false, ['method' => 'bar']],
83
            [false, ['callee' => 'foo']],
84
        );
85
    }
86
87
    /**
88
     * @test
89
     * @dataProvider dataProviderForShouldCreateCallArgumentFromString
90
     */
91
    public function shouldCreateCallArgumentFromString(
92
        ?ArgumentCall $expectedResult,
93
        string $source,
94
        bool $shouldRejectCreation
95
    ) {
96
        if ($shouldRejectCreation) {
97
            $this->expectException(InvalidArgumentException::class);
98
99
        } else {
100
            $this->argumentFactory->method('createArgumentFromString')->willReturn($this->createMock(Argument::class));
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
101
        }
102
103
        $actualResult = $this->factory->createArgumentFromString($source);
104
105
        $this->assertEquals($expectedResult, $actualResult);
106
    }
107
108
    public function dataProviderForShouldCreateCallArgumentFromString(): array
109
    {
110
        return array(
111
            [new ArgumentCall($this->createMock(Argument::class), 'someMethod', []), 'some-callee::someMethod', false],
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...uments\Argument::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGenerics\Arguments\Argument>.

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...
112
            [new ArgumentCall($this->createMock(Argument::class), 'someMethod', [
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...uments\Argument::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGenerics\Arguments\Argument>.

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...
113
                $this->createMock(Argument::class),
114
                $this->createMock(Argument::class)
115
            ]), 'some-callee::someMethod(a, b)', false],
116
            [null, 'a::', true],
117
            [null, '::b', true],
118
            [null, '::', true],
119
        );
120
    }
121
122
    /**
123
     * @test
124
     */
125
    public function shouldCreateArgumentsFromArgumentsInString()
126
    {
127
        $this->argumentFactory->expects($this->exactly(3))->method('createArgumentFromString')->withConsecutive(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
128
            [$this->equalTo('a')],
129
            [$this->equalTo('b')],
130
            [$this->equalTo('some-callee')]
131
        )->willReturn($this->createMock(Argument::class));
132
        $this->factory->createArgumentFromString('some-callee::someMethod(a, b)');
133
    }
134
135
    /**
136
     * @test
137
     * @dataProvider dataProviderForShouldCreateCallArgumentFromArray
138
     */
139
    public function shouldCreateCallArgumentFromArray(
140
        $expectedResult,
141
        array $source,
142
        bool $shouldRejectCreation
143
    ) {
144
        if ($shouldRejectCreation) {
145
            $this->expectException(InvalidArgumentException::class);
146
147
        } else {
148
            $this->argumentFactory->method('createArgumentFromString')->willReturn($this->createMock(Argument::class));
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
149
            $this->argumentFactory->method('createArgumentFromArray')->willReturn($this->createMock(Argument::class));
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\SymfonyGen...actory\ArgumentFactory>.

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...
150
        }
151
152
        $actualResult = $this->factory->createArgumentFromArray($source);
153
154
        $this->assertEquals($expectedResult, $actualResult);
155
    }
156
157
    public function dataProviderForShouldCreateCallArgumentFromArray(): array
158
    {
159
        return array(
160
            [null, [], true],
161
            [null, ['method' => 'foo'], true],
162
            [null, ['callee' => 'bar'], true],
163
            [new ArgumentCall($this->createMock(Argument::class), 'someMethod', []), [
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...uments\Argument::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGenerics\Arguments\Argument>.

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...
164
                'callee' => 'some-callee',
165
                'method' => 'someMethod'
166
            ], false],
167
            [new ArgumentCall($this->createMock(Argument::class), 'someMethod', []), [
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...uments\Argument::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGenerics\Arguments\Argument>.

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...
168
                'callee' => ['some-callee'],
169
                'method' => 'someMethod'
170
            ], false],
171
            [new ArgumentCall($this->createMock(Argument::class), 'someMethod', [
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...uments\Argument::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGenerics\Arguments\Argument>.

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...
172
                $this->createMock(Argument::class),
173
                $this->createMock(Argument::class)
174
            ]), [
175
                'callee' => 'some-callee',
176
                'method' => 'someMethod',
177
                'arguments' => [
178
                    'foo',
179
                    'bar'
180
                ]
181
            ], false],
182
            [new ArgumentCall($this->createMock(Argument::class), 'someMethod', [
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...uments\Argument::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGenerics\Arguments\Argument>.

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...
183
                $this->createMock(Argument::class),
184
                $this->createMock(Argument::class)
185
            ]), [
186
                'callee' => ['some-callee'],
187
                'method' => 'someMethod',
188
                'arguments' => [
189
                    'foo',
190
                    'bar'
191
                ]
192
            ], false],
193
            [new ArgumentCall($this->createMock(Argument::class), 'someMethod', [
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...uments\Argument::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGenerics\Arguments\Argument>.

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...
194
                $this->createMock(Argument::class),
195
                $this->createMock(Argument::class)
196
            ]), [
197
                'callee' => 'some-callee',
198
                'method' => 'someMethod',
199
                'arguments' => [
200
                    ['foo'],
201
                    ['bar']
202
                ]
203
            ], false],
204
        );
205
    }
206
207
}
208