CiConfigurationTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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\Preg;
  14. use PhpCsFixer\Tests\TestCase;
  15. use PhpCsFixer\Tokenizer\Tokens;
  16. use PHPUnit\Framework\Constraint\TraversableContainsIdentical;
  17. use Symfony\Component\Yaml\Yaml;
  18. /**
  19. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  20. *
  21. * @internal
  22. *
  23. * @coversNothing
  24. *
  25. * @group auto-review
  26. * @group covers-nothing
  27. */
  28. final class CiConfigurationTest extends TestCase
  29. {
  30. public function testTestJobsRunOnEachPhp(): void
  31. {
  32. $supportedVersions = [];
  33. $supportedMinPhp = (float) $this->getMinPhpVersionFromEntryFile();
  34. $supportedMaxPhp = (float) $this->getMaxPhpVersionFromEntryFile();
  35. if ($supportedMaxPhp >= 8) {
  36. $supportedVersions = array_merge(
  37. $supportedVersions,
  38. self::generateMinorVersionsRange($supportedMinPhp, 7.4)
  39. );
  40. $supportedMinPhp = 8;
  41. }
  42. $supportedVersions = [
  43. ...$supportedVersions,
  44. ...self::generateMinorVersionsRange($supportedMinPhp, $supportedMaxPhp),
  45. ];
  46. self::assertTrue(\count($supportedVersions) > 0);
  47. $ciVersions = $this->getAllPhpVersionsUsedByCiForTests();
  48. self::assertNotEmpty($ciVersions);
  49. self::assertSupportedPhpVersionsAreCoveredByCiJobs($supportedVersions, $ciVersions);
  50. self::assertUpcomingPhpVersionIsCoveredByCiJob(end($supportedVersions), $ciVersions);
  51. }
  52. public function testDeploymentJobsRunOnLatestStablePhpThatIsSupportedByTool(): void
  53. {
  54. $ciVersionsForDeployments = $this->getAllPhpVersionsUsedByCiForDeployments();
  55. $ciVersions = $this->getAllPhpVersionsUsedByCiForTests();
  56. $expectedPhp = $this->getMaxPhpVersionFromEntryFile();
  57. if (\in_array($expectedPhp.'snapshot', $ciVersions, true)) {
  58. // last version of used PHP is snapshot. we should test against previous one, that is stable
  59. $expectedPhp = (string) ((float) $expectedPhp - 0.1);
  60. }
  61. self::assertGreaterThanOrEqual(1, \count($ciVersionsForDeployments));
  62. self::assertGreaterThanOrEqual(1, \count($ciVersions));
  63. foreach ($ciVersionsForDeployments as $ciVersionsForDeployment) {
  64. self::assertTrue(
  65. version_compare($expectedPhp, $ciVersionsForDeployment, 'eq'),
  66. sprintf('Expects %s to be %s', $ciVersionsForDeployment, $expectedPhp)
  67. );
  68. }
  69. }
  70. /**
  71. * @return list<numeric-string>
  72. */
  73. private static function generateMinorVersionsRange(float $from, float $to): array
  74. {
  75. $range = [];
  76. for ($version = $from; $version <= $to; $version += 0.1) {
  77. $range[] = sprintf('%.1f', $version);
  78. }
  79. return $range;
  80. }
  81. private static function ensureTraversableContainsIdenticalIsAvailable(): void
  82. {
  83. if (!class_exists(TraversableContainsIdentical::class)) {
  84. self::markTestSkipped('TraversableContainsIdentical not available.');
  85. }
  86. }
  87. /**
  88. * @param numeric-string $lastSupportedVersion
  89. * @param list<numeric-string> $ciVersions
  90. */
  91. private static function assertUpcomingPhpVersionIsCoveredByCiJob(string $lastSupportedVersion, array $ciVersions): void
  92. {
  93. if ('8.2' === $lastSupportedVersion) {
  94. return; // no further releases available yet
  95. }
  96. self::ensureTraversableContainsIdenticalIsAvailable();
  97. self::assertThat($ciVersions, self::logicalOr(
  98. // if `$lastsupportedVersion` is already a snapshot version
  99. new TraversableContainsIdentical(sprintf('%.1fsnapshot', $lastSupportedVersion)),
  100. // if `$lastsupportedVersion` is not snapshot version, expect CI to run snapshot of next PHP version
  101. new TraversableContainsIdentical('nightly'),
  102. new TraversableContainsIdentical(sprintf('%.1fsnapshot', $lastSupportedVersion + 0.1)),
  103. // GitHub CI uses just versions, without suffix, e.g. 8.1 for 8.1snapshot as of writing
  104. new TraversableContainsIdentical(sprintf('%.1f', $lastSupportedVersion + 0.1)),
  105. new TraversableContainsIdentical(sprintf('%.1f', round($lastSupportedVersion + 1.0)))
  106. ));
  107. }
  108. /**
  109. * @param list<numeric-string> $supportedVersions
  110. * @param list<numeric-string> $ciVersions
  111. */
  112. private static function assertSupportedPhpVersionsAreCoveredByCiJobs(array $supportedVersions, array $ciVersions): void
  113. {
  114. $lastSupportedVersion = array_pop($supportedVersions);
  115. foreach ($supportedVersions as $expectedVersion) {
  116. self::assertContains($expectedVersion, $ciVersions);
  117. }
  118. self::ensureTraversableContainsIdenticalIsAvailable();
  119. self::assertThat($ciVersions, self::logicalOr(
  120. new TraversableContainsIdentical($lastSupportedVersion),
  121. new TraversableContainsIdentical(sprintf('%.1fsnapshot', $lastSupportedVersion))
  122. ));
  123. }
  124. /**
  125. * @return array<int, string>
  126. */
  127. private function getAllPhpVersionsUsedByCiForDeployments(): array
  128. {
  129. $jobs = array_filter($this->getGitHubJobs(), static fn (array $job): bool => isset($job['execute-deployment']) && 'yes' === $job['execute-deployment']);
  130. return array_map(static fn ($job): string => \is_string($job['php-version']) ? $job['php-version'] : sprintf('%.1f', $job['php-version']), $jobs);
  131. }
  132. /**
  133. * @return list<numeric-string>
  134. */
  135. private function getAllPhpVersionsUsedByCiForTests(): array
  136. {
  137. return $this->getPhpVersionsUsedByGitHub();
  138. }
  139. private function convertPhpVerIdToNiceVer(string $verId): string
  140. {
  141. $matchResult = Preg::match('/^(?<major>\d{1,2})(?<minor>\d{2})(?<patch>\d{2})$/', $verId, $capture);
  142. if (!$matchResult) {
  143. throw new \LogicException(sprintf('Can\'t parse version "%s" id.', $verId));
  144. }
  145. return sprintf('%d.%d', $capture['major'], $capture['minor']);
  146. }
  147. private function getMaxPhpVersionFromEntryFile(): string
  148. {
  149. $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer'));
  150. $sequence = $tokens->findSequence([
  151. [T_STRING, 'PHP_VERSION_ID'],
  152. [T_IS_GREATER_OR_EQUAL],
  153. [T_LNUMBER],
  154. ]);
  155. if (null === $sequence) {
  156. throw new \LogicException("Can't find version - perhaps entry file was modified?");
  157. }
  158. $phpVerId = (int) end($sequence)->getContent();
  159. return $this->convertPhpVerIdToNiceVer((string) ($phpVerId - 100));
  160. }
  161. private function getMinPhpVersionFromEntryFile(): string
  162. {
  163. $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer'));
  164. $sequence = $tokens->findSequence([
  165. [T_STRING, 'PHP_VERSION_ID'],
  166. '<',
  167. [T_LNUMBER],
  168. ]);
  169. if (null === $sequence) {
  170. throw new \LogicException("Can't find version - perhaps entry file was modified?");
  171. }
  172. $phpVerId = end($sequence)->getContent();
  173. return $this->convertPhpVerIdToNiceVer($phpVerId);
  174. }
  175. /**
  176. * @return list<array<string, scalar>>
  177. */
  178. private function getGitHubJobs(): array
  179. {
  180. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/ci.yml'));
  181. return $yaml['jobs']['tests']['strategy']['matrix']['include'];
  182. }
  183. /**
  184. * @return list<numeric-string>
  185. */
  186. private function getPhpVersionsUsedByGitHub(): array
  187. {
  188. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/ci.yml'));
  189. $phpVersions = $yaml['jobs']['tests']['strategy']['matrix']['php-version'] ?? [];
  190. foreach ($yaml['jobs']['tests']['strategy']['matrix']['include'] as $job) {
  191. $phpVersions[] = $job['php-version'];
  192. }
  193. return $phpVersions;
  194. }
  195. }