File tree Expand file tree Collapse file tree 7 files changed +92
-11
lines changed Expand file tree Collapse file tree 7 files changed +92
-11
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,11 @@ public function it_has_a_valid_response()
37
37
38
38
$response = $this->get('/foo');
39
39
40
- $response->assertMatchesJsonSchema(json_encode($schema));
40
+ // Schema as an array
41
+ $response->assertJsonSchema($schema);
42
+
43
+ // Schema from a file
44
+ $response->assertJsonSchema(base_path('schemas/foo.json'));
41
45
}
42
46
```
43
47
@@ -56,7 +60,7 @@ Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed re
56
60
Please see [ CONTRIBUTING] ( CONTRIBUTING.md ) for details.
57
61
58
62
## Code Style
59
- In addition to the php-cs-fixer rules, StyleCI will apply the [ Laravel preset] ( https://docs.styleci.io/presets#laravel ) .
63
+ In addition to the php-cs-fixer rules, StyleCI will apply the [ Laravel preset] ( https://docs.styleci.io/presets#laravel ) .
60
64
61
65
### Linting
62
66
``` bash
Original file line number Diff line number Diff line change 20
20
],
21
21
"require" : {
22
22
"php" : " ^7.1|^7.2" ,
23
- "swaggest/json-schema" : " ^0.12.0"
23
+ "swaggest/json-schema" : " ^0.12.0" ,
24
+ "phpunit/phpunit" : " ^7.0"
24
25
},
25
26
"require-dev" : {
26
27
"friendsofphp/php-cs-fixer" : " ^2.12" ,
27
- "orchestra/testbench" : " ^3.5|^3.6" ,
28
- "phpunit/phpunit" : " ^7.0"
28
+ "orchestra/testbench" : " ^3.5|^3.6"
29
29
},
30
30
"autoload" : {
31
31
"psr-4" : {
Original file line number Diff line number Diff line change 5
5
use Swaggest \JsonSchema \Schema ;
6
6
use Swaggest \JsonSchema \InvalidValue ;
7
7
use PHPUnit \Framework \Assert as PHPUnit ;
8
+ use sixlive \Laravel \JsonSchemaAssertions \Support \Str ;
8
9
9
10
class SchemaAssertion
10
11
{
11
12
protected $ schema ;
12
13
13
- public function __construct (string $ schema )
14
+ /**
15
+ * @param array|string $schema
16
+ *
17
+ * @return void
18
+ */
19
+ public function __construct ($ schema )
14
20
{
15
- $ this ->schema = Schema::import (json_decode ($ schema ));
21
+ if (is_array ($ schema )) {
22
+ $ schema = json_encode ($ schema );
23
+ }
24
+
25
+ if (is_string ($ schema ) && Str::isJson ($ schema )) {
26
+ $ schema = json_decode ($ schema );
27
+ }
28
+
29
+ $ this ->schema = Schema::import ($ schema );
16
30
}
17
31
32
+ /**
33
+ * Assert JSON against the loaded schema.
34
+ *
35
+ * @param string $data
36
+ *
37
+ * @return void
38
+ */
18
39
public function assert (string $ data )
19
40
{
20
41
try {
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ class ServiceProvider extends Provider
12
12
*/
13
13
public function boot ()
14
14
{
15
- TestResponse::macro ('assertMatchesJsonSchema ' , function ($ schema ) {
15
+ TestResponse::macro ('assertJsonSchema ' , function ($ schema ) {
16
16
(new SchemaAssertion ($ schema ))->assert ($ this ->content ());
17
17
});
18
18
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace sixlive \Laravel \JsonSchemaAssertions \Support ;
4
+
5
+ class Str
6
+ {
7
+ /**
8
+ * Test to see if a string is json.
9
+ *
10
+ * @param string $string
11
+ *
12
+ * @return bool
13
+ */
14
+ public static function isJson ($ string )
15
+ {
16
+ json_decode ($ string );
17
+
18
+ return json_last_error () === JSON_ERROR_NONE ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ public function setUp()
19
19
}
20
20
21
21
/** @test */
22
- public function valid_schema_passes ()
22
+ public function valid_schema_passes_as_json ()
23
23
{
24
24
$ schema = [
25
25
'type ' => 'object ' ,
@@ -33,7 +33,32 @@ public function valid_schema_passes()
33
33
],
34
34
];
35
35
36
- $ this ->get ('foo ' )->assertMatchesJsonSchema (json_encode ($ schema ));
36
+ $ this ->get ('foo ' )->assertJsonSchema (json_encode ($ schema ));
37
+ }
38
+
39
+ /** @test */
40
+ public function valid_schema_passes_as_array ()
41
+ {
42
+ $ schema = [
43
+ 'type ' => 'object ' ,
44
+ 'properties ' => [
45
+ 'foo ' => [
46
+ 'type ' => 'string ' ,
47
+ ],
48
+ ],
49
+ 'required ' => [
50
+ 'foo ' ,
51
+ ],
52
+ ];
53
+
54
+ $ this ->get ('foo ' )->assertJsonSchema ($ schema );
55
+ }
56
+
57
+ /** @test */
58
+ public function valid_schema_passes_as_file_path ()
59
+ {
60
+ $ this ->get ('foo ' )
61
+ ->assertJsonSchema (__DIR__ .'/Support/Schemas/foo.json ' );
37
62
}
38
63
39
64
/** @test */
@@ -53,6 +78,6 @@ public function invalid_schema_fails_with_message()
53
78
],
54
79
];
55
80
56
- $ this ->get ('foo ' )->assertMatchesJsonSchema (json_encode ($ schema ));
81
+ $ this ->get ('foo ' )->assertJsonSchema (json_encode ($ schema ));
57
82
}
58
83
}
Original file line number Diff line number Diff line change
1
+ {
2
+ "properties" : {
3
+ "foo" : {
4
+ "type" : " string"
5
+ }
6
+ },
7
+ "required" : [
8
+ " foo"
9
+ ],
10
+ "type" : " object"
11
+ }
You can’t perform that action at this time.
0 commit comments