Completed
Push — master ( d2145e...0f5aad )
by Martijn
13s
created

testSettingAdditionalPropertiesTwiceWithCommandFails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class ObjectTypeTest extends SwaggerGen_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
	protected $parent;
7
8
	protected function setUp()
9
	{
10
		$this->parent = $this->getMockForAbstractClass('\SwaggerGen\Swagger\AbstractObject');
11
	}
12
13
	protected function assertPreConditions()
14
	{
15
		$this->assertInstanceOf('\SwaggerGen\Swagger\AbstractObject', $this->parent);
16
	}
17
18
	/**
19
	 * @covers \SwaggerGen\Swagger\Type\ObjectType::__construct
20
	 */
21
	public function testConstructNotAnObject()
22
	{
23
		$this->expectException('\SwaggerGen\Exception', "Not an object: 'wrong'");
24
25
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'wrong');
0 ignored issues
show
Unused Code introduced by
$object is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
	}
27
28
	/**
29
	 * @covers \SwaggerGen\Swagger\Type\ObjectType::__construct
30
	 */
31
	public function testConstructNoDefault()
32
	{
33
		$this->expectException('\SwaggerGen\Exception', "Unparseable object definition: 'object=a'");
34
35
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object=a');
0 ignored issues
show
Unused Code introduced by
$object is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36
	}
37
38
	/**
39
	 * @covers \SwaggerGen\Swagger\Type\ObjectType::__construct
40
	 */
41
	public function testConstructEmptyRange()
42
	{
43
		$this->expectException('\SwaggerGen\Exception', "Empty object range: 'object[,]'");
44
45
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object[,]');
0 ignored issues
show
Unused Code introduced by
$object is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
46
	}
47
48
	/**
49
	 * @covers \SwaggerGen\Swagger\Type\ObjectType::__construct
50
	 */
51
	public function testConstructRangeStart()
52
	{
53
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object[0,]');
54
55
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
56
57
		$this->assertSame(array(
58
			'type' => 'object',
59
			'minProperties' => 0,
60
				), $object->toArray());
61
	}
62
63
	/**
64
	 * @covers \SwaggerGen\Swagger\Type\ObjectType::__construct
65
	 */
66
	public function testConstructRangeBoth()
67
	{
68
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object<2,4]');
69
70
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
71
72
		$this->assertSame(array(
73
			'type' => 'object',
74
			'minProperties' => 3,
75
			'maxProperties' => 4,
76
				), $object->toArray());
77
	}
78
79
	/**
80
	 * @covers \SwaggerGen\Swagger\Type\ObjectType::__construct
81
	 */
82
	public function testConstructEmptyProperties()
83
	{
84
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object()');
85
86
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
87
88
		$this->assertSame(array(
89
			'type' => 'object',
90
				), $object->toArray());
91
	}
92
93
	/**
94
	 * @covers \SwaggerGen\Swagger\Type\ObjectType::__construct
95
	 */
96
	public function testConstructBadProperties()
97
	{
98
		$this->expectException('\SwaggerGen\Exception', "Unparseable property definition: '1'");
99
100
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object(1)');
0 ignored issues
show
Unused Code introduced by
$object is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
101
	}
102
103
	/**
104
	 * @covers \SwaggerGen\Swagger\Type\ObjectType::__construct
105
	 */
106
	public function testConstructTypeProperty()
107
	{
108
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object(foo:string)');
109
110
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
111
112
		$this->assertSame(array(
113
			'type' => 'object',
114
			'required' => array(
115
				'foo',
116
			),
117
			'properties' => array(
118
				'foo' => array(
119
					'type' => 'string',
120
				),
121
			),
122
				), $object->toArray());
123
	}
124
125
	/**
126
	 * @covers \SwaggerGen\Swagger\Type\ObjectType::__construct
127
	 */
128
	public function testConstructTypeProperties()
