Skip to content

Commit c389725

Browse files
committed
Attempt at making it compatible with PHPUnit 12
1 parent eedc4ae commit c389725

File tree

12 files changed

+296
-96
lines changed

12 files changed

+296
-96
lines changed

.github/workflows/php.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ jobs:
5959
- { php-version: "8.4", symfony-version: "7.1", phpunit-version: "10" }
6060
- { php-version: "8.4", symfony-version: "7.2", phpunit-version: "10" }
6161

62+
- { php-version: "8.3", symfony-version: "6.1", phpunit-version: "11" }
63+
- { php-version: "8.3", symfony-version: "6.2", phpunit-version: "11" }
64+
- { php-version: "8.3", symfony-version: "6.3", phpunit-version: "11" }
65+
- { php-version: "8.3", symfony-version: "6.4", phpunit-version: "11" }
66+
- { php-version: "8.3", symfony-version: "7.0", phpunit-version: "11" }
67+
- { php-version: "8.3", symfony-version: "7.1", phpunit-version: "11" }
68+
- { php-version: "8.3", symfony-version: "7.2", phpunit-version: "11" }
69+
70+
- { php-version: "8.4", symfony-version: "7.0", phpunit-version: "11" }
71+
- { php-version: "8.4", symfony-version: "7.1", phpunit-version: "11" }
72+
- { php-version: "8.4", symfony-version: "7.2", phpunit-version: "11" }
73+
74+
- { php-version: "8.3", symfony-version: "6.1", phpunit-version: "12" }
75+
- { php-version: "8.3", symfony-version: "6.2", phpunit-version: "12" }
76+
- { php-version: "8.3", symfony-version: "6.3", phpunit-version: "12" }
77+
- { php-version: "8.3", symfony-version: "6.4", phpunit-version: "12" }
78+
- { php-version: "8.3", symfony-version: "7.0", phpunit-version: "12" }
79+
- { php-version: "8.3", symfony-version: "7.1", phpunit-version: "12" }
80+
- { php-version: "8.3", symfony-version: "7.2", phpunit-version: "12" }
81+
82+
- { php-version: "8.4", symfony-version: "7.0", phpunit-version: "12" }
83+
- { php-version: "8.4", symfony-version: "7.1", phpunit-version: "12" }
84+
- { php-version: "8.4", symfony-version: "7.2", phpunit-version: "12" }
85+
6286
name: PHP ${{ matrix.php-version }}, Symfony ${{ matrix.symfony-version }}, PHPUnit ${{ matrix.phpunit-version }}
6387
steps:
6488
- uses: actions/checkout@v4

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"symfony/framework-bundle": "^6.1|^7.0"
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": "^9.6|^10.0",
28+
"phpunit/phpunit": "^9.6|^10.0|^11|^12",
2929
"symfony/maker-bundle": "^1.43"
3030
}
3131
}

phpunit-10.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<server name="APP_ENV" value="test" force="true" />
1717
<server name="SHELL_VERBOSITY" value="-1" />
1818
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
19-
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
19+
<server name="SYMFONY_PHPUNIT_VERSION" value="10.5" />
2020
</php>
2121

2222
<testsuites>

phpunit-11.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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="tests/bootstrap.php"
5+
colors="true"
6+
executionOrder="depends,defects"
7+
failOnRisky="true"
8+
failOnWarning="true"
9+
cacheDirectory=".phpunit.cache"
10+
>
11+
12+
<php>
13+
<server name="KERNEL_CLASS" value="App\Kernel" />
14+
<ini name="display_errors" value="1" />
15+
<ini name="error_reporting" value="-1" />
16+
<server name="APP_ENV" value="test" force="true" />
17+
<server name="SHELL_VERBOSITY" value="-1" />
18+
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
19+
<server name="SYMFONY_PHPUNIT_VERSION" value="11.5" />
20+
</php>
21+
22+
<testsuites>
23+
<testsuite name="default">
24+
<directory>tests</directory>
25+
</testsuite>
26+
</testsuites>
27+
28+
<source>
29+
<include>
30+
<directory suffix=".php">src</directory>
31+
</include>
32+
</source>
33+
</phpunit>

phpunit-12.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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="tests/bootstrap.php"
5+
colors="true"
6+
executionOrder="depends,defects"
7+
failOnRisky="true"
8+
failOnWarning="true"
9+
cacheDirectory=".phpunit.cache"
10+
>
11+
12+
<php>
13+
<server name="KERNEL_CLASS" value="App\Kernel" />
14+
<ini name="display_errors" value="1" />
15+
<ini name="error_reporting" value="-1" />
16+
<server name="APP_ENV" value="test" force="true" />
17+
<server name="SHELL_VERBOSITY" value="-1" />
18+
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
19+
<server name="SYMFONY_PHPUNIT_VERSION" value="12.0" />
20+
</php>
21+
22+
<testsuites>
23+
<testsuite name="default">
24+
<directory>tests</directory>
25+
</testsuite>
26+
</testsuites>
27+
28+
<source>
29+
<include>
30+
<directory suffix=".php">src</directory>
31+
</include>
32+
</source>
33+
</phpunit>

