Skip to content

Commit fb3cc4a

Browse files
committed
Prepare test code to work with Symfony 8
1 parent 4f687d0 commit fb3cc4a

File tree

5 files changed

+87
-63
lines changed

5 files changed

+87
-63
lines changed

phpunit.xml.dist

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="tests/bootstrap.php" cacheDirectory=".phpunit.cache">
3-
<php>
4-
<env name="SHELL_VERBOSITY" value="-1" />
5-
<server name="KERNEL_CLASS" value="\Webfactory\ShortcodeBundle\Tests\Fixtures\TestKernel" />
6-
<ini name="error_reporting" value="-1" />
7-
<server name="APP_ENV" value="test" force="true" />
8-
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=9999" />
9-
</php>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
bootstrap="tests/bootstrap.php"
6+
colors="true">
7+
108
<testsuites>
119
<testsuite name="Project Test Suite">
1210
<directory>tests</directory>
1311
<exclude>tests/Functional/ShortcodeTest.php</exclude>
1412
</testsuite>
1513
</testsuites>
14+
15+
<source ignoreSuppressionOfDeprecations="true">
16+
<include>
17+
<directory>src</directory>
18+
</include>
19+
</source>
20+
21+
<php>
22+
<server name="KERNEL_CLASS" value="Webfactory\ShortcodeBundle\Tests\Fixtures\TestKernel" />
23+
<env name="SHELL_VERBOSITY" value="-1" />
24+
</php>
25+
1626
</phpunit>

tests/Fixtures/TestKernel.php

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
66
use Symfony\Bundle\TwigBundle\TwigBundle;
77
use Symfony\Component\Config\Loader\LoaderInterface;
8+
use Symfony\Component\DependencyInjection\ChildDefinition;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
810
use Symfony\Component\HttpKernel\Kernel;
11+
use Thunder\Shortcode\Handler\PlaceholderHandler;
12+
use Webfactory\ShortcodeBundle\Tests\Fixtures\Controller\InvokableShortcodeTestController;
13+
use Webfactory\ShortcodeBundle\Tests\Fixtures\Controller\ShortcodeTestController;
914
use Webfactory\ShortcodeBundle\WebfactoryShortcodeBundle;
1015

1116
final class TestKernel extends Kernel
@@ -21,17 +26,68 @@ public function registerBundles(): array
2126

2227
public function registerContainerConfiguration(LoaderInterface $loader): void
2328
{
24-
$loader->load(__DIR__.'/config/config.yml');
25-
$loader->load(__DIR__.'/config/test_shortcodes.xml');
29+
$loader->load(static function (ContainerBuilder $container): void {
30+
$container->loadFromExtension('framework', [
31+
'secret' => 'top-secret',
32+
'test' => true,
33+
'esi' => ['enabled' => true],
34+
'fragments' => ['enabled' => true],
35+
'router' => ['resource' => '%kernel.project_dir%/src/Resources/config/guide-routing.xml'],
36+
] + (Kernel::VERSION_ID < 70000 ? ['annotations' => ['enabled' => false]] : []));
37+
38+
$container->loadFromExtension('twig', [
39+
'strict_variables' => true,
40+
]);
41+
42+
$testControllerAction = ShortcodeTestController::class.'::test';
43+
44+
$container->loadFromExtension('webfactory_shortcode', [
45+
'shortcodes' => [
46+
'test-config-inline' => $testControllerAction,
47+
'test-config-esi' => [
48+
'controller' => $testControllerAction,
49+
'method' => 'esi',
50+
],
51+
'test-config-invokable' => InvokableShortcodeTestController::class,
52+
'test-shortcode-guide' => [
53+
'controller' => $testControllerAction,
54+
'description' => "Description for the 'test-shortcode-guide' shortcode",
55+
'example' => 'test-shortcode-guide test=true',
56+
],
57+
],
58+
]);
59+
60+
$container->register(\Webfactory\ShortcodeBundle\Tests\Fixtures\Controller\ShortcodeTestController::class)
61+
->setAutoconfigured(true)
62+
->setAutowired(true)
63+
->addTag('controller.service_arguments');
64+
65+
$container->register(\Webfactory\ShortcodeBundle\Tests\Fixtures\Controller\InvokableShortcodeTestController::class)
66+
->setAutoconfigured(true)
67+
->setAutowired(true)
68+
->addTag('controller.service_arguments');
69+
70+
// Service definitions from test_shortcodes.xml
71+
$container->setDefinition('test_esi', new ChildDefinition('Webfactory\ShortcodeBundle\Handler\EmbeddedShortcodeHandler.esi'))
72+
->replaceArgument(1, $testControllerAction)
73+
->addTag('webfactory.shortcode', ['shortcode' => 'test-service-esi']);
74+
75+
$container->setDefinition('test_inline', new ChildDefinition('Webfactory\ShortcodeBundle\Handler\EmbeddedShortcodeHandler.inline'))
76+
->replaceArgument(1, $testControllerAction)
77+
->addTag('webfactory.shortcode', ['shortcode' => 'test-service-inline']);
78+
79+
$container->register(PlaceholderHandler::class)
80+
->addTag('webfactory.shortcode', ['shortcode' => 'placeholder']);
81+
});
2682
}
2783

2884
public function getCacheDir(): string
2985
{
30-
return __DIR__.'/cache/'.$this->environment;
86+
return __DIR__.'/var/cache/'.$this->environment;
3187
}
3288

3389
public function getLogDir(): string
3490
{
35-
return __DIR__.'/logs';
91+
return __DIR__.'/var/log';
3692
}
3793
}

tests/Fixtures/config/config.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/Fixtures/config/test_shortcodes.xml

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/bootstrap.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
<?php
22

33
use Symfony\Component\ErrorHandler\DebugClassLoader;
4+
use Symfony\Component\ErrorHandler\ErrorHandler;
45

56
require_once __DIR__.'/../vendor/autoload.php';
67

8+
// Work around https://github.com/symfony/symfony/issues/53812 for the time being (issue starting with Symfony 6.4 or 7.1 and PHPUnit 11 or 12?)
9+
set_exception_handler([new ErrorHandler(), 'handleException']);
10+
11+
// Ensure a fresh cache every time tests are run,
12+
// even with debug mode disabled (https://symfony.com/doc/current/testing.html#set-up-your-test-environment)
13+
(new Symfony\Component\Filesystem\Filesystem())->remove(__DIR__.'/Fixtures/var/cache/test');
14+
15+
// Use DebugClassLoader to catch certain deprecations that can only be found through source code analysis
716
DebugClassLoader::enable();

0 commit comments

Comments
 (0)