Passed
Push — master ( 928c25...8579cb )
by Gerrit
02:16
created

dataProviderForShouldCreateCallArgumentFromArray()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 72
rs 8.6109
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
20
21
final class ArgumentCallFactoryTest extends TestCase
22
{
23
24
    /**
25
     * @var ArgumentCallFactory
26
     */
27
    private $factory;
28
29
    /**
30
     * @var ArgumentCompilerInterface
31
     */
32
    private $argumentCompiler;
33
34
    /**
35
     * @var ArgumentFactory
36
     */
37
    private $argumentFactory;
38
39
    public function setUp()
40
    {
41
        $this->argumentCompiler = $this->createMock(ArgumentCompilerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...mpilerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\SymfonyGen...umentCompilerInterface> of property $argumentCompiler.

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

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...
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...
45
    }
46
47
    /**
48
     * @test
49
     * @dataProvider dataProviderForShouldKnowIfUnderstandString
50
     */
51
    public function shouldKnowIfUnderstandString(bool $expectedResult, string $source)
52
    {
53
        /** @var bool $actualResult */
54
        $actualResult = $this->factory->understandsString($source);
55
56
        $this->assertEquals($expectedResult, $actualResult);
57
    }
58
59
    public function dataProviderForShouldKnowIfUnderstandString()
60
    {
61
        return array(
62
            [true, 'a::b'],
63
            [true, 'foo::bar'],
64
            [true, 'foo::bar(baz)'],
65
            [false, '::b'],
66
            [false, 'a::'],
67
            [false, '::'],
68
            [false, ''],
69
        );
70
    }
71
72
    /**
73
     * @test
74
     * @dataProvider dataProviderForShouldKnowIfUnderstandArray
75
     */
76
    public function shouldKnowIfUnderstandArray(bool $expectedResult, array $source)
77
    {
78
        /** @var bool $actualResult */
79
        $actualResult = $this->factory->understandsArray($source);
80
81
        $this->assertEquals($expectedResult, $actualResult);
82
    }
83
84
    public function dataProviderForShouldKnowIfUnderstandArray()
85
    {
86
        return array(
87
            [true,  ['callee' => 'foo', 'method' => 'bar']],
88
            [true,  ['callee' => 'foo', 'method' => 'bar', 'arguments' => []]],
89
            [false, ['method' => 'bar']],
90
            [false, ['callee' => 'foo']],
91
        );
92
    }
93
94
    /**
95
     * @test
96
     * @dataProvider dataProviderForShouldCreateCallArgumentFromString
97
     */
98
    public function shouldCreateCallArgumentFromString(
99
        ?ArgumentCall $expectedResult,
100
        string $source,
101
        bool $shouldRejectCreation
102
    ) {
103
        if ($shouldRejectCreation) {
104
            $this->expectException(InvalidArgumentException::class);
105
106
        } else {
107
            $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...
108
        }
109
110
        $actualResult = $this->factory->createArgumentFromString($source);
111
112
        $this->assertEquals($expectedResult, $actualResult);
113
    }
114
115
    public function dataProviderForShouldCreateCallArgumentFromString(): array
116
    {
117
        return array(
118
            [
119
                new ArgumentCall(
120
                    $this->createMock(ArgumentCompilerInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...mpilerInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...umentCompilerInterface>.

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...
121
                    $this->createMock(Argument::class),
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...
122
                    'someMethod',
123
                    []
124
                ),
125
                'some-callee::someMethod',
126
                false
127
            ],
128
            [
129
                new ArgumentCall(
130
                    $this->createMock(ArgumentCompilerInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...mpilerInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...umentCompilerInterface>.

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...
131
                    $this->createMock(Argument::class),
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...
132
                    'someMethod',
133
                    [
134
                        $this->createMock(Argument::class),
135
                        $this->createMock(Argument::class)
136
                    ]
137
                ),
138
                'some-callee::someMethod(a, b)',
139
                false
140
            ],
141
            [null, 'a::', true],
142
            [null, '::b', true],
143
            [null, '::', true],
144
        );
145
    }
146
147
    /**
148
     * @test
149
     */
150
    public function shouldCreateArgumentsFromArgumentsInString()
151
    {
152
        $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...
153
            [$this->equalTo('a')],
154
            [$this->equalTo('b')],
155
            [$this->equalTo('some-callee')]
156
        )->willReturn($this->createMock(Argument::class));
157
        $this->factory->createArgumentFromString('some-callee::someMethod(a, b)');
158
    }
159
160
    /**
161
     * @test
162
     * @dataProvider dataProviderForShouldCreateCallArgumentFromArray
163
     */
164
    public function shouldCreateCallArgumentFromArray(
165
        $expectedResult,
166
        array $source,
167
        bool $shouldRejectCreation
168
    ) {
169
        if ($shouldRejectCreation) {
170
            $this->expectException(InvalidArgumentException::class);
171
172
        } else {
173
            $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...
174
            $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...
175
        }
176
177
        $actualResult = $this->factory->createArgumentFromArray($source);
178
179
        $this->assertEquals($expectedResult, $actualResult);
180
    }
181
182
    public function dataProviderForShouldCreateCallArgumentFromArray(): array
183
    {
184
        return array(
185
            [null, [], true],
186
            [null, ['method' => 'foo'], true],
187
            [null, ['callee' => 'bar'], true],
188
            [new ArgumentCall(
189
                $this->createMock(ArgumentCompilerInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...mpilerInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...umentCompilerInterface>.

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...
190
                $this->createMock(Argument::class),
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...
191
                'someMethod',
192
                []
193
            ), [
194
                'callee' => 'some-callee',
195
                'method' => 'someMethod'
196
            ], false],
197
            [new ArgumentCall(
198
                $this->createMock(ArgumentCompilerInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...mpilerInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...umentCompilerInterface>.

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...
199
                $this->createMock(Argument::class),
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...
200
                'someMethod',
201
                []
202
            ), [
203
                'callee' => ['some-callee'],
204
                'method' => 'someMethod'
205
            ], false],
206
            [new ArgumentCall(
207
                $this->createMock(ArgumentCompilerInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...mpilerInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...umentCompilerInterface>.

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...
208
                $this->createMock(Argument::class),
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...
209
                'someMethod',
210
                [
211
                    $this->createMock(Argument::class),
212
                    $this->createMock(Argument::class)
213
                ]
214
            ), [
215
                'callee' => 'some-callee',
216
                'method' => 'someMethod',
217
                'arguments' => [
218
                    'foo',
219
                    'bar'
220
                ]
221
            ], false],
222
            [new ArgumentCall(
223
                $this->createMock(ArgumentCompilerInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...mpilerInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...umentCompilerInterface>.

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...
224
                $this->createMock(Argument::class),
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...
225
                'someMethod', [
226
                    $this->createMock(Argument::class),
227
                    $this->createMock(Argument::class)
228
                ]
229
            ), [
230
                'callee' => ['some-callee'],
231
                'method' => 'someMethod',
232
                'arguments' => [
233
                    'foo',
234
                    'bar'
235
                ]
236
            ], false],
237
            [new ArgumentCall(
238
                $this->createMock(ArgumentCompilerInterface::class),
0 ignored issues
show
Documentation introduced by
$this->createMock(\Addik...mpilerInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...umentCompilerInterface>.

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...
239
                $this->createMock(Argument::class),
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...
240
                'someMethod', [
241
                    $this->createMock(Argument::class),
242
                    $this->createMock(Argument::class)
243
                ]
244
            ), [
245
                'callee' => 'some-callee',
246
                'method' => 'someMethod',
247
                'arguments' => [
248
                    ['foo'],
249
                    ['bar']
250
                ]
251
            ], false],
252
        );
253
    }
254
255
}
256