phpunit-9.6.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<server name="APP_ENV" value="test" force="true" />
1919
<server name="SHELL_VERBOSITY" value="-1" />
2020
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
21-
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
21+
<server name="SYMFONY_PHPUNIT_VERSION" value="9.6" />
2222
</php>
2323

2424
<testsuites>

src/Maker/Resources/FunctionalSmokeTest.attributes.tpl.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
<?php declare(strict_types=1);
22
echo "<?php\n"; ?>
33

4-
namespace <?php echo $namespace; ?>;
4+
namespace <?php echo $namespace ?? 'UndefinedNamespace'; ?>;
55

66
use PHPUnit\Framework\Attributes\TestDox;
77
use PHPUnit\Framework\Attributes\TestWith;
8-
<?php if ($with_dto): ?>
8+
<?php if ($with_dto ?? false): ?>
99
use Pierstoval\SmokeTesting\FunctionalSmokeTester;
1010
use Pierstoval\SmokeTesting\FunctionalTestData;
1111
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1212
<?php endif; ?>
1313
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1414

15-
class <?php echo $class_name; ?> extends WebTestCase
15+
class <?php echo $class_name ?? 'UndefinedClassName' ?> extends WebTestCase
1616
{
17-
<?php if ($with_dto): ?>
17+
<?php if ($with_dto ?? false): ?>
1818
use FunctionalSmokeTester;
1919
<?php endif; ?>
2020

21-
<?php foreach ($routes as $route){
21+
<?php foreach ($routes ?? [] as $route){
2222
if (str_starts_with($route['routePath'], '/_')) { continue; } ?>
2323
#[TestWith(['<?php echo $route['httpMethod']; ?>', '<?php echo $route['routePath']; ?>', '<?php echo $route['routeName']; ?>'])]
2424
<?php } ?>
2525
#[TestDox('$method $url ($route)')]
2626
public function testRoute(string $method, string $url, string $route): void
2727
{
28-
<?php if ($with_dto): ?>
28+
<?php if ($with_dto ?? false): ?>
2929
$this->runFunctionalTest(
3030
FunctionalTestData::withUrl($url)
3131
->withMethod($method)
@@ -47,7 +47,7 @@ public function testRoute(string $method, string $url, string $route): void
4747
<?php endif; ?>
4848
}
4949

50-
<?php if ($with_dto): ?>
50+
<?php if ($with_dto ?? false): ?>
5151
public function assertStatusCodeLessThan500(string $method, string $url): \Closure
5252
{
5353
return static function (KernelBrowser $browser) use ($method, $url) {

src/Maker/Resources/FunctionalSmokeTest.provider.tpl.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<?php declare(strict_types=1);
22
echo "<?php\n"; ?>
33

4-
namespace <?php echo $namespace; ?>;
4+
namespace <?php echo $namespace ?? 'UndefinedNamespace'; ?>;
55

66
use PHPUnit\Framework\Attributes\DataProvider;
7-
<?php if ($with_dto): ?>
7+
<?php if ($with_dto ?? false): ?>
88
use Pierstoval\SmokeTesting\FunctionalSmokeTester;
99
use Pierstoval\SmokeTesting\FunctionalTestData;
1010
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1111
<?php endif; ?>
1212
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1313

14-
class <?php echo $class_name; ?> extends WebTestCase
14+
class <?php echo $class_name ?? 'UndefinedClassName' ?> extends WebTestCase
1515
{
16-
<?php if ($with_dto): ?>
16+
<?php if ($with_dto ?? false): ?>
1717
use FunctionalSmokeTester;
1818
<?php endif; ?>
1919

2020
public static function provideRoutes(): \Generator
2121
{
22-
<?php foreach ($routes as $route): ?>
22+
<?php foreach ($routes ?? [] as $route): ?>
2323
<?php if (str_starts_with($route['routePath'], '/_')) continue;
2424
?> yield '<?php echo $route['httpMethod']; ?> <?php echo $route['routePath']; ?>' => ['<?php echo $route['httpMethod']; ?>', '<?php echo $route['routePath']; ?>','<?php echo $route['routeName']; ?>'];
2525
<?php endforeach; ?>
@@ -28,7 +28,7 @@ public static function provideRoutes(): \Generator
2828
#[DataProvider('provideRoutes')]
2929
public function testRoute(string $method, string $url, string $route): void
3030
{
31-
<?php if ($with_dto): ?>
31+
<?php if ($with_dto ?? false): ?>
3232
$this->runFunctionalTest(
3333
FunctionalTestData::withUrl($url)
3434
->withMethod($method)
@@ -51,7 +51,7 @@ public function testRoute(string $method, string $url, string $route): void
5151

5252
}
5353

54-
<?php if ($with_dto): ?>
54+
<?php if ($with_dto ?? false): ?>
5555
public function assertStatusCodeLessThan500(string $method, string $url): \Closure
5656
{
5757
return static function (KernelBrowser $browser) use ($method, $url) {

src/Maker/Resources/FunctionalSmokeTest.tpl.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<?php declare(strict_types=1);
22
echo "<?php\n"; ?>
33

4-
namespace <?php echo $namespace; ?>;
4+
namespace <?php echo $namespace ?? 'UndefinedNamespace'; ?>;
55

6-
<?php if ($with_dto): ?>
6+
<?php if ($with_dto ?? false): ?>
77
use Pierstoval\SmokeTesting\FunctionalSmokeTester;
88
use Pierstoval\SmokeTesting\FunctionalTestData;
99
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1010
<?php endif; ?>
1111
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1212

13-
class <?php echo $class_name; ?> extends WebTestCase
13+
class <?php echo $class_name ?? 'UndefinedClassName' ?> extends WebTestCase
1414
{
15-
<?php if ($with_dto): ?>
15+
<?php if ($with_dto ?? false): ?>
1616
use FunctionalSmokeTester;
1717
<?php endif; ?>
18-
<?php foreach ($routes as $route): ?>
18+
<?php foreach ($routes ?? [] as $route): ?>
1919

2020
public function testRoute<?php echo ucfirst(preg_replace_callback('~_([a-z0-9])~isUu', function($matches) {return strtoupper($matches[1]);}, $route['routeName'])).'WithMethod'.ucfirst(strtolower($route['httpMethod'])); ?>(): void
2121
{
22-
<?php if ($with_dto): ?>
22+
<?php if ($with_dto ?? false): ?>
2323
$this->runFunctionalTest(
2424
FunctionalTestData::withUrl('<?php echo $route['routePath']; ?>')
2525
->withMethod('<?php echo $route['httpMethod']; ?>')
@@ -39,7 +39,7 @@ public function testRoute<?php echo ucfirst(preg_replace_callback('~_([a-z0-9])~
3939
}
4040
<?php endforeach; ?>
4141

42-
<?php if ($with_dto): ?>
42+
<?php if ($with_dto ?? false): ?>
4343
public function assertStatusCodeLessThan500(string $method, string $url): \Closure
4444
{
4545
return static function (KernelBrowser $browser) use ($method, $url) {

src/PhpUnitVersions/PhpUnit12.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Pierstoval\SmokeTesting\PhpUnitVersions;
4+
5+
use Pierstoval\SmokeTesting\RoutesExtractor;
6+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
7+
use function sprintf;
8+
use Generator;
9+
use RuntimeException;
10+
use Symfony\Bundle\FrameworkBundle\Test\TestContainer;
11+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
12+
use Symfony\Component\Routing\Route;
13+
use Symfony\Component\Routing\RouterInterface;
14+
15+
abstract class PhpUnit12 extends WebTestCase
16+
{
17+
protected function beforeRequest(KernelBrowser $client, string $routeName, string $routePath): void
18+
{
19+
// To be overriden by the end-user.
20+
}
21+
22+
protected function afterRequest(KernelBrowser $client, string $routeName, string $routePath): void
23+
{
24+
// To be overriden by the end-user.
25+
}
26+
27+
protected function afterAssertion(KernelBrowser $client, string $routeName, string $routePath): void
28+
{
29+
// To be overriden by the end-user.
30+
}
31+
32+
/**
33+
* @return Generator<string, Route>
34+
*/
35+
public static function provideRouteCollection(): Generator
36+
{
37+
if (!is_a(self::class, WebTestCase::class, true)) {
38+
throw new RuntimeException(sprintf('The "%s" trait trait can only be used in an instance of "%s"', self::class, WebTestCase::class));
39+
}
40+
41+
static::bootKernel();
42+
43+
/** @var TestContainer $container */
44+
$container = static::getContainer();
45+
46+
/** @var RouterInterface $router */
47+
$router = $container->get(RouterInterface::class);
48+
49+
$routes = $router->getRouteCollection();
50+
51+
static::ensureKernelShutdown();
52+
53+
if (!$routes->count()) {
54+
throw new RuntimeException('No routes found in the application.');
55+
}
56+
57+
yield from RoutesExtractor::extractRoutesFromRouter($router);
58+
}
59+
60+
public function testRoutesDoNotReturnInternalError(string $httpMethod, string $routeName, string $routePath): void
61+
{
62+
$client = static::createClient();
63+
64+
$this->beforeRequest($client, $routeName, $routePath);
65+
66+
$client->request($httpMethod, $routePath);
67+
68+
$this->afterRequest($client, $routeName, $routePath);
69+
70+
$response = $client->getResponse();
71+
static::assertLessThan(
72+
500,
73+
$response->getStatusCode(),
74+
sprintf('Request "%s %s" for route "%s" returned an internal error.', $httpMethod, $routePath, $routeName),
75+
);
76+
77+
$this->afterAssertion($client, $routeName, $routePath);
78+
}
79+
}

0 commit comments

Comments
 (0)