Completed
Push — master ( e0b298...15a82e )
by TJ
06:59
created

SchemaAssertion   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 35
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 4
A assert() 0 10 2
A isJson() 0 6 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