Debian Codegraph @

tests/phpunit/JsonSchemaAssertionTrait.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19 use JsonSchema\Constraints\Constraint;
20 use JsonSchema\Constraints\Factory;
21 use JsonSchema\Validator;
22
23 /**
24 * Trait for validating data structures against JSON schemas.
25 *
26 * Based on the jsonrainbow/json-schema package, supports Draft-3 and Draft-4
27 * schemas.
28 *
29 * @stable to use in extensions
30 * @since 1.43
31 */
32 trait JsonSchemaAssertionTrait {
33
34 /**
35 * Load data from a JSON file.
36 *
37 * @param string $jsonFile The path of the JSON file
38 *
39 * @return mixed
40 */
41 private static function loadJsonData( string $jsonFile ) {
42 $json = file_get_contents( $jsonFile );
43
44 if ( $json === false ) {
45 throw new InvalidArgumentException( "Unable to load content of $jsonFile" );
46 }
47
48 return json_decode( $json, false, JSON_THROW_ON_ERROR );
49 }
50
51 /**
52 * @param string|array $schema A JSON schema as an array structure, or the
53 * file path of a JSON schema file.
54 * @param string|array|stdClass $json A JSONic data structure, or a JSON string.
55 * @param array<string, string> $resources Mapping of schema IDs to local files,
56 * to avoid loading schemas over the network during testing.
57 */
58 private function assertMatchesJsonSchema( $schema, $json, array $resources = [] ): void {
59 if ( is_string( $json ) ) {
60 try {
61 $json = json_decode( $json, false, JSON_THROW_ON_ERROR );
62 } catch ( JsonException $ex ) {
63 self::fail( 'Invalid JSON: ' . $ex->getMessage() );
64 }
65 }
66 if ( is_string( $schema ) ) {
67 // Let the JsonException propagate, this indicates a bug in the test,
68 // not a test failure.
69 $schema = self::loadJsonData( $schema );
70 }
71
72 $factory = new Factory();
73
74 foreach ( $resources as $id => $rc ) {
75 $factory->getSchemaStorage()->addSchema(
76 rtrim( $id, '#' ),
77 self::loadJsonData( $rc )
78 );
79 }
80
81 $validator = new Validator( $factory );
82 $validator->validate(
83 $json, $schema,
84 Constraint::CHECK_MODE_TYPE_CAST
85 );
86
87 if ( !$validator->isValid() ) {
88 foreach ( $validator->getErrors() as $error ) {
89 $error = json_encode( $error );
90 self::fail( "JSON schema validation failed: $error" );
91 }
92 }
93
94 $this->addToAssertionCount( 1 );
95 }
96
97 /**
98 * Validate a JSON schema.
99 *
100 * Currently only works for schemas that match Draft-4.
101 *
102 * @param array $schema The schema to validate.
103 * @throws LogicException if the schema is invalid
104 */
105 public function assertValidJsonSchema( array $schema ): void {
106 // Load the draft-04 schema from the local file
107 $metaSchema = MW_INSTALL_PATH . '/vendor/justinrainbow/json-schema/dist/' .
108 'schema/json-schema-draft-04.json';
109
110 self::assertMatchesJsonSchema( $metaSchema, $schema );
111 }
112
113 }