Passed
Pull Request — master (#3003)
by Han Hui
04:31
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 ApiPlatform\Core\JsonSchema\Schema;
17
use JsonSchema\Validator;
18
use PHPUnit\Framework\Constraint\Constraint;
19
20
/**
21
 * Asserts that a JSON document matches a given JSON Schema.
22
 *
23
 * @author Kévin Dunglas <[email protected]>
24
 *
25
 * @experimental
26
 */
27
final class MatchesJsonSchema extends Constraint
28
{
29
    private $schema;
30
    private $checkMode;
31
32
    /**
33
     * @param array|string $schema
34
     */
35
    public function __construct($schema, ?int $checkMode = null)
36
    {
37
        $this->schema = \is_array($schema) ? (object) $schema : json_decode($schema);
38
        $this->checkMode = $checkMode;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function toString(): string
45
    {
46
        return 'matches the provided JSON Schema';
47
    }
48
49
    /**
50
     * @param array|object $other
51
     */
52
    protected function matches($other): bool
53
    {
54
        if (!class_exists(Validator::class)) {
55
            throw new \RuntimeException('The "justinrainbow/json-schema" library must be installed to use "assertMatchesJsonSchema()". Try running "composer require --dev justinrainbow/json-schema".');
56
        }
57
58
        $other = $this->canonicalizeJson($other);
59
60
        $validator = new Validator();
61
        $validator->validate($other, $this->schema, $this->checkMode);
62
63
        return $validator->isValid();
64
    }
65
66
    /**
67
     * @param array|object $other
68
     */
69
    protected function additionalFailureDescription($other): string
70
    {
71
        $other = $this->canonicalizeJson($other);
72
73
        $validator = new Validator();
74
        $validator->check($other, $this->schema);
75
76
        $errors = [];
77
        foreach ($validator->getErrors() as $error) {
78
            $property = $error['property'] ? $error['property'].': ' : '';
79
            $errors[] = $property.$error['message'];
80
        }
81
82
        return implode("\n", $errors);
83
    }
84
85
    /**
86
     * @param array|object $data A representation of a JSON array or JSON object
87
     *
88
     * @return array|object An array if data is a JSON array, or an object if data is a JSON object
89
     */
90
    private function canonicalizeJson($data)
91
    {
92
        if (!(\is_array($data) || \is_object($data))) {
93
            throw new \InvalidArgumentException('Data must be array or object.');
94
        }
95
96
        $data = (string) json_encode($data);
97
        $data = json_decode($data);
98
99
        if (!(\is_array($data) || \is_object($data))) {
100
            throw new \UnexpectedValueException('JSON encoding / decoding failed.');
101
        }
102
103
        return $data;
104
    }
105
}
106