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); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$this->factory = new ArgumentCallFactory($this->argumentFactory); |
|
|
|
|
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
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @test |
66
|
|
|
* @dataProvider dataProviderForShouldKnowIfUnderstandArray |
67
|
|
|
*/ |
68
|
|
|
public function shouldKnowIfUnderstandArray(bool $expectedResult, array $source) |
69
|
|
|
{ |
70
|
|
|
/** @var bool $actualResult */ |
71
|
|
|
$actualResult = $this->factory->understandsArray($source); |
72
|
|
|
|
73
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function dataProviderForShouldKnowIfUnderstandArray() |
77
|
|
|
{ |
78
|
|
|
return array( |
79
|
|
|
[true, ['callee' => 'foo', 'method' => 'bar']], |
80
|
|
|
[true, ['callee' => 'foo', 'method' => 'bar', 'arguments' => []]], |
81
|
|
|
[false, ['method' => 'bar']], |
82
|
|
|
[false, ['callee' => 'foo']], |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @test |
88
|
|
|
* @dataProvider dataProviderForShouldCreateCallArgumentFromString |
89
|
|
|
*/ |
90
|
|
|
public function shouldCreateCallArgumentFromString( |
91
|
|
|
?ArgumentCall $expectedResult, |
92
|
|
|
string $source, |
93
|
|
|
bool $shouldRejectCreation |
94
|
|
|
) { |
95
|
|
|
if ($shouldRejectCreation) { |
96
|
|
|
$this->expectException(InvalidArgumentException::class); |
97
|
|
|
|
98
|
|
|
} else { |
99
|
|
|
$this->argumentFactory->method('createArgumentFromString')->willReturn($this->createMock(Argument::class)); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$actualResult = $this->factory->createArgumentFromString($source); |
103
|
|
|
|
104
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function dataProviderForShouldCreateCallArgumentFromString(): array |
108
|
|
|
{ |
109
|
|
|
return array( |
110
|
|
|
[new ArgumentCall($this->createMock(Argument::class), 'someMethod', []), 'some-callee::someMethod', false], |
|
|
|
|
111
|
|
|
[new ArgumentCall($this->createMock(Argument::class), 'someMethod', [ |
|
|
|
|
112
|
|
|
$this->createMock(Argument::class), |
113
|
|
|
$this->createMock(Argument::class) |
114
|
|
|
]), 'some-callee::someMethod(a, b)', false], |
115
|
|
|
[null, 'a::', true], |
116
|
|
|
[null, '::b', true], |
117
|
|
|
[null, '::', true], |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @test |
123
|
|
|
* @dataProvider dataProviderForShouldCreateCallArgumentFromArray |
124
|
|
|
*/ |
125
|
|
|
public function shouldCreateCallArgumentFromArray( |
126
|
|
|
$expectedResult, |
127
|
|
|
array $source, |
128
|
|
|
bool $shouldRejectCreation |
129
|
|
|
) { |
130
|
|
|
if ($shouldRejectCreation) { |
131
|
|
|
$this->expectException(InvalidArgumentException::class); |
132
|
|
|
|
133
|
|
|
} else { |
134
|
|
|
$this->argumentFactory->method('createArgumentFromString')->willReturn($this->createMock(Argument::class)); |
|
|
|
|
135
|
|
|
$this->argumentFactory->method('createArgumentFromArray')->willReturn($this->createMock(Argument::class)); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$actualResult = $this->factory->createArgumentFromArray($source); |
139
|
|
|
|
140
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function dataProviderForShouldCreateCallArgumentFromArray(): array |
144
|
|
|
{ |
145
|
|
|
return array( |
146
|
|
|
[null, [], true], |
147
|
|
|
[null, ['method' => 'foo'], true], |
148
|
|
|
[null, ['callee' => 'bar'], true], |
149
|
|
|
[new ArgumentCall($this->createMock(Argument::class), 'someMethod', []), [ |
|
|
|
|
150
|
|
|
'callee' => 'some-callee', |
151
|
|
|
'method' => 'someMethod' |
152
|
|
|
], false], |
153
|
|
|
[new ArgumentCall($this->createMock(Argument::class), 'someMethod', []), [ |
|
|
|
|
154
|
|
|
'callee' => ['some-callee'], |
155
|
|
|
'method' => 'someMethod' |
156
|
|
|
], false], |
157
|
|
|
[new ArgumentCall($this->createMock(Argument::class), 'someMethod', [ |
|
|
|
|
158
|
|
|
$this->createMock(Argument::class), |
159
|
|
|
$this->createMock(Argument::class) |
160
|
|
|
]), [ |
161
|
|
|
'callee' => 'some-callee', |
162
|
|
|
'method' => 'someMethod', |
163
|
|
|
'arguments' => [ |
164
|
|
|
'foo', |
165
|
|
|
'bar' |
166
|
|
|
] |
167
|
|
|
], false], |
168
|
|
|
[new ArgumentCall($this->createMock(Argument::class), 'someMethod', [ |
|
|
|
|
169
|
|
|
$this->createMock(Argument::class), |
170
|
|
|
$this->createMock(Argument::class) |
171
|
|
|
]), [ |
172
|
|
|
'callee' => ['some-callee'], |
173
|
|
|
'method' => 'someMethod', |
174
|
|
|
'arguments' => [ |
175
|
|
|
'foo', |
176
|
|
|
'bar' |
177
|
|
|
] |
178
|
|
|
], false], |
179
|
|
|
[new ArgumentCall($this->createMock(Argument::class), 'someMethod', [ |
|
|
|
|
180
|
|
|
$this->createMock(Argument::class), |
181
|
|
|
$this->createMock(Argument::class) |
182
|
|
|
]), [ |
183
|
|
|
'callee' => 'some-callee', |
184
|
|
|
'method' => 'someMethod', |
185
|
|
|
'arguments' => [ |
186
|
|
|
['foo'], |
187
|
|
|
['bar'] |
188
|
|
|
] |
189
|
|
|
], false], |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
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..