Skip to content

Commit 0fb498f

Browse files
committed
When searching for packages, ensure they are filtered to match the extension name
1 parent d612255 commit 0fb498f

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

src/Command/CommandHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ static function (array $match) use ($output, $pieComposer): array {
390390

391391
return $match;
392392
},
393-
$findMatchingPackages->for($pieComposer, $requestedPackageName),
393+
$findMatchingPackages->for($pieComposer, $requestedPackageName, $exception->requestedPackageAndVersion->version ?? '*'),
394394
);
395395

396396
if (count($matches)) {

src/Command/InstallExtensionsForProjectCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ function (Link $link) use ($pieComposer, $phpEnabledExtensions, $installedPiePac
201201
));
202202

203203
try {
204-
$matches = $this->findMatchingPackages->for($pieComposer, $extension->name());
204+
$matches = $this->findMatchingPackages->for($pieComposer, $extension->name(), $linkRequiresConstraint);
205205
} catch (OutOfRangeException) {
206206
$anyErrorsHappened = true;
207207

src/Installing/InstallForPhpProject/FindMatchingPackages.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
namespace Php\Pie\Installing\InstallForPhpProject;
66

77
use Composer\Composer;
8+
use Composer\Package\CompletePackage;
89
use Composer\Repository\RepositoryInterface;
910
use OutOfRangeException;
11+
use Php\Pie\DependencyResolver\Package;
12+
use Php\Pie\ExtensionName;
1013

14+
use function array_filter;
1115
use function array_key_exists;
1216
use function array_merge;
1317
use function count;
@@ -21,14 +25,31 @@
2125
class FindMatchingPackages
2226
{
2327
/** @return MatchingPackages */
24-
public function for(Composer $pieComposer, string $searchTerm): array
28+
public function for(Composer $pieComposer, string $searchTerm, string $stringConstraint): array
2529
{
2630
$matches = [];
2731
foreach ($pieComposer->getRepositoryManager()->getRepositories() as $repo) {
2832
$matches = array_merge($matches, $repo->search($searchTerm, RepositoryInterface::SEARCH_FULLTEXT, 'php-ext'));
2933
$matches = array_merge($matches, $repo->search($searchTerm, RepositoryInterface::SEARCH_FULLTEXT, 'php-ext-zend'));
3034
}
3135

36+
if (ExtensionName::isValidExtensionName($searchTerm)) {
37+
$extensionName = ExtensionName::normaliseFromString($searchTerm);
38+
39+
$matches = array_filter(
40+
$matches,
41+
static function (array $match) use ($pieComposer, $extensionName, $stringConstraint): bool {
42+
$package = $pieComposer->getRepositoryManager()->findPackage($match['name'], $stringConstraint);
43+
44+
if (! $package instanceof CompletePackage) {
45+
return false;
46+
}
47+
48+
return Package::fromComposerCompletePackage($package)->extensionName()->name() === $extensionName->name();
49+
},
50+
);
51+
}
52+
3253
if (! count($matches)) {
3354
throw new OutOfRangeException('No matches found for ' . $searchTerm);
3455
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\PieUnitTest\Installing\InstallForPhpProject;
6+
7+
use Composer\Composer;
8+
use Composer\Config;
9+
use Composer\IO\IOInterface;
10+
use Composer\Package\CompletePackage;
11+
use Composer\Repository\ArrayRepository;
12+
use Composer\Repository\RepositoryManager;
13+
use Composer\Util\HttpDownloader;
14+
use Php\Pie\ExtensionType;
15+
use Php\Pie\Installing\InstallForPhpProject\FindMatchingPackages;
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\TestCase;
18+
19+
#[CoversClass(FindMatchingPackages::class)]
20+
final class FindMatchingPackagesTest extends TestCase
21+
{
22+
public function testSearchResultsAreFilteredByExtensionName(): void
23+
{
24+
$repository = new ArrayRepository([
25+
(static function (): CompletePackage {
26+
$package = new CompletePackage('another/bar', '1.5.0.0', '1.5.0');
27+
$package->setDescription('These are not the extensions you are looking for');
28+
$package->setType(ExtensionType::PhpModule->value);
29+
$package->setPhpExt(['extension-name' => 'something_else']);
30+
31+
return $package;
32+
})(),
33+
(static function (): CompletePackage {
34+
$package = new CompletePackage('foo/bar', '1.2.3.0', '1.2.3');
35+
$package->setDescription('The best extension there is');
36+
$package->setType(ExtensionType::PhpModule->value);
37+
38+
return $package;
39+
})(),
40+
(static function (): CompletePackage {
41+
$package = new CompletePackage('foo/bar', '2.0.0.0', '2.0.0');
42+
$package->setDescription('The best extension there is');
43+
$package->setType(ExtensionType::PhpModule->value);
44+
45+
return $package;
46+
})(),
47+
]);
48+
49+
$repoManager = new RepositoryManager(
50+
$this->createMock(IOInterface::class),
51+
$this->createMock(Config::class),
52+
$this->createMock(HttpDownloader::class),
53+
null,
54+
null,
55+
);
56+
$repoManager->addRepository($repository);
57+
58+
$composer = $this->createMock(Composer::class);
59+
$composer->method('getRepositoryManager')->willReturn($repoManager);
60+
61+
self::assertSame(
62+
[
63+
[
64+
'name' => 'foo/bar',
65+
'description' => 'The best extension there is',
66+
],
67+
],
68+
(new FindMatchingPackages())->for($composer, 'bar', '*'),
69+
);
70+
}
71+
}

0 commit comments

Comments
 (0)