AssertJsonSchemaTrait.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Test\Assert;
  13. use JsonSchema\Validator;
  14. /** @internal */
  15. trait AssertJsonSchemaTrait
  16. {
  17. private static function assertJsonSchema(string $schemaFile, string $json): void
  18. {
  19. $data = json_decode($json, null, 512, JSON_THROW_ON_ERROR);
  20. $validator = new Validator();
  21. $validator->validate($data, (object) ['$ref' => 'file://'.realpath($schemaFile)]);
  22. self::assertTrue(
  23. $validator->isValid(),
  24. implode(
  25. "\n",
  26. array_map(
  27. static fn (array $item): string => \sprintf('Property `%s`: %s.', $item['property'], $item['message']),
  28. $validator->getErrors(),
  29. )
  30. )
  31. );
  32. }
  33. }