Completed
Push — master ( 1d628c...646e70 )
by TJ
10:26
created

SchemaAssertion::isJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace sixlive\Laravel\JsonSchemaAssertions;
4
5
use Swaggest\JsonSchema\Schema;
6
use Swaggest\JsonSchema\InvalidValue;
7
use PHPUnit\Framework\Assert as PHPUnit;
8
use sixlive\Laravel\JsonSchemaAssertions\Support\Str;
9
10
class SchemaAssertion
11
{
12
    protected $schema;
13
    
14
15
    /**
16
     * @param  array|string  $schema
17
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
18
     */
19 4
    public function __construct($schema)
20
    {
21 4
        if (is_array($schema)) {
22 1
            $schema = json_encode($schema);
23
        }
24
25 4
        if (is_string($schema) && Str::isJson($schema)) {
26 3
            $schema = json_decode($schema);
27
        }
28
29 4
        $this->schema = Schema::import($schema);
30 4
    }
31
32
    /**
33
     * Assert JSON against the loaded schema
34
     * 
35
     * @param  string  $data
36
     * @return void
37
     */
38 4
    public function assert(string $data)
39
    {
40
        try {
41 4
            $this->schema->in(json_decode($data));
42 1
        } catch (InvalidValue $e) {
43 1
            PHPUnit::fail($e->getMessage());
44
        }
45
46 3
        PHPUnit::assertTrue(true);
47 3
    }
48
}
49