|
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_TestCase; |
|
15
|
|
|
use Puli\Discovery\Api\Type\BindingParameter; |
|
16
|
|
|
use Puli\Discovery\Api\Type\BindingType; |
|
17
|
|
|
use Puli\Discovery\Binding\AbstractBinding; |
|
18
|
|
|
use Puli\Discovery\Test\Fixtures\Bar; |
|
19
|
|
|
use Puli\Discovery\Test\Fixtures\Foo; |
|
20
|
|
|
use stdClass; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @since 1.0 |
|
24
|
|
|
* |
|
25
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class AbstractBindingTest extends PHPUnit_Framework_TestCase |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $typeName |
|
31
|
|
|
* @param array $parameterValues |
|
32
|
|
|
* |
|
33
|
|
|
* @return AbstractBinding |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract protected function createBinding($typeName, array $parameterValues = array()); |
|
36
|
|
|
|
|
37
|
1 |
|
public function testCreate() |
|
38
|
|
|
{ |
|
39
|
1 |
|
$binding = $this->createBinding(Foo::clazz); |
|
40
|
|
|
|
|
41
|
1 |
|
$this->assertSame(Foo::clazz, $binding->getTypeName()); |
|
42
|
1 |
|
$this->assertSame(array(), $binding->getParameterValues()); |
|
43
|
1 |
|
$this->assertFalse($binding->hasParameterValue('param')); |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @expectedException \InvalidArgumentException |
|
48
|
|
|
* @expectedExceptionMessage stdClass |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function testCreateFailsIfInvalidType() |
|
51
|
|
|
{ |
|
52
|
1 |
|
$this->createBinding(new stdClass()); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
public function testCreateWithParameters() |
|
56
|
|
|
{ |
|
57
|
1 |
|
$binding = $this->createBinding(Foo::clazz, array( |
|
58
|
1 |
|
'param1' => 'value', |
|
59
|
|
|
)); |
|
60
|
|
|
|
|
61
|
1 |
|
$this->assertSame(Foo::clazz, $binding->getTypeName()); |
|
62
|
1 |
|
$this->assertSame(array( |
|
63
|
1 |
|
'param1' => 'value', |
|
64
|
1 |
|
), $binding->getParameterValues()); |
|
65
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param1')); |
|
66
|
1 |
|
$this->assertFalse($binding->hasParameterValue('foo')); |
|
67
|
1 |
|
$this->assertSame('value', $binding->getParameterValue('param1')); |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
public function testInitialize() |
|
71
|
|
|
{ |
|
72
|
1 |
|
$binding = $this->createBinding(Foo::clazz); |
|
73
|
1 |
|
$type = new BindingType(Foo::clazz, get_class($binding)); |
|
74
|
|
|
|
|
75
|
1 |
|
$this->assertFalse($binding->isInitialized()); |
|
76
|
|
|
|
|
77
|
1 |
|
$binding->initialize($type); |
|
78
|
|
|
|
|
79
|
1 |
|
$this->assertSame($type, $binding->getType()); |
|
80
|
1 |
|
$this->assertTrue($binding->isInitialized()); |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
public function testInitializeWithParameters() |
|
84
|
|
|
{ |
|
85
|
1 |
|
$binding = $this->createBinding(Foo::clazz, array( |
|
86
|
1 |
|
'param1' => 'value', |
|
87
|
|
|
)); |
|
88
|
|
|
|
|
89
|
1 |
|
$type = new BindingType(Foo::clazz, get_class($binding), array( |
|
90
|
1 |
|
new BindingParameter('param1'), |
|
91
|
1 |
|
new BindingParameter('param2'), |
|
92
|
|
|
)); |
|
93
|
|
|
|
|
94
|
1 |
|
$this->assertSame(array('param1' => 'value'), $binding->getParameterValues()); |
|
95
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param1')); |
|
96
|
1 |
|
$this->assertFalse($binding->hasParameterValue('param2')); |
|
97
|
1 |
|
$this->assertFalse($binding->hasParameterValue('foo')); |
|
98
|
1 |
|
$this->assertSame('value', $binding->getParameterValue('param1')); |
|
99
|
|
|
|
|
100
|
1 |
|
$binding->initialize($type); |
|
101
|
|
|
|
|
102
|
1 |
|
$this->assertSame(array('param1' => 'value', 'param2' => null), $binding->getParameterValues()); |
|
103
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param1')); |
|
104
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param2')); |
|
105
|
1 |
|
$this->assertFalse($binding->hasParameterValue('foo')); |
|
106
|
1 |
|
$this->assertSame('value', $binding->getParameterValue('param1')); |
|
107
|
1 |
|
$this->assertNull($binding->getParameterValue('param2')); |
|
108
|
|
|
|
|
109
|
|
|
// exclude default values |
|
110
|
1 |
|
$this->assertSame(array('param1' => 'value'), $binding->getParameterValues(false)); |
|
111
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param1', false)); |
|
112
|
1 |
|
$this->assertFalse($binding->hasParameterValue('param2', false)); |
|
113
|
1 |
|
$this->assertFalse($binding->hasParameterValue('foo', false)); |
|
114
|
1 |
|
$this->assertSame('value', $binding->getParameterValue('param1', false)); |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
1 |
View Code Duplication |
public function testInitializeWithParameterDefaults() |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
1 |
|
$binding = $this->createBinding(Foo::clazz, array( |
|
120
|
1 |
|
'param2' => 'value', |
|
121
|
|
|
)); |
|
122
|
|
|
|
|
123
|
1 |
|
$type = new BindingType(Foo::clazz, get_class($binding), array( |
|
124
|
1 |
|
new BindingParameter('param1', BindingParameter::OPTIONAL, 'default'), |
|
125
|
1 |
|
new BindingParameter('param2'), |
|
126
|
|
|
)); |
|
127
|
|
|
|
|
128
|
1 |
|
$this->assertSame(array('param2' => 'value'), $binding->getParameterValues()); |
|
129
|
1 |
|
$this->assertFalse($binding->hasParameterValue('param1')); |
|
130
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param2')); |
|
131
|
1 |
|
$this->assertSame('value', $binding->getParameterValue('param2')); |
|
132
|
|
|
|
|
133
|
1 |
|
$binding->initialize($type); |
|
134
|
|
|
|
|
135
|
1 |
|
$this->assertSame(array('param1' => 'default', 'param2' => 'value'), $binding->getParameterValues()); |
|
136
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param1')); |
|
137
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param2')); |
|
138
|
1 |
|
$this->assertSame('default', $binding->getParameterValue('param1')); |
|
139
|
1 |
|
$this->assertSame('value', $binding->getParameterValue('param2')); |
|
140
|
|
|
|
|
141
|
|
|
// exclude default values |
|
142
|
1 |
|
$this->assertSame(array('param2' => 'value'), $binding->getParameterValues(false)); |
|
143
|
1 |
|
$this->assertFalse($binding->hasParameterValue('param1', false)); |
|
144
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param2', false)); |
|
145
|
1 |
|
$this->assertSame('value', $binding->getParameterValue('param2', false)); |
|
146
|
1 |
|
} |
|
147
|
|
|
|
|
148
|
1 |
View Code Duplication |
public function testInitializeWithRequiredParameters() |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
1 |
|
$binding = $this->createBinding(Foo::clazz, array( |
|
151
|
1 |
|
'param2' => 'value', |
|
152
|
|
|
)); |
|
153
|
|
|
|
|
154
|
1 |
|
$type = new BindingType(Foo::clazz, get_class($binding), array( |
|
155
|
1 |
|
new BindingParameter('param1', BindingParameter::OPTIONAL, 'default'), |
|
156
|
1 |
|
new BindingParameter('param2', BindingParameter::REQUIRED), |
|
157
|
|
|
)); |
|
158
|
|
|
|
|
159
|
1 |
|
$this->assertSame(array('param2' => 'value'), $binding->getParameterValues()); |
|
160
|
1 |
|
$this->assertFalse($binding->hasParameterValue('param1')); |
|
161
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param2')); |
|
162
|
1 |
|
$this->assertSame('value', $binding->getParameterValue('param2')); |
|
163
|
|
|
|
|
164
|
1 |
|
$binding->initialize($type); |
|
165
|
|
|
|
|
166
|
1 |
|
$this->assertSame(array('param1' => 'default', 'param2' => 'value'), $binding->getParameterValues()); |
|
167
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param1')); |
|
168
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param2')); |
|
169
|
1 |
|
$this->assertSame('default', $binding->getParameterValue('param1')); |
|
170
|
1 |
|
$this->assertSame('value', $binding->getParameterValue('param2')); |
|
171
|
|
|
|
|
172
|
|
|
// exclude default values |
|
173
|
1 |
|
$this->assertSame(array('param2' => 'value'), $binding->getParameterValues(false)); |
|
174
|
1 |
|
$this->assertFalse($binding->hasParameterValue('param1', false)); |
|
175
|
1 |
|
$this->assertTrue($binding->hasParameterValue('param2', false)); |
|
176
|
1 |
|
$this->assertSame('value', $binding->getParameterValue('param2', false)); |
|
177
|
1 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @expectedException \Puli\Discovery\Api\Type\MissingParameterException |
|
181
|
|
|
* @expectedExceptionMessage param |
|
182
|
|
|
*/ |
|
183
|
1 |
|
public function testInitializeFailsIfMissingRequiredParameter() |
|
184
|
|
|
{ |
|
185
|
1 |
|
$binding = $this->createBinding(Foo::clazz); |
|
186
|
|
|
|
|
187
|
1 |
|
$type = new BindingType(Foo::clazz, get_class($binding), array( |
|
188
|
1 |
|
new BindingParameter('param', BindingParameter::REQUIRED), |
|
189
|
|
|
)); |
|
190
|
|
|
|
|
191
|
1 |
|
$binding->initialize($type); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @expectedException \Puli\Discovery\Api\Type\NoSuchParameterException |
|
196
|
|
|
* @expectedExceptionMessage foo |
|
197
|
|
|
*/ |
|
198
|
1 |
View Code Duplication |
public function testInitializeFailsIfUnknownParameter() |
|
|
|
|
|
|
199
|
|
|
{ |
|
200
|
1 |
|
$binding = $this->createBinding(Foo::clazz, array( |
|
201
|
1 |
|
'foo' => 'bar', |
|
202
|
|
|
)); |
|
203
|
|
|
|
|
204
|
1 |
|
$type = new BindingType(Foo::clazz, get_class($binding)); |
|
205
|
|
|
|
|
206
|
1 |
|
$binding->initialize($type); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @expectedException \InvalidArgumentException |
|
211
|
|
|
* @expectedExceptionMessage Bar |
|
212
|
|
|
*/ |
|
213
|
1 |
|
public function testInitializeFailsIfWrongType() |
|
214
|
|
|
{ |
|
215
|
1 |
|
$binding = $this->createBinding(Bar::clazz); |
|
216
|
|
|
|
|
217
|
1 |
|
$type = new BindingType(Foo::clazz, get_class($binding)); |
|
218
|
|
|
|
|
219
|
1 |
|
$binding->initialize($type); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @expectedException \Puli\Discovery\Api\Type\BindingNotAcceptedException |
|
224
|
|
|
*/ |
|
225
|
1 |
View Code Duplication |
public function testInitializeFailsIfBindingNotAccepted() |
|
|
|
|
|
|
226
|
|
|
{ |
|
227
|
1 |
|
$binding = $this->createBinding(Foo::clazz); |
|
228
|
|
|
|
|
229
|
1 |
|
$type = new BindingType(Foo::clazz, __CLASS__); |
|
230
|
|
|
|
|
231
|
1 |
|
$binding->initialize($type); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @expectedException \Puli\Discovery\Api\Type\NoSuchParameterException |
|
236
|
|
|
* @expectedExceptionMessage foo |
|
237
|
|
|
*/ |
|
238
|
1 |
|
public function testGetParameterFailsIfNotFound() |
|
239
|
|
|
{ |
|
240
|
1 |
|
$binding = $this->createBinding(Foo::clazz); |
|
241
|
|
|
|
|
242
|
1 |
|
$binding->getParameterValue('foo'); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @expectedException \Puli\Discovery\Api\Binding\Initializer\NotInitializedException |
|
247
|
|
|
*/ |
|
248
|
1 |
|
public function testGetTypeFailsIfNotInitialized() |
|
249
|
|
|
{ |
|
250
|
1 |
|
$binding = $this->createBinding(Foo::clazz); |
|
251
|
|
|
|
|
252
|
1 |
|
$binding->getType(); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
1 |
|
public function testSerialize() |
|
256
|
|
|
{ |
|
257
|
1 |
|
$binding = $this->createBinding(Foo::clazz, array( |
|
258
|
1 |
|
'param1' => 'value', |
|
259
|
|
|
)); |
|
260
|
|
|
|
|
261
|
1 |
|
$unserialized = unserialize(serialize($binding)); |
|
262
|
|
|
|
|
263
|
1 |
|
$this->assertEquals($binding, $unserialized); |
|
264
|
1 |
|
} |
|
265
|
|
|
|
|
266
|
1 |
|
public function testSerializeInitialized() |
|
267
|
|
|
{ |
|
268
|
1 |
|
$binding = $this->createBinding(Foo::clazz, array( |
|
269
|
1 |
|
'param1' => 'value', |
|
270
|
|
|
)); |
|
271
|
|
|
|
|
272
|
1 |
|
$type = new BindingType(Foo::clazz, get_class($binding), array( |
|
273
|
1 |
|
new BindingParameter('param1'), |
|
274
|
1 |
|
new BindingParameter('param2'), |
|
275
|
|
|
)); |
|
276
|
|
|
|
|
277
|
1 |
|
$binding->initialize($type); |
|
278
|
|
|
|
|
279
|
1 |
|
$unserialized = unserialize(serialize($binding)); |
|
280
|
1 |
|
$unserialized->initialize($type); |
|
281
|
|
|
|
|
282
|
1 |
|
$this->assertEquals($binding, $unserialized); |
|
283
|
1 |
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
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: