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\ServiceArgumentFactory; |
15
|
|
|
use Psr\Container\ContainerInterface; |
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use Addiks\SymfonyGenerics\Arguments\ServiceArgument; |
18
|
|
|
use Addiks\SymfonyGenerics\Arguments\LiteralArgument; |
19
|
|
|
|
20
|
|
|
final class ServiceArgumentFactoryTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ServiceArgumentFactory |
25
|
|
|
*/ |
26
|
|
|
private $factory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ContainerInterface |
30
|
|
|
*/ |
31
|
|
|
private $container; |
32
|
|
|
|
33
|
|
|
public function setUp() |
34
|
|
|
{ |
35
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$this->factory = new ServiceArgumentFactory($this->container); |
|
|
|
|
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, "@foo"], |
56
|
|
|
[true, "@f"], |
57
|
|
|
[false, "@"], |
58
|
|
|
[false, "foo"], |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
* @dataProvider dataProviderForShouldKnowIfUnderstandArray |
65
|
|
|
*/ |
66
|
|
|
public function shouldKnowIfUnderstandArray(bool $expectedResult, array $source) |
67
|
|
|
{ |
68
|
|
|
/** @var bool $actualResult */ |
69
|
|
|
$actualResult = $this->factory->understandsArray($source); |
70
|
|
|
|
71
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function dataProviderForShouldKnowIfUnderstandArray() |
75
|
|
|
{ |
76
|
|
|
return array( |
77
|
|
|
[true, ["service-id" => 'foo']], |
78
|
|
|
[false, ["foo"]], |
79
|
|
|
[false, ["service-id" => null]], |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
* @dataProvider dataProviderForShouldCreateArgumentFromString |
86
|
|
|
*/ |
87
|
|
|
public function shouldCreateArgumentFromString($expectedResult, string $source, bool $expectException) |
88
|
|
|
{ |
89
|
|
|
if ($expectException) { |
90
|
|
|
$this->expectException(InvalidArgumentException::class); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** @var bool $actualResult */ |
94
|
|
|
$actualResult = $this->factory->createArgumentFromString($source); |
95
|
|
|
|
96
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function dataProviderForShouldCreateArgumentFromString() |
100
|
|
|
{ |
101
|
|
|
$this->setUp(); |
102
|
|
|
|
103
|
|
|
return array( |
104
|
|
|
[new ServiceArgument($this->container, new LiteralArgument('foo')), "@foo", false], |
105
|
|
|
[new ServiceArgument($this->container, new LiteralArgument('f')), "@f", false], |
106
|
|
|
[null, "@", true], |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @test |
112
|
|
|
* @dataProvider dataProviderForShouldCreateArgumentFromArray |
113
|
|
|
*/ |
114
|
|
|
public function shouldCreateArgumentFromArray($expectedResult, array $source, bool $expectException) |
115
|
|
|
{ |
116
|
|
|
if ($expectException) { |
117
|
|
|
$this->expectException(InvalidArgumentException::class); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** @var bool $actualResult */ |
121
|
|
|
$actualResult = $this->factory->createArgumentFromArray($source); |
122
|
|
|
|
123
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function dataProviderForShouldCreateArgumentFromArray() |
127
|
|
|
{ |
128
|
|
|
$this->setUp(); |
129
|
|
|
|
130
|
|
|
return array( |
131
|
|
|
[new ServiceArgument($this->container, new LiteralArgument('foo')), ["service-id" => 'foo'], false], |
132
|
|
|
[new ServiceArgument($this->container, new LiteralArgument('f')), ["service-id" => 'f'], false], |
133
|
|
|
[null, ["service-id" => null], true], |
134
|
|
|
[null, ["service-id"], true], |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
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..