Skip to content

Commit 5b718eb

Browse files
committed
style(finder): simplify discoverByType closure, achieve 100% coverage
1 parent 6394ab6 commit 5b718eb

2 files changed

Lines changed: 84 additions & 91 deletions

File tree

src/Finder.php

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,6 @@ public function discover(string|array $forPackage): array
5555
->value();
5656
}
5757

58-
/**
59-
* Discover packages of a specific Composer type wanting to interact with your service
60-
*
61-
* @param string|array<int, string> $forPackage
62-
* @return array<string, array<string, mixed>>
63-
*/
64-
public function discoverByType(string $type, string|array $forPackage): array
65-
{
66-
return array_filter(
67-
$this->discover($forPackage),
68-
function (mixed $meta, string $packageName) use ($type): bool {
69-
foreach ($this->installedPackages() as $package) {
70-
if ($package['name'] === $packageName) {
71-
return ($package['type'] ?? '') === $type;
72-
}
73-
}
74-
75-
return false;
76-
},
77-
ARRAY_FILTER_USE_BOTH,
78-
);
79-
}
80-
8158
/**
8259
* Discover all packages that have any extra metadata
8360
*
@@ -97,16 +74,19 @@ public function discoverAll(): array
9774
}
9875

9976
/**
100-
* Get names of all installed packages
77+
* Discover packages of a specific Composer type wanting to interact with your service
10178
*
102-
* @return array<int, string>
79+
* @param string|array<int, string> $forPackage
80+
* @return array<string, array<string, mixed>>
10381
*/
104-
public function installedPackageNames(): array
82+
public function discoverByType(string $type, string|array $forPackage): array
10583
{
106-
return array_values(array_map(
107-
fn (array $package): string => $package['name'],
108-
$this->installedPackages(),
109-
));
84+
return array_filter(
85+
$this->discover($forPackage),
86+
fn (mixed $meta, string $packageName): bool => $this->has($packageName)
87+
&& ($this->findPackage($packageName)['type'] ?? '') === $type,
88+
ARRAY_FILTER_USE_BOTH,
89+
);
11090
}
11191

