From 1b7a71f7e78278739dac6f51568073ece85fd5f0 Mon Sep 17 00:00:00 2001 From: Yuto Takakura <62083026+yutotakakura@users.noreply.github.com> Date: Thu, 22 Feb 2024 04:59:50 +0900 Subject: [PATCH] Add tests for SelfUpdateCommand (#11816) Co-authored-by: Jordi Boggiano --- .gitignore | 1 + tests/Composer/Test/AllFunctionalTest.php | 1 + .../Test/Command/SelfUpdateCommandTest.php | 96 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 tests/Composer/Test/Command/SelfUpdateCommandTest.php diff --git a/.gitignore b/.gitignore index fce10c34f..88e30178d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ /composer.phar /vendor /nbproject +/tests/composer-test.phar .phpunit.result.cache phpunit.xml .vagrant diff --git a/tests/Composer/Test/AllFunctionalTest.php b/tests/Composer/Test/AllFunctionalTest.php index c5fa6fa93..7d6834431 100644 --- a/tests/Composer/Test/AllFunctionalTest.php +++ b/tests/Composer/Test/AllFunctionalTest.php @@ -94,6 +94,7 @@ class AllFunctionalTest extends TestCase } $this->assertFileExists(self::$pharPath); + copy(self::$pharPath, __DIR__.'/../../composer-test.phar'); } /** diff --git a/tests/Composer/Test/Command/SelfUpdateCommandTest.php b/tests/Composer/Test/Command/SelfUpdateCommandTest.php new file mode 100644 index 000000000..d0b855563 --- /dev/null +++ b/tests/Composer/Test/Command/SelfUpdateCommandTest.php @@ -0,0 +1,96 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Command; + +use Composer\Test\TestCase; + +/** + * @group slow + * @depends Composer\Test\AllFunctionalTest::testBuildPhar + */ +class SelfUpdateCommandTest extends TestCase +{ + /** + * @var string + */ + private $prevArgv; + + public function setUp(): void + { + parent::setUp(); + + $this->prevArgv = $_SERVER['argv'][0]; + $dir = $this->initTempComposer(); + copy(__DIR__.'/../../../composer-test.phar', $dir.'/composer.phar'); + $_SERVER['argv'][0] = $dir.'/composer.phar'; + } + + public function tearDown(): void + { + parent::tearDown(); + + $_SERVER['argv'][0] = $this->prevArgv; + } + + public function testSuccessfulUpdate(): void + { + $appTester = $this->getApplicationTester(); + $appTester->run(['command' => 'self-update']); + + $appTester->assertCommandIsSuccessful(); + $this->assertStringContainsString('Upgrading to version', $appTester->getDisplay()); + } + + public function testUpdateToSpecificVersion(): void + { + $appTester = $this->getApplicationTester(); + $appTester->run(['command' => 'self-update', 'version' => '2.4.0']); + + $appTester->assertCommandIsSuccessful(); + $this->assertStringContainsString('Upgrading to version 2.4.0', $appTester->getDisplay()); + } + + public function testUpdateWithInvalidOptionThrowsException(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The "invalid-option" argument does not exist.'); + + $appTester = $this->getApplicationTester(); + $appTester->run(['command' => 'self-update', 'invalid-option' => true]); + } + + /** + * @dataProvider channelOptions + */ + public function testUpdateToDifferentChannel(string $option, string $expectedOutput): void + { + $appTester = $this->getApplicationTester(); + $appTester->run(['command' => 'self-update', $option => true]); + $appTester->assertCommandIsSuccessful(); + + $this->assertStringContainsString('Upgrading to version', $appTester->getDisplay()); + $this->assertStringContainsString($expectedOutput, $appTester->getDisplay()); + } + + /** + * @return array> + */ + public function channelOptions(): array + { + return [ + ['--stable', 'stable channel'], + ['--preview', 'preview channel'], + ['--snapshot', 'snapshot channel'], + ]; + } +}