Skip to content

Commit f080a50

Browse files
committed
add dev boilerplate and fix requirement for twig
1 parent 1c21604 commit f080a50

File tree

8 files changed

+258
-2
lines changed

8 files changed

+258
-2
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
.idea/
3+
phpstan.neon
4+
phpunit.xml

composer.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212
"php": ">=8.2",
1313
"cuyz/valinor": "^1.0.0",
1414
"symfony/framework-bundle": "^6.0 || ^7.0",
15-
"symfony/http-client": "^6.0 || ^7.0"
15+
"symfony/http-client": "^6.0 || ^7.0",
16+
"symfony/twig-bundle": "^6.0 || ^7.0"
1617
},
1718
"authors": [
1819
{
1920
"name": "Philipp Scheit",
2021
"email": "[email protected]"
2122
}
2223
],
23-
"minimum-stability": "stable"
24+
"minimum-stability": "stable",
25+
"require-dev": {
26+
"phpstan/phpstan": "^2.1",
27+
"phpunit/phpunit": "^12.0",
28+
"webmozart/assert": "^1.11"
29+
}
2430
}

config/docker/php/Dockerfile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM php:8.4-fpm AS php-installed
2+
3+
RUN apt-get update && apt-get install -y \
4+
git zip unzip
5+
6+
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
7+
8+
RUN mkdir -p /app
9+
WORKDIR /app
10+
11+
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
12+
ENV COMPOSER_ALLOW_SUPERUSER=1
13+
14+
# install and cache composer dependencies
15+
COPY composer.json ./
16+
RUN composer install --prefer-dist --no-interaction --no-scripts

docker-compose.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
php:
3+
build:
4+
context: .
5+
dockerfile: config/docker/php/Dockerfile
6+
7+
volumes:
8+
- .:/app
9+
- /var/www/.cache

justfile

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
set dotenv-load := false
2+
3+
set positional-arguments
4+
5+
project_root := justfile_directory()
6+
7+
default:
8+
@just --list
9+
10+
php := "docker compose exec --user=www-data php"
11+
php_root := "docker compose exec --user=root php"
12+
13+
cli *args='':
14+
{{ php }} bin/console "${@}"
15+
16+
phpstan *args='':
17+
{{php}} vendor/bin/phpstan "${@}"
18+
19+
phpunit *args='':
20+
{{ php }} vendor/bin/phpunit "$@"
21+
22+
composer *args='':
23+
{{ php }} composer "${@}"
24+
25+
prep:
26+
j phpstan
27+
j phpunit
28+
29+
up:
30+
docker compose up -d --remove-orphans
31+
just fix-docker-permissions
32+
33+
fix-docker-permissions:
34+
docker compose exec --user=root php bash -c "mkdir -p /var/www/.composer && chown -R 33:33 /var/www"

phpstan.neon.dist

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
includes:
2+
# - vendor/phpstan/phpstan-doctrine/extension.neon
3+
# - vendor/phpstan/phpstan-symfony/extension.neon
4+
# - vendor/phpstan/phpstan-phpunit/extension.neon
5+
# - config/phpstan/phpstan-symfony.php
6+
#- phpstan-baseline.neon
7+
#- vendor/dave-liddament/phpstan-php-language-extensions/extension.neon
8+
9+
parameters:
10+
level: max
11+
paths:
12+
- src
13+
- tests
14+
15+
editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%&project=symfony-vite-bundle'

phpunit.xml.dist

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheDirectory="var/.phpunit.cache"
6+
executionOrder="depends,defects"
7+
requireCoverageMetadata="false"
8+
beStrictAboutCoverageMetadata="true"
9+
beStrictAboutOutputDuringTests="true"
10+
colors="true"
11+
failOnRisky="true"
12+
failOnWarning="true"
13+
displayDetailsOnTestsThatTriggerDeprecations="true"
14+
displayDetailsOnTestsThatTriggerWarnings="true"
15+
>
16+
<testsuites>
17+
<testsuite name="default">
18+
<directory>tests</directory>
19+
</testsuite>
20+
</testsuites>
21+
22+
<source restrictNotices="true" restrictWarnings="true">
23+
<include>
24+
<directory>src</directory>
25+
</include>
26+
</source>
27+
</phpunit>

