Skip to content

Commit

Permalink
feature #98 PHP 8.0 compatibility (jmsche)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the master branch.

Discussion
----------

PHP 8.0 compatibility

Tests are also run against PHP7.4.

Commits
-------

0c797de PHP 8.0 compatibility
  • Loading branch information
weaverryan committed Oct 28, 2020
2 parents 5c0f659 + 0c797de commit c879bc5
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 28 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ matrix:
- php: 7.2
- php: 7.3
env: deps=low
- php: 7.4
env: SYMFONY_PHPUNIT_VERSION=9.4
- php: nightly
env: SYMFONY_PHPUNIT_VERSION=9.4

before_install:
- phpenv config-rm xdebug.ini || true
- composer global require --no-progress --no-scripts --no-plugins symfony/flex dev-master
- composer global require --no-progress --no-scripts --no-plugins symfony/flex dev-main

install:
- |
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"minimum-stability": "dev",
"require": {
"php": "^7.1.3",
"php": ">=7.1.3",
"symfony/asset": "^3.4 || ^4.0 || ^5.0",
"symfony/config": "^3.4 || ^4.0 || ^5.0",
"symfony/dependency-injection": "^3.4 || ^4.0 || ^5.0",
Expand Down
7 changes: 2 additions & 5 deletions tests/Asset/TagRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Symfony\Component\Asset\Packages;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
use Symfony\WebpackEncoreBundle\Asset\IntegrityDataProviderInterface;
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
use Symfony\WebpackEncoreBundle\Tests\TestEntrypointLookupIntegrityDataProviderInterface;

class TagRendererTest extends TestCase
{
Expand Down Expand Up @@ -138,10 +138,7 @@ public function testRenderScriptTagsWithinAnEntryPointCollection()

public function testRenderScriptTagsWithHashes()
{
$entrypointLookup = $this->createMock([
EntrypointLookupInterface::class,
IntegrityDataProviderInterface::class,
]);
$entrypointLookup = $this->createMock(TestEntrypointLookupIntegrityDataProviderInterface::class);
$entrypointLookup->expects($this->once())
->method('getJavaScriptFiles')
->willReturn(['/build/file1.js', '/build/file2.js']);
Expand Down
6 changes: 5 additions & 1 deletion tests/EventListener/ExceptionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Symfony\WebpackEncoreBundle\Tests\EventListener;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -39,7 +40,10 @@ public function testItResetsAllEntrypointLookups()

$request = new Request();
$exception = new \Exception();
$event = new GetResponseForExceptionEvent(
$exceptionEventClass = class_exists(ExceptionEvent::class)
? ExceptionEvent::class
: GetResponseForExceptionEvent::class;
$event = new $exceptionEventClass(
$this->createMock(HttpKernelInterface::class),
$request,
HttpKernelInterface::MASTER_REQUEST,
Expand Down
67 changes: 47 additions & 20 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function testTwigIntegration()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();
$container = $kernel->getContainer();
$twig = $this->getTwigEnvironmentFromBootedKernel($kernel);

$html1 = $container->get('twig')->render('@integration_test/template.twig');
$html1 = $twig->render('@integration_test/template.twig');
$this->assertStringContainsString(
'<script src="/build/file1.js" integrity="sha384-Q86c+opr0lBUPWN28BLJFqmLhho+9ZcJpXHorQvX6mYDWJ24RQcdDarXFQYN8HLc"></script>',
$html1
Expand All @@ -58,7 +58,7 @@ public function testTwigIntegration()
$html1
);

$html2 = $container->get('twig')->render('@integration_test/manual_template.twig');
$html2 = $twig->render('@integration_test/manual_template.twig');
$this->assertStringContainsString(
'<script src="/build/file3.js"></script>',
$html2
Expand All @@ -73,10 +73,10 @@ public function testEntriesAreNotRepeatedWhenAlreadyOutputIntegration()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();
$container = $kernel->getContainer();
$twig = $this->getTwigEnvironmentFromBootedKernel($kernel);

$html1 = $container->get('twig')->render('@integration_test/template.twig');
$html2 = $container->get('twig')->render('@integration_test/manual_template.twig');
$html1 = $twig->render('@integration_test/template.twig');
$html2 = $twig->render('@integration_test/manual_template.twig');
$this->assertStringContainsString(
'<script src="/build/file3.js"></script>',
$html2
Expand All @@ -102,7 +102,7 @@ public function testCacheWarmer()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();
$container = $kernel->getContainer();
$container = $this->getContainerFromBootedKernel($kernel);

$cacheWarmer = $container->get(WebpackEncoreCacheWarmerTester::class);

Expand All @@ -124,8 +124,8 @@ public function testEnabledStrictMode_throwsException_ifBuildMissing()
$kernel->outputPath = 'missing_build';
$kernel->builds = ['different_build' => 'missing_build'];
$kernel->boot();
$container = $kernel->getContainer();
$container->get('twig')->render('@integration_test/template.twig');
$twig = $this->getTwigEnvironmentFromBootedKernel($kernel);
$twig->render('@integration_test/template.twig');
}

public function testDisabledStrictMode_ignoresMissingBuild()
Expand All @@ -135,24 +135,24 @@ public function testDisabledStrictMode_ignoresMissingBuild()
$kernel->strictMode = false;
$kernel->builds = ['different_build' => 'missing_build'];
$kernel->boot();
$container = $kernel->getContainer();
$html = $container->get('twig')->render('@integration_test/template.twig');
$twig = $this->getTwigEnvironmentFromBootedKernel($kernel);
$html = $twig->render('@integration_test/template.twig');
self::assertSame('', trim($html));
}

public function testAutowireableInterfaces()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();
$container = $kernel->getContainer();
$container = $this->getContainerFromBootedKernel($kernel);
$this->assertInstanceOf(WebpackEncoreAutowireTestService::class, $container->get(WebpackEncoreAutowireTestService::class));
}

public function testPreload()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();
$container = $kernel->getContainer();
$container = $this->getContainerFromBootedKernel($kernel);

/** @var TagRenderer $tagRenderer */
$tagRenderer = $container->get('public.webpack_encore.tag_renderer');
Expand All @@ -168,14 +168,34 @@ public function testAutowireDefaultBuildArgument()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->boot();
$container = $kernel->getContainer();
$container = $this->getContainerFromBootedKernel($kernel);

$container->get('public.webpack_encore.entrypoint_lookup_collection')
->getEntrypointLookup();

// Testing that it doesn't throw an exception is enough
$this->assertTrue(true);
}

private function getContainerFromBootedKernel(WebpackEncoreIntegrationTestKernel $kernel)
{
if ($kernel::VERSION_ID >= 40100) {
return $kernel->getContainer()->get('test.service_container');
}

return $kernel->getContainer();
}

private function getTwigEnvironmentFromBootedKernel(WebpackEncoreIntegrationTestKernel $kernel)
{
$container = $this->getContainerFromBootedKernel($kernel);

if ($container->has(\Twig\Environment::class)) {
return $container->get(\Twig\Environment::class);
}

return $container->get('twig');
}
}

abstract class AbstractWebpackEncoreIntegrationTestKernel extends Kernel
Expand Down Expand Up @@ -206,12 +226,19 @@ public function registerBundles()

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
$container->loadFromExtension('framework', [
$frameworkConfig = [
'secret' => 'foo',
'assets' => [
'enabled' => $this->enableAssets,
],
]);
'test' => true,
];
if (AbstractWebpackEncoreIntegrationTestKernel::VERSION_ID >= 50100) {
$frameworkConfig['router'] = [
'utf8' => true,
];
}
$container->loadFromExtension('framework', $frameworkConfig);

$container->loadFromExtension('twig', [
'paths' => [
Expand All @@ -238,10 +265,10 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa
->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->getAlias('public.webpack_encore.tag_renderer')->setPublic(true);

$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);
$container->getAlias('public.webpack_encore.entrypoint_lookup_collection')->setPublic(true);

// avoid logging request logs
$container->register('logger', Logger::class)
Expand All @@ -264,11 +291,11 @@ public function renderFoo()
}
}

if (method_exists(AbstractWebpackEncoreIntegrationTestKernel::class, 'configureRouting')) {
if (AbstractWebpackEncoreIntegrationTestKernel::VERSION_ID >= 50100) {
class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel {
protected function configureRouting(RoutingConfigurator $routes): void
{
$routes->add('/foo', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderFoo');
$routes->add('/foo', 'kernel::renderFoo');
}
}
} else {
Expand Down
17 changes: 17 additions & 0 deletions tests/TestEntrypointLookupIntegrityDataProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony WebpackEncoreBundle package.
* (c) Fabien Potencier <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\WebpackEncoreBundle\Tests;

use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
use Symfony\WebpackEncoreBundle\Asset\IntegrityDataProviderInterface;

interface TestEntrypointLookupIntegrityDataProviderInterface extends EntrypointLookupInterface, IntegrityDataProviderInterface
{
}

0 comments on commit c879bc5

Please sign in to comment.