Skip to content

Commit 0da060a

Browse files
committed
Add test for GitHub API error
1 parent 968aedc commit 0da060a

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

src/Integrations/SupportedPhpExtensions.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ public function get(string $phpVersion = null): array
1414
return $this->cache;
1515
}
1616

17-
try {
18-
$contents = $this->fetch();
19-
20-
return $this->cache = collect($contents)
21-
->filter(fn (string $extension): bool => is_null($phpVersion) || str($extension)->contains($phpVersion))
22-
->map(fn (string $extension): string => str($extension)->trim()->before(' '))
23-
->filter()
24-
->values()
25-
->toArray();
26-
} catch (\ErrorException) {
17+
$contents = $this->fetch();
18+
19+
if ($contents === false) {
2720
return [];
2821
}
22+
23+
return $this->cache = collect($contents)
24+
->filter(fn (string $extension): bool => is_null($phpVersion) || str($extension)->contains($phpVersion))
25+
->map(fn (string $extension): string => str($extension)->trim()->before(' '))
26+
->filter()
27+
->values()
28+
->toArray();
2929
}
3030

3131
protected function fetch(): array|false

tests/Unit/Integrations/SupportedPhpExtensionsTest.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,26 @@
1313
*/
1414
class SupportedPhpExtensionsTest extends TestCase
1515
{
16-
protected function setUp(): void
16+
public function providePhpVersions(): array
1717
{
18-
parent::setUp();
18+
return [
19+
[['apcu', 'pdo_mysql'], '8.2'],
20+
[['apcu', 'pdo_pgsql', 'redis'], '8.1'],
21+
[['pdo_pgsql', 'memcached'], '8.0'],
22+
];
23+
}
1924

20-
$this->partialMock(SupportedPhpExtensions::class, function (MockInterface $mock) {
25+
/** @dataProvider providePhpVersions */
26+
public function testItFiltersVersions($expected, $version): void
27+
{
28+
$this->mock(SupportedPhpExtensions::class, function (MockInterface $mock) use ($version) {
2129
$mock->shouldAllowMockingProtectedMethods();
2230
$mock->shouldReceive('get')
31+
->with($version)
2332
->once()
2433
->passthru();
2534
$mock->shouldReceive('fetch')
35+
->withNoArgs()
2636
->once()
2737
->andReturn([
2838
'apcu 8.1 8.2',
@@ -32,22 +42,22 @@ protected function setUp(): void
3242
'redis 8.1',
3343
]);
3444
});
35-
}
3645

37-
public function providePhpVersions(): array
38-
{
39-
return [
40-
[['apcu', 'pdo_mysql'], '8.2'],
41-
[['apcu', 'pdo_pgsql', 'redis'], '8.1'],
42-
[['pdo_pgsql', 'memcached'], '8.0'],
43-
];
46+
$supported = app(SupportedPhpExtensions::class)->get($version);
47+
48+
self::assertEquals($expected, $supported);
4449
}
4550

46-
/** @dataProvider providePhpVersions */
47-
public function testItFiltersVersions($expected, $version): void
51+
public function testItReturnsEmptyArrayOnError(): void
4852
{
49-
$supported = app(SupportedPhpExtensions::class)->get($version);
53+
$this->mock(SupportedPhpExtensions::class, function(MockInterface $mock) {
54+
$mock->shouldAllowMockingProtectedMethods();
55+
$mock->shouldReceive('get')->once()->passthru();
56+
$mock->shouldReceive('fetch')->once()->withNoArgs()->andReturn(false);
57+
});
5058

51-
self::assertEquals($expected, $supported);
59+
$supported = app(SupportedPhpExtensions::class)->get('8.2');
60+
61+
self::assertEquals([], $supported);
5262
}
5363
}

0 commit comments

Comments
 (0)