|
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\AdditionalDataArgumentFactory; |
|
15
|
|
|
use Addiks\SymfonyGenerics\Arguments\ArgumentContextInterface; |
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use Addiks\SymfonyGenerics\Arguments\AdditionalDataArgument; |
|
18
|
|
|
|
|
19
|
|
|
final class AdditionalDataArgumentFactoryTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var AdditionalDataArgumentFactory |
|
24
|
|
|
*/ |
|
25
|
|
|
private $factory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ArgumentContextInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $context; |
|
31
|
|
|
|
|
32
|
|
|
public function setUp() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->context = $this->createMock(ArgumentContextInterface::class); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
$this->factory = new AdditionalDataArgumentFactory($this->context); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @test |
|
41
|
|
|
* @dataProvider dataproviderForShouldKnowWhatStringsToUnderstand |
|
42
|
|
|
*/ |
|
43
|
|
|
public function shouldKnowWhatStringsToUnderstand(bool $expectedResult, string $source) |
|
44
|
|
|
{ |
|
45
|
|
|
/** @var bool $actualResult */ |
|
46
|
|
|
$actualResult = $this->factory->understandsString($source); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function dataproviderForShouldKnowWhatStringsToUnderstand(): array |
|
52
|
|
|
{ |
|
53
|
|
|
return array( |
|
54
|
|
|
[true, '%foo%'], |
|
55
|
|
|
[true, '%bar%'], |
|
56
|
|
|
[true, '%baz%'], |
|
57
|
|
|
[false, 'foo'], |
|
58
|
|
|
[false, '%foo'], |
|
59
|
|
|
[false, 'foo%'], |
|
60
|
|
|
[false, '$foo'], |
|
61
|
|
|
[false, ''], |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @test |
|
67
|
|
|
* @dataProvider dataproviderForShouldKnowWhatArraysToUnderstand |
|
68
|
|
|
*/ |
|
69
|
|
|
public function shouldKnowWhatArraysToUnderstand(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 dataproviderForShouldKnowWhatArraysToUnderstand(): array |
|
78
|
|
|
{ |
|
79
|
|
|
return array( |
|
80
|
|
|
[true, ['type' => 'additional-data', 'key' => 'foo']], |
|
81
|
|
|
[true, ['type' => 'additional-data', 'key' => 'bar']], |
|
82
|
|
|
[true, ['type' => 'additional-data', 'key' => 'a']], |
|
83
|
|
|
[true, ['type' => 'additional-data', 'key' => 'aaaaaaaaaaaaaaaaaa']], |
|
84
|
|
|
[false, ['type' => 'additional-data', 'key' => null]], |
|
85
|
|
|
[false, ['type' => 'additional-NULL', 'key' => 'foo']], |
|
86
|
|
|
[false, ['type' => 'additional-data', 'ley' => 'foo']], |
|
87
|
|
|
[false, ['type' => 'additional-data']], |
|
88
|
|
|
[false, ['key' => 'foo']], |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @test |
|
94
|
|
|
* @dataProvider dataproviderForShouldCreateArgumentFromString |
|
95
|
|
|
*/ |
|
96
|
|
View Code Duplication |
public function shouldCreateArgumentFromString($expectedResult, string $source, bool $shouldReject) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
if ($shouldReject) { |
|
99
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** @var object $actualResult */ |
|
103
|
|
|
$actualResult = $this->factory->createArgumentFromString($source); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function dataproviderForShouldCreateArgumentFromString(): array |
|
109
|
|
|
{ |
|
110
|
|
|
$this->setUp(); |
|
111
|
|
|
|
|
112
|
|
|
return array( |
|
113
|
|
|
[new AdditionalDataArgument('foo', $this->context), '%foo%', false], |
|
114
|
|
|
[new AdditionalDataArgument('lorem-ipsum', $this->context), '%lorem-ipsum%', false], |
|
115
|
|
|
[new AdditionalDataArgument('a', $this->context), '%a%', false], |
|
116
|
|
|
[null, '%%', true], |
|
117
|
|
|
[null, 'foo%', true], |
|
118
|
|
|
[null, '%foo', true], |
|
119
|
|
|
[null, 'foo', true], |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @test |
|
125
|
|
|
* @dataProvider dataproviderForShouldCreateArgumentFromArray |
|
126
|
|
|
*/ |
|
127
|
|
View Code Duplication |
public function shouldCreateArgumentFromArray($expectedResult, array $source, bool $shouldReject) |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
|
|
if ($shouldReject) { |
|
130
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** @var object $actualResult */ |
|
134
|
|
|
$actualResult = $this->factory->createArgumentFromArray($source); |
|
135
|
|
|
|
|
136
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function dataproviderForShouldCreateArgumentFromArray(): array |
|
140
|
|
|
{ |
|
141
|
|
|
$this->setUp(); |
|
142
|
|
|
|
|
143
|
|
|
return array( |
|
144
|
|
|
[new AdditionalDataArgument('foo', $this->context), ['type' => 'additional-data', 'key' => 'foo'], false], |
|
145
|
|
|
[new AdditionalDataArgument('lorem-ipsum', $this->context), ['type' => 'additional-data', 'key' => 'lorem-ipsum'], false], |
|
146
|
|
|
[new AdditionalDataArgument('a', $this->context), ['type' => 'additional-data', 'key' => 'a'], false], |
|
147
|
|
|
[null, ['type' => 'additional-data', 'key' => null], true], |
|
148
|
|
|
[null, ['type' => 'additional-data', ], true], |
|
149
|
|
|
[null, ['key' => null], true], |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
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..