Skip to content

Commit

Permalink
Update code for PHPUnit 11
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Dec 13, 2024
1 parent 95c1cc0 commit f714211
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 42 deletions.
27 changes: 11 additions & 16 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<!-- https://docs.phpunit.de/en/11.5/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
failOnNotice="true"
failOnWarning="true"
colors="true"
bootstrap="tests/bootstrap.php"
convertDeprecationsToExceptions="false"
>
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="9.6" />
<ini name="display_errors" value="1"/>
<ini name="error_reporting" value="-1"/>
<server name="APP_ENV" value="test" force="true"/>
<server name="SHELL_VERBOSITY" value="-1"/>
</php>

<testsuites>
Expand All @@ -23,22 +21,19 @@
</testsuite>
</testsuites>

<coverage processUncoveredFiles="true">
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
</source>

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

<!-- Run `composer require symfony/panther` before enabling this extension -->
<!-- <extension class="Symfony\Component\Panther\ServerExtension" /> -->
<!-- <bootstrap class="Symfony\Component\Panther\ServerExtension" /> -->
</extensions>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;

abstract class AbstractCommandTest extends KernelTestCase
abstract class AbstractCommandTestCase extends KernelTestCase
{
/**
* This helper method abstracts the boilerplate code needed to test the
Expand Down
14 changes: 7 additions & 7 deletions tests/Command/AddUserCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@

use App\Command\AddUserCommand;
use App\Repository\UserRepository;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

final class AddUserCommandTest extends AbstractCommandTest
final class AddUserCommandTest extends AbstractCommandTestCase
{
/**
* @var string[]
Expand All @@ -35,11 +36,10 @@ protected function setUp(): void
}

/**
* @dataProvider isAdminDataProvider
*
* This test provides all the arguments required by the command, so the
* command runs non-interactively and it won't ask for any argument.
*/
#[DataProvider('isAdminDataProvider')]
public function testCreateUserNonInteractive(bool $isAdmin): void
{
$input = $this->userData;
Expand All @@ -52,13 +52,13 @@ public function testCreateUserNonInteractive(bool $isAdmin): void
}

/**
* @dataProvider isAdminDataProvider
*
* This test doesn't provide all the arguments required by the command, so
* the command runs interactively and it will ask for the value of the missing
* arguments.
* See https://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input
*
* @see https://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input
*/
#[DataProvider('isAdminDataProvider')]
public function testCreateUserInteractive(bool $isAdmin): void
{
$this->executeCommand(
Expand All @@ -76,7 +76,7 @@ public function testCreateUserInteractive(bool $isAdmin): void
* This is used to execute the same test twice: first for normal users
* (isAdmin = false) and then for admin users (isAdmin = true).
*/
public function isAdminDataProvider(): \Generator
public static function isAdminDataProvider(): \Generator
{
yield [false];
yield [true];
Expand Down
8 changes: 4 additions & 4 deletions tests/Command/ListUsersCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
namespace App\Tests\Command;

use App\Command\ListUsersCommand;
use PHPUnit\Framework\Attributes\DataProvider;

final class ListUsersCommandTest extends AbstractCommandTest
final class ListUsersCommandTest extends AbstractCommandTestCase
{
/**
* @dataProvider maxResultsProvider
*
* This test verifies the amount of data is right according to the given parameter max results.
*/
#[DataProvider('maxResultsProvider')]
public function testListUsers(int $maxResults): void
{
$tester = $this->executeCommand(
Expand All @@ -30,7 +30,7 @@ public function testListUsers(int $maxResults): void
$this->assertSame($emptyDisplayLines + $maxResults, mb_substr_count($tester->getDisplay(), "\n"));
}

public function maxResultsProvider(): \Generator
public static function maxResultsProvider(): \Generator
{
yield [1];
yield [2];
Expand Down
7 changes: 3 additions & 4 deletions tests/Controller/Admin/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Entity\User;
use App\Repository\PostRepository;
use App\Repository\UserRepository;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -49,9 +50,7 @@ protected function setUp(): void
$this->client->loginUser($user);
}

/**
* @dataProvider getUrlsForRegularUsers
*/
#[DataProvider('getUrlsForRegularUsers')]
public function testAccessDeniedForRegularUsers(string $httpMethod, string $url): void
{
$this->client->getCookieJar()->clear();
Expand All @@ -67,7 +66,7 @@ public function testAccessDeniedForRegularUsers(string $httpMethod, string $url)
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
}

public function getUrlsForRegularUsers(): \Generator
public static function getUrlsForRegularUsers(): \Generator
{
yield ['GET', '/en/admin/post/'];
yield ['GET', '/en/admin/post/1'];
Expand Down
11 changes: 5 additions & 6 deletions tests/Controller/DefaultControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use App\Entity\Post;
use Doctrine\Bundle\DoctrineBundle\Registry;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -32,9 +33,8 @@ final class DefaultControllerTest extends WebTestCase
* PHPUnit's data providers allow to execute the same tests repeated times
* using a different set of data each time.
* See https://symfony.com/doc/current/testing.html#testing-against-different-sets-of-data.
*
* @dataProvider getPublicUrls
*/
#[DataProvider('getPublicUrls')]
public function testPublicUrls(string $url): void
{
$client = static::createClient();
Expand Down Expand Up @@ -69,9 +69,8 @@ public function testPublicBlogPost(): void
* The application contains a lot of secure URLs which shouldn't be
* publicly accessible. This tests ensures that whenever a user tries to
* access one of those pages, a redirection to the login form is performed.
*
* @dataProvider getSecureUrls
*/
#[DataProvider('getSecureUrls')]
public function testSecureUrls(string $url): void
{
$client = static::createClient();
Expand All @@ -84,14 +83,14 @@ public function testSecureUrls(string $url): void
);
}

public function getPublicUrls(): \Generator
public static function getPublicUrls(): \Generator
{
yield ['/'];
yield ['/en/blog/'];
yield ['/en/login'];
}

public function getSecureUrls(): \Generator
public static function getSecureUrls(): \Generator
{
yield ['/en/admin/post/'];
yield ['/en/admin/post/new'];
Expand Down
7 changes: 3 additions & 4 deletions tests/Controller/UserControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use App\Entity\User;
use App\Repository\UserRepository;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -33,9 +34,7 @@
*/
final class UserControllerTest extends WebTestCase
{
/**
* @dataProvider getUrlsForAnonymousUsers
*/
#[DataProvider('getUrlsForAnonymousUsers')]
public function testAccessDeniedForAnonymousUsers(string $httpMethod, string $url): void
{
$client = static::createClient();
Expand All @@ -48,7 +47,7 @@ public function testAccessDeniedForAnonymousUsers(string $httpMethod, string $ur
);
}

public function getUrlsForAnonymousUsers(): \Generator
public static function getUrlsForAnonymousUsers(): \Generator
{
yield ['GET', '/en/profile/edit'];
yield ['GET', '/en/profile/change-password'];
Expand Down

0 comments on commit f714211

Please sign in to comment.