CiConfigurationTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. self::assertSupportedPhpVersionsAreCoveredByCiJobs($supportedVersions, $this->getPhpVersionsUsedForBuildingOfficialImages());
  62. self::assertSupportedPhpVersionsAreCoveredByCiJobs($supportedVersions, $this->getPhpVersionsUsedForBuildingLocalImages());
  63. }
  64. public function testDeploymentJobRunOnLatestStablePhpThatIsSupportedByTool(): void
  65. {
  66. $ciVersionsForDeployment = $this->getPhpVersionUsedByCiForDeployments();
  67. $ciVersions = $this->getAllPhpVersionsUsedByCiForTests();
  68. $expectedPhp = $this->getMaxPhpVersionFromEntryFile();
  69. if (\in_array($expectedPhp.'snapshot', $ciVersions, true)) {
  70. // last version of used PHP is snapshot. we should test against previous one, that is stable
  71. $expectedPhp = (string) ((float) $expectedPhp - 0.1);
  72. }
  73. self::assertTrue(
  74. version_compare($expectedPhp, $ciVersionsForDeployment, 'eq'),
  75. \sprintf('Expects %s to be %s', $ciVersionsForDeployment, $expectedPhp)
  76. );
  77. }
  78. /**
  79. * @return list<numeric-string>
  80. */
  81. private static function generateMinorVersionsRange(float $from, float $to): array
  82. {
  83. $range = [];
  84. for ($version = $from; $version <= $to; $version += 0.1) {
  85. $range[] = \sprintf('%.1f', $version);
  86. }
  87. return $range;
  88. }
  89. private static function ensureTraversableContainsIdenticalIsAvailable(): void
  90. {
  91. if (!class_exists(TraversableContainsIdentical::class)) {
  92. self::markTestSkipped('TraversableContainsIdentical not available.');
  93. }
  94. }
  95. /**
  96. * @param numeric-string $lastSupportedVersion
  97. * @param list<numeric-string> $ciVersions
  98. */
  99. private static function assertUpcomingPhpVersionIsCoveredByCiJob(string $lastSupportedVersion, array $ciVersions): void
  100. {
  101. self::ensureTraversableContainsIdenticalIsAvailable();
  102. self::assertThat($ciVersions, self::logicalOr(
  103. // if `$lastsupportedVersion` is already a snapshot version
  104. new TraversableContainsIdentical(\sprintf('%.1fsnapshot', $lastSupportedVersion)),
  105. // if `$lastsupportedVersion` is not snapshot version, expect CI to run snapshot of next PHP version
  106. new TraversableContainsIdentical('nightly'),
  107. new TraversableContainsIdentical(\sprintf('%.1fsnapshot', $lastSupportedVersion + 0.1)),
  108. // GitHub CI uses just versions, without suffix, e.g. 8.1 for 8.1snapshot as of writing
  109. new TraversableContainsIdentical(\sprintf('%.1f', $lastSupportedVersion + 0.1)),
  110. new TraversableContainsIdentical(\sprintf('%.1f', floor($lastSupportedVersion + 1.0)))
  111. ));
  112. }
  113. /**
  114. * @param list<numeric-string> $supportedVersions
  115. * @param list<numeric-string> $ciVersions
  116. */
  117. private static function assertSupportedPhpVersionsAreCoveredByCiJobs(array $supportedVersions, array $ciVersions): void
  118. {
  119. $lastSupportedVersion = array_pop($supportedVersions);
  120. foreach ($supportedVersions as $expectedVersion) {
  121. self::assertContains($expectedVersion, $ciVersions);
  122. }
  123. self::ensureTraversableContainsIdenticalIsAvailable();
  124. self::assertThat($ciVersions, self::logicalOr(
  125. new TraversableContainsIdentical($lastSupportedVersion),
  126. new TraversableContainsIdentical(\sprintf('%.1fsnapshot', $lastSupportedVersion))
  127. ));
  128. }
  129. private function getPhpVersionUsedByCiForDeployments(): string
  130. {
  131. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/ci.yml'));
  132. $version = $yaml['jobs']['deployment']['env']['php-version'];
  133. return \is_string($version) ? $version : \sprintf('%.1f', $version);
  134. }
  135. /**
  136. * @return list<numeric-string>
  137. */
  138. private function getAllPhpVersionsUsedByCiForTests(): array
  139. {
  140. return $this->getPhpVersionsUsedByGitHub();
  141. }
  142. private function convertPhpVerIdToNiceVer(string $verId): string
  143. {
  144. $matchResult = Preg::match('/^(?<major>\d{1,2})_?(?<minor>\d{2})_?(?<patch>\d{2})$/', $verId, $capture);
  145. if (!$matchResult) {
  146. throw new \LogicException(\sprintf('Can\'t parse version "%s" id.', $verId));
  147. }
  148. return \sprintf('%d.%d', $capture['major'], $capture['minor']);
  149. }
  150. private function getMaxPhpVersionFromEntryFile(): string
  151. {
  152. $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer'));
  153. $sequence = $tokens->findSequence([
  154. [T_STRING, 'PHP_VERSION_ID'],
  155. [T_IS_GREATER_OR_EQUAL],
  156. [T_INT_CAST],
  157. [T_CONSTANT_ENCAPSED_STRING],
  158. ]);
  159. if (null === $sequence) {
  160. throw new \LogicException("Can't find version - perhaps entry file was modified?");
  161. }
  162. $phpVerId = trim(end($sequence)->getContent(), '\'');
  163. return $this->convertPhpVerIdToNiceVer((string) ((int) $phpVerId - 100));
  164. }
  165. private function getMinPhpVersionFromEntryFile(): string
  166. {
  167. $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer'));
  168. $sequence = $tokens->findSequence([
  169. [T_STRING, 'PHP_VERSION_ID'],
  170. '<',
  171. [T_INT_CAST],
  172. [T_CONSTANT_ENCAPSED_STRING],
  173. ]);
  174. if (null === $sequence) {
  175. throw new \LogicException("Can't find version - perhaps entry file was modified?");
  176. }
  177. $phpVerId = trim(end($sequence)->getContent(), '\'');
  178. return $this->convertPhpVerIdToNiceVer($phpVerId);
  179. }
  180. /**
  181. * @return array<string, string>
  182. */
  183. private function getGitHubCiEnvs(): array
  184. {
  185. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/ci.yml'));
  186. return $yaml['env'];
  187. }
  188. /**
  189. * @return list<numeric-string>
  190. */
  191. private function getPhpVersionsUsedByGitHub(): array
  192. {
  193. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/ci.yml'));
  194. $phpVersions = $yaml['jobs']['tests']['strategy']['matrix']['php-version'] ?? [];
  195. foreach ($yaml['jobs']['tests']['strategy']['matrix']['include'] as $job) {
  196. $phpVersions[] = $job['php-version'];
  197. }
  198. return $phpVersions;
  199. }
  200. /**
  201. * @return list<numeric-string>
  202. */
  203. private function getPhpVersionsUsedForBuildingOfficialImages(): array
  204. {
  205. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/release.yml'));
  206. return array_map(
  207. static fn ($item) => $item['php-version'],
  208. $yaml['jobs']['docker-images']['strategy']['matrix']['include']
  209. );
  210. }
  211. /**
  212. * @return list<numeric-string>
  213. */
  214. private function getPhpVersionsUsedForBuildingLocalImages(): array
  215. {
  216. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/docker.yml'));
  217. return array_map(
  218. static fn ($item) => $item['php-version'],
  219. $yaml['jobs']['docker-compose-build']['strategy']['matrix']['include']
  220. );
  221. }
  222. }