From 127df8060c1071fc2b0175197fdd8dc0be80975d Mon Sep 17 00:00:00 2001 From: Hugo Thunnissen Date: Sat, 18 Jun 2022 19:46:04 +0200 Subject: [PATCH] Test RequireCommand --- .../Test/Command/RequireCommandTest.php | 199 ++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 tests/Composer/Test/Command/RequireCommandTest.php diff --git a/tests/Composer/Test/Command/RequireCommandTest.php b/tests/Composer/Test/Command/RequireCommandTest.php new file mode 100644 index 000000000..63662721d --- /dev/null +++ b/tests/Composer/Test/Command/RequireCommandTest.php @@ -0,0 +1,199 @@ + + * 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\Command\InitCommand; +use Composer\Util\Filesystem; +use Symfony\Component\Console\Tester\ApplicationTester; +use Composer\Command\RequireCommand; +use Symfony\Component\Console\Input\ArrayInput; +use Composer\Console\Application; +use Composer\Test\TestCase; + +class RequireCommandTest extends TestCase +{ + /** + * @var string + */ + private $working_directory; + private $local_repository; + + public function setUp(): void + { + $this->local_repository = $this->getUniqueTmpDirectory(); + + $repo_manifest = [ + 'name' => 'testvendor/testpackage', + 'version' => '1.0.0', + 'type' => 'library', + 'autoload' => [ + 'psr-4' => [ + 'TestVendor\\Testpackage\\' => 'src/', + ] + ] + ]; + + file_put_contents( + $this->local_repository.'/composer.json', + json_encode( + $repo_manifest, + JSON_UNESCAPED_SLASHES, + ) + ); + + $this->working_directory = $this->getUniqueTmpDirectory(); + + $manifest = [ + 'name' => 'test/something', + 'repositories' => [ + [ + 'type' => 'path', + 'url' => $this->local_repository, + ], + [ + 'packagist.org' => false + ] + ] + ]; + + file_put_contents( + $this->working_directory.'/composer.json', + json_encode($manifest, JSON_UNESCAPED_SLASHES) + ); + + } + + protected function tearDown(): void + { + parent::tearDown(); + + $fs = new Filesystem(); + $fs->removeDirectory($this->working_directory); + $fs->removeDirectory($this->local_repository); + } + + public function testInstallPackage(): void + { + // Arrange + $application = new Application(); + $application->setAutoExit(false); + $application->add(new RequireCommand()); + + // Act + $tester = new ApplicationTester($application); + $tester->run([ + 'command' => 'require', + 'packages' => [ 'testvendor/testpackage' ], + '-d' => $this->working_directory, + ]); + + // Assert + $output = $tester->getDisplay(); + $tester->assertCommandIsSuccessful(); + $this->assertStringContainsString('Installing testvendor/testpackage', $output); + $this->assertTrue(file_exists($this->working_directory . '/vendor/testvendor/testpackage')); + } + + public function testInstallPackageSaveToComposerJson(): void + { + // Arrange + $application = new Application(); + $application->setAutoExit(false); + $application->add(new RequireCommand()); + + // Act + $tester = new ApplicationTester($application); + $tester->run([ + 'command' => 'require', + 'packages' => [ 'testvendor/testpackage 1.0.0' ], + '-d' => $this->working_directory, + ]); + + // Assert + $manifest = json_decode( + file_get_contents($this->working_directory . '/composer.json'), + true + ); + $this->assertArrayHasKey('testvendor/testpackage', $manifest['require']); + $this->assertEquals('1.0.0', $manifest['require']['testvendor/testpackage']); + } + + + public function testInstallPackagesSaveToComposerLock(): void + { + // Arrange + $application = new Application(); + $application->setAutoExit(false); + $application->add(new RequireCommand()); + + // Act + $tester = new ApplicationTester($application); + $tester->run([ + 'command' => 'require', + 'packages' => [ 'testvendor/testpackage 1.0.0' ], + '-d' => $this->working_directory, + ]); + + // Assert + $lockfile = json_decode( + file_get_contents($this->working_directory . '/composer.lock'), + true + ); + $packages = $lockfile['packages']; + $installed_package = null; + foreach($packages as $package) { + if ($package['name'] === 'testvendor/testpackage') { + $installed_package = $package; + } + } + + $this->assertNotNull($installed_package); + $this->assertEquals('1.0.0', $installed_package['version']); + } + + public function testInstallMisspelledPackage(): void + { + // Arrange + $application = new Application(); + $application->setAutoExit(false); + $application->add(new RequireCommand()); + + // Act + $tester = new ApplicationTester($application); + $tester->run([ + 'command' => 'require', + 'packages' => [ 'testvendor/testpcakage 1.0.0' ], + '-d' => $this->working_directory, + ]); + + // Assert + $this->assertNotEquals(0, $tester->getStatusCode()); + $output = $tester->getDisplay(); + + $this->assertStringContainsString( + 'Your requirements could not be resolved to an installable set of packages.', + $output + ); + + $this->assertStringContainsString( + 'requires testvendor/testpcakage, it could not be found in any version', + $output + ); + + $this->assertStringContainsString( + '- A typo in the package name', + $output + ); + } + +}