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\ArgumentFactoryLazyLoadProxy; |
15
|
|
|
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory; |
16
|
|
|
use Psr\Container\ContainerInterface; |
17
|
|
|
use Addiks\SymfonyGenerics\Arguments\Argument; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
final class ArgumentFactoryLazyLoadProxyTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ArgumentFactoryLazyLoadProxy |
25
|
|
|
*/ |
26
|
|
|
private $factory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ContainerInterface |
30
|
|
|
*/ |
31
|
|
|
private $container; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ArgumentFactory |
35
|
|
|
*/ |
36
|
|
|
private $loadedArgumentFactory; |
37
|
|
|
|
38
|
|
|
public function setUp() |
39
|
|
|
{ |
40
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
41
|
|
|
$this->loadedArgumentFactory = $this->createMock(ArgumentFactory::class); |
|
|
|
|
42
|
|
|
|
43
|
|
|
$this->factory = new ArgumentFactoryLazyLoadProxy($this->container, 'some-service-id'); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
* @dataProvider dataProviderShouldAskInnerFactoryIfUnderstandsString |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function shouldAskInnerFactoryIfUnderstandsString(bool $expectedResult, string $source) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
53
|
|
|
$this->equalTo('some-service-id') |
54
|
|
|
)->willReturn($this->loadedArgumentFactory); |
55
|
|
|
|
56
|
|
|
$this->loadedArgumentFactory->expects($this->once())->method('understandsString')->with( |
|
|
|
|
57
|
|
|
$this->equalTo($source) |
58
|
|
|
)->willReturn($expectedResult); |
59
|
|
|
|
60
|
|
|
/** @var bool $actualResult */ |
61
|
|
|
$actualResult = $this->factory->understandsString($source); |
62
|
|
|
|
63
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function dataProviderShouldAskInnerFactoryIfUnderstandsString() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return array( |
69
|
|
|
[true, 'foo'], |
70
|
|
|
[false, 'foo'], |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
* @dataProvider dataProviderShouldAskInnerFactoryIfUnderstandsArray |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function shouldAskInnerFactoryIfUnderstandsArray(bool $expectedResult, array $source) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
81
|
|
|
$this->equalTo('some-service-id') |
82
|
|
|
)->willReturn($this->loadedArgumentFactory); |
83
|
|
|
|
84
|
|
|
$this->loadedArgumentFactory->expects($this->once())->method('understandsArray')->with( |
|
|
|
|
85
|
|
|
$this->equalTo($source) |
86
|
|
|
)->willReturn($expectedResult); |
87
|
|
|
|
88
|
|
|
/** @var bool $actualResult */ |
89
|
|
|
$actualResult = $this->factory->understandsArray($source); |
90
|
|
|
|
91
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public function dataProviderShouldAskInnerFactoryIfUnderstandsArray() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
return array( |
97
|
|
|
[true, ['foo']], |
98
|
|
|
[false, ['foo']], |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @test |
104
|
|
|
* @dataProvider dataProviderShouldCreateArgumentFromStringUsingInnerFactory |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function shouldCreateArgumentFromStringUsingInnerFactory(Argument $expectedResult, string $source) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
109
|
|
|
$this->equalTo('some-service-id') |
110
|
|
|
)->willReturn($this->loadedArgumentFactory); |
111
|
|
|
|
112
|
|
|
$this->loadedArgumentFactory->expects($this->once())->method('createArgumentFromString')->with( |
|
|
|
|
113
|
|
|
$this->equalTo($source) |
114
|
|
|
)->willReturn($expectedResult); |
115
|
|
|
|
116
|
|
|
/** @var mixed $actualResult */ |
117
|
|
|
$actualResult = $this->factory->createArgumentFromString($source); |
118
|
|
|
|
119
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
public function dataProviderShouldCreateArgumentFromStringUsingInnerFactory() |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
return array( |
125
|
|
|
[$this->createMock(Argument::class), 'foo'], |
126
|
|
|
[$this->createMock(Argument::class), 'foo'], |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @test |
132
|
|
|
* @dataProvider dataProviderShouldCreateArgumentFromArrayUsingInnerFactory |
133
|
|
|
*/ |
134
|
|
View Code Duplication |
public function shouldCreateArgumentFromArrayUsingInnerFactory(Argument $expectedResult, array $source) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$this->container->expects($this->once())->method('get')->with( |
|
|
|
|
137
|
|
|
$this->equalTo('some-service-id') |
138
|
|
|
)->willReturn($this->loadedArgumentFactory); |
139
|
|
|
|
140
|
|
|
$this->loadedArgumentFactory->expects($this->once())->method('createArgumentFromArray')->with( |
|
|
|
|
141
|
|
|
$this->equalTo($source) |
142
|
|
|
)->willReturn($expectedResult); |
143
|
|
|
|
144
|
|
|
/** @var mixed $actualResult */ |
145
|
|
|
$actualResult = $this->factory->createArgumentFromArray($source); |
146
|
|
|
|
147
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
View Code Duplication |
public function dataProviderShouldCreateArgumentFromArrayUsingInnerFactory() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
return array( |
153
|
|
|
[$this->createMock(Argument::class), ['foo']], |
154
|
|
|
[$this->createMock(Argument::class), ['foo']], |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @test |
160
|
|
|
*/ |
161
|
|
|
public function shouldRejectInvalidService() |
162
|
|
|
{ |
163
|
|
|
$this->expectException(InvalidArgumentException::class); |
164
|
|
|
$this->expectExceptionMessage(sprintf( |
165
|
|
|
"Expected service 'some-service-id' to be instance of '%s'!", |
166
|
|
|
ArgumentFactory::class |
167
|
|
|
)); |
168
|
|
|
|
169
|
|
|
$container = $this->createMock(ContainerInterface::class); |
170
|
|
|
$container->expects($this->once())->method('get')->with( |
171
|
|
|
$this->equalTo('some-service-id') |
172
|
|
|
)->willReturn(null); |
173
|
|
|
|
174
|
|
|
$factory = new ArgumentFactoryLazyLoadProxy($container, 'some-service-id'); |
|
|
|
|
175
|
|
|
$factory->createArgumentFromArray([]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
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..