|
| 1 | +<?php |
| 2 | + |
| 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 | + |
| 13 | +namespace PhpCsFixer\Tests\AutoReview; |
| 14 | + |
| 15 | +use PhpCsFixer\Preg; |
| 16 | +use PhpCsFixer\Tests\TestCase; |
| 17 | +use PhpCsFixer\Tokenizer\Tokens; |
| 18 | +use PHPUnit\Framework\Constraint\TraversableContains; |
| 19 | +use Symfony\Component\Yaml\Yaml; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Dariusz Rumiński <dariusz.ruminski@gmail.com> |
| 23 | + * |
| 24 | + * @internal |
| 25 | + * |
| 26 | + * @coversNothing |
| 27 | + * @group auto-review |
| 28 | + * @group covers-nothing |
| 29 | + */ |
| 30 | +final class TravisTest extends TestCase |
| 31 | +{ |
| 32 | + public function testTestJobsRunOnEachPhp() |
| 33 | + { |
| 34 | + $expectedVersions = []; |
| 35 | + $expectedMinPhp = (float) $this->getMinPhpVersionFromEntryFile(); |
| 36 | + $expectedMaxPhp = (float) $this->getMaxPhpVersionFromEntryFile(); |
| 37 | + |
| 38 | + if ($expectedMinPhp < 7) { |
| 39 | + $expectedMinPhp = 7; |
| 40 | + $expectedVersions[] = '5.6'; |
| 41 | + } |
| 42 | + |
| 43 | + for ($version = $expectedMinPhp; $version <= $expectedMaxPhp; $version += 0.1) { |
| 44 | + $expectedVersions[] = sprintf('%.1f', $version); |
| 45 | + } |
| 46 | + |
| 47 | + $jobs = array_filter($this->getTravisJobs(), function ($job) { |
| 48 | + return false !== strpos($job['stage'], 'Test'); |
| 49 | + }); |
| 50 | + static::assertGreaterThanOrEqual(1, \count($jobs)); |
| 51 | + |
| 52 | + $versions = array_map(function ($job) { |
| 53 | + return $job['php']; |
| 54 | + }, $jobs); |
| 55 | + |
| 56 | + foreach ($expectedVersions as $expectedVersion) { |
| 57 | + static::assertContains($expectedVersion, $versions); |
| 58 | + } |
| 59 | + |
| 60 | + if (!class_exists(TraversableContains::class)) { |
| 61 | + static::markTestSkipped('TraversableContains not available.'); |
| 62 | + } |
| 63 | + |
| 64 | + static::assertThat($versions, static::logicalOr( |
| 65 | + new TraversableContains('nightly'), |
| 66 | + new TraversableContains(sprintf('%.1fsnapshot', end($expectedVersions) + 0.1)) |
| 67 | + )); |
| 68 | + } |
| 69 | + |
| 70 | + public function testDeploymentJobsRunOnLatestPhp() |
| 71 | + { |
| 72 | + $jobs = array_filter($this->getTravisJobs(), function ($job) { |
| 73 | + return 'Deployment' === $job['stage']; |
| 74 | + }); |
| 75 | + static::assertGreaterThanOrEqual(1, \count($jobs)); |
| 76 | + |
| 77 | + $expectedPhp = $this->getMaxPhpVersionFromEntryFile(); |
| 78 | + |
| 79 | + foreach ($jobs as $job) { |
| 80 | + $jobPhp = (string) $job['php']; |
| 81 | + static::assertTrue( |
| 82 | + version_compare($expectedPhp, $jobPhp, 'eq'), |
| 83 | + sprintf('Expects %s to be %s', $jobPhp, $expectedPhp) |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private function convertPhpVerIdToNiceVer($verId) |
| 89 | + { |
| 90 | + $matchResult = Preg::match('/^(?<major>\d{1,2})(?<minor>\d{2})(?<patch>\d{2})$/', $verId, $capture); |
| 91 | + if (1 !== $matchResult) { |
| 92 | + throw new \LogicException("Can't parse version id."); |
| 93 | + } |
| 94 | + |
| 95 | + return sprintf('%d.%d', $capture['major'], $capture['minor']); |
| 96 | + } |
| 97 | + |
| 98 | + private function getMaxPhpVersionFromEntryFile() |
| 99 | + { |
| 100 | + $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer')); |
| 101 | + $sequence = $tokens->findSequence([ |
| 102 | + [T_STRING, 'PHP_VERSION_ID'], |
| 103 | + [T_IS_GREATER_OR_EQUAL], |
| 104 | + [T_LNUMBER], |
| 105 | + ]); |
| 106 | + |
| 107 | + if (null === $sequence) { |
| 108 | + throw new \LogicException("Can't find version - perhaps entry file was modified?"); |
| 109 | + } |
| 110 | + |
| 111 | + $phpVerId = end($sequence)->getContent(); |
| 112 | + |
| 113 | + return $this->convertPhpVerIdToNiceVer((string) ($phpVerId - 100)); |
| 114 | + } |
| 115 | + |
| 116 | + private function getMinPhpVersionFromEntryFile() |
| 117 | + { |
| 118 | + $tokens = Tokens::fromCode(file_get_contents(__DIR__.'/../../php-cs-fixer')); |
| 119 | + $sequence = $tokens->findSequence([ |
| 120 | + [T_STRING, 'PHP_VERSION_ID'], |
| 121 | + '<', |
| 122 | + [T_LNUMBER], |
| 123 | + ]); |
| 124 | + |
| 125 | + if (null === $sequence) { |
| 126 | + throw new \LogicException("Can't find version - perhaps entry file was modified?"); |
| 127 | + } |
| 128 | + |
| 129 | + $phpVerId = end($sequence)->getContent(); |
| 130 | + |
| 131 | + return $this->convertPhpVerIdToNiceVer($phpVerId); |
| 132 | + } |
| 133 | + |
| 134 | + private function getTravisJobs() |
| 135 | + { |
| 136 | + $yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.travis.yml')); |
| 137 | + |
| 138 | + return $yaml['jobs']['include']; |
| 139 | + } |
| 140 | +} |
0 commit comments