|
3 | 3 |
|
4 | 4 | namespace Leviy\ReleaseTool\Tests\System;
|
5 | 5 |
|
6 |
| -use Leviy\ReleaseTool\Console\Application; |
7 | 6 | use Leviy\ReleaseTool\Vcs\Git;
|
8 | 7 | use PHPUnit\Framework\TestCase;
|
9 |
| -use Symfony\Component\Console\Tester\ApplicationTester; |
10 |
| -use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 8 | +use Symfony\Component\Process\InputStream; |
| 9 | +use Symfony\Component\Process\Process; |
11 | 10 | use function exec;
|
| 11 | +use const PHP_EOL; |
12 | 12 |
|
13 | 13 | class ApplicationTest extends TestCase
|
14 | 14 | {
|
15 |
| - /** |
16 |
| - * @var ApplicationTester |
17 |
| - */ |
18 |
| - private $applicationTester; |
19 |
| - |
20 | 15 | protected function setUp(): void
|
21 | 16 | {
|
22 | 17 | Git::execute('init');
|
23 | 18 | Git:: execute( 'remote add origin [email protected]:org/repo.git');
|
| 19 | + } |
24 | 20 |
|
25 |
| - $container = new ContainerBuilder(); |
26 |
| - $application = new Application($container); |
| 21 | + protected function tearDown(): void |
| 22 | + { |
| 23 | + exec('rm -rf $GIT_DIR'); |
| 24 | + } |
27 | 25 |
|
28 |
| - $application->setAutoExit(false); |
| 26 | + public function testBootsWithoutErrors(): void |
| 27 | + { |
| 28 | + $process = new Process(['build/release-tool.phar']); |
| 29 | + $process->run(); |
29 | 30 |
|
30 |
| - $this->applicationTester = new ApplicationTester($application); |
| 31 | + $this->assertTrue($process->isSuccessful(), 'The command returned a non-zero exit code.'); |
| 32 | + $this->assertContains('Leviy Release Tool', $process->getOutput()); |
31 | 33 | }
|
32 | 34 |
|
33 |
| - protected function tearDown(): void |
| 35 | + public function testAsksForConfirmationBeforeReleasingAVersion(): void |
34 | 36 | {
|
35 |
| - exec('rm -rf $GIT_DIR'); |
| 37 | + if (!Process::isPtySupported()) { |
| 38 | + $this->markTestSkipped('PTY is not supported on this operating system.'); |
| 39 | + } |
| 40 | + |
| 41 | + $this->commitFile('README.md', 'Initial commit'); |
| 42 | + |
| 43 | + $input = new InputStream(); |
| 44 | + |
| 45 | + $process = new Process(['build/release-tool.phar', 'release', '--no-ansi', '1.0.0']); |
| 46 | + $process->setInput($input); |
| 47 | + $process->setPty(true); |
| 48 | + $process->start(); |
| 49 | + |
| 50 | + // EOL simulates [Enter] |
| 51 | + // Do you want to continue? (yes/no) |
| 52 | + $input->write('no' . PHP_EOL); |
| 53 | + $input->close(); |
| 54 | + |
| 55 | + $process->wait(); |
| 56 | + |
| 57 | + $this->assertContains('This will release version 1.0.0', $process->getOutput()); |
| 58 | + $this->assertContains('Do you want to continue?', $process->getOutput()); |
| 59 | + $this->assertEmpty($this->getTags()); |
| 60 | + } |
| 61 | + |
| 62 | + public function testReleasesWithGivenVersionNumber(): void |
| 63 | + { |
| 64 | + if (!Process::isPtySupported()) { |
| 65 | + $this->markTestSkipped('PTY is not supported on this operating system.'); |
| 66 | + } |
| 67 | + |
| 68 | + $this->commitFile('README.md', 'Initial commit'); |
| 69 | + |
| 70 | + $input = new InputStream(); |
| 71 | + |
| 72 | + $process = new Process(['build/release-tool.phar', 'release', '1.0.0']); |
| 73 | + $process->setInput($input); |
| 74 | + $process->setPty(true); |
| 75 | + $process->start(); |
| 76 | + |
| 77 | + // EOL simulates [Enter] |
| 78 | + // Do you want to continue? (yes/no) |
| 79 | + $input->write('yes' . PHP_EOL); |
| 80 | + |
| 81 | + // A VCS tag has been created for version 1.0.0. Do you want to push it to the remote repository and perform |
| 82 | + // additional release steps? (yes/no) |
| 83 | + $input->write('no' . PHP_EOL); |
| 84 | + $input->close(); |
| 85 | + |
| 86 | + $process->wait(); |
| 87 | + |
| 88 | + $this->assertTrue($process->isSuccessful(), 'The command returned a non-zero exit code.'); |
| 89 | + $this->assertSame(['v1.0.0'], $this->getTags()); |
36 | 90 | }
|
37 | 91 |
|
38 |
| - public function testThatTheApplicationIsBooted(): void |
| 92 | + public function testShowsTheChangelogBeforeAskingInteractiveQuestions(): void |
39 | 93 | {
|
40 |
| - $this->applicationTester->run([]); |
| 94 | + if (!Process::isPtySupported()) { |
| 95 | + $this->markTestSkipped('PTY is not supported on this operating system.'); |
| 96 | + } |
41 | 97 |
|
42 |
| - $this->assertOutputContains('Leviy Release Tool', $this->applicationTester); |
| 98 | + $this->commitFile('README.md', 'Initial commit'); |
| 99 | + $this->createTag('v1.0.0'); |
| 100 | + $this->commitFile('phpunit.xml', 'Merge pull request #3 from branchname' . PHP_EOL . PHP_EOL . 'My PR title'); |
| 101 | + |
| 102 | + $input = new InputStream(); |
| 103 | + |
| 104 | + $process = new Process(['build/release-tool.phar', 'release']); |
| 105 | + $process->setInput($input); |
| 106 | + $process->setPty(true); |
| 107 | + $process->start(); |
| 108 | + |
| 109 | + // EOL simulates [Enter] |
| 110 | + // Does this release contain backward incompatible changes? (yes/no) |
| 111 | + $input->write('no' . PHP_EOL); |
| 112 | + |
| 113 | + // Does this release contain new features? (yes/no) |
| 114 | + $input->write('yes' . PHP_EOL); |
| 115 | + |
| 116 | + // Do you want to continue? (yes/no) |
| 117 | + $input->write('no' . PHP_EOL); |
| 118 | + $input->close(); |
| 119 | + |
| 120 | + $process->wait(); |
| 121 | + |
| 122 | + $this->assertContains('My PR title (pull request #3)', $process->getOutput()); |
43 | 123 | }
|
44 | 124 |
|
45 |
| - public function testThatTheApplicationReturnsTheCurrentVersion(): void |
| 125 | + public function testDeterminesTheVersionNumberBasedOnInteractiveQuestions(): void |
46 | 126 | {
|
47 |
| - Git::execute('add README.md'); |
48 |
| - Git::execute('commit --no-gpg-sign -m "Test commit"'); |
49 |
| - Git::execute('tag --annotate --message="Test tag" v1.2.5'); |
| 127 | + if (!Process::isPtySupported()) { |
| 128 | + $this->markTestSkipped('PTY is not supported on this operating system.'); |
| 129 | + } |
| 130 | + |
| 131 | + $this->commitFile('README.md', 'Initial commit'); |
| 132 | + $this->createTag('v1.0.0'); |
| 133 | + $this->commitFile('phpunit.xml', 'Merge pull request #3 from branchname' . PHP_EOL . PHP_EOL . 'Foo'); |
| 134 | + |
| 135 | + $input = new InputStream(); |
| 136 | + |
| 137 | + $process = new Process(['build/release-tool.phar', 'release']); |
| 138 | + $process->setInput($input); |
| 139 | + $process->setPty(true); |
| 140 | + $process->start(); |
| 141 | + |
| 142 | + // EOL simulates [Enter] |
| 143 | + // Does this release contain backward incompatible changes? (yes/no) |
| 144 | + $input->write('no' . PHP_EOL); |
| 145 | + |
| 146 | + // Does this release contain new features? (yes/no) |
| 147 | + $input->write('yes' . PHP_EOL); |
| 148 | + |
| 149 | + // Do you want to continue? (yes/no) |
| 150 | + $input->write('yes' . PHP_EOL); |
| 151 | + |
| 152 | + // A VCS tag has been created for version 1.0.0. Do you want to push it to the remote repository and perform |
| 153 | + // additional release steps? (yes/no) |
| 154 | + $input->write('no' . PHP_EOL); |
| 155 | + $input->close(); |
50 | 156 |
|
51 |
| - $this->applicationTester->run(['current']); |
| 157 | + $process->wait(); |
52 | 158 |
|
53 |
| - $this->assertOutputContains('Current version:', $this->applicationTester); |
| 159 | + $this->assertTrue($process->isSuccessful(), 'The command returned a non-zero exit code.'); |
| 160 | + $this->assertContains('v1.1.0', $this->getTags()); |
54 | 161 | }
|
55 | 162 |
|
56 |
| - private function assertOutputContains(string $text, ApplicationTester $applicationTester): void |
| 163 | + public function testReturnsTheCurrentVersion(): void |
| 164 | + { |
| 165 | + $this->commitFile('README.md', 'Initial commit'); |
| 166 | + $this->createTag('v1.2.5'); |
| 167 | + |
| 168 | + $process = new Process(['build/release-tool.phar', 'current']); |
| 169 | + $process->run(); |
| 170 | + |
| 171 | + $this->assertTrue($process->isSuccessful(), 'The command returned a non-zero exit code.'); |
| 172 | + $this->assertContains('Current version: 1.2.5', $process->getOutput()); |
| 173 | + } |
| 174 | + |
| 175 | + private function commitFile(string $filename, string $commitMessage): void |
| 176 | + { |
| 177 | + Git::execute('add ' . $filename); |
| 178 | + Git::execute('commit --no-gpg-sign -m "' . $commitMessage . '"'); |
| 179 | + } |
| 180 | + |
| 181 | + private function createTag(string $tag): void |
| 182 | + { |
| 183 | + Git::execute('tag --annotate --message="Test tag" ' . $tag); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * @return string[] |
| 188 | + */ |
| 189 | + private function getTags(): array |
57 | 190 | {
|
58 |
| - $this->assertContains($text, $applicationTester->getDisplay()); |
| 191 | + return Git::execute('tag'); |
59 | 192 | }
|
60 | 193 | }
|
0 commit comments