Skip to content

Commit 6688678

Browse files
committed
Add tests for InteractsWithTwig
1 parent 0d755a6 commit 6688678

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed

src/Traits/InteractsWithTwig.php

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

33
namespace BlameButton\LaravelDockerBuilder\Traits;
44

5-
use RuntimeException;
65
use Twig\Environment as TwigEnvironment;
7-
use Twig\Error\Error;
6+
use Twig\Error\LoaderError;
7+
use Twig\Error\RuntimeError;
8+
use Twig\Error\SyntaxError;
89
use Twig\Loader\FilesystemLoader;
910

1011
trait InteractsWithTwig
1112
{
1213
private TwigEnvironment|null $twig = null;
1314

14-
private function twig(): TwigEnvironment
15+
public function twig(): TwigEnvironment
1516
{
1617
if (! is_null($this->twig)) {
1718
return $this->twig;
@@ -29,13 +30,13 @@ private function twig(): TwigEnvironment
2930
* @param string $name
3031
* @param array<string, mixed> $context
3132
* @return string
33+
*
34+
* @throws LoaderError when the template cannot be found
35+
* @throws SyntaxError when an error occurred during compilation
36+
* @throws RuntimeError when an error occurred during rendering
3237
*/
33-
private function render(string $name, array $context): string
38+
public function render(string $name, array $context): string
3439
{
35-
try {
36-
return $this->twig()->render($name, $context);
37-
} catch (Error $error) {
38-
throw new RuntimeException($error->getMessage());
39-
}
40+
return $this->twig()->render($name, $context);
4041
}
4142
}

tests/Objects/ConfigurationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public function provideCommandOptions(): array
1717
return [
1818
[
1919
'php artisan docker:generate -n -p 8.2 -e bcmath,pdo_mysql -o -m npm -b vite',
20-
new Configuration('8.2', ['bcmath', 'pdo_mysql'], true, 'npm', 'vite')
20+
new Configuration('8.2', ['bcmath', 'pdo_mysql'], true, 'npm', 'vite'),
2121
],
2222
[
2323
'php artisan docker:generate -n -p 8.1 -e bcmath,pdo_pgsql,redis -o -m yarn -b vite',
24-
new Configuration('8.1', ['bcmath', 'pdo_pgsql', 'redis'], true, 'yarn', 'vite')
24+
new Configuration('8.1', ['bcmath', 'pdo_pgsql', 'redis'], true, 'yarn', 'vite'),
2525
],
2626
[
2727
'php artisan docker:generate -n -p 8.0 -e bcmath,pdo_pgsql,apcu -m yarn -b mix',
28-
new Configuration('8.0', ['bcmath', 'pdo_pgsql', 'apcu'], false, 'yarn', 'mix')
28+
new Configuration('8.0', ['bcmath', 'pdo_pgsql', 'apcu'], false, 'yarn', 'mix'),
2929
],
3030
];
3131
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Tests\Traits;
4+
5+
use BlameButton\LaravelDockerBuilder\Tests\TestCase;
6+
use BlameButton\LaravelDockerBuilder\Traits\InteractsWithTwig;
7+
use Twig\Environment;
8+
use Twig\Error\LoaderError;
9+
use Twig\Loader\FilesystemLoader;
10+
11+
/**
12+
* @uses \BlameButton\LaravelDockerBuilder\DockerServiceProvider
13+
* @uses package_path()
14+
*
15+
* @covers \BlameButton\LaravelDockerBuilder\Traits\InteractsWithTwig
16+
*/
17+
class InteractsWithTwigTest extends TestCase
18+
{
19+
public function testItCreatesTwig(): void
20+
{
21+
/** @var InteractsWithTwig $class */
22+
$class = $this->getObjectForTrait(InteractsWithTwig::class);
23+
24+
$twig = $class->twig();
25+
self::assertInstanceOf(Environment::class, $twig);
26+
$loader = $twig->getLoader();
27+
self::assertInstanceOf(FilesystemLoader::class, $loader);
28+
self::assertEquals([package_path('docker/template')], $loader->getPaths());
29+
}
30+
31+
public function testItThrowsErrorOnMissingTemplates(): void
32+
{
33+
/** @var InteractsWithTwig $class */
34+
$class = $this->getObjectForTrait(InteractsWithTwig::class);
35+
36+
$this->expectException(LoaderError::class);
37+
38+
$class->render('invalid-filename', []);
39+
}
40+
41+
public function testItRendersNginxTemplate(): void
42+
{
43+
/** @var InteractsWithTwig $class */
44+
$class = $this->getObjectForTrait(InteractsWithTwig::class);
45+
46+
$output = $class->render('nginx.dockerfile.twig', []);
47+
48+
self::assertStringContainsString('FROM nginx:1-alpine', $output);
49+
}
50+
51+
public function testItRendersPhpTemplate(): void
52+
{
53+
/** @var InteractsWithTwig $class */
54+
$class = $this->getObjectForTrait(InteractsWithTwig::class);
55+
56+
$output = $class->render('php.dockerfile.twig', [
57+
'php_version' => '8.2',
58+
]);
59+
60+
self::assertStringContainsString('FROM php:8.2-fpm-alpine', $output);
61+
}
62+
}

0 commit comments

Comments
 (0)