ComposerFileTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\AutoReview;
  13. use PhpCsFixer\Tests\TestCase;
  14. /**
  15. * @internal
  16. *
  17. * @coversNothing
  18. *
  19. * @group covers-nothing
  20. * @group auto-review
  21. */
  22. final class ComposerFileTest extends TestCase
  23. {
  24. public function testScriptsHaveDescriptions(): void
  25. {
  26. $composerJson = self::readComposerJson();
  27. $scripts = array_keys($composerJson['scripts']);
  28. $descriptions = array_keys($composerJson['scripts-descriptions']);
  29. self::assertSame([], array_diff($scripts, $descriptions), 'There should be no scripts with missing description.');
  30. self::assertSame([], array_diff($descriptions, $scripts), 'There should be no superfluous description for not defined scripts.');
  31. }
  32. public function testScriptsAliasesDescriptionsFollowThePattern(): void
  33. {
  34. $composerJson = self::readComposerJson();
  35. $scripts = array_keys($composerJson['scripts']);
  36. $aliases = array_reduce($scripts, static function (array $carry, string $script) use ($composerJson): array {
  37. $code = $composerJson['scripts'][$script];
  38. if (\is_string($code) && '@' === $code[0]) {
  39. $potentialAlias = substr($code, 1);
  40. if (isset($composerJson['scripts'][$potentialAlias])) {
  41. $carry[$script] = $potentialAlias;
  42. }
  43. }
  44. return $carry;
  45. }, []);
  46. foreach ($aliases as $code => $alias) {
  47. self::assertSame(
  48. "Alias for '{$alias}'",
  49. $composerJson['scripts-descriptions'][$code],
  50. "Script description for '{$code}' alias should be following the pattern.",
  51. );
  52. }
  53. }
  54. /**
  55. * @return array<string, mixed>
  56. */
  57. private static function readComposerJson(): array
  58. {
  59. $composerJsonContent = file_get_contents(__DIR__.'/../../composer.json');
  60. return json_decode($composerJsonContent, true, 512, JSON_THROW_ON_ERROR);
  61. }
  62. }