|
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); |
|
|
|
|
|
|
42
|
|
|
$this->argumentFactory = $this->createMock(ArgumentFactory::class); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$this->factory = new ArgumentCallFactory($this->argumentCompiler, $this->argumentFactory); |
|
|
|
|
|
|
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)); |
|
|
|
|
|
|
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), |
|
|
|
|
|
|
121
|
|
|
$this->createMock(Argument::class), |
|
|
|
|
|
|
122
|
|
|
'someMethod', |
|
123
|
|
|
[] |
|
124
|
|
|
), |
|
125
|
|
|
'some-callee::someMethod', |
|
126
|
|
|
false |
|
127
|
|
|
], |
|
128
|
|
|
[ |
|
129
|
|
|
new ArgumentCall( |
|
130
|
|
|
$this->createMock(ArgumentCompilerInterface::class), |
|
|
|
|
|
|
131
|
|
|
$this->createMock(Argument::class), |
|
|
|
|
|
|
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( |
|
|
|
|
|
|
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)); |
|
|
|
|
|
|
174
|
|
|
$this->argumentFactory->method('createArgumentFromArray')->willReturn($this->createMock(Argument::class)); |
|
|
|
|
|
|
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), |
|
|
|
|
|
|
190
|
|
|
$this->createMock(Argument::class), |
|
|
|
|
|
|
191
|
|
|
'someMethod', |
|
192
|
|
|
[] |
|
193
|
|
|
), [ |
|
194
|
|
|
'callee' => 'some-callee', |
|
195
|
|
|
'method' => 'someMethod' |
|
196
|
|
|
], false], |
|
197
|
|
|
[new ArgumentCall( |
|
198
|
|
|
$this->createMock(ArgumentCompilerInterface::class), |
|
|
|
|
|
|
199
|
|
|
$this->createMock(Argument::class), |
|
|
|
|
|
|
200
|
|
|
'someMethod', |
|
201
|
|
|
[] |
|
202
|
|
|
), [ |
|
203
|
|
|
'callee' => ['some-callee'], |
|
204
|
|
|
'method' => 'someMethod' |
|
205
|
|
|
], false], |
|
206
|
|
|
[new ArgumentCall( |
|
207
|
|
|
$this->createMock(ArgumentCompilerInterface::class), |
|
|
|
|
|
|
208
|
|
|
$this->createMock(Argument::class), |
|
|
|
|
|
|
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), |
|
|
|
|
|
|
224
|
|
|
$this->createMock(Argument::class), |
|
|
|
|
|
|
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), |
|
|
|
|
|
|
239
|
|
|
$this->createMock(Argument::class), |
|
|
|
|
|
|
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
|
|
|
|
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..