Skip to content

Commit ee2edde

Browse files
committed
API Updates (#1)
* Supports Schema as an array * Supports Schema from file as path * Supports Schema as JSON * Updated method (BREAKING CHANGE) * Adds PHPUnit as a required dependency since this is for testing
1 parent c726392 commit ee2edde

File tree

7 files changed

+92
-11
lines changed

7 files changed

+92
-11
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ public function it_has_a_valid_response()
3737

3838
$response = $this->get('/foo');
3939

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'));
4145
}
4246
```
4347

@@ -56,7 +60,7 @@ Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed re
5660
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
5761

5862
## 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).
6064

6165
### Linting
6266
```bash

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
],
2121
"require": {
2222
"php": "^7.1|^7.2",
23-
"swaggest/json-schema": "^0.12.0"
23+
"swaggest/json-schema": "^0.12.0",
24+
"phpunit/phpunit": "^7.0"
2425
},
2526
"require-dev": {
2627
"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"
2929
},
3030
"autoload": {
3131
"psr-4": {

src/SchemaAssertion.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,37 @@
55
use Swaggest\JsonSchema\Schema;
66
use Swaggest\JsonSchema\InvalidValue;
77
use PHPUnit\Framework\Assert as PHPUnit;
8+
use sixlive\Laravel\JsonSchemaAssertions\Support\Str;
89

910
class SchemaAssertion
1011
{
1112
protected $schema;
1213

13-
public function __construct(string $schema)
14+
/**
15+
* @param array|string $schema
16+
*
17+
* @return void
18+
*/
19+
public function __construct($schema)
1420
{
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);
1630
}
1731

32+
/**
33+
* Assert JSON against the loaded schema.
34+
*
35+
* @param string $data
36+
*
37+
* @return void
38+
*/
1839
public function assert(string $data)
1940
{
2041
try {

src/ServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ServiceProvider extends Provider
1212
*/
1313
public function boot()
1414
{
15-
TestResponse::macro('assertMatchesJsonSchema', function ($schema) {
15+
TestResponse::macro('assertJsonSchema', function ($schema) {
1616
(new SchemaAssertion($schema))->assert($this->content());
1717
});
1818
}

src/Support/Str.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

tests/AssertResponseTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function setUp()
1919
}
2020

2121
/** @test */
22-
public function valid_schema_passes()
22+
public function valid_schema_passes_as_json()
2323
{
2424
$schema = [
2525
'type' => 'object',
@@ -33,7 +33,32 @@ public function valid_schema_passes()
3333
],
3434
];
3535

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');
3762
}
3863

3964
/** @test */
@@ -53,6 +78,6 @@ public function invalid_schema_fails_with_message()
5378
],
5479
];
5580

56-
$this->get('foo')->assertMatchesJsonSchema(json_encode($schema));
81+
$this->get('foo')->assertJsonSchema(json_encode($schema));
5782
}
5883
}

tests/Support/Schemas/foo.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"properties": {
3+
"foo": {
4+
"type": "string"
5+
}
6+
},
7+
"required": [
8+
"foo"
9+
],
10+
"type": "object"
11+
}

0 commit comments

Comments
 (0)