Completed
Push — master ( e0b298...15a82e )
by TJ
06:59
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
9
class SchemaAssertion
10
{
11
    protected $schema;
12
13 4
    public function __construct($schema)
14
    {
15 4
        if (is_array($schema)) {
16 1
            $schema = json_encode($schema);
17
        }
18
19 4
        if (is_string($schema) && $this->isJson($schema)) {
20 3
            $schema = json_decode($schema);
21
        }
22
23 4
        $this->schema = Schema::import($schema);
24 4
    }
25
26 4
    public function assert(string $data)
27
    {
28
        try {
29 4
            $this->schema->in(json_decode($data));
30 1
        } catch (InvalidValue $e) {
31 1
            PHPUnit::fail($e->getMessage());
32
        }
33
34 3
        PHPUnit::assertTrue(true);
35 3
    }
36
37 4
    public function isJson($schema)
38
    {
39 4
        json_decode($schema);
40
41 4
        return json_last_error() === JSON_ERROR_NONE;
42
    }
43
}
44