Passed
Pull Request — master (#3003)
by Han Hui
04:27
created

MatchesJsonSchema::canonicalizeJson()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 3
nop 1
dl 0
loc 14
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Constraint;
15
16
use JsonSchema\Validator;
17
use PHPUnit\Framework\Constraint\Constraint;
18
19
/**
20
 * Asserts that a JSON document matches a given JSON Schema.
21
 *
22
 * @author Kévin Dunglas <[email protected]>
23
 *
24
 * @experimental
25
 */
26
final class MatchesJsonSchema extends Constraint
27
{
28
    /**
29
     * @var object
30
     */
31
    private $schema;
32
    private $checkMode;
33
34
    /**
35
     * @param array|string $schema
36
     */
37
    public function __construct($schema, ?int $checkMode = null)
38
    {
39
        $this->schema = \is_array($schema) ? (object) $schema : json_decode($schema);
40
        $this->checkMode = $checkMode;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function toString(): string
47
    {
48
        return 'matches the provided JSON Schema';
49
    }
50
51
    protected function matches($other): bool
52
    {
53
        if (!class_exists(Validator::class)) {
54
            throw new \LogicException('The "justinrainbow/json-schema" library must be installed to use "assertMatchesJsonSchema()". Try running "composer require --dev justinrainbow/json-schema".');
55
        }
56
57
        $other = $this->canonicalizeJson($other);
58
59
        $validator = new Validator();
60
        $validator->validate($other, $this->schema, $this->checkMode);
61
62
        return $validator->isValid();
63
    }
64
65
    protected function additionalFailureDescription($other): string
66
    {
67
        $other = $this->canonicalizeJson($other);
68
69
        $validator = new Validator();
70
        $validator->validate($other, $this->schema, $this->checkMode);
71
72
        $errors = [];
73
        foreach ($validator->getErrors() as $error) {
74
            $property = $error['property'] ? $error['property'].': ' : '';
75
            $errors[] = $property.$error['message'];
76
        }
77
78
        return implode("\n", $errors);
79
    }
80
81
    private function canonicalizeJson($data)
82
    {
83
        if (!(\is_array($data) || \is_object($data))) {
84
            return $data;
85
        }
86
87
        $data = (string) json_encode($data);
88
        $data = json_decode($data);
89
90
        if (!(\is_array($data) || \is_object($data))) {
91
            throw new \UnexpectedValueException('JSON encoding / decoding failed.');
92
        }
93
94
        return $data;
95
    }
96
}
97