additionalFailureDescription()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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