@@ -210,6 +210,118 @@ public function testBuildingNullableParameter()
210
210
$ this ->assertInstanceOf (ExtractorDefinitionInterface::class, $ spec ->getExtractorDefinition ());
211
211
}
212
212
213
+ public function testBuildingParameterWithArrayTypeGuessing ()
214
+ {
215
+ $ this ->argumentDefinitionShouldBuildOn ('[] ' );
216
+ $ this ->extractorDefinitionShouldBuildOn ('[] ' );
217
+
218
+ $ spec = $ this ->builder ->build ('param = [] ' );
219
+
220
+ $ this ->assertInstanceOf (OptionalParameterSpec::class, $ spec );
221
+
222
+ $ this ->assertSame ('param ' , $ spec ->getName ());
223
+ $ this ->assertSame ([], $ spec ->getDefaultValue ());
224
+ $ this ->assertInstanceOf (ExtractorDefinitionInterface::class, $ spec ->getExtractorDefinition ());
225
+ }
226
+
227
+ public function testBuildingParameterWithBoolTrueTypeGuessing ()
228
+ {
229
+ $ this ->argumentDefinitionShouldBuildOn ('true ' );
230
+ $ this ->extractorDefinitionShouldBuildOn ('bool ' );
231
+
232
+ $ spec = $ this ->builder ->build ('param = true ' );
233
+
234
+ $ this ->assertInstanceOf (OptionalParameterSpec::class, $ spec );
235
+
236
+ $ this ->assertSame ('param ' , $ spec ->getName ());
237
+ $ this ->assertSame (true , $ spec ->getDefaultValue ());
238
+ $ this ->assertInstanceOf (ExtractorDefinitionInterface::class, $ spec ->getExtractorDefinition ());
239
+ }
240
+
241
+ public function testBuildingParameterWithBoolFalseTypeGuessing ()
242
+ {
243
+ $ this ->argumentDefinitionShouldBuildOn ('false ' );
244
+ $ this ->extractorDefinitionShouldBuildOn ('bool ' );
245
+
246
+ $ spec = $ this ->builder ->build ('param = false ' );
247
+
248
+ $ this ->assertInstanceOf (OptionalParameterSpec::class, $ spec );
249
+
250
+ $ this ->assertSame ('param ' , $ spec ->getName ());
251
+ $ this ->assertSame (false , $ spec ->getDefaultValue ());
252
+ $ this ->assertInstanceOf (ExtractorDefinitionInterface::class, $ spec ->getExtractorDefinition ());
253
+ }
254
+
255
+ public function testBuildingParameterWithNullableTypeGuessing ()
256
+ {
257
+ $ this ->argumentDefinitionShouldBuildOn ('null ' );
258
+ $ this ->extractorDefinitionShouldBuildOn ('any ' );
259
+
260
+ $ spec = $ this ->builder ->build ('param? ' );
261
+
262
+ $ this ->assertInstanceOf (OptionalParameterSpec::class, $ spec );
263
+
264
+ $ this ->assertSame ('param ' , $ spec ->getName ());
265
+ $ this ->assertSame (null , $ spec ->getDefaultValue ());
266
+ $ this ->assertInstanceOf (ExtractorDefinitionInterface::class, $ spec ->getExtractorDefinition ());
267
+ }
268
+
269
+ public function testBuildingParameterWithDefaultNullTypeGuessing ()
270
+ {
271
+ $ this ->argumentDefinitionShouldBuildOn ('null ' );
272
+ $ this ->extractorDefinitionShouldBuildOn ('any ' );
273
+
274
+ $ spec = $ this ->builder ->build ('param = null ' );
275
+
276
+ $ this ->assertInstanceOf (OptionalParameterSpec::class, $ spec );
277
+
278
+ $ this ->assertSame ('param ' , $ spec ->getName ());
279
+ $ this ->assertSame (null , $ spec ->getDefaultValue ());
280
+ $ this ->assertInstanceOf (ExtractorDefinitionInterface::class, $ spec ->getExtractorDefinition ());
281
+ }
282
+
283
+ public function testBuildingParameterWithDefaultIntNumberTypeGuessing ()
284
+ {
285
+ $ this ->argumentDefinitionShouldBuildOn ('123 ' );
286
+ $ this ->extractorDefinitionShouldBuildOn ('number ' );
287
+
288
+ $ spec = $ this ->builder ->build ('param = 123 ' );
289
+
290
+ $ this ->assertInstanceOf (OptionalParameterSpec::class, $ spec );
291
+
292
+ $ this ->assertSame ('param ' , $ spec ->getName ());
293
+ $ this ->assertEquals (123 , $ spec ->getDefaultValue ());
294
+ $ this ->assertInstanceOf (ExtractorDefinitionInterface::class, $ spec ->getExtractorDefinition ());
295
+ }
296
+
297
+ public function testBuildingParameterWithDefaultFloatNumberTypeGuessing ()
298
+ {
299
+ $ this ->argumentDefinitionShouldBuildOn ('123.42 ' );
300
+ $ this ->extractorDefinitionShouldBuildOn ('number ' );
301
+
302
+ $ spec = $ this ->builder ->build ('param = 123.42 ' );
303
+
304
+ $ this ->assertInstanceOf (OptionalParameterSpec::class, $ spec );
305
+
306
+ $ this ->assertSame ('param ' , $ spec ->getName ());
307
+ $ this ->assertSame (123.42 , $ spec ->getDefaultValue ());
308
+ $ this ->assertInstanceOf (ExtractorDefinitionInterface::class, $ spec ->getExtractorDefinition ());
309
+ }
310
+
311
+ public function testBuildingParameterWithDefaultStringTypeGuessing ()
312
+ {
313
+ $ this ->argumentDefinitionShouldBuildOn ('"test" ' );
314
+ $ this ->extractorDefinitionShouldBuildOn ('string ' );
315
+
316
+ $ spec = $ this ->builder ->build ('param = "test" ' );
317
+
318
+ $ this ->assertInstanceOf (OptionalParameterSpec::class, $ spec );
319
+
320
+ $ this ->assertSame ('param ' , $ spec ->getName ());
321
+ $ this ->assertSame ('"test" ' , $ spec ->getDefaultValue ());
322
+ $ this ->assertInstanceOf (ExtractorDefinitionInterface::class, $ spec ->getExtractorDefinition ());
323
+ }
324
+
213
325
/**
214
326
* @expectedException \Pinepain\JsSandbox\Specs\Builder\Exceptions\ParameterSpecBuilderException
215
327
* @expectedExceptionMessage Unable to parse definition because of extractor failure: ExtractorDefinitionBuilder exception for testing
@@ -223,9 +335,31 @@ public function testBuildingWhenExtractorFailsShouldAlsoFail()
223
335
224
336
protected function argumentDefinitionShouldBuildOn ($ name )
225
337
{
338
+ $ retval = $ name ;
339
+
340
+ if ('[] ' == $ name ) {
341
+ $ retval = [];
342
+ }
343
+
344
+ if ('true ' === $ name ) {
345
+ $ retval = true ;
346
+ }
347
+
348
+ if ('false ' === $ name ) {
349
+ $ retval = false ;
350
+ }
351
+
352
+ if (is_numeric ($ name )) {
353
+ $ retval = is_int ($ name ) ? (int )$ name : (float ) $ name ;
354
+ }
355
+
356
+ if ('null ' === $ name ) {
357
+ $ retval = null ;
358
+ }
359
+
226
360
$ this ->argument_builder ->method ('build ' )
227
361
->with ($ name , false )
228
- ->willReturn ($ name );
362
+ ->willReturn ($ retval );
229
363
}
230
364
231
365
protected function argumentDefinitionShouldThrowOn ($ name )
0 commit comments