Skip to content

Commit 3439084

Browse files
committed
Update code for PHPUnit 11
1 parent da0b030 commit 3439084

7 files changed

+34
-42
lines changed

phpunit.xml.dist

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
3+
<!-- https://docs.phpunit.de/en/11.5/configuration.html -->
44
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
6-
backupGlobals="false"
6+
failOnNotice="true"
7+
failOnWarning="true"
78
colors="true"
89
bootstrap="tests/bootstrap.php"
9-
convertDeprecationsToExceptions="false"
1010
>
1111
<php>
12-
<ini name="display_errors" value="1" />
13-
<ini name="error_reporting" value="-1" />
14-
<server name="APP_ENV" value="test" force="true" />
15-
<server name="SHELL_VERBOSITY" value="-1" />
16-
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
17-
<server name="SYMFONY_PHPUNIT_VERSION" value="9.6" />
12+
<ini name="display_errors" value="1"/>
13+
<ini name="error_reporting" value="-1"/>
14+
<server name="APP_ENV" value="test" force="true"/>
15+
<server name="SHELL_VERBOSITY" value="-1"/>
1816
</php>
1917

2018
<testsuites>
@@ -23,22 +21,19 @@
2321
</testsuite>
2422
</testsuites>
2523

26-
<coverage processUncoveredFiles="true">
24+
<source>
2725
<include>
2826
<directory suffix=".php">src</directory>
2927
</include>
30-
</coverage>
3128

32-
<listeners>
33-
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
34-
</listeners>
29+
</source>
3530

3631
<extensions>
3732
<!-- it begins a database transaction before every testcase and rolls it back after
3833
the test finished, so tests can manipulate the database without affecting other tests -->
39-
<extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" />
34+
<bootstrap class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension"/>
4035

4136
<!-- Run `composer require symfony/panther` before enabling this extension -->
42-
<!-- <extension class="Symfony\Component\Panther\ServerExtension" /> -->
37+
<!-- <bootstrap class="Symfony\Component\Panther\ServerExtension" /> -->
4338
</extensions>
4439
</phpunit>

tests/Command/AbstractCommandTest.php tests/Command/AbstractCommandTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\Component\Console\Command\Command;
1717
use Symfony\Component\Console\Tester\CommandTester;
1818

