CiConfigurationTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\AutoReview;
  12. use PhpCsFixer\Preg;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\Tokens;
  15. use PHPUnit\Framework\Constraint\TraversableContains;
  16. use Symfony\Component\Yaml\Yaml;
  17. /**
  18. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  19. *
  20. * @internal
  21. *
  22. * @coversNothing
  23. * @group auto-review
  24. * @group covers-nothing
  25. */
  26. final class CiConfigurationTest extends TestCase
  27. {
  28. public function testTestJobsRunOnEachPhp()
  29. {
  30. $supportedVersions = [];
  31. $supportedMinPhp = (float) $this->getMinPhpVersionFromEntryFile();
  32. $supportedMaxPhp = (float) $this->getMaxPhpVersionFromEntryFile();
  33. if ($supportedMinPhp < 7) {
  34. $supportedMinPhp = 7;
  35. $supportedVersions[] = '5.6';
  36. }
  37. for ($version = $supportedMinPhp; $version <= $supportedMaxPhp; $version += 0.1) {
  38. $supportedVersions[] = sprintf('%.1f', $version);
  39. }
  40. $ciVersions = $this->getAllPhpVersionsUsedByCiForTests();
  41. static::assertGreaterThanOrEqual(1, \count($ciVersions));
  42. self::assertSupportedPhpVersionsAreCoveredByCiJobs($supportedVersions, $ciVersions);
  43. self::assertUpcomingPhpVersionIsCoveredByCiJob(end($supportedVersions), $ciVersions);
  44. }
  45. public function testDeploymentJobsRunOnLatestStablePhpThatIsSupportedByTool()
  46. {
  47. $ciVersionsForDeployments = $this->getAllPhpVersionsUsedByCiForDeployments();
  48. $ciVersions = $this->getAllPhpVersionsUsedByCiForTests();
  49. $expectedPhp = $this->getMaxPhpVersionFromEntryFile();
  50. if (\in_array($expectedPhp.'snapshot', $ciVersions, true)) {
  51. // last version of used PHP is snapshot. we should test against previous one, that is stable
  52. $expectedPhp = (string) ((float) $expectedPhp - 0.1);
  53. }
  54. static::assertGreaterThanOrEqual(1, \count($ciVersionsForDeployments));
  55. static::assertGreaterThanOrEqual(1, \count($ciVersions));
  56. foreach ($ciVersionsForDeployments as $ciVersionsForDeployment) {
  57. static::assertTrue(
  58. version_compare($expectedPhp, $ciVersionsForDeployment, 'eq'),
  59. sprintf('Expects %s to be %s', $ciVersionsForDeployment, $expectedPhp)
  60. );
  61. }
  62. }
  63. private static function assertUpcomingPhpVersionIsCoveredByCiJob($lastSupportedVersion, array $ciVersions)
  64. {
  65. if (!class_exists(TraversableContains::class)) {
  66. static::markTestSkipped('TraversableContains not available.');
  67. }
  68. static::assertThat($ciVersions, static::logicalOr(
  69. // if `$lastsupportedVersion` is already a snapshot version
  70. new TraversableContains(sprintf('%.1fsnapshot', $lastSupportedVersion)),
  71. // if `$lastsupportedVersion` is not snapshot version, expect CI to run snapshot of next PHP version
  72. new TraversableContains('nightly'),
  73. new TraversableContains(sprintf('%.1fsnapshot', $lastSupportedVersion + 0.1)),
  74. // GitHub CI uses just versions, without suffix, e.g. 8.1 for 8.1snapshot as of writing
  75. new TraversableContains(sprintf('%.1f', $lastSupportedVersion + 0.1)),
  76. new TraversableContains(sprintf('%.1f', round($lastSupportedVersion + 1)))
  77. ));
  78. }
  79. private static function assertSupportedPhpVersionsAreCoveredByCiJobs(array $supportedVersions, array $ciVersions)
  80. {
  81. $lastSupportedVersion = array_pop($supportedVersions);
  82. foreach ($supportedVersions as $expectedVersion) {
  83. static::assertContains($expectedVersion, $ciVersions);
  84. }
  85. if (!class_exists(TraversableContains::class)) {
  86. static::markTestSkipped('TraversableContains not available.');
  87. }
  88. static::assertThat($ciVersions, static::logicalOr(
  89. new TraversableContains($lastSupportedVersion),
  90. new TraversableContains(sprintf('%.1fsnapshot', $lastSupportedVersion))
  91. ));
  92. }
  93. private function getAllPhpVersionsUsedByCiForDeployments()
  94. {
  95. $jobs = array_filter($this->getTravisJobs(), function ($job) {
  96. return 'Deployment' === $job['stage'];
  97. });
  98. return array_map(function ($job) {
  99. return (string) $job['php'];
  100. }, $jobs);
  101. }
  102. private function getAllPhpVersionsUsedByCiForTests()
  103. {
  104. return array_merge(
  105. $this->getPhpVersionsUsedByTravis(),
  106. $this->getPhpVersionsUsedByGitHub()
  107. );
  108. }
  109. private function convertPhpVerIdToNiceVer($verId)
  110. {
  111. $matchResult = Preg::match('/^(?<major>\d{1,2})(?<minor>\d{2})(?<patch>\d{2})$/', $verId, $capture);
  112. if (1 !== $matchResult) {
  113. throw new \LogicException("Can't parse version id.");
  114. }
  115. return sprintf('%d.%d', $capture['major'], $capture['minor']);
  116. }
  117. private function getMaxPhpVersionFromEntryFile()
  118. {
  119. $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer'));
  120. $sequence = $tokens->findSequence([
  121. [T_STRING, 'PHP_VERSION_ID'],
  122. [T_IS_GREATER_OR_EQUAL],
  123. [T_LNUMBER],
  124. ]);
  125. if (null === $sequence) {
  126. throw new \LogicException("Can't find version - perhaps entry file was modified?");
  127. }
  128. $phpVerId = (int) end($sequence)->getContent();
  129. return $this->convertPhpVerIdToNiceVer((string) ($phpVerId - 100));
  130. }
  131. private function getMinPhpVersionFromEntryFile()
  132. {
  133. $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer'));
  134. $sequence = $tokens->findSequence([
  135. [T_STRING, 'PHP_VERSION_ID'],
  136. '<',
  137. [T_LNUMBER],
  138. ]);
  139. if (null === $sequence) {
  140. throw new \LogicException("Can't find version - perhaps entry file was modified?");
  141. }
  142. $phpVerId = end($sequence)->getContent();
  143. return $this->convertPhpVerIdToNiceVer($phpVerId);
  144. }
  145. private function getTravisJobs()
  146. {
  147. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.travis.yml'));
  148. return $yaml['jobs']['include'];
  149. }
  150. private function getPhpVersionsUsedByGitHub()
  151. {
  152. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/ci.yaml'));
  153. $phpVersions = $yaml['jobs']['tests']['strategy']['matrix']['php-version'];
  154. foreach ($yaml['jobs']['tests']['strategy']['matrix']['include'] as $job) {
  155. $phpVersions[] = $job['php-version'];
  156. }
  157. return $phpVersions;
  158. }
  159. private function getPhpVersionsUsedByTravis()
  160. {
  161. $jobs = array_filter($this->getTravisJobs(), function ($job) {
  162. return false !== strpos($job['stage'], 'Test');
  163. });
  164. return array_map(function ($job) {
  165. return (string) $job['php'];
  166. }, $jobs);
  167. }
  168. }