JsonSchemaConstraint   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A toString() 0 4 1
A matches() 0 6 1
A failureDescription() 0 4 1
A additionalFailureDescription() 0 4 1
1
<?php
2
/**
3
 * This source file is proprietary and part of Rebilly.
4
 *
5
 * (c) Rebilly SRL
6
 *     Rebilly Ltd.
7
 *     Rebilly Inc.
8
 *
9
 * @see https://www.rebilly.com
10
 */
11
12
namespace Rebilly\OpenAPI\PhpUnit;
13
14
use PHPUnit\Framework\Constraint\Constraint;
15
use Rebilly\OpenAPI\JsonSchema\Validator;
16
use stdClass;
17
18
/**
19
 * Constraint that asserts that the object matches the expected JSON Schema.
20
 */
21
final class JsonSchemaConstraint extends Constraint
22
{
23
    private $schema;
24
25
    private $context;
26
27
    private $validator;
28
29
    private $errors = [];
30
31 9
    public function __construct(stdClass $schema, string $context = null)
32
    {
33 9
        $this->schema = $schema;
34 9
        $this->context = $context ?: 'schema';
35 9
        $this->validator = new Validator($this->context);
36
    }
37
38 4
    public function toString(): string
39
    {
40 4
        return "matches defined {$this->context}";
41
    }
42
43 9
    protected function matches($other): bool
44
    {
45 9
        $this->errors = $this->validator->validate($other, $this->schema);
46
47 9
        return empty($this->errors);
48
    }
49
50 4
    protected function failureDescription($other): string
51
    {
52 4
        return json_encode($other) . ' ' . $this->toString();
53
    }
54
55 4
    protected function additionalFailureDescription($other): string
56
    {
57 4
        return $this->validator->serializeErrors($this->errors);
58
    }
59
}
60