Completed
Push — master ( 1d5edb...a1155a )
by Veaceslav
04:30
created

JsonSchemaConstraint::failureDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of Rebilly.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @see http://rebilly.com
9
 */
10
11
namespace Rebilly\OpenAPI\PhpUnit;
12
13
use PHPUnit_Framework_Constraint as BaseConstraint;
14
use Rebilly\OpenAPI\JsonSchema\Validator;
15
16
/**
17
 * Constraint that asserts that the object matches the expected JSON Schema.
18
 */
19
final class JsonSchemaConstraint extends BaseConstraint
20
{
21
    /**
22
     * @var object
23
     */
24
    private $schema;
25
26
    /**
27
     * @var string
28
     */
29
    private $context;
30
31
    /**
32
     * @var Validator
33
     */
34
    private $validator;
35
36
    /**
37
     * @var array
38
     */
39
    private $errors = [];
40
41
    /**
42
     * @param object $schema
43
     * @param string|null $context
44
     */
45 5
    public function __construct($schema, $context = null)
46
    {
47 5
        parent::__construct();
48
49 5
        $this->schema = $schema;
50 5
        $this->context = $context ?: 'schema';
51 5
        $this->validator = new Validator($this->context);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 5
    protected function matches($other)
58
    {
59 5
        $this->errors = $this->validator->validate($other, $this->schema);
60
61 5
        return empty($this->errors);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 4
    protected function failureDescription($other)
68
    {
69 4
        return json_encode($other) . ' ' . $this->toString();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 4
    protected function additionalFailureDescription($other)
76
    {
77 4
        return $this->validator->serializeErrors($this->errors);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 4
    public function toString()
84
    {
85 4
        return "matches defined {$this->context}";
86
    }
87
}
88