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

SchemaAssertion   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 39
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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