129
	{
130
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object(foo:string,bar?:int[3,10>=5)');
131
132
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
133
134
		$this->assertSame(array(
135
			'type' => 'object',
136
			'required' => array(
137
				'foo',
138
			),
139
			'properties' => array(
140
				'foo' => array(
141
					'type' => 'string',
142
				),
143
				'bar' => array(
144
					'type' => 'integer',
145
					'format' => 'int32',
146
					'default' => 5,
147
					'minimum' => 3,
148
					'maximum' => 10,
149
					'exclusiveMaximum' => true,
150
				)
151
			),
152
				), $object->toArray());
153
	}
154
155
	/**
156
	 * @covers \SwaggerGen\Swagger\Type\ObjectType->handleCommand
157
	 */
158
	public function testCommandMinUpperBound()
159
	{
160
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object[3,5]');
161
162
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
163
164
		$this->expectException('\SwaggerGen\Exception', "Minimum greater than maximum: '6'");
165
166
		$object->handleCommand('min', '6');
167
	}
168
169
	/**
170
	 * @covers \SwaggerGen\Swagger\Type\ObjectType->handleCommand
171
	 */
172
	public function testCommandMinLowerBound()
173
	{
174
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object[3,5]');
175
176
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
177
178
		$this->expectException('\SwaggerGen\Exception', "Minimum less than zero: '-1'");
179
180
		$object->handleCommand('min', '-1');
181
	}
182
183
	/**
184
	 * @covers \SwaggerGen\Swagger\Type\ObjectType->handleCommand
185
	 */
186
	public function testCommandMin()
187
	{
188
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object[3,5]');
189
190
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
191
192
		$object->handleCommand('min', '4');
193
194
		$this->assertSame(array(
195
			'type' => 'object',
196
			'minProperties' => 4,
197
			'maxProperties' => 5,
198
				), $object->toArray());
199
	}
200
201
	/**
202
	 * @covers \SwaggerGen\Swagger\Type\ObjectType->handleCommand
203
	 */
204
	public function testCommandMaxLowerBound()
205
	{
206
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object[3,5]');
207
208
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
209
210
		$this->expectException('\SwaggerGen\Exception', "Maximum less than minimum: '2'");
211
212
		$object->handleCommand('max', '2');
213
	}
214
215
	/**
216
	 * @covers \SwaggerGen\Swagger\Type\ObjectType->handleCommand
217
	 */
218
	public function testCommandMaxUpperBound()
219
	{
220
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object');
221
222
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
223
224
		$this->expectException('\SwaggerGen\Exception', "Maximum less than zero: '-1'");
225
226
		$object->handleCommand('max', '-1');
227
	}
228
229
	/**
230
	 * @covers \SwaggerGen\Swagger\Type\ObjectType->handleCommand
231
	 */
232
	public function testCommandMax()
233
	{
234
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object[3,5]');
235
236
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
237
238
		$object->handleCommand('max', '4');
239
240
		$this->assertSame(array(
241
			'type' => 'object',
242
			'minProperties' => 3,
243
			'maxProperties' => 4,
244
				), $object->toArray());
245
	}
246
247
	/**
248
	 * @covers \SwaggerGen\Swagger\Type\ObjectType->handleCommand
249
	 */
250
	public function testCommandPropertyMissingDefinition()
251
	{
252
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object');
253
254
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
255
256
		$this->expectException('\SwaggerGen\Exception', "Missing property definition");
257
258
		$object->handleCommand('property', '');
259
	}
260
261
	/**
262
	 * @covers \SwaggerGen\Swagger\Type\ObjectType->handleCommand
263
	 */
264
	public function testCommandPropertyMissingName()
265
	{
266
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object');
267
268
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
269
270
		$this->expectException('\SwaggerGen\Exception', "Missing property name: 'string'");
271
272
		$object->handleCommand('property', 'string');
273
	}
274
275
	/**
276
	 * @covers \SwaggerGen\Swagger\Type\ObjectType->handleCommand
277
	 */
278
	public function testCommandProperty()
279
	{
280
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object');
281
282
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
283
284
		$object->handleCommand('property', 'string foo Some words here');
285
286
		$this->assertSame(array(
287
			'type' => 'object',
288
			'required' => array(
289
				'foo',
290
			),
291
			'properties' => array(
292
				'foo' => array(
293
					'type' => 'string',
294
					'description' => 'Some words here',
295
				),
296
			),
297
				), $object->toArray());
298
	}
299
300
	/**
301
	 * @covers \SwaggerGen\Swagger\Type\ObjectType->handleCommand
302
	 */
303
	public function testCommandPropertyOptional()
304
	{
305
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object');
306
307
		$this->assertInstanceOf('\SwaggerGen\Swagger\Type\ObjectType', $object);
308
309
		$object->handleCommand('property?', 'string foo Some words here');
310
311
		$this->assertSame(array(
312
			'type' => 'object',
313
			'properties' => array(
314
				'foo' => array(
315
					'type' => 'string',
316
					'description' => 'Some words here',
317
				),
318
			),
319
				), $object->toArray());
320
	}
321
322
	public function testObjectProperties()
323
	{
324
		$object = new \SwaggerGen\SwaggerGen();
325
		$array = $object->getSwagger(array('
326
			api Test
327
			endpoint /test
328
			method GET something
329
			response 200 object(a:array(A),b:array(B))
330
		'));
331
332
		$this->assertSame('{"swagger":2,"info":{"title":"undefined","version":0}'
333
				. ',"paths":{"\/test":{"get":{"tags":["Test"],"summary":"something"'
334
				. ',"responses":{"200":{"description":"OK","schema":{"type":"object","required":["a","b"]'
335
				. ',"properties":{"a":{"type":"array","items":{"$ref":"#\/definitions\/A"}}'
336
				. ',"b":{"type":"array","items":{"$ref":"#\/definitions\/B"}}}}}}}}}'
337
				. ',"tags":[{"name":"Test"}]}', json_encode($array, JSON_NUMERIC_CHECK));
338
	}
339
340
	public function testObjectPropertiesReadOnly()
341
	{
342
		$object = new \SwaggerGen\SwaggerGen();
343
		$array = $object->getSwagger(array('
344
			api Test
345
			endpoint /test
346
			method GET something
347
			response 200 object(a!:array(A),b:array(B))
348
		'));
349
350
		$this->assertSame('{"swagger":2,"info":{"title":"undefined","version":0}'
351
			. ',"paths":{"\/test":{"get":{"tags":["Test"],"summary":"something"'
352
			. ',"responses":{"200":{"description":"OK","schema":{"type":"object","required":["b"]'
353
			. ',"properties":{"a":{"type":"array","items":{"$ref":"#\/definitions\/A"}}'
354
			. ',"b":{"type":"array","items":{"$ref":"#\/definitions\/B"}}}}}}}}}'
355
			. ',"tags":[{"name":"Test"}]}', json_encode($array, JSON_NUMERIC_CHECK));
356
	}
357
358
	public function testDeepObjectProperties()
359
	{
360
		$object = new \SwaggerGen\SwaggerGen();
361
		$array = $object->getSwagger(array('
362
			api Test
363
			endpoint /test
364
			method GET something
365
			response 200 object(a:object(c:csv(C),d:int),b?:array(B))
366
		'));
367
368
		$this->assertSame('{"swagger":2,"info":{"title":"undefined","version":0}'
369
				. ',"paths":{"\/test":{"get":{"tags":["Test"],"summary":"something"'
370
				. ',"responses":{"200":{"description":"OK","schema":{"type":"object"'
371
				. ',"required":["a"],"properties":{'
372
				. '"a":{"type":"object","required":["c","d"],"properties":{'
373
				. '"c":{"type":"array","items":{"$ref":"#\/definitions\/C"}}'
374
				. ',"d":{"type":"integer","format":"int32"}}}'
375
				. ',"b":{"type":"array","items":{"$ref":"#\/definitions\/B"}}}}}}}}}'
376
				. ',"tags":[{"name":"Test"}]}', json_encode($array, JSON_NUMERIC_CHECK));
377
	}
378
379
	public function testDeepObjectProperties_JsonNotation()
380
	{
381
		$object = new \SwaggerGen\SwaggerGen();
382
		$array = $object->getSwagger(array('
383
			api Test
384
			endpoint /test
385
			method GET something
386
			response 200 {a:{c:csv(C),d:int},b?:[B]}
387
		'));
388
389
		$this->assertSame('{"swagger":2,"info":{"title":"undefined","version":0}'
390
				. ',"paths":{"\/test":{"get":{"tags":["Test"],"summary":"something"'
391
				. ',"responses":{"200":{"description":"OK","schema":{"type":"object"'
392
				. ',"required":["a"],"properties":{'
393
				. '"a":{"type":"object","required":["c","d"],"properties":{'
394
				. '"c":{"type":"array","items":{"$ref":"#\/definitions\/C"}}'
395
				. ',"d":{"type":"integer","format":"int32"}}}'
396
				. ',"b":{"type":"array","items":{"$ref":"#\/definitions\/B"}}}}}}}}}'
397
				. ',"tags":[{"name":"Test"}]}', json_encode($array, JSON_NUMERIC_CHECK));
398
	}
399
400
	public function testAddingOptionalPropertyFailsWhenItIsDiscriminator()
401
	{
402
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object');
403
		$object->handleCommand('discriminator', 'type');
404
		$this->expectException('\SwaggerGen\Exception', "Discriminator must be a required property, property 'type' is not required");
405
		$object->handleCommand('property?', 'string type');
406
	}
407
408
	public function testAddingReadonlyPropertyFailsWhenItIsDiscriminator()
409
	{
410
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object');
411
		$object->handleCommand('discriminator', 'type');
412
		$this->expectException('\SwaggerGen\Exception', "Discriminator must be a required property, property 'type' is not required");
413
		$object->handleCommand('property!', 'string type');
414
	}
415
416
	public function testDiscriminatingOnOptionalPropertyFails()
417
	{
418
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object');
419
		$object->handleCommand('property?', 'string type');
420
		$this->expectException('\SwaggerGen\Exception', "Discriminator must be a required property, property 'type' is not required");
421
		$object->handleCommand('discriminator', 'type');
422
	}
423
424
	public function testDiscriminatingOnReadonlyPropertyFails()
425
	{
426
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object');
427
		$object->handleCommand('property!', 'string type');
428
		$this->expectException('\SwaggerGen\Exception', "Discriminator must be a required property, property 'type' is not required");
429
		$object->handleCommand('discriminator', 'type');
430
	}
431
432
	public function testSettingAnotherDiscriminatorFails()
433
	{
434
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object');
435
		$object->handleCommand('discriminator', 'type');
436
		$this->expectException('\SwaggerGen\Exception', "Discriminator may only be set once, trying to change it from 'type' to 'petType'");
437
		$object->handleCommand('discriminator', 'petType');
438
	}
439
440
	public function testSettingAdditionalPropertiesTwiceInlineFails()
441
	{
442
		$this->expectException('\SwaggerGen\Exception', "Additional properties may only be set once");
443
		new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object(...,...)');
444
	}
445
446
	public function testSettingAdditionalPropertiesTwiceWithCommandFails()
447
	{
448
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object');
449
		$object->handleCommand('additionalproperties', 'false');
450
		$this->expectException('\SwaggerGen\Exception', "Additional properties may only be set once");
451
		$object->handleCommand('additionalproperties', 'false');
452
	}
453
454
	public function testSettingAdditionalPropertiesTwiceMixedFails()
455
	{
456
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object(...)');
457
		$this->expectException('\SwaggerGen\Exception', "Additional properties may only be set once");
458
		$object->handleCommand('additionalproperties', 'string');
459
	}
460
461
	public function testSettingAdditionalPropertiesWithInvalidSyntaxFails()
462
	{
463
		$this->expectException('\SwaggerGen\Exception', "Unparseable property definition: '!...!'");
464
		new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object(!...!)');
465
	}
466
467
	public function testSettingAdditionalPropertiesWithInvalidTypeInlineFails()
468
	{
469
		$this->expectException('\SwaggerGen\Exception', "Unparseable additional properties definition: '...?&#'");
470
		new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object(...?&#)');
471
	}
472
473
	public function testSettingAdditionalPropertiesWithInvalidTypeCommandFails()
474
	{
475
		$object = new SwaggerGen\Swagger\Type\ObjectType($this->parent, 'object');
476
		$this->expectException('\SwaggerGen\Exception', "Unparseable additional properties definition: '?&#'");
477
		$object->handleCommand('additionalproperties', '?&#');
478
	}
479
480
481
}
482