Skip to content

Commit 1c0a28d

Browse files
author
Justin Rainbow
committed
Initial conversion of selenium tests to PHPUnit
1 parent 4436675 commit 1c0a28d

23 files changed

+1208
-0
lines changed

bootstrap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
require 'JsonSchema.php';
4+
require 'JsonSchemaUndefined.php';
5+
require 'tests/BaseTestCase.php';

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="bootstrap.php"
13+
verbose="true"
14+
>
15+
<testsuites>
16+
<testsuite name="JSON Schema Test Suite">
17+
<directory suffix="Test.php">tests</directory>
18+
</testsuite>
19+
</testsuites>
20+
</phpunit>

tests/AdditionalPropertiesTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
class AdditionalPropertiesTest extends BaseTestCase
4+
{
5+
public function getInvalidTests()
6+
{
7+
return array(
8+
array(
9+
'{
10+
"prop":"1",
11+
"additionalProp":"2"
12+
}',
13+
'{
14+
"type":"object",
15+
"properties":{
16+
"prop":{"type":"string"}
17+
},
18+
"additionalProperties": false
19+
}'
20+
),
21+
array(
22+
'{
23+
"prop":"1",
24+
"additionalProp":2
25+
}',
26+
'{
27+
"type":"object",
28+
"properties":{
29+
"prop":{"type":"string"}
30+
},
31+
"additionalProperties": {"type":"string"}
32+
}'
33+
)
34+
);
35+
}
36+
37+
public function getValidTests()
38+
{
39+
return array(
40+
array(
41+
'{
42+
"prop":"1",
43+
"additionalProp":"2"
44+
}',
45+
'{
46+
"type":"object",
47+
"properties":{
48+
"prop":{"type":"string"}
49+
}
50+
}'
51+
),
52+
array(
53+
'{
54+
"prop":"1",
55+
"additionalProp":"2"
56+
}',
57+
'{
58+
"type":"object",
59+
"properties":{
60+
"prop":{"type":"string"}
61+
},
62+
"additionalProperties": {"type":"string"}
63+
}'
64+
)
65+
);
66+
}
67+
}

tests/ArraysTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
class ArraysTest extends BaseTestCase
4+
{
5+
public function getInvalidTests()
6+
{
7+
return array(
8+
array(
9+
'{
10+
"array":[1,2,"a"]
11+
}',
12+
'{
13+
"type":"object",
14+
"properties":{
15+
"array":{
16+
"type":"array",
17+
"items":{"type":"number"}
18+
}
19+
}
20+
}'
21+
),
22+
array(
23+
'{
24+
"array":[1,2,null]
25+
}',
26+
'{
27+
"type":"object",
28+
"properties":{
29+
"array":{
30+
"type":"array",
31+
"items":{"type":["number","boolean"]}
32+
}
33+
}
34+
}'
35+
)
36+
);
37+
}
38+
39+
public function getValidTests()
40+
{
41+
return array(
42+
array(
43+
'{
44+
"array":[1,2,"a"]
45+
}',
46+
'{
47+
"type":"object",
48+
"properties":{
49+
"array":{"type":"array"}
50+
}
51+
}'
52+
)
53+
);
54+
}
55+
}

tests/BaseTestCase.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
abstract class BaseTestCase extends PHPUnit_Framework_TestCase
4+
{
5+
/**
6+
* @dataProvider getInvalidTests
7+
*/
8+
public function testInvalidCases($input, $schema, $errors = array())
9+
{
10+
$result = JsonSchema::validate(json_decode($input), json_decode($schema));
11+
if (array() !== $errors) {
12+
$this->assertEquals($errors, $result->errors);
13+
}
14+
$this->assertFalse($result->valid, var_export($result, true));
15+
}
16+
17+
/**
18+
* @dataProvider getValidTests
19+
*/
20+
public function testValidCases($input, $schema, $checkMode = null)
21+
{
22+
if (null === $checkMode) {
23+
$checkMode = JsonSchema::CHECK_MODE_NORMAL;
24+
}
25+
JsonSchema::$checkMode = $checkMode;
26+
$result = JsonSchema::validate(json_decode($input), json_decode($schema));
27+
$this->assertTrue($result->valid, var_export($result, true));
28+
}
29+
30+
abstract public function getValidTests();
31+
32+
abstract public function getInvalidTests();
33+
}

