Skip to content

Commit 115585c

Browse files
author
Justin Rainbow
committed
Starting to add error message testing to PHPUnit tests
1 parent 1c0a28d commit 115585c

File tree

4 files changed

+72
-15
lines changed

4 files changed

+72
-15
lines changed

JsonSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ static function checkType($type, &$value, $path) {
317317
return array(
318318
array(
319319
'property'=>$path,
320-
'message'=>gettype($value)." value found, but a ".$type." is required "
320+
'message'=>gettype($value)." value found, but a ".$type." is required"
321321
)
322322
);
323323
}

tests/AdditionalPropertiesTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ public function getInvalidTests()
1818
"additionalProperties": false
1919
}'
2020
),
21+
array(
22+
'{
23+
"prop":"1",
24+
"additionalProp":"2"
25+
}',
26+
'{
27+
"type":"object",
28+
"properties":{
29+
"prop":{"type":"string"}
30+
},
31+
"additionalProperties": false
32+
}',
33+
JsonSchema::CHECK_MODE_TYPE_CAST
34+
),
2135
array(
2236
'{
2337
"prop":"1",
@@ -30,6 +44,20 @@ public function getInvalidTests()
3044
},
3145
"additionalProperties": {"type":"string"}
3246
}'
47+
),
48+
array(
49+
'{
50+
"prop":"1",
51+
"additionalProp":2
52+
}',
53+
'{
54+
"type":"object",
55+
"properties":{
56+
"prop":{"type":"string"}
57+
},
58+
"additionalProperties": {"type":"string"}
59+
}',
60+
JsonSchema::CHECK_MODE_TYPE_CAST
3361
)
3462
);
3563
}
@@ -49,6 +77,19 @@ public function getValidTests()
4977
}
5078
}'
5179
),
80+
array(
81+
'{
82+
"prop":"1",
83+
"additionalProp":"2"
84+
}',
85+
'{
86+
"type":"object",
87+
"properties":{
88+
"prop":{"type":"string"}
89+
}
90+
}',
91+
JsonSchema::CHECK_MODE_TYPE_CAST
92+
),
5293
array(
5394
'{
5495
"prop":"1",

tests/BaseTestCase.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ abstract class BaseTestCase extends PHPUnit_Framework_TestCase
55
/**
66
* @dataProvider getInvalidTests
77
*/
8-
public function testInvalidCases($input, $schema, $errors = array())
8+
public function testInvalidCases($input, $schema, $checkMode = null, $errors = array())
99
{
10+
if (null === $checkMode) {
11+
$checkMode = JsonSchema::CHECK_MODE_NORMAL;
12+
}
13+
14+
JsonSchema::$checkMode = $checkMode;
15+
1016
$result = JsonSchema::validate(json_decode($input), json_decode($schema));
1117
if (array() !== $errors) {
12-
$this->assertEquals($errors, $result->errors);
18+
$this->assertEquals($errors, $result->errors, var_export($result, true));
1319
}
1420
$this->assertFalse($result->valid, var_export($result, true));
1521
}
@@ -22,7 +28,9 @@ public function testValidCases($input, $schema, $checkMode = null)
2228
if (null === $checkMode) {
2329
$checkMode = JsonSchema::CHECK_MODE_NORMAL;
2430
}
31+
2532
JsonSchema::$checkMode = $checkMode;
33+
2634
$result = JsonSchema::validate(json_decode($input), json_decode($schema));
2735
$this->assertTrue($result->valid, var_export($result, true));
2836
}

tests/PhpTypeCastModeTest.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@ public function getInvalidTests()
1414
"properties":{
1515
"a":{"type":"number"}
1616
}
17-
}'
18-
),
19-
array(
20-
'{
21-
"a":"9"
2217
}',
23-
'{
24-
"type":"object",
25-
"properties":{
26-
"a":{"type":"number"}
27-
}
28-
}'
18+
null,
19+
array(
20+
array(
21+
'property' => 'a',
22+
'message' => 'string value found, but a number is required'
23+
)
24+
)
2925
),
3026
array(
3127
'{
@@ -55,7 +51,19 @@ public function getValidTests()
5551
}
5652
}',
5753
JsonSchema::CHECK_MODE_TYPE_CAST
58-
)
54+
),
55+
array(
56+
'{
57+
"a":"9"
58+
}',
59+
'{
60+
"type":"object",
61+
"properties":{
62+
"a":{"type":"number"}
63+
}
64+
}',
65+
JsonSchema::CHECK_MODE_TYPE_CAST
66+
),
5967
);
6068
}
6169
}

0 commit comments

Comments
 (0)