CiConfigurationTest.php 8.5 KB

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