tests/BasicTypesTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
class BasicTypesTest extends BaseTestCase
4+
{
5+
public function getInvalidTests()
6+
{
7+
return array(
8+
array(
9+
'{
10+
"string":null,
11+
"number":null,
12+
"integer":null,
13+
"boolean":null,
14+
"object":null,
15+
"array":null,
16+
"null":1
17+
}',
18+
'{
19+
"type":"object",
20+
"properties":{
21+
"string":{"type":"string"},
22+
"number":{"type":"number"},
23+
"integer":{"type":"integer"},
24+
"boolean":{"type":"boolean"},
25+
"object":{"type":"object"},
26+
"array":{"type":"array"},
27+
"null":{"type":"null"}
28+
},
29+
"additionalProperties":false
30+
}'
31+
)
32+
);
33+
}
34+
35+
public function getValidTests()
36+
{
37+
return array(
38+
array(
39+
'{
40+
"string":"string test",
41+
"number":1,
42+
"integer":1,
43+
"boolean":true,
44+
"object":{},
45+
"array":[],
46+
"null":null,
47+
"any": "string",
48+
"any1": 2.6,
49+
"any2": 4,
50+
"any3": false,
51+
"any4": {},
52+
"any5": [],
53+
"any6": null
54+
}',
55+
'{
56+
"type":"object",
57+
"properties":{
58+
"string":{"type":"string"},
59+
"number":{"type":"number"},
60+
"integer":{"type":"integer"},
61+
"boolean":{"type":"boolean"},
62+
"object":{"type":"object"},
63+
"array":{"type":"array"},
64+
"null":{"type":"null"},
65+
"any": {"type":"any"},
66+
"any1": {"type":"any"},
67+
"any2": {"type":"any"},
68+
"any3": {"type":"any"},
69+
"any4": {"type":"any"},
70+
"any5": {"type":"any"},
71+
"any6": {"type":"any"}
72+
},
73+
"additionalProperties":false
74+
}'
75+
)
76+
);
77+
}
78+
}

tests/DisallowTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
class DisallowTest extends BaseTestCase
4+
{
5+
public function getInvalidTests()
6+
{
7+
return array(
8+
array(
9+
'{
10+
"value":" The xpto is weird"
11+
}',
12+
'{
13+
"type":"object",
14+
"properties":{
15+
"value":{
16+
"type":"any",
17+
"disallow":{"type":"string","pattern":"xpto"}
18+
}
19+
}
20+
}'
21+
),
22+
array(
23+
'{
24+
"value":null
25+
}',
26+
'{
27+
"type":"object",
28+
"properties":{
29+
"value":{
30+
"type":"any",
31+
"disallow":{"type":"null"}
32+
}
33+
}
34+
}'
35+
)
36+
);
37+
}
38+
39+
public function getValidTests()
40+
{
41+
return array(
42+
array(
43+
'{
44+
"value":" The xpto is weird"
45+
}',
46+
'{
47+
"type":"object",
48+
"properties":{
49+
"value":{
50+
"type":"any",
51+
"disallow":{"type":"string","pattern":"^xpto"}
52+
}
53+
}
54+
}'
55+
),
56+
array(
57+
'{
58+
"value":1
59+
}',
60+
'{
61+
"type":"object",
62+
"properties":{
63+
"value":{
64+
"type":"any",
65+
"disallow":{"type":"null"}
66+
}
67+
}
68+
}'
69+
)
70+
);
71+
}
72+
}

tests/EnumTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
class EnumTest extends BaseTestCase
4+
{
5+
public function getInvalidTests()
6+
{
7+
return array(
8+
array(
9+
'{
10+
"value":"Morango"
11+
}',
12+
'{
13+
"type":"object",
14+
"properties":{
15+
"value":{"type":"string","enum":["Abacate","Manga","Pitanga"]}
16+
},
17+
"additionalProperties":false
18+
}'
19+
)
20+
);
21+
}
22+
23+
public function getValidTests()
24+
{
25+
return array(
26+
array(
27+
'{
28+
"value":"Abacate"
29+
}',
30+
'{
31+
"type":"object",
32+
"properties":{
33+
"value":{"type":"string","enum":["Abacate","Manga","Pitanga"]}
34+
},
35+
"additionalProperties":false
36+
}'
37+
)
38+
);
39+
}
40+
}

0 commit comments

Comments
 (0)