CiConfigurationTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. $supportedMinPhp = (float) $this->getMinPhpVersionFromEntryFile();
  43. $supportedMaxPhp = (float) $this->getMaxPhpVersionFromEntryFile();
  44. $supportedVersions = self::generateMinorVersionsRange($supportedMinPhp, $supportedMaxPhp);
  45. self::assertTrue(\count($supportedVersions) > 0);
  46. $ciVersions = $this->getAllPhpVersionsUsedByCiForTests();
  47. self::assertNotEmpty($ciVersions);
  48. self::assertSupportedPhpVersionsAreCoveredByCiJobs($supportedVersions, $ciVersions);
  49. self::assertUpcomingPhpVersionIsCoveredByCiJob(end($supportedVersions), $ciVersions);
  50. self::assertSupportedPhpVersionsAreCoveredByCiJobs($supportedVersions, $this->getPhpVersionsUsedForBuildingOfficialImages());
  51. self::assertSupportedPhpVersionsAreCoveredByCiJobs($supportedVersions, $this->getPhpVersionsUsedForBuildingLocalImages());
  52. self::assertPhpCompatibilityRangeIsValid($supportedMinPhp, $supportedMaxPhp);
  53. }
  54. public function testDeploymentJobRunOnLatestStablePhpThatIsSupportedByTool(): void
  55. {
  56. $ciVersionsForDeployment = $this->getPhpVersionUsedByCiForDeployments();
  57. $ciVersions = $this->getAllPhpVersionsUsedByCiForTests();
  58. $expectedPhp = $this->getMaxPhpVersionFromEntryFile();
  59. if (\in_array($expectedPhp.'snapshot', $ciVersions, true)) {
  60. // last version of used PHP is snapshot. we should test against previous one, that is stable
  61. $expectedPhp = (string) ((float) $expectedPhp - 0.1);
  62. }
  63. self::assertTrue(
  64. version_compare($expectedPhp, $ciVersionsForDeployment, 'eq'),
  65. \sprintf('Expects %s to be %s', $ciVersionsForDeployment, $expectedPhp)
  66. );
  67. }
  68. public function testDockerCIBuildsComposeServices(): void
  69. {
  70. $compose = Yaml::parseFile(__DIR__.'/../../compose.yaml');
  71. $composeServices = array_keys($compose['services']);
  72. sort($composeServices);
  73. $ci = Yaml::parseFile(__DIR__.'/../../.github/workflows/docker.yml');
  74. $ciServices = array_map(
  75. static fn ($item) => $item['docker-service'],
  76. $ci['jobs']['docker-compose-build']['strategy']['matrix']['include']
  77. );
  78. sort($ciServices);
  79. self::assertSame($composeServices, $ciServices);
  80. }
  81. /**
  82. * @return list<numeric-string>
  83. */
  84. private static function generateMinorVersionsRange(float $from, float $to): array
  85. {
  86. $range = [];
  87. $lastMinorVersions = [7.4];
  88. $version = $from;
  89. while ($version <= $to) {
  90. $range[] = \sprintf('%.1f', $version);
  91. if (\in_array($version, $lastMinorVersions, true)) {
  92. $version = ceil($version);
  93. } else {
  94. $version += 0.1;
  95. }
  96. }
  97. return $range;
  98. }
  99. private static function ensureTraversableContainsIdenticalIsAvailable(): void
  100. {
  101. if (!class_exists(TraversableContainsIdentical::class)) {
  102. self::markTestSkipped('TraversableContainsIdentical not available.');
  103. }
  104. }
  105. /**
  106. * @param numeric-string $lastSupportedVersion
  107. * @param list<numeric-string> $ciVersions
  108. */
  109. private static function assertUpcomingPhpVersionIsCoveredByCiJob(string $lastSupportedVersion, array $ciVersions): void
  110. {
  111. self::ensureTraversableContainsIdenticalIsAvailable();
  112. self::assertThat($ciVersions, self::logicalOr(
  113. // if `$lastsupportedVersion` is already a snapshot version
  114. new TraversableContainsIdentical(\sprintf('%.1fsnapshot', $lastSupportedVersion)),
  115. // if `$lastsupportedVersion` is not snapshot version, expect CI to run snapshot of next PHP version
  116. new TraversableContainsIdentical('nightly'),
  117. new TraversableContainsIdentical(\sprintf('%.1fsnapshot', $lastSupportedVersion + 0.1)),
  118. // GitHub CI uses just versions, without suffix, e.g. 8.1 for 8.1snapshot as of writing
  119. new TraversableContainsIdentical(\sprintf('%.1f', $lastSupportedVersion + 0.1)),
  120. new TraversableContainsIdentical(\sprintf('%.1f', floor($lastSupportedVersion + 1.0)))
  121. ));
  122. }
  123. /**
  124. * @param list<numeric-string> $supportedVersions
  125. * @param list<numeric-string> $ciVersions
  126. */
  127. private static function assertSupportedPhpVersionsAreCoveredByCiJobs(array $supportedVersions, array $ciVersions): void
  128. {
  129. $lastSupportedVersion = array_pop($supportedVersions);
  130. foreach ($supportedVersions as $expectedVersion) {
  131. self::assertContains($expectedVersion, $ciVersions);
  132. }
  133. self::ensureTraversableContainsIdenticalIsAvailable();
  134. self::assertThat($ciVersions, self::logicalOr(
  135. new TraversableContainsIdentical($lastSupportedVersion),
  136. new TraversableContainsIdentical(\sprintf('%.1fsnapshot', $lastSupportedVersion))
  137. ));
  138. }
  139. private static function assertPhpCompatibilityRangeIsValid(float $supportedMinPhp, float $supportedMaxPhp): void
  140. {
  141. $matchResult = Preg::match(
  142. '/<config name="testVersion" value="(?<min>\d+\.\d+)-(?<max>\d+\.\d+)"\/>/',
  143. // @phpstan-ignore argument.type (This is file that is always present in the project, it won't return `false`)
  144. file_get_contents(__DIR__.'/../../dev-tools/php-compatibility/phpcs-php-compatibility.xml'),
  145. $capture
  146. );
  147. if (!$matchResult) {
  148. throw new \LogicException('Can\'t parse PHP version range for verifying compatibility.');
  149. }
  150. self::assertSame($supportedMinPhp, (float) $capture['min']);
  151. self::assertSame($supportedMaxPhp, (float) $capture['max']);
  152. }
  153. private function getPhpVersionUsedByCiForDeployments(): string
  154. {
  155. $yaml = Yaml::parseFile(__DIR__.'/../../.github/workflows/ci.yml');
  156. $version = $yaml['jobs']['deployment']['env']['php-version'];
  157. return \is_string($version) ? $version : \sprintf('%.1f', $version);
  158. }
  159. /**
  160. * @return list<numeric-string>
  161. */
  162. private function getAllPhpVersionsUsedByCiForTests(): array
  163. {
  164. return $this->getPhpVersionsUsedByGitHub();
  165. }
  166. private function convertPhpVerIdToNiceVer(string $verId): string
  167. {
  168. $matchResult = Preg::match('/^(?<major>\d{1,2})_?(?<minor>\d{2})_?(?<patch>\d{2})$/', $verId, $capture);
  169. if (!$matchResult) {
  170. throw new \LogicException(\sprintf('Can\'t parse version "%s" id.', $verId));
  171. }
  172. return \sprintf('%d.%d', $capture['major'], $capture['minor']);
  173. }
  174. private function getMaxPhpVersionFromEntryFile(): string
  175. {
  176. $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer'));
  177. $sequence = $tokens->findSequence([
  178. [T_STRING, 'PHP_VERSION_ID'],
  179. [T_IS_GREATER_OR_EQUAL],
  180. [T_INT_CAST],
  181. [T_CONSTANT_ENCAPSED_STRING],
  182. ]);
  183. if (null === $sequence) {
  184. throw new \LogicException("Can't find version - perhaps entry file was modified?");
  185. }
  186. $phpVerId = trim(end($sequence)->getContent(), '\'');
  187. return $this->convertPhpVerIdToNiceVer((string) ((int) $phpVerId - 100));
  188. }
  189. private function getMinPhpVersionFromEntryFile(): string
  190. {
  191. $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer'));
  192. $sequence = $tokens->findSequence([
  193. [T_STRING, 'PHP_VERSION_ID'],
  194. '<',
  195. [T_INT_CAST],
  196. [T_CONSTANT_ENCAPSED_STRING],
  197. ]);
  198. if (null === $sequence) {
  199. throw new \LogicException("Can't find version - perhaps entry file was modified?");
  200. }
  201. $phpVerId = trim(end($sequence)->getContent(), '\'');
  202. return $this->convertPhpVerIdToNiceVer($phpVerId);
  203. }
  204. /**
  205. * @return array<string, string>
  206. */
  207. private function getGitHubCiEnvs(): array
  208. {
  209. $yaml = Yaml::parseFile(__DIR__.'/../../.github/workflows/ci.yml');
  210. return $yaml['env'];
  211. }
  212. /**
  213. * @return list<numeric-string>
  214. */
  215. private function getPhpVersionsUsedByGitHub(): array
  216. {
  217. $yaml = Yaml::parseFile(__DIR__.'/../../.github/workflows/ci.yml');
  218. $phpVersions = $yaml['jobs']['tests']['strategy']['matrix']['php-version'] ?? [];
  219. foreach ($yaml['jobs']['tests']['strategy']['matrix']['include'] as $job) {
  220. $phpVersions[] = $job['php-version'];
  221. }
  222. return array_unique($phpVersions); // @phpstan-ignore return.type (we know it's a list of parsed strings)
  223. }
  224. /**
  225. * @return list<numeric-string>
  226. */
  227. private function getPhpVersionsUsedForBuildingOfficialImages(): array
  228. {
  229. $yaml = Yaml::parseFile(__DIR__.'/../../.github/workflows/release.yml');
  230. return array_map(
  231. static fn ($item) => $item['php-version'],
  232. $yaml['jobs']['docker-images']['strategy']['matrix']['include']
  233. );
  234. }
  235. /**
  236. * @return list<numeric-string>
  237. */
  238. private function getPhpVersionsUsedForBuildingLocalImages(): array
  239. {
  240. $yaml = Yaml::parseFile(__DIR__.'/../../.github/workflows/docker.yml');
  241. return array_map(
  242. static fn ($item) => substr($item, 4),
  243. array_filter(
  244. array_map(
  245. static fn ($item) => $item['docker-service'],
  246. $yaml['jobs']['docker-compose-build']['strategy']['matrix']['include']
  247. ),
  248. static fn ($item) => str_starts_with($item, 'php-')
  249. )
  250. );
  251. }
  252. }