GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 2f6688...e75e20 )
by Enrico
161:33 queued 154:24
created

Assert::assertJsonMatchesSchemaDepr()   A

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 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the phpunit-json-assertions package.
5
 *
6
 * (c) Enrico Stahn <[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
namespace EnricoStahn\JsonAssert;
13
14
use JsonSchema\Constraints\Factory;
15
use JsonSchema\SchemaStorage;
16
use JsonSchema\Validator;
17
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
18
19
/**
20
 * Asserts to validate JSON data.
21
 *
22
 * - All assert methods expect deserialised JSON data (an actual object or array)
23
 *   since the deserialisation method should be up to the user.
24
 * - We provide a convenience method to transfer whatever into a JSON object (see ::getJsonObject(mixed))
25
 */
26
trait Assert
27
{
28
    /**
29
     * @var SchemaStorage
30
     */
31
    private static $schemaStorage = null;
32
33
    /**
34
     * Asserts that json content is valid according to the provided schema file.
35
     *
36
     * Example:
37
     *
38 9
     *   static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json')
39
     *
40
     * @param string|null  $schema  Path to the schema file
41
     * @param array|object $content JSON array or object
42 9
     */
43 9
    public static function assertJsonMatchesSchema($content, $schema = null)
44
    {
45 9
        if (self::$schemaStorage === null) {
46 9
            self::$schemaStorage = new SchemaStorage();
47
        }
48 9
49 9
        if ($schema !== null && !file_exists($schema)) {
50 3
            throw new FileNotFoundException($schema);
51 9
        }
52 9
53
        $schemaObject = null;
54 9
55 6
        if ($schema !== null) {
56
            $schemaObject = json_decode(file_get_contents($schema));
57
            self::$schemaStorage->addSchema('file://'.$schema, $schemaObject);
58
        }
59
60
        $validator = new Validator(new Factory(self::$schemaStorage));
61
        $validator->validate($content, $schemaObject);
62
63 2
        $message = '- Property: %s, Contraint: %s, Message: %s';
64
        $messages = array_map(function ($exception) use ($message) {
65 2
            return sprintf($message, $exception['property'], $exception['constraint'], $exception['message']);
66 2
        }, $validator->getErrors());
67
        $messages[] = '- Response: '.json_encode($content);
68 2
69 2
        \PHPUnit\Framework\Assert::assertTrue($validator->isValid(), implode("\n", $messages));
70
    }
71
72
    /**
73
     * Asserts that json content is valid according to the provided schema file.
74
     *
75
     * Example:
76
     *
77
     *   static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json')
78
     *
79
     * @param string|null  $schema  Path to the schema file
80
     * @param array|object $content JSON array or object
81
     *
82
     * @deprecated This will be removed in the next major version (4.x).
83 4
     */
84
    public static function assertJsonMatchesSchemaDepr($schema, $content)
85 4
    {
86
        self::assertJsonMatchesSchema($content, $schema);
87 4
    }
88 3
89 3
    /**
90
     * Asserts that json content is valid according to the provided schema string.
91
     *
92
     * @param string       $schema  Schema data
93
     * @param array|object $content JSON content
94
     */
95
    public static function assertJsonMatchesSchemaString($schema, $content)
96
    {
97
        $file = tempnam(sys_get_temp_dir(), 'json-schema-');
98 4
        file_put_contents($file, $schema);
99
100 4
        self::assertJsonMatchesSchema($content, $file);
101
    }
102
103
    /**
104
     * Asserts if the value retrieved with the expression equals the expected value.
105
     *
106
     * Example:
107
     *
108
     *     static::assertJsonValueEquals(33, 'foo.bar[0]', $json);
109
     *
110
     * @param mixed        $expected   Expected value
111
     * @param string       $expression Expression to retrieve the result
112
     *                                 (e.g. locations[?state == 'WA'].name | sort(@))
113
     * @param array|object $json       JSON Content
114
     */
115
    public static function assertJsonValueEquals($expected, $expression, $json)
116
    {
117
        $result = \JmesPath\Env::search($expression, $json);
118
119
        \PHPUnit\Framework\Assert::assertEquals($expected, $result);
120
        \PHPUnit\Framework\Assert::assertInternalType(strtolower(gettype($expected)), $result);
121
    }
122
123
    /**
124
     * Helper method to deserialise a JSON string into an object.
125
     *
126
     * @param mixed $data The JSON string
127
     *
128
     * @return array|object
129
     */
130
    public static function getJsonObject($data)
131
    {
132
        return (is_array($data) || is_object($data)) ? $data : json_decode($data);
133
    }
134
}
135