10
10
*/
11
11
class JsonSchema extends Module
12
12
{
13
-
14
13
/**
15
14
* Validate response by json schema
16
- * @param string $schemaRef object or json string
15
+ * @param string|object|array|null $schemaRef object or json string
16
+ * @return void
17
17
*/
18
- public function seeResponseIsValidOnSchema ($ schemaRef)
18
+ public function seeResponseIsValidOnSchema ($ schemaRef, $ response = null ): void
19
19
{
20
- $ response = $ this ->getModule ('REST ' )->response ;
20
+ if (!isset ($ response )) {
21
+ $ response = $ this ->getModule ('REST ' )->grabResponse ();
22
+ }
23
+ if (is_array ($ response ) || is_object ($ response )) {
24
+ $ response = json_encode ($ response );
25
+ }
21
26
22
27
$ validator = new Validator ();
23
28
$ decodedResponse = json_decode ($ response );
@@ -26,6 +31,7 @@ public function seeResponseIsValidOnSchema($schemaRef)
26
31
$ message = '' ;
27
32
$ isValid = $ validator ->isValid ();
28
33
if (!$ isValid ) {
34
+ $ this ->debug ($ response );
29
35
$ message = 'JSON does not validate. Violations: ' . PHP_EOL ;
30
36
foreach ($ validator ->getErrors () as $ error ) {
31
37
$ message .= $ error ['property ' ] . ' ' . $ error ['message ' ] . PHP_EOL ;
@@ -39,9 +45,20 @@ public function seeResponseIsValidOnSchema($schemaRef)
39
45
* Validate response by json schema
40
46
* @param string $schema path to json schema file
41
47
*/
42
- public function seeResponseIsValidOnSchemaFile ($ schema )
48
+ public function seeResponseIsValidOnSchemaFile (string $ schema )
43
49
{
44
50
$ schemaRef = (object )['$ref ' => 'file:// ' . realpath ($ schema )];
45
51
$ this ->seeResponseIsValidOnSchema ($ schemaRef );
46
52
}
53
+
54
+ /**
55
+ * Validate response by json schema
56
+ * @param string|array|null $json json data
57
+ * @param string $schema path to json schema file
58
+ */
59
+ public function seeJsonValidOnSchemaFile ($ json , string $ schema )
60
+ {
61
+ $ schemaRef = (object )['$ref ' => 'file:// ' . realpath ($ schema )];
62
+ $ this ->seeResponseIsValidOnSchema ($ schemaRef , $ json );
63
+ }
47
64
}
0 commit comments