Skip to content

Commit 53149ef

Browse files
committed
Add test for SupportedPhpExtensions
1 parent 517ccaa commit 53149ef

File tree

5 files changed

+71
-14
lines changed

5 files changed

+71
-14
lines changed

src/Commands/GenerateQuestions/Choices/PhpExtensions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class PhpExtensions
88
{
99
public static function values(string $phpVersion = null): array
1010
{
11-
return app(SupportedPhpExtensions::class)->fetch($phpVersion);
11+
return app(SupportedPhpExtensions::class)->get($phpVersion);
1212
}
1313
}

src/Integrations/SupportedPhpExtensions.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ class SupportedPhpExtensions
88

99
private array|null $cache = null;
1010

11-
public function fetch(string $phpVersion = null): array
11+
public function get(string $phpVersion = null): array
1212
{
1313
if (! is_null($this->cache)) {
1414
return $this->cache;
1515
}
1616

1717
try {
18-
$contents = file_get_contents(self::URL);
18+
$contents = $this->fetch();
1919

2020
return $this->cache = str($contents)
2121
->explode("\n")
@@ -27,4 +27,9 @@ public function fetch(string $phpVersion = null): array
2727
return [];
2828
}
2929
}
30+
31+
protected function fetch(): string|false
32+
{
33+
return file_get_contents(self::URL);
34+
}
3035
}

tests/Feature/Commands/DockerGenerateCommandTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
* @covers \BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\NodeBuildTool
2828
* @covers \BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\NodeBuildToolQuestion
2929
* @covers \BlameButton\LaravelDockerBuilder\Objects\Configuration
30-
*
3130
* @covers \BlameButton\LaravelDockerBuilder\Commands\DockerGenerateCommand
3231
*/
3332
class DockerGenerateCommandTest extends TestCase
@@ -39,29 +38,29 @@ public function provideCommands(): array
3938
[
4039
"FROM php:8.2-fpm-alpine AS composer\n",
4140
"FROM node:lts-alpine AS node\n",
42-
"COPY /package.json /package-lock.json /app/",
41+
'COPY /package.json /package-lock.json /app/',
4342
"COPY /vite.config.js /app/\n",
4443
"RUN npm run build\n",
4544
"RUN install-php-extensions bcmath pdo_pgsql redis\n",
4645
"COPY --from=node /app/public/build/ /app/public/build/\n",
47-
"RUN echo \"php artisan optimize --no-ansi && php-fpm\" >> /usr/bin/entrypoint.sh",
48-
"CMD [\"/usr/bin/entrypoint.sh\"]\n"
46+
'RUN echo "php artisan optimize --no-ansi && php-fpm" >> /usr/bin/entrypoint.sh',
47+
"CMD [\"/usr/bin/entrypoint.sh\"]\n",
4948
],
5049
'docker:generate -n -p 8.2 -e bcmath,pdo_pgsql,redis -o -a -m npm -b vite',
5150
],
5251
'8.1, mysql, apcu, no optimize, no alpine, yarn, mix' => [
5352
[
5453
"FROM php:8.1-fpm AS composer\n",
5554
"FROM node:lts AS node\n",
56-
"COPY /package.json /yarn.lock /app/",
55+
'COPY /package.json /yarn.lock /app/',
5756
"COPY /webpack.mix.js /app/\n",
5857
"RUN yarn run production\n",
5958
"RUN install-php-extensions bcmath pdo_mysql apcu\n",
6059
"COPY --from=node /app/public/css/ /app/public/css/\n",
6160
"COPY --from=node /app/public/js/ /app/public/js/\n",
6261
"COPY --from=node /app/public/fonts/ /app/public/fonts/\n",
63-
"RUN echo \"php-fpm\" >> /usr/bin/entrypoint.sh",
64-
"CMD [\"/usr/bin/entrypoint.sh\"]\n"
62+
'RUN echo "php-fpm" >> /usr/bin/entrypoint.sh',
63+
"CMD [\"/usr/bin/entrypoint.sh\"]\n",
6564
],
6665
'docker:generate -n -p 8.1 -e bcmath,pdo_mysql,apcu --no-optimize --no-alpine -m yarn -b mix',
6766
],
@@ -72,7 +71,7 @@ public function provideCommands(): array
7271
public function testItGeneratesConfigurations(array $expected, string $command): void
7372
{
7473
$this->mock(SupportedPhpExtensions::class, function (MockInterface $mock) {
75-
$mock->shouldReceive('fetch')->withAnyArgs()->andReturn([
74+
$mock->shouldReceive('get')->withAnyArgs()->andReturn([
7675
'bcmath', 'pdo_mysql', 'pdo_pgsql', 'redis', 'apcu',
7776
]);
7877
});
@@ -89,8 +88,8 @@ public function testItGeneratesConfigurations(array $expected, string $command):
8988
public function testItAsksQuestions(): void
9089
{
9190
$this->mock(SupportedPhpExtensions::class, function (MockInterface $mock) {
92-
$mock->shouldReceive('fetch')->with('8.2')->once()->andReturn(['bcmath', 'redis']);
93-
$mock->shouldReceive('fetch')->with(null)->andReturn(['not the same as with 8.2']);
91+
$mock->shouldReceive('get')->with('8.2')->once()->andReturn(['bcmath', 'redis']);
92+
$mock->shouldReceive('get')->with(null)->andReturn(['not the same as with 8.2']);
9493
});
9594

9695
$command = $this->artisan('docker:generate');

tests/Unit/Commands/GenerateQuestions/Choices/PhpExtensionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class PhpExtensionsTest extends TestCase
1717
public function testItCallsSupportedPhpExtensions(): void
1818
{
1919
$this->mock(SupportedPhpExtensions::class, function (MockInterface $mock) {
20-
$mock->shouldReceive('fetch')->once()->andReturn([
20+
$mock->shouldReceive('get')->once()->andReturn([
2121
'bcmath',
2222
'pdo_mysql',
2323
'redis',
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Tests\Unit\Integrations;
4+
5+
use BlameButton\LaravelDockerBuilder\Integrations\SupportedPhpExtensions;
6+
use BlameButton\LaravelDockerBuilder\Tests\TestCase;
7+
use Mockery\MockInterface;
8+
9+
/**
10+
* @uses \BlameButton\LaravelDockerBuilder\DockerServiceProvider::boot()
11+
*
12+
* @covers \BlameButton\LaravelDockerBuilder\Integrations\SupportedPhpExtensions
13+
*/
14+
class SupportedPhpExtensionsTest extends TestCase
15+
{
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
$this->partialMock(SupportedPhpExtensions::class, function (MockInterface $mock) {
21+
$mock->shouldAllowMockingProtectedMethods();
22+
$mock->shouldReceive('get')
23+
->once()
24+
->passthru();
25+
$mock->shouldReceive('fetch')
26+
->once()
27+
->andReturn(<<<'RES'
28+
apcu 8.1 8.2
29+
pdo_mysql 8.2
30+
pdo_pgsql 8.0 8.1
31+
memcached 8.0
32+
redis 8.1
33+
RES);
34+
});
35+
}
36+
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+
];
44+
}
45+
46+
/** @dataProvider providePhpVersions */
47+
public function testItFiltersVersions(): void
48+
{
49+
$supported = app(SupportedPhpExtensions::class)->get('8.2');
50+
51+
self::assertEquals(['apcu', 'pdo_mysql'], $supported);
52+
}
53+
}

0 commit comments

Comments
 (0)