|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the puli/discovery package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Puli\Discovery\Test; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
|
15
|
|
|
use PHPUnit_Framework_TestCase; |
|
16
|
|
|
use Puli\Discovery\Api\Binding\Binding; |
|
17
|
|
|
use Puli\Discovery\Api\Binding\Initializer\BindingInitializer; |
|
18
|
|
|
use Puli\Discovery\Api\Discovery; |
|
19
|
|
|
use Puli\Discovery\Api\Type\BindingParameter; |
|
20
|
|
|
use Puli\Discovery\Api\Type\BindingType; |
|
21
|
|
|
use Puli\Discovery\Binding\ClassBinding; |
|
22
|
|
|
use Puli\Discovery\Test\Fixtures\Bar; |
|
23
|
|
|
use Puli\Discovery\Test\Fixtures\Foo; |
|
24
|
|
|
use Puli\Discovery\Test\Fixtures\StringBinding; |
|
25
|
|
|
use stdClass; |
|
26
|
|
|
use Webmozart\Expression\Expr; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @since 1.0 |
|
30
|
|
|
* |
|
31
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract class AbstractDiscoveryTest extends PHPUnit_Framework_TestCase |
|
34
|
|
|
{ |
|
35
|
|
|
const STRING_BINDING = 'Puli\Discovery\Test\Fixtures\StringBinding'; |
|
36
|
|
|
|
|
37
|
|
|
const CLASS_BINDING = 'Puli\Discovery\Binding\ClassBinding'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject|BindingInitializer |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $initializer; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param BindingType[] $types |
|
46
|
|
|
* @param Binding[] $bindings |
|
47
|
|
|
* |
|
48
|
|
|
* @return Discovery |
|
49
|
|
|
*/ |
|
50
|
|
|
abstract protected function createLoadedDiscovery(array $types = array(), array $bindings = array(), array $initializers = array()); |
|
51
|
|
|
|
|
52
|
233 |
|
protected function setUp() |
|
53
|
|
|
{ |
|
54
|
233 |
|
$this->initializer = $this->getMock('Puli\Discovery\Api\Binding\Initializer\BindingInitializer'); |
|
55
|
233 |
|
} |
|
56
|
|
|
|
|
57
|
5 |
|
public function testFindBindings() |
|
58
|
|
|
{ |
|
59
|
5 |
|
$type1 = new BindingType(Foo::clazz, self::STRING_BINDING); |
|
60
|
5 |
|
$type2 = new BindingType(Bar::clazz, self::CLASS_BINDING); |
|
61
|
5 |
|
$binding1 = new StringBinding('string1', Foo::clazz); |
|
62
|
5 |
|
$binding2 = new StringBinding('string2', Foo::clazz); |
|
63
|
5 |
|
$binding3 = new ClassBinding(__CLASS__, Bar::clazz); |
|
64
|
|
|
|
|
65
|
5 |
|
$discovery = $this->createLoadedDiscovery(array($type1, $type2), array($binding1, $binding2, $binding3)); |
|
66
|
|
|
|
|
67
|
5 |
|
$this->assertEquals(array($binding1, $binding2), $discovery->findBindings(Foo::clazz)); |
|
68
|
5 |
|
$this->assertEquals(array($binding3), $discovery->findBindings(Bar::clazz)); |
|
69
|
5 |
|
} |
|
70
|
|
|
|
|
71
|
5 |
|
public function testFindBindingsWithExpression() |
|
72
|
|
|
{ |
|
73
|
5 |
|
$type1 = new BindingType(Foo::clazz, self::STRING_BINDING, array( |
|
74
|
5 |
|
new BindingParameter('param1'), |
|
75
|
5 |
|
new BindingParameter('param2'), |
|
76
|
|
|
)); |
|
77
|
5 |
|
$type2 = new BindingType(Bar::clazz, self::CLASS_BINDING, array( |
|
78
|
5 |
|
new BindingParameter('param1'), |
|
79
|
5 |
|
new BindingParameter('param2'), |
|
80
|
|
|
)); |
|
81
|
5 |
|
$binding1 = new StringBinding('string1', Foo::clazz, array('param1' => 'value1', 'param2' => 'value2')); |
|
82
|
5 |
|
$binding2 = new StringBinding('string2', Foo::clazz, array('param1' => 'value1')); |
|
83
|
5 |
|
$binding3 = new ClassBinding(__CLASS__, Bar::clazz, array('param1' => 'value1', 'param2' => 'value2')); |
|
84
|
|
|
|
|
85
|
5 |
|
$discovery = $this->createLoadedDiscovery(array($type1, $type2), array($binding1, $binding2, $binding3)); |
|
86
|
|
|
|
|
87
|
5 |
|
$exprParam1 = Expr::method('getParameterValue', 'param1', Expr::same('value1')); |
|
88
|
5 |
|
$exprParam2 = Expr::method('getParameterValue', 'param1', Expr::same('value1')) |
|
89
|
5 |
|
->andMethod('getParameterValue', 'param2', Expr::same('value2')); |
|
90
|
|
|
|
|
91
|
5 |
|
$this->assertEquals(array($binding1, $binding2), $discovery->findBindings(Foo::clazz, $exprParam1)); |
|
92
|
5 |
|
$this->assertEquals(array($binding1), $discovery->findBindings(Foo::clazz, $exprParam2)); |
|
93
|
5 |
|
$this->assertEquals(array($binding3), $discovery->findBindings(Bar::clazz, $exprParam2)); |
|
94
|
5 |
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
public function testFindBindingsReturnsEmptyArrayIfUnknownType() |
|
97
|
|
|
{ |
|
98
|
5 |
|
$discovery = $this->createLoadedDiscovery(); |
|
99
|
|
|
|
|
100
|
5 |
|
$this->assertEquals(array(), $discovery->findBindings(Foo::clazz)); |
|
101
|
5 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @expectedException \InvalidArgumentException |
|
105
|
|
|
* @expectedExceptionMessage stdClass |
|
106
|
|
|
*/ |
|
107
|
5 |
|
public function testFindBindingsFailsIfInvalidType() |
|
108
|
|
|
{ |
|
109
|
5 |
|
$discovery = $this->createLoadedDiscovery(); |
|
110
|
5 |
|
$discovery->findBindings(new stdClass()); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
5 |
|
public function testGetBindings() |
|
114
|
|
|
{ |
|
115
|
5 |
|
$type1 = new BindingType(Foo::clazz, self::STRING_BINDING); |
|
116
|
5 |
|
$type2 = new BindingType(Bar::clazz, self::CLASS_BINDING); |
|
117
|
5 |
|
$binding1 = new StringBinding('string1', Foo::clazz); |
|
118
|
5 |
|
$binding2 = new StringBinding('string2', Foo::clazz); |
|
119
|
5 |
|
$binding3 = new ClassBinding(__CLASS__, Bar::clazz); |
|
120
|
|
|
|
|
121
|
5 |
|
$discovery = $this->createLoadedDiscovery(array($type1, $type2), array($binding1, $binding2, $binding3)); |
|
122
|
|
|
|
|
123
|
5 |
|
$this->assertEquals(array($binding1, $binding2, $binding3), $discovery->getBindings()); |
|
124
|
5 |
|
} |
|
125
|
|
|
|
|
126
|
5 |
|
public function testGetNoBindings() |
|
127
|
|
|
{ |
|
128
|
5 |
|
$discovery = $this->createLoadedDiscovery(); |
|
129
|
|
|
|
|
130
|
5 |
|
$this->assertEquals(array(), $discovery->getBindings()); |
|
131
|
5 |
|
} |
|
132
|
|
|
|
|
133
|
5 |
|
public function testHasBindings() |
|
134
|
|
|
{ |
|
135
|
5 |
|
$type = new BindingType(Foo::clazz, self::STRING_BINDING); |
|
136
|
5 |
|
$binding = new StringBinding('string1', Foo::clazz); |
|
137
|
|
|
|
|
138
|
5 |
|
$discovery = $this->createLoadedDiscovery(array($type), array($binding)); |
|
139
|
|
|
|
|
140
|
5 |
|
$this->assertTrue($discovery->hasBindings()); |
|
141
|
5 |
|
} |
|
142
|
|
|
|
|
143
|
5 |
|
public function testHasNoBindings() |
|
144
|
|
|
{ |
|
145
|
5 |
|
$type = new BindingType(Foo::clazz, self::STRING_BINDING); |
|
146
|
|
|
|
|
147
|
5 |
|
$discovery = $this->createLoadedDiscovery(array($type)); |
|
148
|
|
|
|
|
149
|
5 |
|
$this->assertFalse($discovery->hasBindings()); |
|
150
|
5 |
|
} |
|
151
|
|
|
|
|
152
|
5 |
|
public function testHasBindingsWithType() |
|
153
|
|
|
{ |
|
154
|
5 |
|
$type1 = new BindingType(Foo::clazz, self::STRING_BINDING); |
|
155
|
5 |
|
$type2 = new BindingType(Bar::clazz, self::STRING_BINDING); |
|
156
|
5 |
|
$binding = new StringBinding('string1', Foo::clazz); |
|
157
|
|
|
|
|
158
|
5 |
|
$discovery = $this->createLoadedDiscovery(array($type1, $type2), array($binding)); |
|
159
|
|
|
|
|
160
|
5 |
|
$this->assertTrue($discovery->hasBindings(Foo::clazz)); |
|
161
|
5 |
|
$this->assertFalse($discovery->hasBindings(Bar::clazz)); |
|
162
|
5 |
|
} |
|
163
|
|
|
|
|
164
|
5 |
|
public function testHasBindingsWithTypeReturnsFalseIfUnknownType() |
|
165
|
|
|
{ |
|
166
|
5 |
|
$discovery = $this->createLoadedDiscovery(); |
|
167
|
5 |
|
$this->assertFalse($discovery->hasBindings(Foo::clazz)); |
|
168
|
5 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @expectedException \InvalidArgumentException |
|
172
|
|
|
* @expectedExceptionMessage stdClass |
|
173
|
|
|
*/ |
|
174
|
5 |
|
public function testHasBindingsWithTypeFailsIfInvalidType() |
|
175
|
|
|
{ |
|
176
|
5 |
|
$discovery = $this->createLoadedDiscovery(); |
|
177
|
5 |
|
$discovery->hasBindings(new stdClass()); |
|
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
5 |
|
public function testHasBindingsWithTypeAndExpression() |
|
181
|
|
|
{ |
|
182
|
5 |
|
$type1 = new BindingType(Foo::clazz, self::STRING_BINDING, array( |
|
183
|
5 |
|
new BindingParameter('param'), |
|
184
|
|
|
)); |
|
185
|
5 |
|
$type2 = new BindingType(Bar::clazz, self::STRING_BINDING); |
|
186
|
5 |
|
$binding = new StringBinding('string1', Foo::clazz, array('param' => 'foo')); |
|
187
|
|
|
|
|
188
|
5 |
|
$discovery = $this->createLoadedDiscovery(array($type1, $type2), array($binding)); |
|
189
|
|
|
|
|
190
|
5 |
|
$this->assertTrue($discovery->hasBindings(Foo::clazz, Expr::method('getParameterValue', 'param', Expr::same('foo')))); |
|
191
|
5 |
|
$this->assertFalse($discovery->hasBindings(Foo::clazz, Expr::method('getParameterValue', 'param', Expr::same('bar')))); |
|
192
|
5 |
|
} |
|
193
|
|
|
|
|
194
|
5 |
|
public function testHasBindingsWithTypeAndExpressionReturnsFalseIfUnknownType() |
|
195
|
|
|
{ |
|
196
|
5 |
|
$discovery = $this->createLoadedDiscovery(); |
|
197
|
5 |
|
$this->assertFalse($discovery->hasBindings(Foo::clazz, Expr::method('getParameterValue', 'param', Expr::same('foo')))); |
|
198
|
5 |
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @expectedException \InvalidArgumentException |
|
202
|
|
|
* @expectedExceptionMessage stdClass |
|
203
|
|
|
*/ |
|
204
|
5 |
|
public function testHasBindingsWithTypeAndParametersFailsIfInvalidType() |
|
205
|
|
|
{ |
|
206
|
5 |
|
$discovery = $this->createLoadedDiscovery(); |
|
207
|
5 |
|
$discovery->hasBindings(new stdClass(), Expr::method('getParameterValue', 'param', Expr::same('foo'))); |
|
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
5 |
|
public function testGetBindingType() |
|
211
|
|
|
{ |
|
212
|
5 |
|
$type1 = new BindingType(Foo::clazz, self::STRING_BINDING); |
|
213
|
5 |
|
$type2 = new BindingType(Bar::clazz, self::STRING_BINDING); |
|
214
|
|
|
|
|
215
|
5 |
|
$discovery = $this->createLoadedDiscovery(array($type1, $type2)); |
|
216
|
|
|
|
|
217
|
5 |
|
$this->assertEquals($type1, $discovery->getBindingType(Foo::clazz)); |
|
218
|
5 |
|
$this->assertEquals($type2, $discovery->getBindingType(Bar::clazz)); |
|
219
|
5 |
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @expectedException \Puli\Discovery\Api\Type\NoSuchTypeException |
|
223
|
|
|
* @expectedExceptionMessage Foo |
|
224
|
|
|
*/ |
|
225
|
5 |
|
public function testGetBindingTypeFailsIfUnknownType() |
|
226
|
|
|
{ |
|
227
|
5 |
|
$discovery = $this->createLoadedDiscovery(); |
|
228
|
|
|
|
|
229
|
5 |
|
$discovery->getBindingType(Foo::clazz); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @expectedException \InvalidArgumentException |
|
234
|
|
|
* @expectedExceptionMessage stdClass |
|
235
|
|
|
*/ |
|
236
|
5 |
|
public function testGetBindingTypeFailsIfInvalidType() |
|
237
|
|
|
{ |
|
238
|
5 |
|
$discovery = $this->createLoadedDiscovery(); |
|
239
|
5 |
|
$discovery->getBindingType(new stdClass()); |
|
|
|
|
|
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
5 |
|
public function testGetBindingTypes() |
|
243
|
|
|
{ |
|
244
|
5 |
|
$type1 = new BindingType(Foo::clazz, self::STRING_BINDING); |
|
245
|
5 |
|
$type2 = new BindingType(Bar::clazz, self::STRING_BINDING); |
|
246
|
|
|
|
|
247
|
5 |
|
$discovery = $this->createLoadedDiscovery(array($type1, $type2)); |
|
248
|
|
|
|
|
249
|
5 |
|
$this->assertEquals(array($type1, $type2), $discovery->getBindingTypes()); |
|
250
|
5 |
|
} |
|
251
|
|
|
|
|
252
|
5 |
|
public function testHasBindingType() |
|
253
|
|
|
{ |
|
254
|
5 |
|
$type = new BindingType(Foo::clazz, self::STRING_BINDING); |
|
255
|
|
|
|
|
256
|
5 |
|
$discovery = $this->createLoadedDiscovery(array($type)); |
|
257
|
|
|
|
|
258
|
5 |
|
$this->assertTrue($discovery->hasBindingType(Foo::clazz)); |
|
259
|
5 |
|
$this->assertFalse($discovery->hasBindingType(Bar::clazz)); |
|
260
|
5 |
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @expectedException \InvalidArgumentException |
|
264
|
|
|
* @expectedExceptionMessage stdClass |
|
265
|
|
|
*/ |
|
266
|
5 |
|
public function testHasBindingTypeFailsIfInvalidType() |
|
267
|
|
|
{ |
|
268
|
5 |
|
$discovery = $this->createLoadedDiscovery(); |
|
269
|
5 |
|
$discovery->hasBindingType(new stdClass()); |
|
|
|
|
|
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: