SchemaAssertions::validatorPasses()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace HSkrasek\JsonSchema;
4
5
use HSkrasek\JsonSchema\Validator\Errors;
6
use League\JsonGuard\Validator;
7
use League\JsonReference\Dereferencer;
8
use PHPUnit\Framework\Constraint\Constraint;
9
10
trait SchemaAssertions
11
{
12
    /**
13
     * @var Dereferencer
14
     */
15
    private $dereferencer;
16
17
    public function setupSchemaAssertions()
18
    {
19
        $this->dereferencer = Dereferencer::draft4();
20
    }
21
22
    /**
23
     * @param mixed $json
24
     * @param string $schemaPath
25
     */
26
    public function assertJsonMatchesSchema($json, string $schemaPath): void
27
    {
28
        $validator = new Validator($json, $this->dereferencer->dereference($this->normalizeSchemaPath($schemaPath)));
29
30
        static::assertThat($validator, static::validatorPasses(), new Errors($validator->errors()));
31
    }
32
33
    private function normalizeSchemaPath(string $schemaPath): string
34
    {
35
        if (strpos($schemaPath, 'http://') === 0 || strpos($schemaPath, 'https://') === 0) {
36
            return $schemaPath;
37
        }
38
39
        return "file://$schemaPath";
40
    }
41
42
    /**
43
     * @return ValidatorPasses
44
     */
45
    private static function validatorPasses(): Constraint
46
    {
47
        return new ValidatorPasses;
48
    }
49
}
50