tests/ServiceTest.php

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Vite\Dto\ViteManifestCssDto;
8+
use Vite\Dto\ViteManifestDto;
9+
use Vite\Dto\ViteManifestRecordDto;
10+
use Vite\Dto\ViteRenderedLinkTagDto;
11+
use Vite\Dto\ViteRenderedScriptTagDto;
12+
use Vite\Exception\OnlyViteDevServerIsRunningException;
13+
use Vite\ManifestParserInterface;
14+
use Vite\Service;
15+
16+
/**
17+
* @covers \Vite\Service
18+
*/
19+
class ServiceTest extends TestCase
20+
{
21+
private Service $viteService;
22+
23+
private ManifestParserInterface&\PHPUnit\Framework\MockObject\MockObject $parser;
24+
25+
protected function setUp(): void
26+
{
27+
$this->parser = $this->createMock(ManifestParserInterface::class);
28+
29+
$this->viteService = new Service(
30+
$this->parser,
31+
'https://web.local.dev:3030',
32+
''
33+
);
34+
}
35+
36+
public function testRendersAllJavascriptDependenciesAsTagWhenInProd(): void
37+
{
38+
$this->parser->method('getParsedManifest')->willReturn(
39+
new ViteManifestDto([
40+
"assets/app.js" => new ViteManifestRecordDto(
41+
file: "app.2baa1153.js",
42+
src: "assets/app.js",
43+
isEntry: true,
44+
css: [
45+
new ViteManifestCssDto(
46+
"app.076763a6.css"
47+
)
48+
]
49+
)
50+
]
51+
)
52+
);
53+
54+
$tags = $this->viteService->generateTags('js', 'app');
55+
56+
self::assertCount(1, $tags);
57+
$app = $tags[0];
58+
self::assertInstanceOf(ViteRenderedScriptTagDto::class, $app);
59+
self::assertSame('/build/app.2baa1153.js', $app->src);
60+
self::assertSame('module', $app->type);
61+
62+
$tags = $this->viteService->generateTags('css', 'app');
63+
64+
self::assertCount(1, $tags);
65+
$app = $tags[0];
66+
self::assertInstanceOf(ViteRenderedLinkTagDto::class, $app);
67+
self::assertSame('/build/app.076763a6.css', $app->href);
68+
self::assertSame('stylesheet', $app->rel);
69+
}
70+
71+
public function testUsesTheCdnUrlInfront(): void
72+
{
73+
$cdnViteService = new Service(
74+
$this->parser,
75+
'https://web.local.dev:3030',
76+
'https://cdnb.yaymemories.com'
77+
);
78+
79+
$this->parser->method('getParsedManifest')->willReturn(
80+
new ViteManifestDto([
81+
"assets/app.js" => new ViteManifestRecordDto(
82+
file: "app.2baa1153.js",
83+
src: "assets/app.js",
84+
isEntry: true,
85+
css: [
86+
new ViteManifestCssDto(
87+
"app.076763a6.css"
88+
)
89+
]
90+
)
91+
]
92+
)
93+
);
94+
95+
$tags = $cdnViteService->generateTags('js', 'app');
96+
97+
self::assertCount(1, $tags);
98+
$app = $tags[0];
99+
self::assertInstanceOf(ViteRenderedScriptTagDto::class, $app);
100+
self::assertSame('https://cdnb.yaymemories.com/build/app.2baa1153.js', $app->src);
101+
self::assertSame('module', $app->type);
102+
103+
$tags = $cdnViteService->generateTags('css', 'app');
104+
105+
self::assertCount(1, $tags);
106+
$app = $tags[0];
107+
self::assertInstanceOf(ViteRenderedLinkTagDto::class, $app);
108+
self::assertSame('https://cdnb.yaymemories.com/build/app.076763a6.css', $app->href);
109+
self::assertSame('stylesheet', $app->rel);
110+
}
111+
112+
public function testRendersJustTheViteServeTagWhenNotInProd(): void
113+
{
114+
$this->parser->method('getParsedManifest')->willThrowException(
115+
new OnlyViteDevServerIsRunningException('no manifest is written, when vite serve is running')
116+
);
117+
118+
$tags = $this->viteService->generateTags('js', 'app');
119+
120+
self::assertCount(2, $tags);
121+
$server = $tags[0];
122+
self::assertInstanceOf(ViteRenderedScriptTagDto::class, $server);
123+
self::assertSame(
124+
'https://web.local.dev:3030/build/@vite/client',
125+
$server->src
126+
);
127+
self::assertSame(
128+
'module',
129+
$server->type
130+
);
131+
132+
$app = $tags[1];
133+
self::assertInstanceOf(ViteRenderedScriptTagDto::class, $app);
134+
self::assertSame(
135+
'https://web.local.dev:3030/build/assets/app.js',
136+
$app->src
137+
);
138+
self::assertSame(
139+
'module',
140+
$app->type
141+
);
142+
143+
self::assertCount(0, $this->viteService->generateTags('css', 'app'));
144+
}
145+
}

0 commit comments

Comments
 (0)