19-
abstract class AbstractCommandTest extends KernelTestCase
19+
abstract class AbstractCommandTestCase extends KernelTestCase
2020
{
2121
/**
2222
* This helper method abstracts the boilerplate code needed to test the

tests/Command/AddUserCommandTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
use App\Command\AddUserCommand;
1515
use App\Repository\UserRepository;
16+
use PHPUnit\Framework\Attributes\DataProvider;
1617
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
1718

18-
final class AddUserCommandTest extends AbstractCommandTest
19+
final class AddUserCommandTest extends AbstractCommandTestCase
1920
{
2021
/**
2122
* @var string[]
@@ -35,11 +36,10 @@ protected function setUp(): void
3536
}
3637

3738
/**
38-
* @dataProvider isAdminDataProvider
39-
*
4039
* This test provides all the arguments required by the command, so the
4140
* command runs non-interactively and it won't ask for any argument.
4241
*/
42+
#[DataProvider('isAdminDataProvider')]
4343
public function testCreateUserNonInteractive(bool $isAdmin): void
4444
{
4545
$input = $this->userData;
@@ -52,13 +52,13 @@ public function testCreateUserNonInteractive(bool $isAdmin): void
5252
}
5353

5454
/**
55-
* @dataProvider isAdminDataProvider
56-
*
5755
* This test doesn't provide all the arguments required by the command, so
5856
* the command runs interactively and it will ask for the value of the missing
5957
* arguments.
60-
* See https://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input
58+
*
59+
* @see https://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input
6160
*/
61+
#[DataProvider('isAdminDataProvider')]
6262
public function testCreateUserInteractive(bool $isAdmin): void
6363
{
6464
$this->executeCommand(
@@ -76,7 +76,7 @@ public function testCreateUserInteractive(bool $isAdmin): void
7676
* This is used to execute the same test twice: first for normal users
7777
* (isAdmin = false) and then for admin users (isAdmin = true).
7878
*/
79-
public function isAdminDataProvider(): \Generator
79+
public static function isAdminDataProvider(): \Generator
8080
{
8181
yield [false];
8282
yield [true];

tests/Command/ListUsersCommandTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
namespace App\Tests\Command;
1313

1414
use App\Command\ListUsersCommand;
15+
use PHPUnit\Framework\Attributes\DataProvider;
1516

16-
final class ListUsersCommandTest extends AbstractCommandTest
17+
final class ListUsersCommandTest extends AbstractCommandTestCase
1718
{
1819
/**
19-
* @dataProvider maxResultsProvider
20-
*
2120
* This test verifies the amount of data is right according to the given parameter max results.
2221
*/
22+
#[DataProvider('maxResultsProvider')]
2323
public function testListUsers(int $maxResults): void
2424
{
2525
$tester = $this->executeCommand(
@@ -30,7 +30,7 @@ public function testListUsers(int $maxResults): void
3030
$this->assertSame($emptyDisplayLines + $maxResults, mb_substr_count($tester->getDisplay(), "\n"));
3131
}
3232

33-
public function maxResultsProvider(): \Generator
33+
public static function maxResultsProvider(): \Generator
3434
{
3535
yield [1];
3636
yield [2];

tests/Controller/Admin/BlogControllerTest.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use App\Entity\User;
1515
use App\Repository\PostRepository;
1616
use App\Repository\UserRepository;
17+
use PHPUnit\Framework\Attributes\DataProvider;
1718
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1819
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1920
use Symfony\Component\HttpFoundation\Response;
@@ -49,9 +50,7 @@ protected function setUp(): void
4950
$this->client->loginUser($user);
5051
}
5152

52-
/**
53-
* @dataProvider getUrlsForRegularUsers
54-
*/
53+
#[DataProvider('getUrlsForRegularUsers')]
5554
public function testAccessDeniedForRegularUsers(string $httpMethod, string $url): void
5655
{
5756
$this->client->getCookieJar()->clear();
@@ -67,7 +66,7 @@ public function testAccessDeniedForRegularUsers(string $httpMethod, string $url)
6766
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
6867
}
6968

70-
public function getUrlsForRegularUsers(): \Generator
69+
public static function getUrlsForRegularUsers(): \Generator
7170
{
7271
yield ['GET', '/en/admin/post/'];
7372
yield ['GET', '/en/admin/post/1'];

tests/Controller/DefaultControllerTest.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use App\Entity\Post;
1515
use Doctrine\Bundle\DoctrineBundle\Registry;
16+
use PHPUnit\Framework\Attributes\DataProvider;
1617
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1718
use Symfony\Component\HttpFoundation\Response;
1819

@@ -32,9 +33,8 @@ final class DefaultControllerTest extends WebTestCase
3233
* PHPUnit's data providers allow to execute the same tests repeated times
3334
* using a different set of data each time.
3435
* See https://symfony.com/doc/current/testing.html#testing-against-different-sets-of-data.
35-
*
36-
* @dataProvider getPublicUrls
3736
*/
37+
#[DataProvider('getPublicUrls')]
3838
public function testPublicUrls(string $url): void
3939
{
4040
$client = static::createClient();
@@ -69,9 +69,8 @@ public function testPublicBlogPost(): void
6969
* The application contains a lot of secure URLs which shouldn't be
7070
* publicly accessible. This tests ensures that whenever a user tries to
7171
* access one of those pages, a redirection to the login form is performed.
72-
*
73-
* @dataProvider getSecureUrls
7472
*/
73+
#[DataProvider('getSecureUrls')]
7574
public function testSecureUrls(string $url): void
7675
{
7776
$client = static::createClient();
@@ -84,14 +83,14 @@ public function testSecureUrls(string $url): void
8483
);
8584
}
8685

87-
public function getPublicUrls(): \Generator
86+
public static function getPublicUrls(): \Generator
8887
{
8988
yield ['/'];
9089
yield ['/en/blog/'];
9190
yield ['/en/login'];
9291
}
9392

94-
public function getSecureUrls(): \Generator
93+
public static function getSecureUrls(): \Generator
9594
{
9695
yield ['/en/admin/post/'];
9796
yield ['/en/admin/post/new'];

tests/Controller/UserControllerTest.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use App\Entity\User;
1515
use App\Repository\UserRepository;
16+
use PHPUnit\Framework\Attributes\DataProvider;
1617
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1718
use Symfony\Component\HttpFoundation\Response;
1819

@@ -33,9 +34,7 @@
3334
*/
3435
final class UserControllerTest extends WebTestCase
3536
{
36-
/**
37-
* @dataProvider getUrlsForAnonymousUsers
38-
*/
37+
#[DataProvider('getUrlsForAnonymousUsers')]
3938
public function testAccessDeniedForAnonymousUsers(string $httpMethod, string $url): void
4039
{
4140
$client = static::createClient();
@@ -48,7 +47,7 @@ public function testAccessDeniedForAnonymousUsers(string $httpMethod, string $ur
4847
);
4948
}
5049

51-
public function getUrlsForAnonymousUsers(): \Generator
50+
public static function getUrlsForAnonymousUsers(): \Generator
5251
{
5352
yield ['GET', '/en/profile/edit'];
5453
yield ['GET', '/en/profile/change-password'];

0 commit comments

Comments
 (0)