Skip to content

Commit 8f51217

Browse files
committed
Add test for GitHub call
1 parent c293e99 commit 8f51217

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"require": {
2626
"php": "^8.0",
2727
"composer/semver": "^3.3",
28+
"guzzlehttp/guzzle": "^7.5",
2829
"illuminate/contracts": "^9.0 | ^10.0",
2930
"twig/twig": "^3.0"
3031
},

src/Integrations/SupportedPhpExtensions.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace BlameButton\LaravelDockerBuilder\Integrations;
44

5+
use Illuminate\Support\Facades\Http;
6+
use function Symfony\Component\String\b;
7+
58
class SupportedPhpExtensions
69
{
710
private const URL = 'https://github.com/mlocati/docker-php-extension-installer/raw/master/data/supported-extensions';
@@ -10,7 +13,7 @@ class SupportedPhpExtensions
1013

1114
public function get(string $phpVersion = null): array
1215
{
13-
if (! is_null($this->cache)) {
16+
if (!is_null($this->cache)) {
1417
return $this->cache;
1518
}
1619

@@ -21,18 +24,24 @@ public function get(string $phpVersion = null): array
2124
}
2225

2326
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(' '))
27+
->filter(fn(string $extension): bool => is_null($phpVersion) || str($extension)->contains($phpVersion))
28+
->map(fn(string $extension): string => str($extension)->trim()->before(' '))
2629
->filter()
2730
->values()
2831
->toArray();
2932
}
3033

31-
/**
32-
* @codeCoverageIgnore this fetches an actual file from GitHub
33-
*/
34-
protected function fetch(): array|false
34+
public function fetch(): array|false
3535
{
36-
return file(self::URL);
36+
$response = rescue(
37+
callback: fn() => Http::get(self::URL),
38+
rescue: false,
39+
);
40+
41+
if ($response === false || $response->failed()) {
42+
return false;
43+
}
44+
45+
return array_filter(explode("\n", $response->body()));
3746
}
3847
}

tests/Unit/Integrations/SupportedPhpExtensionsTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BlameButton\LaravelDockerBuilder\Integrations\SupportedPhpExtensions;
66
use BlameButton\LaravelDockerBuilder\Tests\TestCase;
7+
use Illuminate\Support\Facades\Http;
78
use Mockery\MockInterface;
89

910
/**
@@ -72,4 +73,36 @@ public function testItCachesResponse(): void
7273
app(SupportedPhpExtensions::class)->get();
7374
app(SupportedPhpExtensions::class)->get();
7475
}
76+
77+
public function testItReturnsFalseOnError(): void
78+
{
79+
Http::fake([
80+
'github.com/*' => Http::response("bcmath\nmemcached", 500),
81+
]);
82+
83+
$this->mock(SupportedPhpExtensions::class, function (MockInterface $mock) {
84+
$mock->shouldAllowMockingProtectedMethods();
85+
$mock->shouldReceive('fetch')->once()->passthru();
86+
});
87+
88+
$response = app(SupportedPhpExtensions::class)->fetch();
89+
90+
self::assertFalse($response);
91+
}
92+
93+
public function testItReturnsExtensions(): void
94+
{
95+
Http::fake([
96+
'github.com/*' => Http::response("bcmath\nmemcached\n"),
97+
]);
98+
99+
$this->mock(SupportedPhpExtensions::class, function (MockInterface $mock) {
100+
$mock->shouldAllowMockingProtectedMethods();
101+
$mock->shouldReceive('fetch')->once()->passthru();
102+
});
103+
104+
$response = app(SupportedPhpExtensions::class)->fetch();
105+
106+
self::assertEquals(['bcmath', 'memcached'], $response);
107+
}
75108
}

0 commit comments

Comments
 (0)