1 | <?php |
||
25 | trait Assert |
||
26 | { |
||
27 | /** |
||
28 | * Asserts that json content is valid according to the provided schema file. |
||
29 | * |
||
30 | * Example: |
||
31 | * |
||
32 | * static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json') |
||
33 | * |
||
34 | * @param string $schema Path to the schema file |
||
35 | * @param array|object $content JSON array or object |
||
36 | */ |
||
37 | 4 | public static function assertJsonMatchesSchema($schema, $content) |
|
56 | |||
57 | /** |
||
58 | * Asserts that json content is valid according to the provided schema string. |
||
59 | * |
||
60 | * @param string $schema Schema data |
||
61 | * @param array|object $content JSON content |
||
62 | */ |
||
63 | 1 | public static function assertJsonMatchesSchemaString($schema, $content) |
|
64 | { |
||
65 | 1 | $file = tempnam(sys_get_temp_dir(), 'json-schema-'); |
|
66 | 1 | file_put_contents($file, $schema); |
|
67 | |||
68 | 1 | self::assertJsonMatchesSchema($file, $content); |
|
69 | 1 | } |
|
70 | |||
71 | /** |
||
72 | * Asserts if the value retrieved with the expression equals the expected value. |
||
73 | * |
||
74 | * Example: |
||
75 | * |
||
76 | * static::assertJsonValueEquals(33, 'foo.bar[0]', $json); |
||
77 | * |
||
78 | * @param mixed $expected Expected value |
||
79 | * @param string $expression Expression to retrieve the result (e.g. locations[?state == 'WA'].name | sort(@) | {WashingtonCities: join(', ', @)}) |
||
80 | * @param array|object $json JSON Content |
||
81 | */ |
||
82 | 3 | public static function assertJsonValueEquals($expected, $expression, $json) |
|
89 | |||
90 | /** |
||
91 | * Helper method to deserialise a JSON string into an object. |
||
92 | * |
||
93 | * @param mixed $data The JSON string |
||
94 | * |
||
95 | * @return array|object |
||
96 | */ |
||
97 | 4 | public static function getJsonObject($data) |
|
101 | } |
||
102 |