diff --git a/src/Configurator/AddLinesConfigurator.php b/src/Configurator/AddLinesConfigurator.php index 8a4bc6d6..40cc751a 100644 --- a/src/Configurator/AddLinesConfigurator.php +++ b/src/Configurator/AddLinesConfigurator.php @@ -237,8 +237,12 @@ private function isPackageInstalled($packages): bool $installedRepo = $this->composer->getRepositoryManager()->getLocalRepository(); - foreach ($packages as $packageName) { - if (null === $installedRepo->findPackage($packageName, '*')) { + foreach ($packages as $package) { + $package = explode(':', $package, 2); + $packageName = $package[0]; + $constraint = $package[1] ?? '*'; + + if (null === $installedRepo->findPackage($packageName, $constraint)) { return false; } } diff --git a/tests/Configurator/AddLinesConfiguratorTest.php b/tests/Configurator/AddLinesConfiguratorTest.php index 43306947..2ce1cfb9 100644 --- a/tests/Configurator/AddLinesConfiguratorTest.php +++ b/tests/Configurator/AddLinesConfiguratorTest.php @@ -321,6 +321,80 @@ public function testLineProcessedIfRequiredPackageIsPresent() $actualContents); } + public function testLineSkippedIfRequiredPackageVersionIsWrong() + { + $this->saveFile('phpunit.dist.xml', << + + + + +EOF + ); + + $composer = $this->createComposerMockWithPackagesInstalled([ + 'phpunit/phpunit:9', + ]); + + $this->runConfigure([ + [ + 'file' => 'phpunit.dist.xml', + 'position' => 'after_target', + 'target' => '', + 'content' => ' ', + 'requires' => 'phpunit/phpunit:12', + ], + ], $composer); + $actualContents = $this->readFile('phpunit.dist.xml'); + $this->assertSame(<< + + + + +EOF + , + $actualContents); + } + + public function testLineProcessedIfRequiredPackageVersionIsRight() + { + $this->saveFile('phpunit.dist.xml', << + + + + +EOF + ); + + $composer = $this->createComposerMockWithPackagesInstalled([ + 'phpunit/phpunit:12', + ]); + + $this->runConfigure([ + [ + 'file' => 'phpunit.dist.xml', + 'position' => 'after_target', + 'target' => '', + 'content' => ' ', + 'requires' => 'phpunit/phpunit:12', + ], + ], $composer); + + $actualContents = $this->readFile('phpunit.dist.xml'); + $this->assertSame(<< + + + + + +EOF + , + $actualContents); + } + /** * @dataProvider getUnconfigureTests */ @@ -611,11 +685,16 @@ private function readFile(string $filename): string private function createComposerMockWithPackagesInstalled(array $packages) { + $packages = array_map(fn ($package) => explode(':', $package), $packages); + + $packageNames = array_column($packages, 0); + $constraints = array_column($packages, 1); + $repository = $this->getMockBuilder(InstalledRepositoryInterface::class)->getMock(); $repository->expects($this->any()) ->method('findPackage') - ->willReturnCallback(function ($name) use ($packages) { - if (\in_array($name, $packages)) { + ->willReturnCallback(function ($name, $constraint) use ($packageNames, $constraints) { + if (\in_array($name, $packageNames) && ('*' === $constraint || \in_array($constraint, $constraints))) { return new Package($name, '1.0.0', '1.0.0'); }