1 | <?php |
||
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) |
|
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) |
|
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) |
|
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) |
|
134 | } |
||
135 |