diff --git a/README.md b/README.md index c59af3ef..53b1d7a6 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ This package comes with the following normalizers: * [`Localheinz\Json\Normalizer\IndentNormalizer`](#indentnormalizer) * [`Localheinz\Json\Normalizer\JsonEncodeNormalizer`](#jsonencodenormalizer) * [`Localheinz\Json\Normalizer\NoFinalNewLineNormalizer`](#nofinalnewlinenormalizer) +* [`Localheinz\Json\Normalizer\SchemaNormalizer`](#schemanormalizer) :bulb: All of these normalizers implement the `Localheinz\Json\Normalizer\NormalizerInterface`. @@ -221,6 +222,52 @@ $normalized = $normalizer->normalize($json); The normalized version will now not have a final new line or any whitespace at the end. +### `SchemaNormalizer` + +If you want to rebuild a JSON file according to a JSON schema, you can use the `SchemaNormalizer`. + +Let's assume the following schema + +```json +{ + "type": "object", + "additionalProperties": false, + "properties": { + "name" : { + "type" : "string" + }, + "role" : { + "type" : "string" + } + } +} +``` + +exists at `/schema/example.json`. + +```php +use Localheinz\Json\Normalizer; + +$json = <<<'JSON' +{ + "url": "https://localheinz.com", + "name": "Andreas Möller" +} +JSON; + +$normalizer = new Normalizer\SchemaNormalizer('file:///schema/example.json'); + +$normalized = $normalizer->normalize($json); +``` + +The normalized version will now be structured according to the JSON +schema (in this simple case, properties will be reordered). Internally, +the `SchemaNormalizer` uses [`justinrainbow/json-schema`](https://github.com/justinrainbow/json-schema) +to resolve schemas, as well as to ensure (before and after normalization) +that the JSON document is valid. + +:bulb: For more information about JSON schema, visit [json-schema.org](http://json-schema.org). + ## Contributing Please have a look at [`CONTRIBUTING.md`](.github/CONTRIBUTING.md). diff --git a/src/SchemaNormalizer.php b/src/SchemaNormalizer.php new file mode 100644 index 00000000..de4a6ec7 --- /dev/null +++ b/src/SchemaNormalizer.php @@ -0,0 +1,235 @@ +getUriRetriever() + ))); + } + + $this->schemaUri = $schemaUri; + $this->schemaStorage = $schemaStorage; + $this->schemaValidator = $schemaValidator; + } + + public function normalize(string $json): string + { + $decoded = \json_decode($json); + + if (null === $decoded && JSON_ERROR_NONE !== \json_last_error()) { + throw new \InvalidArgumentException(\sprintf( + '"%s" is not valid JSON.', + $json + )); + } + + try { + $schema = $this->schemaStorage->getSchema($this->schemaUri); + } catch (Exception\UriResolverException $exception) { + throw new \RuntimeException(\sprintf( + 'Schema URI "%s" could not be resolved.', + $this->schemaUri + )); + } catch (Exception\ResourceNotFoundException $exception) { + throw new \RuntimeException(\sprintf( + 'Schema URI "%s" does not reference a document that could be read.', + $this->schemaUri + )); + } catch (Exception\InvalidSchemaMediaTypeException $exception) { + throw new \RuntimeException(\sprintf( + 'Schema URI "%s" does not reference a document with media type "application/schema+json".', + $this->schemaUri + )); + } catch (Exception\JsonDecodingException $exception) { + throw new \RuntimeException(\sprintf( + 'Schema URI "%s" does not reference a document with valid JSON syntax.', + $this->schemaUri + )); + } + + if (!$this->schemaValidator->isValid($decoded, $schema)) { + throw new \InvalidArgumentException(\sprintf( + 'Original is not valid according to schema "%s".', + $this->schemaUri + )); + } + + $normalized = $this->normalizeData( + $decoded, + $schema + ); + + if (!$this->schemaValidator->isValid($normalized, $schema)) { + throw new \RuntimeException(\sprintf( + 'Normalized is not valid according to schema "%s".', + $this->schemaUri + )); + } + + return \json_encode($normalized); + } + + /** + * @param array|\stdClass $data + * @param \stdClass $schema + * + * @throws \InvalidArgumentException + * + * @return array|bool|int|string + */ + private function normalizeData($data, \stdClass $schema) + { + if (\is_array($data)) { + return $this->normalizeArray( + $data, + $schema + ); + } + + if ($data instanceof \stdClass) { + return $this->normalizeObject( + $data, + $schema + ); + } + + return $data; + } + + private function normalizeArray(array $array, \stdClass $arraySchema): array + { + if (!$this->hasItemDefinition($arraySchema)) { + return $array; + } + + $itemSchema = $arraySchema->items; + + return \array_map(function ($item) use ($itemSchema) { + return $this->normalizeData( + $item, + $itemSchema + ); + }, $array); + } + + private function normalizeObject(\stdClass $object, \stdClass $objectSchema): \stdClass + { + if ($this->hasReferenceDefinition($objectSchema)) { + $objectSchema = $this->schemaStorage->resolveRefSchema($objectSchema); + } + + if (!$this->hasPropertyDefinitions($objectSchema)) { + return $object; + } + + $normalized = new \stdClass(); + + /** @var \stdClass[] $objectProperties */ + $objectProperties = \array_intersect_key( + \get_object_vars($objectSchema->properties), + \get_object_vars($object) + ); + + foreach ($objectProperties as $name => $valueSchema) { + if ($valueSchema instanceof \stdClass && $this->hasReferenceDefinition($valueSchema)) { + $valueSchema = $this->schemaStorage->resolveRefSchema($valueSchema); + } + + $value = $object->{$name}; + + if ($valueSchema instanceof \stdClass && !\is_scalar($value)) { + $value = $this->normalizeData( + $value, + $valueSchema + ); + } + + $normalized->{$name} = $value; + + unset($object->{$name}); + } + + $remainingProperties = \get_object_vars($object); + + if (\count($remainingProperties)) { + \ksort($remainingProperties); + + foreach ($remainingProperties as $name => $value) { + $normalized->{$name} = $value; + } + } + + return $normalized; + } + + private function hasPropertyDefinitions(\stdClass $schema): bool + { + return \property_exists($schema, 'type') + && 'object' === $schema->type + && \property_exists($schema, 'properties') + && $schema->properties instanceof \stdClass; + } + + private function hasItemDefinition(\stdClass $schema): bool + { + return \property_exists($schema, 'type') + && 'array' === $schema->type + && \property_exists($schema, 'items') + && $schema->items instanceof \stdClass; + } + + private function hasReferenceDefinition(\stdClass $schema): bool + { + return \property_exists($schema, '$ref') && \is_string($schema->{'$ref'}); + } +} diff --git a/test/Bench/.gitkeep b/test/Bench/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/Bench/SchemaNormalizerBench.php b/test/Bench/SchemaNormalizerBench.php new file mode 100644 index 00000000..1e15c4c0 --- /dev/null +++ b/test/Bench/SchemaNormalizerBench.php @@ -0,0 +1,64 @@ +normalize( + __DIR__ . '/../../composer.json', + $this->localComposerSchema() + ); + } + + /** + * @see https://github.com/search?utf8=✓&q=repositories+filename%3Acomposer.json+size%3A%3E25000+path%3A%2F+&type=Code + * + * @Revs(5) + * @Iterations(1) + */ + public function benchNormalizeLargeComposerFile() + { + $this->normalize( + __DIR__ . '/../Fixture/LargeComposerFile/composer.json', + $this->localComposerSchema() + ); + } + + private function normalize(string $file, string $schemaUri) + { + $original = \file_get_contents($file); + + $normalizer = new SchemaNormalizer($schemaUri); + + $normalizer->normalize($original); + } + + private function localComposerSchema(): string + { + return \sprintf( + 'file://%s', + __DIR__ . '/../Fixture/composer-schema.json' + ); + } +} diff --git a/test/Fixture/LargeComposerFile/composer.json b/test/Fixture/LargeComposerFile/composer.json new file mode 100644 index 00000000..5d7732cc --- /dev/null +++ b/test/Fixture/LargeComposerFile/composer.json @@ -0,0 +1,1107 @@ +{ + "name": "ivan-chkv/tinymce-i18n", + "description": "Languages for TinyMCE 4", + "keywords": [ + "tinymce", + "i18n", + "languages" + ], + "homepage": "https://www.tinymce.com/download/language-packages/", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "TinyMCE Community" + } + ], + "scripts": { + "post-update-cmd": [ + "rm -frv langs", + "mkdir -pv langs", + "cp -v vendor/language/*/* langs" + ] + }, + "require-dev": { + "language/ar": "*", + "language/ar_SA": "*", + "language/hy": "*", + "language/az": "*", + "language/eu": "*", + "language/be": "*", + "language/bn_BD": "*", + "language/bs": "*", + "language/bg_BG": "*", + "language/ca": "*", + "language/zh_CN": "*", + "language/zh_TW": "*", + "language/hr": "*", + "language/cs": "*", + "language/cs_CZ": "*", + "language/da": "*", + "language/dv": "*", + "language/nl": "*", + "language/en_CA": "*", + "language/en_GB": "*", + "language/eo": "*", + "language/et": "*", + "language/fo": "*", + "language/fi": "*", + "language/fr_FR": "*", + "language/fr_CH": "*", + "language/gd": "*", + "language/gl": "*", + "language/ka_GE": "*", + "language/de": "*", + "language/de_AT": "*", + "language/el": "*", + "language/he_IL": "*", + "language/hi_IN": "*", + "language/hu_HU": "*", + "language/is_IS": "*", + "language/id": "*", + "language/ga": "*", + "language/it": "*", + "language/ja": "*", + "language/kab": "*", + "language/kk": "*", + "language/km_KH": "*", + "language/ko": "*", + "language/ko_KR": "*", + "language/ku": "*", + "language/ku_IQ": "*", + "language/lv": "*", + "language/lt": "*", + "language/lb": "*", + "language/mk_MK": "*", + "language/ml": "*", + "language/ml_IN": "*", + "language/mn_MN": "*", + "language/nb_NO": "*", + "language/fa": "*", + "language/fa_IR": "*", + "language/pl": "*", + "language/pt_BR": "*", + "language/pt_PT": "*", + "language/ro": "*", + "language/ru": "*", + "language/ru_RU": "*", + "language/sr": "*", + "language/si_LK": "*", + "language/sk": "*", + "language/sl_SI": "*", + "language/es": "*", + "language/es_MX": "*", + "language/sv_SE": "*", + "language/tg": "*", + "language/ta": "*", + "language/ta_IN": "*", + "language/tt": "*", + "language/th_TH": "*", + "language/tr": "*", + "language/tr_TR": "*", + "language/ug": "*", + "language/uk": "*", + "language/uk_UA": "*", + "language/vi": "*", + "language/vi_VN": "*", + "language/cy": "*" + }, + "repositories": [ + { + "type": "package", + "package": { + "name": "language/ar", + "description": "Arabic", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ar" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ar_SA", + "description": "Arabic (Saudi Arabia)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ar_SA" + } + } + }, + { + "type": "package", + "package": { + "name": "language/hy", + "description": "Armenian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/hy" + } + } + }, + { + "type": "package", + "package": { + "name": "language/az", + "description": "Azerbaijani", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/az" + } + } + }, + { + "type": "package", + "package": { + "name": "language/eu", + "description": "Basque", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/eu" + } + } + }, + { + "type": "package", + "package": { + "name": "language/be", + "description": "Belarusian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/be" + } + } + }, + { + "type": "package", + "package": { + "name": "language/bn_BD", + "description": "Bengali (Bangladesh)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/bn_BD" + } + } + }, + { + "type": "package", + "package": { + "name": "language/bs", + "description": "Bosnian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/bs" + } + } + }, + { + "type": "package", + "package": { + "name": "language/bg_BG", + "description": "Bulgarian (Bulgaria)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/bg_BG" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ca", + "description": "Catalan", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ca" + } + } + }, + { + "type": "package", + "package": { + "name": "language/zh_CN", + "description": "Chinese (China)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/zh_CN" + } + } + }, + { + "type": "package", + "package": { + "name": "language/zh_TW", + "description": "Chinese (Taiwan)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/zh_TW" + } + } + }, + { + "type": "package", + "package": { + "name": "language/hr", + "description": "Croatian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/hr" + } + } + }, + { + "type": "package", + "package": { + "name": "language/cs", + "description": "Czech", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/cs" + } + } + }, + { + "type": "package", + "package": { + "name": "language/cs_CZ", + "description": "Czech (Czech Republic)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/cs_CZ" + } + } + }, + { + "type": "package", + "package": { + "name": "language/da", + "description": "Danish", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/da" + } + } + }, + { + "type": "package", + "package": { + "name": "language/dv", + "description": "Divehi", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/dv" + } + } + }, + { + "type": "package", + "package": { + "name": "language/nl", + "description": "Dutch", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/nl" + } + } + }, + { + "type": "package", + "package": { + "name": "language/en_CA", + "description": "English (Canada)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/en_CA" + } + } + }, + { + "type": "package", + "package": { + "name": "language/en_GB", + "description": "English (United Kingdom)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/en_GB" + } + } + }, + { + "type": "package", + "package": { + "name": "language/eo", + "description": "Esperanto", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/eo" + } + } + }, + { + "type": "package", + "package": { + "name": "language/et", + "description": "Estonian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/et" + } + } + }, + { + "type": "package", + "package": { + "name": "language/fo", + "description": "Faroese", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/fo" + } + } + }, + { + "type": "package", + "package": { + "name": "language/fi", + "description": "Finnish", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/fi" + } + } + }, + { + "type": "package", + "package": { + "name": "language/fr_FR", + "description": "French (France)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/fr_FR" + } + } + }, + { + "type": "package", + "package": { + "name": "language/fr_CH", + "description": "French (Switzerland)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/fr_CH" + } + } + }, + { + "type": "package", + "package": { + "name": "language/gd", + "description": "Gaelic, Scottish", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/gd" + } + } + }, + { + "type": "package", + "package": { + "name": "language/gl", + "description": "Galician", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/gl" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ka_GE", + "description": "Georgian (Georgia)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ka_GE" + } + } + }, + { + "type": "package", + "package": { + "name": "language/de", + "description": "German", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/de" + } + } + }, + { + "type": "package", + "package": { + "name": "language/de_AT", + "description": "German (Austria)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/de_AT" + } + } + }, + { + "type": "package", + "package": { + "name": "language/el", + "description": "Greek", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/el" + } + } + }, + { + "type": "package", + "package": { + "name": "language/he_IL", + "description": "Hebrew (Israel)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/he_IL" + } + } + }, + { + "type": "package", + "package": { + "name": "language/hi_IN", + "description": "Hindi (India)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/hi_IN" + } + } + }, + { + "type": "package", + "package": { + "name": "language/hu_HU", + "description": "Hungarian (Hungary)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/hu_HU" + } + } + }, + { + "type": "package", + "package": { + "name": "language/is_IS", + "description": "Icelandic (Iceland)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/is_IS" + } + } + }, + { + "type": "package", + "package": { + "name": "language/id", + "description": "Indonesian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/id" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ga", + "description": "Irish", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ga" + } + } + }, + { + "type": "package", + "package": { + "name": "language/it", + "description": "Italian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/it" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ja", + "description": "Japanese", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ja" + } + } + }, + { + "type": "package", + "package": { + "name": "language/kab", + "description": "Kabyle", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/kab" + } + } + }, + { + "type": "package", + "package": { + "name": "language/kk", + "description": "Kazakh", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/kk" + } + } + }, + { + "type": "package", + "package": { + "name": "language/km_KH", + "description": "Khmer (Cambodia)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/km_KH" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ko", + "description": "Korean", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ko" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ko_KR", + "description": "Korean (Korea)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ko_KR" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ku", + "description": "Kurdish", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ku" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ku_IQ", + "description": "Kurdish (Iraq)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ku_IQ" + } + } + }, + { + "type": "package", + "package": { + "name": "language/lv", + "description": "Latvian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/lv" + } + } + }, + { + "type": "package", + "package": { + "name": "language/lt", + "description": "Lithuanian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/lt" + } + } + }, + { + "type": "package", + "package": { + "name": "language/lb", + "description": "Luxembourgish", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/lb" + } + } + }, + { + "type": "package", + "package": { + "name": "language/mk_MK", + "description": "Macedonian (Macedonia)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/mk_MK" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ml", + "description": "Malayalam", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ml" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ml_IN", + "description": "Malayalam (India)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ml_IN" + } + } + }, + { + "type": "package", + "package": { + "name": "language/mn_MN", + "description": "Mongolian (Mongolia)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/mn_MN" + } + } + }, + { + "type": "package", + "package": { + "name": "language/nb_NO", + "description": "Norwegian Bokmål (Norway)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/nb_NO" + } + } + }, + { + "type": "package", + "package": { + "name": "language/fa", + "description": "Persian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/fa" + } + } + }, + { + "type": "package", + "package": { + "name": "language/fa_IR", + "description": "Persian (Iran)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/fa_IR" + } + } + }, + { + "type": "package", + "package": { + "name": "language/pl", + "description": "Polish", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/pl" + } + } + }, + { + "type": "package", + "package": { + "name": "language/pt_BR", + "description": "Portuguese (Brazil)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/pt_BR" + } + } + }, + { + "type": "package", + "package": { + "name": "language/pt_PT", + "description": "Portuguese (Portugal)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/pt_PT" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ro", + "description": "Romanian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ro" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ru", + "description": "Russian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ru" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ru_RU", + "description": "Russian (Russia)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ru_RU" + } + } + }, + { + "type": "package", + "package": { + "name": "language/sr", + "description": "Serbian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/sr" + } + } + }, + { + "type": "package", + "package": { + "name": "language/si_LK", + "description": "Sinhala (Sri Lanka)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/si_LK" + } + } + }, + { + "type": "package", + "package": { + "name": "language/sk", + "description": "Slovak", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/sk" + } + } + }, + { + "type": "package", + "package": { + "name": "language/sl_SI", + "description": "Slovenian (Slovenia)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/sl_SI" + } + } + }, + { + "type": "package", + "package": { + "name": "language/es", + "description": "Spanish", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/es" + } + } + }, + { + "type": "package", + "package": { + "name": "language/es_MX", + "description": "Spanish (Mexico)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/es_MX" + } + } + }, + { + "type": "package", + "package": { + "name": "language/sv_SE", + "description": "Swedish (Sweden)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/sv_SE" + } + } + }, + { + "type": "package", + "package": { + "name": "language/tg", + "description": "Tajik", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/tg" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ta", + "description": "Tamil", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ta" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ta_IN", + "description": "Tamil (India)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ta_IN" + } + } + }, + { + "type": "package", + "package": { + "name": "language/tt", + "description": "Tatar", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/tt" + } + } + }, + { + "type": "package", + "package": { + "name": "language/th_TH", + "description": "Thai (Thailand)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/th_TH" + } + } + }, + { + "type": "package", + "package": { + "name": "language/tr", + "description": "Turkish", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/tr" + } + } + }, + { + "type": "package", + "package": { + "name": "language/tr_TR", + "description": "Turkish (Turkey)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/tr_TR" + } + } + }, + { + "type": "package", + "package": { + "name": "language/ug", + "description": "Uighur", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/ug" + } + } + }, + { + "type": "package", + "package": { + "name": "language/uk", + "description": "Ukrainian", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/uk" + } + } + }, + { + "type": "package", + "package": { + "name": "language/uk_UA", + "description": "Ukrainian (Ukraine)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/uk_UA" + } + } + }, + { + "type": "package", + "package": { + "name": "language/vi", + "description": "Vietnamese", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/vi" + } + } + }, + { + "type": "package", + "package": { + "name": "language/vi_VN", + "description": "Vietnamese (Viet Nam)", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/vi_VN" + } + } + }, + { + "type": "package", + "package": { + "name": "language/cy", + "description": "Welsh", + "version": "1.0.0", + "dist": { + "type": "zip", + "url": "https://www.tinymce.com/download/language/cy" + } + } + } + ] +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsArray/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsArray/normalized.json new file mode 100644 index 00000000..4c2819a1 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsArray/normalized.json @@ -0,0 +1,5 @@ +[ + "foo", + "bar", + "baz" +] diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsArray/original.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsArray/original.json new file mode 100644 index 00000000..4c2819a1 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsArray/original.json @@ -0,0 +1,5 @@ +[ + "foo", + "bar", + "baz" +] diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsArray/schema.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsArray/schema.json new file mode 100644 index 00000000..fa095fe0 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsArray/schema.json @@ -0,0 +1,6 @@ +{ + "type": "array", + "items": { + "type": "string" + } +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsObject/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsObject/normalized.json new file mode 100644 index 00000000..cccf4ec6 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsObject/normalized.json @@ -0,0 +1,3 @@ +{ + "url": "https://github.com/localheinz/composer-normalize" +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsObject/original.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsObject/original.json new file mode 100644 index 00000000..cccf4ec6 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsObject/original.json @@ -0,0 +1,3 @@ +{ + "url": "https://github.com/localheinz/composer-normalize" +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsObject/schema.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsObject/schema.json new file mode 100644 index 00000000..34980607 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/Original/IsObject/schema.json @@ -0,0 +1,9 @@ +{ + "type": "object", + "additionalProperties": false, + "properties": { + "url": { + "type": "string" + } + } +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ArrayWithoutItemDefinition/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ArrayWithoutItemDefinition/normalized.json new file mode 100644 index 00000000..d8988895 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ArrayWithoutItemDefinition/normalized.json @@ -0,0 +1,8 @@ +[ + { + "baz": 9000 + }, + { + "baz": 9001 + } +] diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ArrayWithoutItemDefinition/original.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ArrayWithoutItemDefinition/original.json new file mode 100644 index 00000000..d8988895 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ArrayWithoutItemDefinition/original.json @@ -0,0 +1,8 @@ +[ + { + "baz": 9000 + }, + { + "baz": 9001 + } +] diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ArrayWithoutItemDefinition/schema.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ArrayWithoutItemDefinition/schema.json new file mode 100644 index 00000000..3e06306a --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ArrayWithoutItemDefinition/schema.json @@ -0,0 +1,3 @@ +{ + "type": "array" +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ObjectWithoutPropertyDefinitions/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ObjectWithoutPropertyDefinitions/normalized.json new file mode 100644 index 00000000..4bceacc0 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ObjectWithoutPropertyDefinitions/normalized.json @@ -0,0 +1,11 @@ +{ + "foo": "hello", + "bar": [ + { + "baz": 9000 + }, + { + "baz": 9001 + } + ] +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ObjectWithoutPropertyDefinitions/original.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ObjectWithoutPropertyDefinitions/original.json new file mode 100644 index 00000000..4bceacc0 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ObjectWithoutPropertyDefinitions/original.json @@ -0,0 +1,11 @@ +{ + "foo": "hello", + "bar": [ + { + "baz": 9000 + }, + { + "baz": 9001 + } + ] +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ObjectWithoutPropertyDefinitions/schema.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ObjectWithoutPropertyDefinitions/schema.json new file mode 100644 index 00000000..f9698869 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PassThrough/ObjectWithoutPropertyDefinitions/schema.json @@ -0,0 +1,4 @@ +{ + "type": "object", + "additionalProperties": true +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsAdditionalPropertiesAlphabetically/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsAdditionalPropertiesAlphabetically/normalized.json new file mode 100644 index 00000000..9b1edde9 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsAdditionalPropertiesAlphabetically/normalized.json @@ -0,0 +1,6 @@ +{ + "name": "Andreas Möller", + "github": "https://github.com/localheinz", + "twitter": "https://twitter.com/localheinz", + "website": "https://localheinz.com" +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsAdditionalPropertiesAlphabetically/original.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsAdditionalPropertiesAlphabetically/original.json new file mode 100644 index 00000000..89911349 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsAdditionalPropertiesAlphabetically/original.json @@ -0,0 +1,6 @@ +{ + "name": "Andreas Möller", + "twitter": "https://twitter.com/localheinz", + "github": "https://github.com/localheinz", + "website": "https://localheinz.com" +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsAdditionalPropertiesAlphabetically/schema.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsAdditionalPropertiesAlphabetically/schema.json new file mode 100644 index 00000000..79edcc54 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsAdditionalPropertiesAlphabetically/schema.json @@ -0,0 +1,9 @@ +{ + "type": "object", + "additionalProperties": true, + "properties": { + "name": { + "type": "string" + } + } +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsPropertiesAsDefinedInSchema/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsPropertiesAsDefinedInSchema/normalized.json new file mode 100644 index 00000000..85a87fd0 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsPropertiesAsDefinedInSchema/normalized.json @@ -0,0 +1,17 @@ +{ + "name": "Andreas Möller", + "urls": [ + { + "type": "Website", + "url": "https://localheinz.com" + }, + { + "type": "GitHub", + "url": "https://github.com/localheinz" + }, + { + "type": "Twitter", + "url": "https://twitter.com/localheinz" + } + ] +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsPropertiesAsDefinedInSchema/original.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsPropertiesAsDefinedInSchema/original.json new file mode 100644 index 00000000..983465ee --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsPropertiesAsDefinedInSchema/original.json @@ -0,0 +1,17 @@ +{ + "name": "Andreas Möller", + "urls": [ + { + "url": "https://localheinz.com", + "type": "Website" + }, + { + "type": "GitHub", + "url": "https://github.com/localheinz" + }, + { + "url": "https://twitter.com/localheinz", + "type": "Twitter" + } + ] +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsPropertiesAsDefinedInSchema/schema.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsPropertiesAsDefinedInSchema/schema.json new file mode 100644 index 00000000..9208f3d1 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/PropertyHandling/SortsPropertiesAsDefinedInSchema/schema.json @@ -0,0 +1,27 @@ +{ + "type": "object", + "additionalProperties": false, + "properties": { + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "urls": { + "type": "array", + "items": { + "type": "object", + "additionalProperties": false, + "properties": { + "type": { + "type": "string" + }, + "url": { + "type": "string" + } + } + } + } + } +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/SchemaReferences/normalized.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/SchemaReferences/normalized.json new file mode 100644 index 00000000..85a87fd0 --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/SchemaReferences/normalized.json @@ -0,0 +1,17 @@ +{ + "name": "Andreas Möller", + "urls": [ + { + "type": "Website", + "url": "https://localheinz.com" + }, + { + "type": "GitHub", + "url": "https://github.com/localheinz" + }, + { + "type": "Twitter", + "url": "https://twitter.com/localheinz" + } + ] +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/SchemaReferences/original.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/SchemaReferences/original.json new file mode 100644 index 00000000..983465ee --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/SchemaReferences/original.json @@ -0,0 +1,17 @@ +{ + "name": "Andreas Möller", + "urls": [ + { + "url": "https://localheinz.com", + "type": "Website" + }, + { + "type": "GitHub", + "url": "https://github.com/localheinz" + }, + { + "url": "https://twitter.com/localheinz", + "type": "Twitter" + } + ] +} diff --git a/test/Fixture/SchemaNormalizer/NormalizeNormalizes/SchemaReferences/schema.json b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/SchemaReferences/schema.json new file mode 100644 index 00000000..7d319eeb --- /dev/null +++ b/test/Fixture/SchemaNormalizer/NormalizeNormalizes/SchemaReferences/schema.json @@ -0,0 +1,35 @@ +{ + "type": "object", + "additionalProperties": false, + "properties": { + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "urls": { + "$ref": "#/definitions/urls" + } + }, + "definitions": { + "urls": { + "type": "array", + "items": { + "$ref": "#/definitions/url" + } + }, + "url": { + "type": "object", + "additionalProperties": false, + "properties": { + "type": { + "type": "string" + }, + "url": { + "type": "string" + } + } + } + } +} diff --git a/test/Fixture/composer-schema.json b/test/Fixture/composer-schema.json new file mode 100644 index 00000000..8c61a624 --- /dev/null +++ b/test/Fixture/composer-schema.json @@ -0,0 +1,835 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "name": "Package", + "type": "object", + "additionalProperties": false, + "required": [ "name", "description" ], + "properties": { + "name": { + "type": "string", + "description": "Package name, including 'vendor-name/' prefix." + }, + "type": { + "description": "Package type, either 'library' for common packages, 'composer-plugin' for plugins, 'metapackage' for empty packages, or a custom type ([a-z0-9-]+) defined by whatever project this package applies to.", + "type": "string" + }, + "target-dir": { + "description": "DEPRECATED: Forces the package to be installed into the given subdirectory path. This is used for autoloading PSR-0 packages that do not contain their full path. Use forward slashes for cross-platform compatibility.", + "type": "string" + }, + "description": { + "type": "string", + "description": "Short package description." + }, + "keywords": { + "type": "array", + "items": { + "type": "string", + "description": "A tag/keyword that this package relates to." + } + }, + "homepage": { + "type": "string", + "description": "Homepage URL for the project.", + "format": "uri" + }, + "version": { + "type": "string", + "description": "Package version, see https://getcomposer.org/doc/04-schema.md#version for more info on valid schemes." + }, + "time": { + "type": "string", + "description": "Package release date, in 'YYYY-MM-DD', 'YYYY-MM-DD HH:MM:SS' or 'YYYY-MM-DDTHH:MM:SSZ' format." + }, + "license": { + "type": ["string", "array"], + "description": "License name. Or an array of license names." + }, + "authors": { + "$ref": "#/definitions/authors" + }, + "require": { + "type": "object", + "description": "This is a hash of package name (keys) and version constraints (values) that are required to run this package.", + "additionalProperties": { + "type": "string" + } + }, + "replace": { + "type": "object", + "description": "This is a hash of package name (keys) and version constraints (values) that can be replaced by this package.", + "additionalProperties": { + "type": "string" + } + }, + "conflict": { + "type": "object", + "description": "This is a hash of package name (keys) and version constraints (values) that conflict with this package.", + "additionalProperties": { + "type": "string" + } + }, + "provide": { + "type": "object", + "description": "This is a hash of package name (keys) and version constraints (values) that this package provides in addition to this package's name.", + "additionalProperties": { + "type": "string" + } + }, + "require-dev": { + "type": "object", + "description": "This is a hash of package name (keys) and version constraints (values) that this package requires for developing it (testing tools and such).", + "additionalProperties": { + "type": "string" + } + }, + "suggest": { + "type": "object", + "description": "This is a hash of package name (keys) and descriptions (values) that this package suggests work well with it (this will be suggested to the user during installation).", + "additionalProperties": { + "type": "string" + } + }, + "config": { + "type": "object", + "description": "Composer options.", + "properties": { + "process-timeout": { + "type": "integer", + "description": "The timeout in seconds for process executions, defaults to 300 (5mins)." + }, + "use-include-path": { + "type": "boolean", + "description": "If true, the Composer autoloader will also look for classes in the PHP include path." + }, + "preferred-install": { + "type": ["string", "object"], + "description": "The install method Composer will prefer to use, defaults to auto and can be any of source, dist, auto, or a hash of {\"pattern\": \"preference\"}." + }, + "notify-on-install": { + "type": "boolean", + "description": "Composer allows repositories to define a notification URL, so that they get notified whenever a package from that repository is installed. This option allows you to disable that behaviour, defaults to true." + }, + "github-protocols": { + "type": "array", + "description": "A list of protocols to use for github.com clones, in priority order, defaults to [\"git\", \"https\", \"http\"].", + "items": { + "type": "string" + } + }, + "github-oauth": { + "type": "object", + "description": "A hash of domain name => github API oauth tokens, typically {\"github.com\":\"\"}.", + "additionalProperties": { + "type": "string" + } + }, + "gitlab-oauth": { + "type": "object", + "description": "A hash of domain name => gitlab API oauth tokens, typically {\"gitlab.com\":\"\"}.", + "additionalProperties": { + "type": "string" + } + }, + "gitlab-token": { + "type": "object", + "description": "A hash of domain name => gitlab private tokens, typically {\"gitlab.com\":\"\"}.", + "additionalProperties": true + }, + "disable-tls": { + "type": "boolean", + "description": "Defaults to `false`. If set to true all HTTPS URLs will be tried with HTTP instead and no network level encryption is performed. Enabling this is a security risk and is NOT recommended. The better way is to enable the php_openssl extension in php.ini." + }, + "secure-http": { + "type": "boolean", + "description": "Defaults to `true`. If set to true only HTTPS URLs are allowed to be downloaded via Composer. If you really absolutely need HTTP access to something then you can disable it, but using \"Let's Encrypt\" to get a free SSL certificate is generally a better alternative." + }, + "cafile": { + "type": "string", + "description": "A way to set the path to the openssl CA file. In PHP 5.6+ you should rather set this via openssl.cafile in php.ini, although PHP 5.6+ should be able to detect your system CA file automatically." + }, + "capath": { + "type": "string", + "description": "If cafile is not specified or if the certificate is not found there, the directory pointed to by capath is searched for a suitable certificate. capath must be a correctly hashed certificate directory." + }, + "http-basic": { + "type": "object", + "description": "A hash of domain name => {\"username\": \"...\", \"password\": \"...\"}.", + "additionalProperties": { + "type": "object", + "required": ["username", "password"], + "properties": { + "username": { + "type": "string", + "description": "The username used for HTTP Basic authentication" + }, + "password": { + "type": "string", + "description": "The password used for HTTP Basic authentication" + } + } + } + }, + "store-auths": { + "type": ["string", "boolean"], + "description": "What to do after prompting for authentication, one of: true (store), false (do not store) or \"prompt\" (ask every time), defaults to prompt." + }, + "platform": { + "type": "object", + "description": "This is a hash of package name (keys) and version (values) that will be used to mock the platform packages on this machine.", + "additionalProperties": { + "type": "string" + } + }, + "vendor-dir": { + "type": "string", + "description": "The location where all packages are installed, defaults to \"vendor\"." + }, + "bin-dir": { + "type": "string", + "description": "The location where all binaries are linked, defaults to \"vendor/bin\"." + }, + "data-dir": { + "type": "string", + "description": "The location where old phar files are stored, defaults to \"$home\" except on XDG Base Directory compliant unixes." + }, + "cache-dir": { + "type": "string", + "description": "The location where all caches are located, defaults to \"~/.composer/cache\" on *nix and \"%LOCALAPPDATA%\\Composer\" on windows." + }, + "cache-files-dir": { + "type": "string", + "description": "The location where files (zip downloads) are cached, defaults to \"{$cache-dir}/files\"." + }, + "cache-repo-dir": { + "type": "string", + "description": "The location where repo (git/hg repo clones) are cached, defaults to \"{$cache-dir}/repo\"." + }, + "cache-vcs-dir": { + "type": "string", + "description": "The location where vcs infos (git clones, github api calls, etc. when reading vcs repos) are cached, defaults to \"{$cache-dir}/vcs\"." + }, + "cache-ttl": { + "type": "integer", + "description": "The default cache time-to-live, defaults to 15552000 (6 months)." + }, + "cache-files-ttl": { + "type": "integer", + "description": "The cache time-to-live for files, defaults to the value of cache-ttl." + }, + "cache-files-maxsize": { + "type": ["string", "integer"], + "description": "The cache max size for the files cache, defaults to \"300MiB\"." + }, + "bin-compat": { + "enum": ["auto", "full"], + "description": "The compatibility of the binaries, defaults to \"auto\" (automatically guessed) and can be \"full\" (compatible with both Windows and Unix-based systems)." + }, + "discard-changes": { + "type": ["string", "boolean"], + "description": "The default style of handling dirty updates, defaults to false and can be any of true, false or \"stash\"." + }, + "autoloader-suffix": { + "type": "string", + "description": "Optional string to be used as a suffix for the generated Composer autoloader. When null a random one will be generated." + }, + "optimize-autoloader": { + "type": "boolean", + "description": "Always optimize when dumping the autoloader." + }, + "prepend-autoloader": { + "type": "boolean", + "description": "If false, the composer autoloader will not be prepended to existing autoloaders, defaults to true." + }, + "classmap-authoritative": { + "type": "boolean", + "description": "If true, the composer autoloader will not scan the filesystem for classes that are not found in the class map, defaults to false." + }, + "apcu-autoloader": { + "type": "boolean", + "description": "If true, the Composer autoloader will check for APCu and use it to cache found/not-found classes when the extension is enabled, defaults to false." + }, + "github-domains": { + "type": "array", + "description": "A list of domains to use in github mode. This is used for GitHub Enterprise setups, defaults to [\"github.com\"].", + "items": { + "type": "string" + } + }, + "github-expose-hostname": { + "type": "boolean", + "description": "Defaults to true. If set to false, the OAuth tokens created to access the github API will have a date instead of the machine hostname." + }, + "gitlab-domains": { + "type": "array", + "description": "A list of domains to use in gitlab mode. This is used for custom GitLab setups, defaults to [\"gitlab.com\"].", + "items": { + "type": "string" + } + }, + "archive-format": { + "type": "string", + "description": "The default archiving format when not provided on cli, defaults to \"tar\"." + }, + "archive-dir": { + "type": "string", + "description": "The default archive path when not provided on cli, defaults to \".\"." + }, + "htaccess-protect": { + "type": "boolean", + "description": "Defaults to true. If set to false, Composer will not create .htaccess files in the composer home, cache, and data directories." + }, + "sort-packages": { + "type": "boolean", + "description": "Defaults to false. If set to true, Composer will sort packages when adding/updating a new dependency." + } + } + }, + "extra": { + "type": ["object", "array"], + "description": "Arbitrary extra data that can be used by plugins, for example, package of type composer-plugin may have a 'class' key defining an installer class name.", + "additionalProperties": true + }, + "autoload": { + "$ref": "#/definitions/autoload" + }, + "autoload-dev": { + "type": "object", + "description": "Description of additional autoload rules for development purpose (eg. a test suite).", + "properties": { + "psr-0": { + "type": "object", + "description": "This is a hash of namespaces (keys) and the directories they can be found into (values, can be arrays of paths) by the autoloader.", + "additionalProperties": { + "type": ["string", "array"], + "items": { + "type": "string" + } + } + }, + "psr-4": { + "type": "object", + "description": "This is a hash of namespaces (keys) and the PSR-4 directories they can map to (values, can be arrays of paths) by the autoloader.", + "additionalProperties": { + "type": ["string", "array"], + "items": { + "type": "string" + } + } + }, + "classmap": { + "type": "array", + "description": "This is an array of directories that contain classes to be included in the class-map generation process." + }, + "files": { + "type": "array", + "description": "This is an array of files that are always required on every request." + } + } + }, + "archive": { + "type": ["object"], + "description": "Options for creating package archives for distribution.", + "properties": { + "exclude": { + "type": "array", + "description": "A list of patterns for paths to exclude or include if prefixed with an exclamation mark." + } + } + }, + "repositories": { + "type": ["object", "array"], + "description": "A set of additional repositories where packages can be found.", + "additionalProperties": { + "oneOf": [ + { "$ref": "#/definitions/repository" }, + { "type": "boolean", "enum": [false] } + ] + }, + "items": { + "oneOf": [ + { "$ref": "#/definitions/repository" }, + { + "type": "object", + "additionalProperties": { "type": "boolean", "enum": [false] }, + "minProperties": 1, + "maxProperties": 1 + } + ] + } + }, + "minimum-stability": { + "type": ["string"], + "description": "The minimum stability the packages must have to be install-able. Possible values are: dev, alpha, beta, RC, stable.", + "pattern": "^dev|alpha|beta|rc|RC|stable$" + }, + "prefer-stable": { + "type": ["boolean"], + "description": "If set to true, stable packages will be preferred to dev packages when possible, even if the minimum-stability allows unstable packages." + }, + "bin": { + "type": ["string", "array"], + "description": "A set of files, or a single file, that should be treated as binaries and symlinked into bin-dir (from config).", + "items": { + "type": "string" + } + }, + "include-path": { + "type": ["array"], + "description": "DEPRECATED: A list of directories which should get added to PHP's include path. This is only present to support legacy projects, and all new code should preferably use autoloading.", + "items": { + "type": "string" + } + }, + "scripts": { + "type": ["object"], + "description": "Script listeners that will be executed before/after some events.", + "properties": { + "pre-install-cmd": { + "type": ["array", "string"], + "description": "Occurs before the install command is executed, contains one or more Class::method callables or shell commands." + }, + "post-install-cmd": { + "type": ["array", "string"], + "description": "Occurs after the install command is executed, contains one or more Class::method callables or shell commands." + }, + "pre-update-cmd": { + "type": ["array", "string"], + "description": "Occurs before the update command is executed, contains one or more Class::method callables or shell commands." + }, + "post-update-cmd": { + "type": ["array", "string"], + "description": "Occurs after the update command is executed, contains one or more Class::method callables or shell commands." + }, + "pre-status-cmd": { + "type": ["array", "string"], + "description": "Occurs before the status command is executed, contains one or more Class::method callables or shell commands." + }, + "post-status-cmd": { + "type": ["array", "string"], + "description": "Occurs after the status command is executed, contains one or more Class::method callables or shell commands." + }, + "pre-package-install": { + "type": ["array", "string"], + "description": "Occurs before a package is installed, contains one or more Class::method callables or shell commands." + }, + "post-package-install": { + "type": ["array", "string"], + "description": "Occurs after a package is installed, contains one or more Class::method callables or shell commands." + }, + "pre-package-update": { + "type": ["array", "string"], + "description": "Occurs before a package is updated, contains one or more Class::method callables or shell commands." + }, + "post-package-update": { + "type": ["array", "string"], + "description": "Occurs after a package is updated, contains one or more Class::method callables or shell commands." + }, + "pre-package-uninstall": { + "type": ["array", "string"], + "description": "Occurs before a package has been uninstalled, contains one or more Class::method callables or shell commands." + }, + "post-package-uninstall": { + "type": ["array", "string"], + "description": "Occurs after a package has been uninstalled, contains one or more Class::method callables or shell commands." + }, + "pre-autoload-dump": { + "type": ["array", "string"], + "description": "Occurs before the autoloader is dumped, contains one or more Class::method callables or shell commands." + }, + "post-autoload-dump": { + "type": ["array", "string"], + "description": "Occurs after the autoloader is dumped, contains one or more Class::method callables or shell commands." + }, + "post-root-package-install": { + "type": ["array", "string"], + "description": "Occurs after the root-package is installed, contains one or more Class::method callables or shell commands." + }, + "post-create-project-cmd": { + "type": ["array", "string"], + "description": "Occurs after the create-project command is executed, contains one or more Class::method callables or shell commands." + } + } + }, + "scripts-descriptions": { + "type": ["object"], + "description": "Descriptions for custom commands, shown in console help.", + "additionalProperties": { + "type": "string" + } + }, + "support": { + "type": "object", + "properties": { + "email": { + "type": "string", + "description": "Email address for support.", + "format": "email" + }, + "issues": { + "type": "string", + "description": "URL to the issue tracker.", + "format": "uri" + }, + "forum": { + "type": "string", + "description": "URL to the forum.", + "format": "uri" + }, + "wiki": { + "type": "string", + "description": "URL to the wiki.", + "format": "uri" + }, + "irc": { + "type": "string", + "description": "IRC channel for support, as irc://server/channel.", + "format": "uri" + }, + "source": { + "type": "string", + "description": "URL to browse or download the sources.", + "format": "uri" + }, + "docs": { + "type": "string", + "description": "URL to the documentation.", + "format": "uri" + }, + "rss": { + "type": "string", + "description": "URL to the RSS feed.", + "format": "uri" + } + } + }, + "non-feature-branches": { + "type": ["array"], + "description": "A set of string or regex patterns for non-numeric branch names that will not be handled as feature branches.", + "items": { + "type": "string" + } + }, + "abandoned": { + "type": ["boolean", "string"], + "description": "Indicates whether this package has been abandoned, it can be boolean or a package name/URL pointing to a recommended alternative. Defaults to false." + }, + "_comment": { + "type": ["array", "string"], + "description": "A key to store comments in" + } + }, + "definitions": { + "authors": { + "type": "array", + "description": "List of authors that contributed to the package. This is typically the main maintainers, not the full list.", + "items": { + "type": "object", + "additionalProperties": false, + "required": [ "name"], + "properties": { + "name": { + "type": "string", + "description": "Full name of the author." + }, + "email": { + "type": "string", + "description": "Email address of the author.", + "format": "email" + }, + "homepage": { + "type": "string", + "description": "Homepage URL for the author.", + "format": "uri" + }, + "role": { + "type": "string", + "description": "Author's role in the project." + } + } + } + }, + "autoload": { + "type": "object", + "description": "Description of how the package can be autoloaded.", + "properties": { + "psr-0": { + "type": "object", + "description": "This is a hash of namespaces (keys) and the directories they can be found in (values, can be arrays of paths) by the autoloader.", + "additionalProperties": { + "type": ["string", "array"], + "items": { + "type": "string" + } + } + }, + "psr-4": { + "type": "object", + "description": "This is a hash of namespaces (keys) and the PSR-4 directories they can map to (values, can be arrays of paths) by the autoloader.", + "additionalProperties": { + "type": ["string", "array"], + "items": { + "type": "string" + } + } + }, + "classmap": { + "type": "array", + "description": "This is an array of directories that contain classes to be included in the class-map generation process." + }, + "files": { + "type": "array", + "description": "This is an array of files that are always required on every request." + }, + "exclude-from-classmap": { + "type": "array", + "description": "This is an array of patterns to exclude from autoload classmap generation. (e.g. \"exclude-from-classmap\": [\"/test/\", \"/tests/\", \"/Tests/\"]" + } + } + }, + "repository": { + "type": "object", + "oneOf": [ + { "$ref": "#/definitions/composer-repository" }, + { "$ref": "#/definitions/vcs-repository" }, + { "$ref": "#/definitions/path-repository" }, + { "$ref": "#/definitions/artifact-repository" }, + { "$ref": "#/definitions/pear-repository" }, + { "$ref": "#/definitions/package-repository" } + ] + }, + "composer-repository": { + "type": "object", + "required": ["type", "url"], + "properties": { + "type": { "type": "string", "enum": ["composer"] }, + "url": { "type": "string" }, + "options": { + "type": "object", + "additionalProperties": true + }, + "allow_ssl_downgrade": { "type": "boolean" }, + "force-lazy-providers": { "type": "boolean" } + } + }, + "vcs-repository": { + "type": "object", + "required": ["type", "url"], + "properties": { + "type": { "type": "string", "enum": ["vcs", "github", "git", "gitlab", "git-bitbucket", "hg", "hg-bitbucket", "fossil", "perforce", "svn"] }, + "url": { "type": "string" }, + "no-api": { "type": "boolean" }, + "secure-http": { "type": "boolean" }, + "svn-cache-credentials": { "type": "boolean" }, + "trunk-path": { "type": ["string", "boolean"] }, + "branches-path": { "type": ["string", "boolean"] }, + "tags-path": { "type": ["string", "boolean"] }, + "package-path": { "type": "string" }, + "depot": { "type": "string" }, + "branch": { "type": "string" }, + "unique_perforce_client_name": { "type": "string" }, + "p4user": { "type": "string" }, + "p4password": { "type": "string" } + } + }, + "path-repository": { + "type": "object", + "required": ["type", "url"], + "properties": { + "type": { "type": "string", "enum": ["path"] }, + "url": { "type": "string" }, + "options": { + "type": "object", + "properties": { + "symlink": { "type": ["boolean", "null"] } + }, + "additionalProperties": true + } + } + }, + "artifact-repository": { + "type": "object", + "required": ["type", "url"], + "properties": { + "type": { "type": "string", "enum": ["artifact"] }, + "url": { "type": "string" } + } + }, + "pear-repository": { + "type": "object", + "required": ["type", "url"], + "properties": { + "type": { "type": "string", "enum": ["pear"] }, + "url": { "type": "string" }, + "vendor-alias": { "type": "string" } + } + }, + "package-repository": { + "type": "object", + "required": ["type", "package"], + "properties": { + "type": { "type": "string", "enum": ["package"] }, + "package": { + "oneOf": [ + { "$ref": "#/definitions/inline-package" }, + { + "type": "array", + "items": { + "type": { "$ref": "#/definitions/inline-package" } + } + } + ] + } + } + }, + "inline-package": { + "required": ["name", "version"], + "properties": { + "name": { + "type": "string", + "description": "Package name, including 'vendor-name/' prefix." + }, + "type": { + "type": "string" + }, + "target-dir": { + "description": "DEPRECATED: Forces the package to be installed into the given subdirectory path. This is used for autoloading PSR-0 packages that do not contain their full path. Use forward slashes for cross-platform compatibility.", + "type": "string" + }, + "description": { + "type": "string" + }, + "keywords": { + "type": "array", + "items": { + "type": "string" + } + }, + "homepage": { + "type": "string", + "format": "uri" + }, + "version": { + "type": "string" + }, + "time": { + "type": "string" + }, + "license": { + "type": [ + "string", + "array" + ] + }, + "authors": { + "$ref": "#/definitions/authors" + }, + "require": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "replace": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "conflict": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "provide": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "require-dev": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "suggest": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "extra": { + "type": ["object", "array"], + "additionalProperties": true + }, + "autoload": { + "$ref": "#/definitions/autoload" + }, + "archive": { + "type": ["object"], + "properties": { + "exclude": { + "type": "array" + } + } + }, + "bin": { + "type": ["string", "array"], + "description": "A set of files, or a single file, that should be treated as binaries and symlinked into bin-dir (from config).", + "items": { + "type": "string" + } + }, + "include-path": { + "type": ["array"], + "description": "DEPRECATED: A list of directories which should get added to PHP's include path. This is only present to support legacy projects, and all new code should preferably use autoloading.", + "items": { + "type": "string" + } + }, + "source": { + "type": "object", + "required": ["type", "url", "reference"], + "properties": { + "type": { + "type": "string" + }, + "url": { + "type": "string" + }, + "reference": { + "type": "string" + }, + "mirrors": { + "type": "array" + } + } + }, + "dist": { + "type": "object", + "required": ["type", "url"], + "properties": { + "type": { + "type": "string" + }, + "url": { + "type": "string" + }, + "reference": { + "type": "string" + }, + "shasum": { + "type": "string" + }, + "mirrors": { + "type": "array" + } + } + } + }, + "additionalProperties": true + } + } +} diff --git a/test/Unit/SchemaNormalizerTest.php b/test/Unit/SchemaNormalizerTest.php new file mode 100644 index 00000000..83594be9 --- /dev/null +++ b/test/Unit/SchemaNormalizerTest.php @@ -0,0 +1,372 @@ +faker()->url; + + $schemaStorage = $this->prophesize(SchemaStorage::class); + + $schemaStorage + ->getSchema(Argument::is($schemaUri)) + ->shouldBeCalled() + ->willThrow(new Exception\UriResolverException()); + + $normalizer = new SchemaNormalizer( + $schemaUri, + $schemaStorage->reveal(), + $this->prophesize(SchemaValidatorInterface::class)->reveal() + ); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage(\sprintf( + 'Schema URI "%s" could not be resolved.', + $schemaUri + )); + + $normalizer->normalize($json); + } + + public function testNormalizeThrowsRuntimeExceptionIfSchemaUriReferencesUnreadableResource() + { + $json = <<<'JSON' +{ + "name": "Andreas Möller", + "url": "https://localheinz.com" +} +JSON; + + $schemaUri = $this->faker()->url; + + $schemaStorage = $this->prophesize(SchemaStorage::class); + + $schemaStorage + ->getSchema(Argument::is($schemaUri)) + ->shouldBeCalled() + ->willThrow(new Exception\ResourceNotFoundException()); + + $normalizer = new SchemaNormalizer( + $schemaUri, + $schemaStorage->reveal(), + $this->prophesize(SchemaValidatorInterface::class)->reveal() + ); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage(\sprintf( + 'Schema URI "%s" does not reference a document that could be read.', + $schemaUri + )); + + $normalizer->normalize($json); + } + + public function testNormalizeThrowsRuntimeExceptionIfSchemaUriReferencesResourceWithInvalidMediaType() + { + $json = <<<'JSON' +{ + "name": "Andreas Möller", + "url": "https://localheinz.com" +} +JSON; + + $schemaUri = $this->faker()->url; + + $schemaStorage = $this->prophesize(SchemaStorage::class); + + $schemaStorage + ->getSchema(Argument::is($schemaUri)) + ->shouldBeCalled() + ->willThrow(new Exception\InvalidSchemaMediaTypeException()); + + $normalizer = new SchemaNormalizer( + $schemaUri, + $schemaStorage->reveal(), + $this->prophesize(SchemaValidatorInterface::class)->reveal() + ); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage(\sprintf( + 'Schema URI "%s" does not reference a document with media type "application/schema+json".', + $schemaUri + )); + + $normalizer->normalize($json); + } + + public function testNormalizeThrowsRuntimeExceptionIfSchemaUriReferencesResourceWithInvalidJson() + { + $json = <<<'JSON' +{ + "name": "Andreas Möller", + "url": "https://localheinz.com" +} +JSON; + + $schemaUri = $this->faker()->url; + + $schemaStorage = $this->prophesize(SchemaStorage::class); + + $schemaStorage + ->getSchema($schemaUri) + ->shouldBeCalled() + ->willThrow(new Exception\JsonDecodingException()); + + $normalizer = new SchemaNormalizer( + $schemaUri, + $schemaStorage->reveal(), + $this->prophesize(SchemaValidatorInterface::class)->reveal() + ); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage(\sprintf( + 'Schema URI "%s" does not reference a document with valid JSON syntax.', + $schemaUri + )); + + $normalizer->normalize($json); + } + + public function testNormalizeRejectsInvalidJsonAccordingToSchema() + { + $json = <<<'JSON' +{ + "name": "Andreas Möller", + "url": "https://localheinz.com" +} +JSON; + + $schemaUri = $this->faker()->url; + + $schema = <<<'JSON' +{ + "type": "array" +} +JSON; + + $jsonDecoded = \json_decode($json); + $schemaDecoded = \json_decode($schema); + + $schemaStorage = $this->prophesize(SchemaStorage::class); + + $schemaStorage + ->getSchema(Argument::is($schemaUri)) + ->shouldBeCalled() + ->willReturn($schemaDecoded); + + $schemaValidator = $this->prophesize(SchemaValidatorInterface::class); + + $schemaValidator + ->isValid( + Argument::exact($jsonDecoded), + Argument::is($schemaDecoded) + ) + ->shouldBeCalled() + ->willReturn(false); + + $normalizer = new SchemaNormalizer( + $schemaUri, + $schemaStorage->reveal(), + $schemaValidator->reveal() + ); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage(\sprintf( + 'Original is not valid according to schema "%s".', + $schemaUri + )); + + $normalizer->normalize($json); + } + + public function testNormalizeThrowsRuntimeExceptionIfNormalizedIsInvalidAccordingToSchema() + { + $json = <<<'JSON' +{ + "url": "https://localheinz.com", + "name": "Andreas Möller" +} +JSON; + + $schemaUri = $this->faker()->url; + + $schema = <<<'JSON' +{ + "type": "object", + "additionalProperties": false, + "properties": { + "name" : { + "type" : "string" + }, + "role" : { + "type" : "string" + } + } +} +JSON; + + $normalized = <<<'JSON' +{ + "name": "Andreas Möller", + "url": "https://localheinz.com" +} +JSON; + + $jsonDecoded = \json_decode($json); + $schemaDecoded = \json_decode($schema); + $normalizedDecoded = \json_decode($normalized); + + $schemaStorage = $this->prophesize(SchemaStorage::class); + + $schemaStorage + ->getSchema(Argument::is($schemaUri)) + ->shouldBeCalled() + ->willReturn($schemaDecoded); + + $schemaValidator = $this->prophesize(SchemaValidatorInterface::class); + + $schemaValidator + ->isValid( + Argument::exact($jsonDecoded), + Argument::is($schemaDecoded) + ) + ->shouldBeCalled() + ->will(function () use ($schemaValidator, $normalizedDecoded, $schemaDecoded) { + $schemaValidator + ->isValid( + Argument::exact($normalizedDecoded), + Argument::is($schemaDecoded) + ) + ->shouldBeCalled() + ->willReturn(false); + + return true; + }); + + $normalizer = new SchemaNormalizer( + $schemaUri, + $schemaStorage->reveal(), + $schemaValidator->reveal() + ); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage(\sprintf( + 'Normalized is not valid according to schema "%s".', + $schemaUri + )); + + $normalizer->normalize($json); + } + + /** + * @dataProvider providerNormalizeNormalizes + * + * @param string $expected + * @param string $json + * @param string $schemaUri + */ + public function testNormalizeNormalizes(string $expected, string $json, string $schemaUri) + { + $normalizer = new SchemaNormalizer($schemaUri); + + $normalized = $normalizer->normalize($json); + + $this->assertSame($expected, $normalized); + } + + public function providerNormalizeNormalizes(): \Generator + { + $basePath = __DIR__ . '/../'; + + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/../Fixture/SchemaNormalizer/NormalizeNormalizes')); + + foreach ($iterator as $fileInfo) { + if (!$fileInfo->isFile()) { + continue; + } + + if ('normalized.json' !== $fileInfo->getBasename()) { + continue; + } + + $normalizedFile = $fileInfo->getRealPath(); + + $jsonFile = \preg_replace( + '/normalized\.json$/', + 'original.json', + $normalizedFile + ); + + if (!\file_exists($jsonFile)) { + throw new \RuntimeException(\sprintf( + 'Expected "%s" to exist, but it does not.', + $jsonFile + )); + } + + $schemaFile = \preg_replace( + '/normalized\.json$/', + 'schema.json', + $normalizedFile + ); + + if (!\file_exists($schemaFile)) { + throw new \RuntimeException(\sprintf( + 'Expected "%s" to exist, but it does not.', + $schemaFile + )); + } + + $expected = $this->jsonFromFile($normalizedFile); + $json = $this->jsonFromFile($jsonFile); + $schemaUri = \sprintf( + 'file://%s', + $schemaFile + ); + + $key = \substr( + $fileInfo->getPath(), + \strlen($basePath) + ); + + yield $key => [ + $expected, + $json, + $schemaUri, + ]; + } + } + + private function jsonFromFile(string $file): string + { + $json = \file_get_contents($file); + + return \json_encode(\json_decode($json)); + } +}