|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the phpunit-json-assertions package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Enrico Stahn <[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 EnricoStahn\JsonAssert; |
|
13
|
|
|
|
|
14
|
|
|
use JsonSchema\Constraints\Factory; |
|
15
|
|
|
use JsonSchema\SchemaStorage; |
|
16
|
|
|
use JsonSchema\Validator; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Asserts to validate JSON data. |
|
21
|
|
|
* |
|
22
|
|
|
* - All assert methods expect deserialised JSON data (an actual object or array) |
|
23
|
|
|
* since the deserialisation method should be up to the user. |
|
24
|
|
|
* - We provide a convenience method to transfer whatever into a JSON object (see ::getJsonObject(mixed)) |
|
25
|
|
|
*/ |
|
26
|
|
|
trait Assert |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var SchemaStorage |
|
30
|
|
|
*/ |
|
31
|
|
|
private static $schemaStorage = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Asserts that json content is valid according to the provided schema file. |
|
35
|
|
|
* |
|
36
|
|
|
* Example: |
|
37
|
|
|
* |
|
38
|
|
|
* static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json') |
|
39
|
|
|
* |
|
40
|
|
|
* @param string|null $schema Path to the schema file |
|
41
|
|
|
* @param array|object $content JSON array or object |
|
42
|
|
|
*/ |
|
43
|
9 |
|
public static function assertJsonMatchesSchema($content, $schema = null) |
|
44
|
|
|
{ |
|
45
|
9 |
|
if (self::$schemaStorage === null) { |
|
46
|
2 |
|
self::$schemaStorage = new SchemaStorage(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
9 |
|
if ($schema !== null && !file_exists($schema)) { |
|
50
|
|
|
throw new FileNotFoundException($schema); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
9 |
|
$schemaObject = null; |
|
54
|
|
|
|
|
55
|
9 |
|
if ($schema !== null) { |
|
56
|
9 |
|
$schemaObject = json_decode(file_get_contents($schema)); |
|
57
|
9 |
|
self::$schemaStorage->addSchema('file://'.$schema, $schemaObject); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
9 |
|
$validator = new Validator(new Factory(self::$schemaStorage)); |
|
61
|
9 |
|
$validator->validate($content, $schemaObject); |
|
62
|
|
|
|
|
63
|
9 |
|
$message = '- Property: %s, Contraint: %s, Message: %s'; |
|
64
|
|
|
$messages = array_map(function ($exception) use ($message) { |
|
65
|
3 |
|
return sprintf($message, $exception['property'], $exception['constraint'], $exception['message']); |
|
66
|
9 |
|
}, $validator->getErrors()); |
|
67
|
9 |
|
$messages[] = '- Response: '.json_encode($content); |
|
68
|
|
|
|
|
69
|
9 |
|
\PHPUnit\Framework\Assert::assertTrue($validator->isValid(), implode("\n", $messages)); |
|
70
|
6 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Asserts that json content is valid according to the provided schema file. |
|
74
|
|
|
* |
|
75
|
|
|
* Example: |
|
76
|
|
|
* |
|
77
|
|
|
* static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json') |
|
78
|
|
|
* |
|
79
|
|
|
* @param string|null $schema Path to the schema file |
|
80
|
|
|
* @param array|object $content JSON array or object |
|
81
|
|
|
* |
|
82
|
|
|
* @deprecated This will be removed in the next major version (4.x). |
|
83
|
|
|
*/ |
|
84
|
7 |
|
public static function assertJsonMatchesSchemaDepr($schema, $content) |
|
85
|
|
|
{ |
|
86
|
7 |
|
self::assertJsonMatchesSchema($content, $schema); |
|
87
|
4 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Asserts that json content is valid according to the provided schema string. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $schema Schema data |
|
93
|
|
|
* @param array|object $content JSON content |
|
94
|
|
|
*/ |
|
95
|
2 |
|
public static function assertJsonMatchesSchemaString($schema, $content) |
|
96
|
|
|
{ |
|
97
|
2 |
|
$file = tempnam(sys_get_temp_dir(), 'json-schema-'); |
|
98
|
2 |
|
file_put_contents($file, $schema); |
|
99
|
|
|
|
|
100
|
2 |
|
self::assertJsonMatchesSchema($content, $file); |
|
101
|
2 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Asserts if the value retrieved with the expression equals the expected value. |
|
105
|
|
|
* |
|
106
|
|
|
* Example: |
|
107
|
|
|
* |
|
108
|
|
|
* static::assertJsonValueEquals(33, 'foo.bar[0]', $json); |
|
109
|
|
|
* |
|
110
|
|
|
* @param mixed $expected Expected value |
|
111
|
|
|
* @param string $expression Expression to retrieve the result |
|
112
|
|
|
* (e.g. locations[?state == 'WA'].name | sort(@)) |
|
113
|
|
|
* @param array|object $json JSON Content |
|
114
|
|
|
*/ |
|
115
|
4 |
|
public static function assertJsonValueEquals($expected, $expression, $json) |
|
116
|
|
|
{ |
|
117
|
4 |
|
$result = \JmesPath\Env::search($expression, $json); |
|
118
|
|
|
|
|
119
|
4 |
|
\PHPUnit\Framework\Assert::assertEquals($expected, $result); |
|
120
|
3 |
|
\PHPUnit\Framework\Assert::assertInternalType(strtolower(gettype($expected)), $result); |
|
121
|
3 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Helper method to deserialise a JSON string into an object. |
|
125
|
|
|
* |
|
126
|
|
|
* @param mixed $data The JSON string |
|
127
|
|
|
* |
|
128
|
|
|
* @return array|object |
|
129
|
|
|
*/ |
|
130
|
4 |
|
public static function getJsonObject($data) |
|
131
|
|
|
{ |
|
132
|
4 |
|
return (is_array($data) || is_object($data)) ? $data : json_decode($data); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|