|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\AutoReview; |
| 13 | + |
| 14 | +use InvalidArgumentException; |
| 15 | +use PHPUnit\Framework\Attributes\CoversNothing; |
| 16 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 17 | +use PHPUnit\Framework\Attributes\Group; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +#[CoversNothing] |
| 24 | +#[Group('AutoReview')] |
| 25 | +final class CreateNewChangelogTest extends TestCase |
| 26 | +{ |
| 27 | + private string $currentVersion; |
| 28 | + |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + exec('git describe --tags --abbrev=0 2>/dev/null', $output, $exitCode); |
| 34 | + |
| 35 | + if ($exitCode !== 0) { |
| 36 | + $this->markTestSkipped('Unable to determine the current version from git tags.'); |
| 37 | + } |
| 38 | + |
| 39 | + // Current tag should already have the next patch docs done, so for testing purposes, |
| 40 | + // we will treat the next patch version as the current version. |
| 41 | + $this->currentVersion = $this->incrementVersion(trim($output[0], 'v')); |
| 42 | + } |
| 43 | + |
| 44 | + #[DataProvider('provideCreateNewChangelog')] |
| 45 | + public function testCreateNewChangelog(string $mode): void |
| 46 | + { |
| 47 | + $currentVersion = $this->currentVersion; |
| 48 | + $newVersion = $this->incrementVersion($currentVersion, $mode); |
| 49 | + |
| 50 | + exec( |
| 51 | + sprintf('php ./admin/create-new-changelog.php %s %s --dry-run', $currentVersion, $newVersion), |
| 52 | + $output, |
| 53 | + $exitCode, |
| 54 | + ); |
| 55 | + |
| 56 | + $this->assertSame(0, $exitCode, "Script exited with code {$exitCode}. Output: " . implode("\n", $output)); |
| 57 | + |
| 58 | + $this->assertStringContainsString( |
| 59 | + "public const CI_VERSION = '{$newVersion}-dev';", |
| 60 | + $this->getContents('./system/CodeIgniter.php'), |
| 61 | + ); |
| 62 | + |
| 63 | + $this->assertFileExists("./user_guide_src/source/changelogs/v{$newVersion}.rst"); |
| 64 | + $this->assertStringContainsString( |
| 65 | + "Version {$newVersion}", |
| 66 | + $this->getContents("./user_guide_src/source/changelogs/v{$newVersion}.rst"), |
| 67 | + ); |
| 68 | + $this->assertStringContainsString( |
| 69 | + $newVersion, |
| 70 | + $this->getContents('./user_guide_src/source/changelogs/index.rst'), |
| 71 | + ); |
| 72 | + |
| 73 | + $versionWithoutDots = str_replace('.', '', $newVersion); |
| 74 | + $this->assertFileExists("./user_guide_src/source/installation/upgrade_{$versionWithoutDots}.rst"); |
| 75 | + $this->assertStringContainsString( |
| 76 | + "Upgrading from {$currentVersion} to {$newVersion}", |
| 77 | + $this->getContents("./user_guide_src/source/installation/upgrade_{$versionWithoutDots}.rst"), |
| 78 | + ); |
| 79 | + $this->assertStringContainsString( |
| 80 | + "upgrade_{$versionWithoutDots}", |
| 81 | + $this->getContents('./user_guide_src/source/installation/upgrading.rst'), |
| 82 | + ); |
| 83 | + |
| 84 | + // cleanup added and modified files |
| 85 | + exec('git restore .'); |
| 86 | + exec('git clean -fd'); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return iterable<string, array{0: string}> |
| 91 | + */ |
| 92 | + public static function provideCreateNewChangelog(): iterable |
| 93 | + { |
| 94 | + yield 'patch update' => ['patch']; |
| 95 | + |
| 96 | + yield 'minor update' => ['minor']; |
| 97 | + |
| 98 | + yield 'major update' => ['major']; |
| 99 | + } |
| 100 | + |
| 101 | + private function incrementVersion(string $version, string $mode = 'patch'): string |
| 102 | + { |
| 103 | + $parts = explode('.', $version); |
| 104 | + |
| 105 | + return match ($mode) { |
| 106 | + 'major' => sprintf('%d.0.0', ++$parts[0]), |
| 107 | + 'minor' => sprintf('%d.%d.0', $parts[0], ++$parts[1]), |
| 108 | + 'patch' => sprintf('%d.%d.%d', $parts[0], $parts[1], ++$parts[2]), |
| 109 | + default => throw new InvalidArgumentException('Invalid version increment mode. Use "major", "minor", or "patch".'), |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + private function getContents(string $path): string |
| 114 | + { |
| 115 | + $contents = @file_get_contents($path); |
| 116 | + |
| 117 | + if ($contents === false) { |
| 118 | + $this->fail("Failed to read file contents from {$path}."); |
| 119 | + } |
| 120 | + |
| 121 | + return $contents; |
| 122 | + } |
| 123 | +} |
0 commit comments