* 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\Package\Link; use Composer\Semver\Constraint\MatchAllConstraint; use Composer\Test\TestCase; use InvalidArgumentException; class UpdateCommandTest extends TestCase { /** * @dataProvider provideUpdates * @param array $composerJson * @param array $command */ public function testUpdate(array $composerJson, array $command, string $expected): void { $this->initTempComposer($composerJson); $appTester = $this->getApplicationTester(); $appTester->run(array_merge(['command' => 'update', '--dry-run' => true, '--no-audit' => true], $command)); self::assertStringMatchesFormat(trim($expected), trim($appTester->getDisplay(true))); } public static function provideUpdates(): \Generator { $rootDepAndTransitiveDep = [ 'repositories' => [ 'packages' => [ 'type' => 'package', 'package' => [ ['name' => 'root/req', 'version' => '1.0.0', 'require' => ['dep/pkg' => '^1']], ['name' => 'dep/pkg', 'version' => '1.0.0'], ['name' => 'dep/pkg', 'version' => '1.0.1'], ['name' => 'dep/pkg', 'version' => '1.0.2'], ], ], ], 'require' => [ 'root/req' => '1.*', ], ]; yield 'simple update' => [ $rootDepAndTransitiveDep, [], << [ $rootDepAndTransitiveDep, ['-vv' => true], << [ $rootDepAndTransitiveDep, ['--with' => ['dep/pkg:1.0.0'], '--no-install' => true], << [ $rootDepAndTransitiveDep, ['--with' => ['dep/pkg:^2']], << satisfiable by root/req[1.0.0]. - root/req 1.0.0 requires dep/pkg ^1 -> found dep/pkg[1.0.0, 1.0.1, 1.0.2] but it conflicts with your temporary update constraint (dep/pkg:^2). OUTPUT ]; yield 'update with temporary constraint failing resolution on root package' => [ $rootDepAndTransitiveDep, ['--with' => ['root/req:^2']], <<initTempComposer([ 'repositories' => [ 'packages' => [ 'type' => 'package', 'package' => [ ['name' => 'root/req', 'version' => '1.0.0'], ], ], ], 'require' => [ 'root/req' => '1.*', ], ]); $this->createComposerLock([self::getPackage('root/req', '1.0.0')]); self::expectExceptionMessage('Could not find any package with new versions available'); $appTester = $this->getApplicationTester(); $appTester->setInputs(['']); $appTester->run(['command' => 'update', '--interactive' => true]); } public function testInteractiveModeThrowsIfNoPackageEntered(): void { $this->initTempComposer([ 'repositories' => [ 'packages' => [ 'type' => 'package', 'package' => [ ['name' => 'root/req', 'version' => '1.0.0'], ['name' => 'root/req', 'version' => '1.0.1'], ], ], ], 'require' => [ 'root/req' => '1.*', ], ]); $this->createComposerLock([self::getPackage('root/req', '1.0.0')]); self::expectExceptionMessage('No package named "" is installed.'); $appTester = $this->getApplicationTester(); $appTester->setInputs(['']); $appTester->run(['command' => 'update', '--interactive' => true]); } /** * @dataProvider provideInteractiveUpdates * @param array $packageNames */ public function testInteractiveTmp(array $packageNames, string $expected): void { $this->initTempComposer([ 'repositories' => [ 'packages' => [ 'type' => 'package', 'package' => [ ['name' => 'root/req', 'version' => '1.0.0', 'require' => ['dep/pkg' => '^1']], ['name' => 'dep/pkg', 'version' => '1.0.0'], ['name' => 'dep/pkg', 'version' => '1.0.1'], ['name' => 'dep/pkg', 'version' => '1.0.2'], ['name' => 'another-dep/pkg', 'version' => '1.0.2'], ], ], ], 'require' => [ 'root/req' => '1.*', ], ]); $rootPackage = self::getPackage('root/req'); $packages = [$rootPackage]; foreach ($packageNames as $pkg => $ver) { $currentPkg = self::getPackage($pkg, $ver); array_push($packages, $currentPkg); } $rootPackage->setRequires([ 'dep/pkg' => new Link( 'root/req', 'dep/pkg', new MatchAllConstraint(), Link::TYPE_REQUIRE, '^1' ), 'another-dep/pkg' => new Link( 'root/req', 'another-dep/pkg', new MatchAllConstraint(), Link::TYPE_REQUIRE, '^1' ), ]); $this->createComposerLock($packages); $this->createInstalledJson($packages); $appTester = $this->getApplicationTester(); $appTester->setInputs(array_merge(array_keys($packageNames), ['', 'yes'])); $appTester->run([ 'command' => 'update', '--interactive' => true, '--no-audit' => true, '--dry-run' => true, ]); self::assertStringEndsWith( trim($expected), trim($appTester->getDisplay(true)) ); } public function provideInteractiveUpdates(): \Generator { yield [ ['dep/pkg' => '1.0.1'], << 1.0.2) Installing dependencies from lock file (including require-dev) Package operations: 1 install, 1 update, 0 removals - Upgrading dep/pkg (1.0.1 => 1.0.2) - Installing another-dep/pkg (1.0.2) OUTPUT ]; yield [ ['dep/pkg' => '1.0.1', 'another-dep/pkg' => '1.0.2'], << 1.0.2) Installing dependencies from lock file (including require-dev) Package operations: 0 installs, 1 update, 0 removals - Upgrading dep/pkg (1.0.1 => 1.0.2) OUTPUT ]; } }