11292
/**
@@ -123,6 +103,19 @@ public function has(string $packageName): bool
123103
return false;
124104
}
125105

106+
/**
107+
* Get names of all installed packages
108+
*
109+
* @return array<int, string>
110+
*/
111+
public function installedPackageNames(): array
112+
{
113+
return array_values(array_map(
114+
fn (array $package): string => $package['name'],
115+
$this->installedPackages(),
116+
));
117+
}
118+
126119
/**
127120
* Get collection of installed packages
128121
*

tests/FinderTest.php

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -42,43 +42,13 @@ public static function discoverDataProvider(): Iterator
4242
];
4343
}
4444

45-
public function testCanDiscoverPackagesByType(): void
46-
{
47-
$basePath = __DIR__.'/Resources/test_one';
48-
$finder = new Finder($basePath);
49-
50-
$plugins = $finder->discoverByType('composer-plugin', 'myerscode');
51-
$this->assertArrayHasKey('myerscode/test-package', $plugins);
52-
53-
$libraries = $finder->discoverByType('library', 'myerscode');
54-
$this->assertSame([], $libraries);
55-
}
56-
57-
public function testDiscoverByTypeReturnsEmptyForUnknownType(): void
58-
{
59-
$basePath = __DIR__.'/Resources/test_one';
60-
$finder = new Finder($basePath);
61-
62-
$this->assertSame([], $finder->discoverByType('unknown-type', 'myerscode'));
63-
}
64-
65-
public function testCanDiscoverMultipleNamespacesAtOnce(): void
66-
{
67-
$basePath = __DIR__.'/Resources/test_locate';
68-
$finder = new Finder($basePath);
69-
70-
$discovered = $finder->discover(['myerscode', 'corgi']);
71-
72-
$this->assertArrayHasKey('myerscode/test-package', $discovered);
73-
$this->assertCount(1, $discovered);
74-
}
75-
76-
public function testDiscoverWithArrayReturnsEmptyForUnknownNamespaces(): void
45+
public function testCanCheckIfPackageIsInstalled(): void
7746
{
7847
$basePath = __DIR__.'/Resources/test_locate';
7948
$finder = new Finder($basePath);
8049

81-
$this->assertSame([], $finder->discover(['unknown-ns']));
50+
$this->assertTrue($finder->has('myerscode/test-package'));
51+
$this->assertFalse($finder->has('myerscode/not-a-package'));
8252
}
8353

8454
public function testCanDiscoverAllPackagesWithExtras(): void
@@ -93,33 +63,27 @@ public function testCanDiscoverAllPackagesWithExtras(): void
9363
$this->assertArrayNotHasKey('myerscode/utilities-bags', $all);
9464
}
9565

96-
public function testDiscoverAllReturnsEmptyWhenNoPackagesHaveExtras(): void
97-
{
98-
$basePath = __DIR__.'/Resources';
99-
$finder = new Finder($basePath);
100-
101-
$this->assertSame([], $finder->discoverAll());
102-
}
103-
104-
public function testCanGetInstalledPackageNames(): void
66+
public function testCanDiscoverMultipleNamespacesAtOnce(): void
10567
{
10668
$basePath = __DIR__.'/Resources/test_locate';
10769
$finder = new Finder($basePath);
10870

109-
$names = $finder->installedPackageNames();
71+
$discovered = $finder->discover(['myerscode', 'corgi']);
11072

111-
$this->assertIsArray($names);
112-
$this->assertContains('myerscode/test-package', $names);
113-
$this->assertContains('myerscode/utilities-bags', $names);
73+
$this->assertArrayHasKey('myerscode/test-package', $discovered);
74+
$this->assertCount(1, $discovered);
11475
}
11576

116-
public function testCanCheckIfPackageIsInstalled(): void
77+
public function testCanDiscoverPackagesByType(): void
11778
{
118-
$basePath = __DIR__.'/Resources/test_locate';
79+
$basePath = __DIR__.'/Resources/test_one';
11980
$finder = new Finder($basePath);
12081

121-
$this->assertTrue($finder->has('myerscode/test-package'));
122-
$this->assertFalse($finder->has('myerscode/not-a-package'));
82+
$plugins = $finder->discoverByType('composer-plugin', 'myerscode');
83+
$this->assertArrayHasKey('myerscode/test-package', $plugins);
84+
85+
$libraries = $finder->discoverByType('library', 'myerscode');
86+
$this->assertSame([], $libraries);
12387
}
12488

12589
public function testCanGePackageExtras(): void
@@ -142,6 +106,18 @@ public function testCanGePackageExtras(): void
142106
], $meta);
143107
}
144108

109+
public function testCanGetInstalledPackageNames(): void
110+
{
111+
$basePath = __DIR__.'/Resources/test_locate';
112+
$finder = new Finder($basePath);
113+
114+
$names = $finder->installedPackageNames();
115+
116+
$this->assertIsArray($names);
117+
$this->assertContains('myerscode/test-package', $names);
118+
$this->assertContains('myerscode/utilities-bags', $names);
119+
}
120+
145121
public function testCanGetMetaForPackageUsingMetaNamespace(): void
146122
{
147123
$basePath = __DIR__.'/Resources/test_locate';
@@ -180,15 +156,28 @@ public function testCanSeeInstalledPackages(): void
180156
$this->assertGreaterThan(0, count($installed));
181157
}
182158

183-
public function testInstalledPackagesAreCached(): void
159+
public function testDiscoverAllReturnsEmptyWhenNoPackagesHaveExtras(): void
184160
{
185-
$basePath = __DIR__.'/../';
161+
$basePath = __DIR__.'/Resources';
186162
$finder = new Finder($basePath);
187163

188-
$first = $finder->installedPackages();
189-
$second = $finder->installedPackages();
164+
$this->assertSame([], $finder->discoverAll());
165+
}
190166

191-
$this->assertSame($first, $second);
167+
public function testDiscoverByTypeReturnsEmptyForUnknownType(): void
168+
{
169+
$basePath = __DIR__.'/Resources/test_one';
170+
$finder = new Finder($basePath);
171+
172+
$this->assertSame([], $finder->discoverByType('unknown-type', 'myerscode'));
173+
}
174+
175+
public function testDiscoverWithArrayReturnsEmptyForUnknownNamespaces(): void
176+
{
177+
$basePath = __DIR__.'/Resources/test_locate';
178+
$finder = new Finder($basePath);
179+
180+
$this->assertSame([], $finder->discover(['unknown-ns']));
192181
}
193182

194183

@@ -213,15 +202,15 @@ public function testHandlesMissingInstallFile(): void
213202
$this->assertCount(0, $installed);
214203
}
215204

216-
public function testThrowsExceptionWhenCannotLocatePackage(): void
205+
public function testInstalledPackagesAreCached(): void
217206
{
218-
$basePath = __DIR__.'/Resources/test_locate';
207+
$basePath = __DIR__.'/../';
219208
$finder = new Finder($basePath);
220-
$packageName = 'myerscode/does-not-exists-package';
221209

222-
$this->expectException(PackageNotFoundException::class);
223-
$this->expectExceptionMessage($packageName . ' is not a known package');
224-
$finder->locate($packageName);
210+
$first = $finder->installedPackages();
211+
$second = $finder->installedPackages();
212+
213+
$this->assertSame($first, $second);
225214
}
226215

227216
public function testPackageNotFoundExceptionExtendsInvalidArgumentException(): void
@@ -237,6 +226,17 @@ public function testPackageNotFoundExceptionExtendsInvalidArgumentException(): v
237226
}
238227
}
239228

229+
public function testThrowsExceptionWhenCannotLocatePackage(): void
230+
{
231+
$basePath = __DIR__.'/Resources/test_locate';
232+
$finder = new Finder($basePath);
233+
$packageName = 'myerscode/does-not-exists-package';
234+
235+
$this->expectException(PackageNotFoundException::class);
236+
$this->expectExceptionMessage($packageName . ' is not a known package');
237+
$finder->locate($packageName);
238+
}
239+
240240
public function testThrowsExceptionWhenPackagePathCannotBeResolved(): void
241241
{
242242
$basePath = __DIR__.'/Resources/test_locate';

0 commit comments

Comments
 (0)