TravisTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 TravisTest 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. ));
  75. }
  76. private static function assertSupportedPhpVersionsAreCoveredByCiJobs(array $supportedVersions, array $ciVersions)
  77. {
  78. $lastSupportedVersion = array_pop($supportedVersions);
  79. foreach ($supportedVersions as $expectedVersion) {
  80. static::assertContains($expectedVersion, $ciVersions);
  81. }
  82. if (!class_exists(TraversableContains::class)) {
  83. static::markTestSkipped('TraversableContains not available.');
  84. }
  85. static::assertThat($ciVersions, static::logicalOr(
  86. new TraversableContains($lastSupportedVersion),
  87. new TraversableContains(sprintf('%.1fsnapshot', $lastSupportedVersion))
  88. ));
  89. }
  90. private function getAllPhpVersionsUsedByCiForDeployments()
  91. {
  92. $jobs = array_filter($this->getTravisJobs(), function ($job) {
  93. return 'Deployment' === $job['stage'];
  94. });
  95. return array_map(function ($job) {
  96. return (string) $job['php'];
  97. }, $jobs);
  98. }
  99. private function getAllPhpVersionsUsedByCiForTests()
  100. {
  101. $jobs = array_filter($this->getTravisJobs(), function ($job) {
  102. return false !== strpos($job['stage'], 'Test');
  103. });
  104. return array_map(function ($job) {
  105. return (string) $job['php'];
  106. }, $jobs);
  107. }
  108. private function convertPhpVerIdToNiceVer($verId)
  109. {
  110. $matchResult = Preg::match('/^(?<major>\d{1,2})(?<minor>\d{2})(?<patch>\d{2})$/', $verId, $capture);
  111. if (1 !== $matchResult) {
  112. throw new \LogicException("Can't parse version id.");
  113. }
  114. return sprintf('%d.%d', $capture['major'], $capture['minor']);
  115. }
  116. private function getMaxPhpVersionFromEntryFile()
  117. {
  118. $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer'));
  119. $sequence = $tokens->findSequence([
  120. [T_STRING, 'PHP_VERSION_ID'],
  121. [T_IS_GREATER_OR_EQUAL],
  122. [T_LNUMBER],
  123. ]);
  124. if (null === $sequence) {
  125. throw new \LogicException("Can't find version - perhaps entry file was modified?");
  126. }
  127. $phpVerId = end($sequence)->getContent();
  128. return $this->convertPhpVerIdToNiceVer((string) ($phpVerId - 100));
  129. }
  130. private function getMinPhpVersionFromEntryFile()
  131. {
  132. $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer'));
  133. $sequence = $tokens->findSequence([
  134. [T_STRING, 'PHP_VERSION_ID'],
  135. '<',
  136. [T_LNUMBER],
  137. ]);
  138. if (null === $sequence) {
  139. throw new \LogicException("Can't find version - perhaps entry file was modified?");
  140. }
  141. $phpVerId = end($sequence)->getContent();
  142. return $this->convertPhpVerIdToNiceVer($phpVerId);
  143. }
  144. private function getTravisJobs()
  145. {
  146. $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.travis.yml'));
  147. return $yaml['jobs']['include'];
  148. }
  149. }