Skip to content

Commit 3319fcf

Browse files
committed
Add option to specify version in add lines requires
1 parent 5cc9859 commit 3319fcf

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

src/Configurator/AddLinesConfigurator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,12 @@ private function isPackageInstalled($packages): bool
237237

238238
$installedRepo = $this->composer->getRepositoryManager()->getLocalRepository();
239239

240-
foreach ($packages as $packageName) {
241-
if (null === $installedRepo->findPackage($packageName, '*')) {
240+
foreach ($packages as $package) {
241+
$package = explode(':', $package, 2);
242+
$packageName = $package[0];
243+
$constraint = $package[1] ?? '*';
244+
245+
if (null === $installedRepo->findPackage($packageName, $constraint)) {
242246
return false;
243247
}
244248
}

tests/Configurator/AddLinesConfiguratorTest.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,80 @@ public function testLineProcessedIfRequiredPackageIsPresent()
321321
$actualContents);
322322
}
323323

324+
public function testLineSkippedIfRequiredPackageVersionIsWrong()
325+
{
326+
$this->saveFile('phpunit.dist.xml', <<<EOF
327+
<?xml version="1.0" encoding="UTF-8"?>
328+
<phpunit>
329+
<extensions>
330+
</extensions>
331+
</phpunit>
332+
EOF
333+
);
334+
335+
$composer = $this->createComposerMockWithPackagesInstalled([
336+
'phpunit/phpunit:9',
337+
]);
338+
339+
$this->runConfigure([
340+
[
341+
'file' => 'phpunit.dist.xml',
342+
'position' => 'after_target',
343+
'target' => '<extensions>',
344+
'content' => ' <bootstrap class="Symfony\Component\Panther\ServerExtension" />',
345+
'requires' => 'phpunit/phpunit:12',
346+
],
347+
], $composer);
348+
$actualContents = $this->readFile('phpunit.dist.xml');
349+
$this->assertSame(<<<EOF
350+
<?xml version="1.0" encoding="UTF-8"?>
351+
<phpunit>
352+
<extensions>
353+
</extensions>
354+
</phpunit>
355+
EOF
356+
,
357+
$actualContents);
358+
}
359+
360+
public function testLineProcessedIfRequiredPackageVersionIsRight()
361+
{
362+
$this->saveFile('phpunit.dist.xml', <<<EOF
363+
<?xml version="1.0" encoding="UTF-8"?>
364+
<phpunit>
365+
<extensions>
366+
</extensions>
367+
</phpunit>
368+
EOF
369+
);
370+
371+
$composer = $this->createComposerMockWithPackagesInstalled([
372+
'phpunit/phpunit:12',
373+
]);
374+
375+
$this->runConfigure([
376+
[
377+
'file' => 'phpunit.dist.xml',
378+
'position' => 'after_target',
379+
'target' => '<extensions>',
380+
'content' => ' <bootstrap class="Symfony\Component\Panther\ServerExtension" />',
381+
'requires' => 'phpunit/phpunit:12',
382+
],
383+
], $composer);
384+
385+
$actualContents = $this->readFile('phpunit.dist.xml');
386+
$this->assertSame(<<<EOF
387+
<?xml version="1.0" encoding="UTF-8"?>
388+
<phpunit>
389+
<extensions>
390+
<bootstrap class="Symfony\Component\Panther\ServerExtension" />
391+
</extensions>
392+
</phpunit>
393+
EOF
394+
,
395+
$actualContents);
396+
}
397+
324398
/**
325399
* @dataProvider getUnconfigureTests
326400
*/
@@ -611,11 +685,16 @@ private function readFile(string $filename): string
611685

612686
private function createComposerMockWithPackagesInstalled(array $packages)
613687
{
688+
$packages = array_map(fn ($package) => explode(':', $package), $packages);
689+
690+
$packageNames = array_column($packages, 0);
691+
$constraints = array_column($packages, 1);
692+
614693
$repository = $this->getMockBuilder(InstalledRepositoryInterface::class)->getMock();
615694
$repository->expects($this->any())
616695
->method('findPackage')
617-
->willReturnCallback(function ($name) use ($packages) {
618-
if (\in_array($name, $packages)) {
696+
->willReturnCallback(function ($name, $constraint) use ($packageNames, $constraints) {
697+
if (\in_array($name, $packageNames) && ('*' === $constraint || \in_array($constraint, $constraints))) {
619698
return new Package($name, '1.0.0', '1.0.0');
620699
}
621700

0 commit comments

Comments
 (0)