| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit\Settings\Source; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Settings\Source\JsonSchemaTrait; |
| 7 |
use MediaWiki\Settings\Source\RefLoopException; |
| 8 |
use PHPUnit\Framework\TestCase; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class JsonSchemaTraitTest extends TestCase { |
| 14 |
use JsonSchemaTrait; |
| 15 |
|
| 16 |
public static function providePhpDocToJson() { |
| 17 |
yield 'int' => [ 'int', 'integer' ]; |
| 18 |
yield 'nullable int' => [ '?int', [ 'integer', 'null' ] ]; |
| 19 |
yield 'array input' => [ [ 'list', 'number' ], [ 'array', 'number' ] ]; |
| 20 |
yield 'false or null' => [ 'false|null', [ 'boolean', 'null' ] ]; |
| 21 |
yield 'string or float' => [ 'string|float', [ 'string', 'number' ] ]; |
| 22 |
yield 'array or object' => [ 'array|object', [ 'array', 'object' ] ]; |
| 23 |
yield 'dict or map' => [ 'dict|map', 'object' ]; |
| 24 |
yield 'stdClass' => [ 'stdClass', 'object' ]; |
| 25 |
yield 'nullable list' => [ '?list', [ 'array', 'null' ] ]; |
| 26 |
yield 'Class' => [ 'Something', 'Something' ]; |
| 27 |
yield 'keep integer' => [ 'integer', 'integer' ]; |
| 28 |
yield 'keep number' => [ 'number', 'number' ]; |
| 29 |
yield 'keep null' => [ 'null', 'null' ]; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function testPhpDocToJson( $phpDoc, $json ) { |
| 38 |
$actual = self::phpDocToJson( $phpDoc ); |
| 39 |
$this->assertSame( $json, $actual ); |
| 40 |
} |
| 41 |
|
| 42 |
public static function provideNormalizeJsonSchema() { |
| 43 |
yield 'nullable int' => [ |
| 44 |
[ 'type' => '?int' ], |
| 45 |
[ 'type' => [ 'integer', 'null' ] ] |
| 46 |
]; |
| 47 |
|
| 48 |
yield 'additionalProperties' => [ |
| 49 |
[ |
| 50 |
'type' => '?map', |
| 51 |
'additionalProperties' => [ 'type' => 'list' ] |
| 52 |
], |
| 53 |
[ |
| 54 |
'type' => [ 'object', 'null' ], |
| 55 |
'additionalProperties' => [ 'type' => 'array' ] |
| 56 |
] |
| 57 |
]; |
| 58 |
|
| 59 |
yield 'items' => [ |
| 60 |
[ |
| 61 |
'type' => 'list|false', |
| 62 |
'items' => [ 'type' => 'float' ] |
| 63 |
], |
| 64 |
[ |
| 65 |
'type' => [ 'array', 'boolean' ], |
| 66 |
'items' => [ 'type' => 'number' ] |
| 67 |
] |
| 68 |
]; |
| 69 |
|
| 70 |
yield 'properties' => [ |
| 71 |
[ |
| 72 |
'type' => 'object', |
| 73 |
'properties' => [ |
| 74 |
'foo' => [ 'type' => 'float' ], |
| 75 |
'bar' => [ 'type' => '?list', 'items' => [ 'type' => 'float' ] ], |
| 76 |
] |
| 77 |
], |
| 78 |
[ |
| 79 |
'type' => 'object', |
| 80 |
'properties' => [ |
| 81 |
'foo' => [ 'type' => 'number' ], |
| 82 |
'bar' => [ 'type' => [ 'array', 'null' ], 'items' => [ 'type' => 'number' ] ], |
| 83 |
] |
| 84 |
] |
| 85 |
]; |
| 86 |
|
| 87 |
yield 'nested' => [ |
| 88 |
[ |
| 89 |
'type' => '?dict', |
| 90 |
'additionalProperties' => [ |
| 91 |
'type' => 'string|list', |
| 92 |
'items' => [ |
| 93 |
'type' => 'float', |
| 94 |
] |
| 95 |
] |
| 96 |
], |
| 97 |
[ |
| 98 |
'type' => [ 'object', 'null' ], |
| 99 |
'additionalProperties' => [ |
| 100 |
'type' => [ 'string', 'array' ], |
| 101 |
'items' => [ |
| 102 |
'type' => 'number', |
| 103 |
] |
| 104 |
] |
| 105 |
] |
| 106 |
]; |
| 107 |
|
| 108 |
yield 'references' => [ |
| 109 |
[ |
| 110 |
'type' => 'object', |
| 111 |
'properties' => [ |
| 112 |
'foo' => [ |
| 113 |
'$ref' => [ |
| 114 |
'class' => ExampleDefinitionsClass::class, |
| 115 |
'field' => 'SOME_SCHEMA', |
| 116 |
] |
| 117 |
], |
| 118 |
] |
| 119 |
], |
| 120 |
[ |
| 121 |
'type' => 'object', |
| 122 |
'properties' => [ |
| 123 |
'foo' => [ '$ref' => '#/$defs/MediaWiki.Tests.Unit.Settings.Source.ExampleDefinitionsClass::SOME_SCHEMA' ], |
| 124 |
] |
| 125 |
], |
| 126 |
[ |
| 127 |
'MediaWiki.Tests.Unit.Settings.Source.ExampleDefinitionsClass::SOME_SCHEMA' => [ |
| 128 |
'type' => 'string', |
| 129 |
] |
| 130 |
] |
| 131 |
]; |
| 132 |
|
| 133 |
yield 'definitions' => [ |
| 134 |
[ |
| 135 |
'type' => 'object', |
| 136 |
'properties' => [ |
| 137 |
'foo' => [ |
| 138 |
'$ref' => [ |
| 139 |
'class' => ExampleDefinitionsClass::class, |
| 140 |
'field' => 'REFERENCING_SCHEMA', |
| 141 |
] |
| 142 |
], |
| 143 |
] |
| 144 |
], |
| 145 |
[ |
| 146 |
'type' => 'object', |
| 147 |
'properties' => [ |
| 148 |
'foo' => [ '$ref' => '#/$defs/MediaWiki.Tests.Unit.Settings.Source.ExampleDefinitionsClass::REFERENCING_SCHEMA' ], |
| 149 |
] |
| 150 |
], |
| 151 |
[ |
| 152 |
'MediaWiki.Tests.Unit.Settings.Source.ExampleDefinitionsClass::REFERENCING_SCHEMA' => [ |
| 153 |
'$ref' => '#/$defs/MediaWiki.Tests.Unit.Settings.Source.ExampleDefinitionsClass::SOME_SCHEMA', |
| 154 |
], |
| 155 |
'MediaWiki.Tests.Unit.Settings.Source.ExampleDefinitionsClass::SOME_SCHEMA' => [ |
| 156 |
'type' => 'string', |
| 157 |
] |
| 158 |
] |
| 159 |
]; |
| 160 |
|
| 161 |
yield 'inlined' => [ |
| 162 |
[ |
| 163 |
'type' => 'object', |
| 164 |
'properties' => [ |
| 165 |
'foo' => [ |
| 166 |
'$ref' => [ |
| 167 |
'class' => ExampleDefinitionsClass::class, |
| 168 |
'field' => 'REFERENCING_SCHEMA', |
| 169 |
] |
| 170 |
], |
| 171 |
] |
| 172 |
], |
| 173 |
[ |
| 174 |
'type' => 'object', |
| 175 |
'properties' => [ |
| 176 |
'foo' => [ 'type' => 'string' ], |
| 177 |
] |
| 178 |
], |
| 179 |
[], |
| 180 |
true |
| 181 |
]; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function testNormalizeJsonSchema( $schema, $expectedSchema, $expectedDefs = [], $inlinedReferences = false ) { |
| 192 |
$actualDefs = []; |
| 193 |
$actual = self::normalizeJsonSchema( |
| 194 |
$schema, |
| 195 |
$actualDefs, |
| 196 |
'provideNormalizeJsonSchema', |
| 197 |
'foo', |
| 198 |
$inlinedReferences |
| 199 |
); |
| 200 |
$this->assertSame( $expectedSchema, $actual ); |
| 201 |
$this->assertEquals( $expectedDefs, $actualDefs ); |
| 202 |
} |
| 203 |
|
| 204 |
public static function provideCycleDetection() { |
| 205 |
yield 'cycle' => [ |
| 206 |
[ |
| 207 |
'type' => 'object', |
| 208 |
'properties' => [ |
| 209 |
'foo' => [ |
| 210 |
'$ref' => [ |
| 211 |
'class' => ExampleDefinitionsClass::class, |
| 212 |
'field' => 'CYCLED_SCHEMA', |
| 213 |
] |
| 214 |
] |
| 215 |
] |
| 216 |
] |
| 217 |
]; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public function testCycleDetection( $schema ) { |
| 225 |
$actualDefs = []; |
| 226 |
$this->expectException( RefLoopException::class ); |
| 227 |
$this->expectExceptionMessage( |
| 228 |
'Found a loop while resolving reference MediaWiki.Tests.Unit.Settings.Source.ExampleDefinitionsClass::CYCLED_SCHEMA in foo. Root schema location: provideCycleDetection-cycle' |
| 229 |
); |
| 230 |
self::normalizeJsonSchema( |
| 231 |
$schema, |
| 232 |
$actualDefs, |
| 233 |
'provideCycleDetection-cycle', |
| 234 |
'foo' |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
public static function provideJsonToPhpDoc() { |
| 239 |
yield 'integer' => [ 'integer', 'int' ]; |
| 240 |
yield 'double' => [ 'double', 'float' ]; |
| 241 |
yield 'integer or null' => [ [ 'integer', 'null' ], '?int' ]; |
| 242 |
yield 'boolean' => [ 'boolean', 'bool' ]; |
| 243 |
yield 'string or number' => [ [ 'string', 'number' ], 'string|float' ]; |
| 244 |
yield 'array' => [ 'array', 'array' ]; |
| 245 |
yield 'object' => [ 'object', 'array' ]; |
| 246 |
yield 'Class' => [ 'Something', 'Something' ]; |
| 247 |
yield 'keep int' => [ 'int', 'int' ]; |
| 248 |
yield 'keep float' => [ 'float', 'float' ]; |
| 249 |
yield 'keep null' => [ 'null', 'null' ]; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public function testJsonToPhpDoc( $json, $phpDoc ) { |
| 258 |
$actual = self::jsonToPhpDoc( $json ); |
| 259 |
$this->assertSame( $phpDoc, $actual ); |
| 260 |
} |
| 261 |
|
| 262 |
public static function provideJsonToPhpDoc_invalidArgument() { |
| 263 |
yield 'null' => [ null ]; |
| 264 |
yield 'list with null' => [ [ 'int', null ] ]; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function testJsonToPhpDoc_invalidArgument( $bad ) { |
| 272 |
$this->expectException( InvalidArgumentException::class ); |
| 273 |
self::jsonToPhpDoc( $bad ); |
| 274 |
} |
| 275 |
|
| 276 |
public function testPhpDocToJson_invalidArgument() { |
| 277 |
$this->expectException( InvalidArgumentException::class ); |
| 278 |
self::phpDocToJson( null ); |
| 279 |
} |
| 280 |
|
| 281 |
public static function provideGetDefaultFromJsonSchema() { |
| 282 |
yield 'empty' => [ [], null ]; |
| 283 |
|
| 284 |
yield 'no default, no properties' => [ |
| 285 |
[ 'type' => 'string' ], |
| 286 |
null |
| 287 |
]; |
| 288 |
|
| 289 |
yield 'simple default, no properties' => [ |
| 290 |
[ 'type' => 'string', 'default' => 'kitten' ], |
| 291 |
'kitten' |
| 292 |
]; |
| 293 |
|
| 294 |
yield 'no default, but properties' => [ |
| 295 |
[ |
| 296 |
'properties' => [ |
| 297 |
'a' => [ 'type' => 'int' ], |
| 298 |
'b' => [ 'type' => 'int' ], |
| 299 |
] |
| 300 |
], |
| 301 |
[ 'a' => null, 'b' => null ] |
| 302 |
]; |
| 303 |
|
| 304 |
yield 'default from properties' => [ |
| 305 |
[ |
| 306 |
'properties' => [ |
| 307 |
'a' => [ 'default' => 1 ], |
| 308 |
'b' => [ 'default' => 2 ], |
| 309 |
] |
| 310 |
], |
| 311 |
[ 'a' => 1, 'b' => 2 ] |
| 312 |
]; |
| 313 |
|
| 314 |
yield 'combined default' => [ |
| 315 |
[ |
| 316 |
'default' => [ |
| 317 |
'a' => 11, |
| 318 |
'x' => 99 |
| 319 |
], |
| 320 |
'properties' => [ |
| 321 |
'a' => [ 'default' => 1 ], |
| 322 |
'b' => [ 'default' => 2 ], |
| 323 |
] |
| 324 |
], |
| 325 |
[ 'a' => 1, 'x' => 99, 'b' => 2 ] |
| 326 |
]; |
| 327 |
|
| 328 |
yield 'nested properties' => [ |
| 329 |
[ |
| 330 |
'properties' => [ |
| 331 |
'a' => [ 'default' => 1 ], |
| 332 |
'b' => [ |
| 333 |
'default' => [], |
| 334 |
'properties' => [ |
| 335 |
'x' => [ 'default' => 7 ], |
| 336 |
'y' => [ |
| 337 |
'properties' => [ |
| 338 |
'foo' => [ 'default' => 13 ], |
| 339 |
'bar' => [ 'type' => 'int' ] |
| 340 |
] |
| 341 |
], |
| 342 |
] |
| 343 |
], |
| 344 |
] |
| 345 |
], |
| 346 |
[ |
| 347 |
'a' => 1, |
| 348 |
'b' => [ |
| 349 |
'x' => 7, |
| 350 |
'y' => [ 'foo' => 13, 'bar' => null ] |
| 351 |
] |
| 352 |
] |
| 353 |
]; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public function testGetDefaultFromJsonSchema( $schema, $default ) { |
| 362 |
$actual = self::getDefaultFromJsonSchema( $schema ); |
| 363 |
$this->assertSame( $default, $actual ); |
| 364 |
} |
| 365 |
|
| 366 |
} |