CiConfigurationTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. if ('8.2' === $lastSupportedVersion) {
  102. return; // no further releases available yet
  103. }
  104. self::ensureTraversableContainsIdenticalIsAvailable();
  105. self::assertThat($ciVersions, self::logicalOr(
  106. // if `$lastsupportedVersion` is already a snapshot version
  107. new TraversableContainsIdentical(\sprintf('%.1fsnapshot', $lastSupportedVersion)),
  108. // if `$lastsupportedVersion` is not snapshot version, expect CI to run snapshot of next PHP version
  109. new TraversableContainsIdentical('nightly'),
  110. new TraversableContainsIdentical(\sprintf('%.1fsnapshot', $lastSupportedVersion + 0.1)),
  111. // GitHub CI uses just versions, without suffix, e.g. 8.1 for 8.1snapshot as of writing
  112. new TraversableContainsIdentical(\sprintf('%.1f', $lastSupportedVersion + 0.1)),
  113. new TraversableContainsIdentical(\sprintf('%.1f', round($lastSupportedVersion + 1.0)))
  114. ));
  115. }
  116. /**
  117. * @param list<numeric-string> $supportedVersions
  118. * @param list<numeric-string> $ciVersions
  119. */
  120. private static function assertSupportedPhpVersionsAreCoveredByCiJobs(array $supportedVersions, array $ciVersions): void
  121. {
  122. $lastSupportedVersion = array_pop($supportedVersions);
  123. foreach ($supportedVersions as $expectedVersion) {
  124. self::assertContains($expectedVersion, $ciVersions);
  125. }
  126. self::ensureTraversableContainsIdenticalIsAvailable();
  127. self::assertThat($ciVersions, self::logicalOr(
  128. new TraversableContainsIdentical($lastSupportedVersion),
  129. new TraversableContainsIdentical(\sprintf('%.1fsnapshot', $lastSupportedVersion))
  130. ));
  131. }
  132. private function getPhpVersionUsedByCiForDeployments(): string
  133. {
  134. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/ci.yml'));
  135. $version = $yaml['jobs']['deployment']['env']['php-version'];
  136. return \is_string($version) ? $version : \sprintf('%.1f', $version);
  137. }
  138. /**
  139. * @return list<numeric-string>
  140. */
  141. private function getAllPhpVersionsUsedByCiForTests(): array
  142. {
  143. return $this->getPhpVersionsUsedByGitHub();
  144. }
  145. private function convertPhpVerIdToNiceVer(string $verId): string
  146. {
  147. $matchResult = Preg::match('/^(?<major>\d{1,2})(?<minor>\d{2})(?<patch>\d{2})$/', $verId, $capture);
  148. if (!$matchResult) {
  149. throw new \LogicException(\sprintf('Can\'t parse version "%s" id.', $verId));
  150. }
  151. return \sprintf('%d.%d', $capture['major'], $capture['minor']);
  152. }
  153. private function getMaxPhpVersionFromEntryFile(): string
  154. {
  155. $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer'));
  156. $sequence = $tokens->findSequence([
  157. [T_STRING, 'PHP_VERSION_ID'],
  158. [T_IS_GREATER_OR_EQUAL],
  159. [T_LNUMBER],
  160. ]);
  161. if (null === $sequence) {
  162. throw new \LogicException("Can't find version - perhaps entry file was modified?");
  163. }
  164. $phpVerId = (int) end($sequence)->getContent();
  165. return $this->convertPhpVerIdToNiceVer((string) ($phpVerId - 100));
  166. }
  167. private function getMinPhpVersionFromEntryFile(): string
  168. {
  169. $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer'));
  170. $sequence = $tokens->findSequence([
  171. [T_STRING, 'PHP_VERSION_ID'],
  172. '<',
  173. [T_LNUMBER],
  174. ]);
  175. if (null === $sequence) {
  176. throw new \LogicException("Can't find version - perhaps entry file was modified?");
  177. }
  178. $phpVerId = end($sequence)->getContent();
  179. return $this->convertPhpVerIdToNiceVer($phpVerId);
  180. }
  181. /**
  182. * @return array<string, string>
  183. */
  184. private function getGitHubCiEnvs(): array
  185. {
  186. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/ci.yml'));
  187. return $yaml['env'];
  188. }
  189. /**
  190. * @return list<numeric-string>
  191. */
  192. private function getPhpVersionsUsedByGitHub(): array
  193. {
  194. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/ci.yml'));
  195. $phpVersions = $yaml['jobs']['tests']['strategy']['matrix']['php-version'] ?? [];
  196. foreach ($yaml['jobs']['tests']['strategy']['matrix']['include'] as $job) {
  197. $phpVersions[] = $job['php-version'];
  198. }
  199. return $phpVersions;
  200. }
  201. /**
  202. * @return list<numeric-string>
  203. */
  204. private function getPhpVersionsUsedForBuildingOfficialImages(): array
  205. {
  206. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/release.yml'));
  207. return array_map(
  208. static fn ($item) => $item['php-version'],
  209. $yaml['jobs']['docker-images']['strategy']['matrix']['include']
  210. );
  211. }
  212. /**
  213. * @return list<numeric-string>
  214. */
  215. private function getPhpVersionsUsedForBuildingLocalImages(): array
  216. {
  217. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/docker.yml'));
  218. return array_map(
  219. static fn ($item) => $item['php-version'],
  220. $yaml['jobs']['docker-compose-build']['strategy']['matrix']['include']
  221. );
  222. }
  223. }