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

shouldCreateArgumentFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 9.28
c 0
b 0
f 0
cc 2
nc 2
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Arguments\ArgumentFactory;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactoryAggregate;
15
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory;
16
use stdClass;
17
use InvalidArgumentException;
18
use Addiks\SymfonyGenerics\Arguments\Argument;
19
use Addiks\SymfonyGenerics\Arguments\LiteralArgument;
20
use Addiks\SymfonyGenerics\Arguments\EntityArgument;
21
22
final class ArgumentFactoryAggregateTest extends TestCase
23
{
24
25
    /**
26
     * @var ArgumentFactoryAggregate
27
     */
28
    private $factoryAggregate;
29
30
    /**
31
     * @var ArgumentFactory
32
     */
33
    private $innerArgumentFactoryA;
34
35
    /**
36
     * @var ArgumentFactory
37
     */
38
    private $innerArgumentFactoryB;
39
40
    /**
41
     * @var ArgumentFactory
42
     */
43
    private $innerArgumentFactoryC;
44
45
    public function setUp()
46
    {
47
        $this->innerArgumentFactoryA = $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 $innerArgumentFactoryA.

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...
48
        $this->innerArgumentFactoryB = $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 $innerArgumentFactoryB.

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...
49
        $this->innerArgumentFactoryC = $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 $innerArgumentFactoryC.

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...
50
51
        $this->factoryAggregate = new ArgumentFactoryAggregate([
52
            $this->innerArgumentFactoryA,
53
            $this->innerArgumentFactoryB,
54
            $this->innerArgumentFactoryC,
55
        ]);
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function shouldRejectNonArgumentFactoryInConstructor()
62
    {
63
        $this->expectException(InvalidArgumentException::class);
64
65
        new ArgumentFactoryAggregate([
66
            $this->innerArgumentFactoryA,
67
            $this->innerArgumentFactoryB,
68
            $this->createMock(stdClass::class),
69
        ]);
70
    }
71
72
    /**
73
     * @test
74
     * @dataProvider dataProviderForShouldKnowWhenToUnderstandString
75
     */
76
    public function shouldKnowWhenToUnderstandString(bool $expectedResult, string $source, bool $a, bool $b, bool $c)
77
    {
78
        $this->innerArgumentFactoryA->expects($this->any())->method('understandsString')->with(
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...
79
            $this->equalTo($source)
80
        )->willReturn($a);
81
        $this->innerArgumentFactoryB->expects($this->any())->method('understandsString')->with(
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...
82
            $this->equalTo($source)
83
        )->willReturn($b);
84
        $this->innerArgumentFactoryC->expects($this->any())->method('understandsString')->with(
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...
85
            $this->equalTo($source)
86
        )->willReturn($c);
87
88
        /** @var bool $actualResult */
89
        $actualResult = $this->factoryAggregate->understandsString($source);
90
91
        $this->assertEquals($expectedResult, $actualResult);
92
    }
93
94
    public function dataProviderForShouldKnowWhenToUnderstandString()
95
    {
96
        return array(
97
            [false, "foo", false, false, false],
98
            [true,  "foo", true,  false, false],
99
            [true,  "foo", false, true,  false],
100
            [true,  "foo", false, false, true],
101
        );
102
    }
103
104
    /**
105
     * @test
106
     * @dataProvider dataProviderForShouldKnowWhenToUnderstandArray
107
     */
108
    public function shouldKnowWhenToUnderstandArray(bool $expectedResult, array $source, bool $a, bool $b, bool $c)
109
    {
110
        $this->innerArgumentFactoryA->expects($this->any())->method('understandsArray')->with(
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...
111
            $this->equalTo($source)
112
        )->willReturn($a);
113
        $this->innerArgumentFactoryB->expects($this->any())->method('understandsArray')->with(
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...
114
            $this->equalTo($source)
115
        )->willReturn($b);
116
        $this->innerArgumentFactoryC->expects($this->any())->method('understandsArray')->with(
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...
117
            $this->equalTo($source)
118
        )->willReturn($c);
119
120
        /** @var bool $actualResult */
121
        $actualResult = $this->factoryAggregate->understandsArray($source);
122
123
        $this->assertEquals($expectedResult, $actualResult);
124
    }
125
126
    public function dataProviderForShouldKnowWhenToUnderstandArray()
127
    {
128
        return array(
129
            [false, ["foo"], false, false, false],
130
            [true,  ["foo"], true,  false, false],
131
            [true,  ["foo"], false, true,  false],
132
            [true,  ["foo"], false, false, true],
133
        );
134
    }
135
136
    /**
137
     * @test
138
     * @dataProvider dataProviderForShouldCreateArgumentFromString
139
     */
140
    public function shouldCreateArgumentFromString(
141
        $expectedResult,
142
        string $source,
143
        bool $au,
144
        ?Argument $ar,
145
        bool $bu,
146
        ?Argument $br,
147
        bool $cu,
148
        ?Argument $cr,
149
        bool $expectException
150
    ) {
151
        if ($expectException) {
152
            $this->expectException(InvalidArgumentException::class);
153
        }
154
155
        $this->innerArgumentFactoryA->expects($this->any())->method('understandsString')->with(
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...
156
            $this->equalTo($source)
157
        )->willReturn($au);
158
        $this->innerArgumentFactoryB->expects($this->any())->method('understandsString')->with(
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...
159
            $this->equalTo($source)
160
        )->willReturn($bu);
161
        $this->innerArgumentFactoryC->expects($this->any())->method('understandsString')->with(
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...
162
            $this->equalTo($source)
163
        )->willReturn($cu);
164
165
        $this->innerArgumentFactoryA->expects($this->any())->method('createArgumentFromString')->with(
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...
166
            $this->equalTo($source)
167
        )->willReturn($ar);
168
        $this->innerArgumentFactoryB->expects($this->any())->method('createArgumentFromString')->with(
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...
169
            $this->equalTo($source)
170
        )->willReturn($br);
171
        $this->innerArgumentFactoryC->expects($this->any())->method('createArgumentFromString')->with(
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...
172
            $this->equalTo($source)
173
        )->willReturn($cr);
174
175
        /** @var mixed $actualResult */
176
        $actualResult = $this->factoryAggregate->createArgumentFromString($source);
177
178
        $this->assertEquals($expectedResult, $actualResult);
179
    }
180
181
    public function dataProviderForShouldCreateArgumentFromString()
182
    {
183
        return array(
184
            [new LiteralArgument(true), 'foo', true, new LiteralArgument(true), false, null, false, null, false],
185
            [new LiteralArgument(true), 'foo', false, null, true, new LiteralArgument(true), true, new LiteralArgument(false), false],
186
            [new LiteralArgument(true), 'foo', false, null, false, null, true, new LiteralArgument(true), false],
187
            [null, 'foo', false, null, false, null, false, null, true],
188
        );
189
    }
190
191
    /**
192
     * @test
193
     * @dataProvider dataProviderForShouldCreateArgumentFromArray
194
     */
195
    public function shouldCreateArgumentFromArray(
196
        $expectedResult,
197
        array $source,
198
        bool $au,
199
        ?Argument $ar,
200
        bool $bu,
201
        ?Argument $br,
202
        bool $cu,
203
        ?Argument $cr,
204
        bool $expectException
205
    ) {
206
        if ($expectException) {
207
            $this->expectException(InvalidArgumentException::class);
208
        }
209
210
        $this->innerArgumentFactoryA->expects($this->any())->method('understandsArray')->with(
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...
211
            $this->equalTo($source)
212
        )->willReturn($au);
213
        $this->innerArgumentFactoryB->expects($this->any())->method('understandsArray')->with(
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...
214
            $this->equalTo($source)
215
        )->willReturn($bu);
216
        $this->innerArgumentFactoryC->expects($this->any())->method('understandsArray')->with(
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...
217
            $this->equalTo($source)
218
        )->willReturn($cu);
219
220
        $this->innerArgumentFactoryA->expects($this->any())->method('createArgumentFromArray')->with(
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...
221
            $this->equalTo($source)
222
        )->willReturn($ar);
223
        $this->innerArgumentFactoryB->expects($this->any())->method('createArgumentFromArray')->with(
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...
224
            $this->equalTo($source)
225
        )->willReturn($br);
226
        $this->innerArgumentFactoryC->expects($this->any())->method('createArgumentFromArray')->with(
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...
227
            $this->equalTo($source)
228
        )->willReturn($cr);
229
230
        /** @var mixed $actualResult */
231
        $actualResult = $this->factoryAggregate->createArgumentFromArray($source);
232
233
        $this->assertEquals($expectedResult, $actualResult);
234
    }
235
236
    public function dataProviderForShouldCreateArgumentFromArray()
237
    {
238
        return array(
239
            [new LiteralArgument(true), ['foo'], true, new LiteralArgument(true), false, null, false, null, false],
240
            [new LiteralArgument(true), ['foo'], false, null, true, new LiteralArgument(true), true, new LiteralArgument(false), false],
241
            [new LiteralArgument(true), ['foo'], false, null, false, null, true, new LiteralArgument(true), false],
242
            [null, ['foo'], false, null, false, null, false, null, true],
243
        );
244
    }
245
246
    /**
247
     * @test
248
     */
249
    public function shouldNotUnderstandAnythingWihtoutInnerFactories()
250
    {
251
        $this->assertFalse((new ArgumentFactoryAggregate([]))->understandsString(""));
252
        $this->assertFalse((new ArgumentFactoryAggregate([]))->understandsArray([]));
253
    }
254
255
    /**
256
     * @test
257
     */
258
    public function shouldRejectConstructorBeingCalledTwice()
259
    {
260
        $this->expectException(InvalidArgumentException::class);
261
        $this->factoryAggregate->__construct([]);
262
    }
263
264
}
265