forked from symfony/webpack-encore-bundle
/
IntegrationTest.php
303 lines (261 loc) · 10.7 KB
/
IntegrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/*
* This file is part of the Symfony WebpackEncoreBundle package.
* (c) Fabien Potencier <fabien@symfony.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\WebpackEncoreBundle\Tests;
use PHPUnit\Framework\TestCase;
use Psr\Log\LogLevel;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\Log\Logger;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
use Symfony\WebpackEncoreBundle\CacheWarmer\EntrypointCacheWarmer;
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
class IntegrationTest extends TestCase
{
public function testTwigIntegration()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();
$container = $kernel->getContainer();
$html1 = $container->get('twig')->render('@integration_test/template.twig');
$this->assertStringContainsString(
'<script src="/build/file1.js" integrity="sha384-Q86c+opr0lBUPWN28BLJFqmLhho+9ZcJpXHorQvX6mYDWJ24RQcdDarXFQYN8HLc"></script>',
$html1
);
$this->assertStringContainsString(
'<link rel="stylesheet" href="/build/styles.css" integrity="sha384-4g+Zv0iELStVvA4/B27g4TQHUMwZttA5TEojjUyB8Gl5p7sarU4y+VTSGMrNab8n">'.
'<link rel="stylesheet" href="/build/styles2.css" integrity="sha384-hfZmq9+2oI5Cst4/F4YyS2tJAAYdGz7vqSMP8cJoa8bVOr2kxNRLxSw6P8UZjwUn">',
$html1
);
$this->assertStringContainsString(
'<script src="/build/other3.js"></script>',
$html1
);
$this->assertStringContainsString(
'<link rel="stylesheet" href="/build/styles3.css">'.
'<link rel="stylesheet" href="/build/styles4.css">',
$html1
);
$html2 = $container->get('twig')->render('@integration_test/manual_template.twig');
$this->assertStringContainsString(
'<script src="/build/file3.js"></script>',
$html2
);
$this->assertStringContainsString(
'<script src="/build/other4.js"></script>',
$html2
);
}
public function testEntriesAreNotRepeatedWhenAlreadyOutputIntegration()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();
$container = $kernel->getContainer();
$html1 = $container->get('twig')->render('@integration_test/template.twig');
$html2 = $container->get('twig')->render('@integration_test/manual_template.twig');
$this->assertStringContainsString(
'<script src="/build/file3.js"></script>',
$html2
);
// file1.js is not repeated
$this->assertStringNotContainsString(
'<script src="/build/file1.js"></script>',
$html2
);
// styles3.css is not repeated
$this->assertStringNotContainsString(
'<link rel="stylesheet" href="/build/styles3.css">',
$html2
);
// styles4.css is not repeated
$this->assertStringNotContainsString(
'<link rel="stylesheet" href="/build/styles4.css">',
$html2
);
}
public function testCacheWarmer()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();
$container = $kernel->getContainer();
$cacheWarmer = $container->get(WebpackEncoreCacheWarmerTester::class);
$cacheWarmer->warmCache($kernel->getCacheDir());
$cachePath = $kernel->getCacheDir().'/webpack_encore.cache.php';
$this->assertFileExists($cachePath);
$data = require $cachePath;
// check for both build keys
$this->assertSame(['_default', 'different_build'], array_keys($data[0] ?? $data));
}
public function testEnabledStrictMode_throwsException_ifBuildMissing()
{
$this->expectException(\Twig\Error\RuntimeError::class);
$this->expectExceptionMessageRegExp('/Could not find the entrypoints file/');
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->outputPath = 'missing_build';
$kernel->builds = ['different_build' => 'missing_build'];
$kernel->boot();
$container = $kernel->getContainer();
$container->get('twig')->render('@integration_test/template.twig');
}
public function testDisabledStrictMode_ignoresMissingBuild()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->outputPath = 'missing_build';
$kernel->strictMode = false;
$kernel->builds = ['different_build' => 'missing_build'];
$kernel->boot();
$container = $kernel->getContainer();
$html = $container->get('twig')->render('@integration_test/template.twig');
self::assertSame('', trim($html));
}
public function testAutowireableInterfaces()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();
$container = $kernel->getContainer();
$this->assertInstanceOf(WebpackEncoreAutowireTestService::class, $container->get(WebpackEncoreAutowireTestService::class));
}
public function testPreload()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();
$container = $kernel->getContainer();
/** @var TagRenderer $tagRenderer */
$tagRenderer = $container->get('public.webpack_encore.tag_renderer');
$tagRenderer->renderWebpackLinkTags('my_entry');
$tagRenderer->renderWebpackScriptTags('my_entry');
$request = Request::create('/foo');
$response = $kernel->handle($request);
$this->assertStringContainsString('</build/file1.js>; rel="preload"; as="script"', $response->headers->get('Link'));
}
public function testAutowireDefaultBuildArgument()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();
$container = $kernel->getContainer();
$container->get('public.webpack_encore.entrypoint_lookup_collection')
->getEntrypointLookup();
// Testing that it doesn't throw an exception is enough
$this->assertTrue(true);
}
}
abstract class AbstractWebpackEncoreIntegrationTestKernel extends Kernel
{
use MicroKernelTrait;
private $enableAssets;
public $strictMode = true;
public $outputPath = __DIR__.'/fixtures/build';
public $builds = [
'different_build' => __DIR__.'/fixtures/different_build',
];
public function __construct(bool $enableAssets)
{
parent::__construct('test', true);
$this->enableAssets = $enableAssets;
}
public function registerBundles()
{
return [
new FrameworkBundle(),
new TwigBundle(),
new WebpackEncoreBundle(),
];
}
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
$container->loadFromExtension('framework', [
'secret' => 'foo',
'assets' => [
'enabled' => $this->enableAssets,
],
]);
$container->loadFromExtension('twig', [
'paths' => [
__DIR__.'/fixtures' => 'integration_test',
],
'strict_variables' => true,
'exception_controller' => null,
]);
$container->loadFromExtension('webpack_encore', [
'output_path' => $this->outputPath,
'cache' => true,
'crossorigin' => false,
'preload' => true,
'builds' => $this->builds,
'strict_mode' => $this->strictMode,
]);
$container->register(WebpackEncoreCacheWarmerTester::class)
->addArgument(new Reference('webpack_encore.entrypoint_lookup.cache_warmer'))
->setPublic(true);
$container->autowire(WebpackEncoreAutowireTestService::class)
->setPublic(true);
$container->setAlias(new Alias('public.webpack_encore.tag_renderer', true), 'webpack_encore.tag_renderer');
$container->getAlias('public.webpack_encore.tag_renderer')->setPrivate(false);
$container->setAlias(new Alias('public.webpack_encore.entrypoint_lookup_collection', true), 'webpack_encore.entrypoint_lookup_collection');
$container->getAlias('public.webpack_encore.entrypoint_lookup_collection')->setPrivate(false);
// avoid logging request logs
$container->register('logger', Logger::class)
->setArgument(0, LogLevel::EMERGENCY);
}
public function getCacheDir()
{
return sys_get_temp_dir().'/cache'.spl_object_hash($this);
}
public function getLogDir()
{
return sys_get_temp_dir().'/logs'.spl_object_hash($this);
}
public function renderFoo()
{
return new Response('I am a page!');
}
}
if (method_exists(AbstractWebpackEncoreIntegrationTestKernel::class, 'configureRouting')) {
class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel {
protected function configureRouting(RoutingConfigurator $routes): void
{
$routes->add('/foo', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderFoo');
}
}
} else {
class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel {
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->add('/foo', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderFoo');
}
}
}
class WebpackEncoreCacheWarmerTester
{
private $entrypointCacheWarmer;
public function __construct(EntrypointCacheWarmer $entrypointCacheWarmer)
{
$this->entrypointCacheWarmer = $entrypointCacheWarmer;
}
public function warmCache(string $cacheDir)
{
$this->entrypointCacheWarmer->warmUp($cacheDir);
}
}
class WebpackEncoreAutowireTestService
{
public function __construct(EntrypointLookupInterface $entrypointLookup, EntrypointLookupCollectionInterface $entrypointLookupCollection)
{
}
}