Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to PHPUnit 11 #1549

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -97,8 +97,5 @@ jobs:
- name: Install dependencies
run: composer install --ansi --no-interaction --no-progress

- name: Install PHPUnit
run: vendor/bin/simple-phpunit install

- name: Run PHPStan
run: vendor/bin/phpstan analyze --no-progress
11 changes: 1 addition & 10 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -8,9 +8,6 @@ on:

env:
fail-fast: true
PHPUNIT_FLAGS: "-v"
SYMFONY_PHPUNIT_DIR: "$HOME/symfony-bridge/.phpunit"
SYMFONY_DEPRECATIONS_HELPER: 'max[indirect]=52'

permissions:
contents: read
@@ -55,11 +52,5 @@ jobs:
php bin/console sass:build
php bin/console asset-map:compile
- name: "Install PHPUnit"
run: vendor/bin/simple-phpunit install

- name: "PHPUnit version"
run: vendor/bin/simple-phpunit --version

- name: "Run tests"
run: vendor/bin/simple-phpunit ${{ env.PHPUNIT_FLAGS }}
run: vendor/bin/phpunit
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -26,3 +26,8 @@
###> phpstan/phpstan ###
phpstan.neon
###< phpstan/phpstan ###

###> phpunit/phpunit ###
/phpunit.xml
.phpunit.result.cache
###< phpunit/phpunit ###
23 changes: 0 additions & 23 deletions bin/phpunit

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -57,11 +57,11 @@
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-doctrine": "^2.0",
"phpstan/phpstan-symfony": "^2.0",
"phpunit/phpunit": "^11.3",
"symfony/browser-kit": "^7",
"symfony/css-selector": "^7",
"symfony/debug-bundle": "^7",
"symfony/maker-bundle": "^1.36",
"symfony/phpunit-bridge": "^7",
"symfony/stopwatch": "^7",
"symfony/web-profiler-bundle": "^7",
"twbs/bootstrap": "^5"
1,948 changes: 1,748 additions & 200 deletions composer.lock

Large diffs are not rendered by default.

35 changes: 19 additions & 16 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<?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"
failOnDeprecation="true"
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>
@@ -23,22 +22,26 @@
</testsuite>
</testsuites>

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

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
<deprecationTrigger>
<function>trigger_deprecation</function>
<method>Doctrine\Deprecations\Deprecation::trigger</method>
<method>Doctrine\Deprecations\Deprecation::delegateTriggerToBackend</method>
</deprecationTrigger>
</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>
14 changes: 14 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
@@ -62,6 +62,20 @@
"phpstan.dist.neon"
]
},
"phpunit/phpunit": {
"version": "11.5",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "9.6",
"ref": "7364a21d87e658eb363c5020c072ecfdc12e2326"
},
"files": [
".env.test",
"phpunit.xml.dist",
"tests/bootstrap.php"
]
},
"symfony/apache-pack": {
"version": "1.0",
"recipe": {
Original file line number Diff line number Diff line change
@@ -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
14 changes: 7 additions & 7 deletions tests/Command/AddUserCommandTest.php
Original file line number Diff line number Diff line change
@@ -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[]
@@ -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;
@@ -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(
@@ -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];
8 changes: 4 additions & 4 deletions tests/Command/ListUsersCommandTest.php
Original file line number Diff line number Diff line change
@@ -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(
@@ -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];
7 changes: 3 additions & 4 deletions tests/Controller/Admin/BlogControllerTest.php
Original file line number Diff line number Diff line change
@@ -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;
@@ -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();
@@ -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'];
11 changes: 5 additions & 6 deletions tests/Controller/DefaultControllerTest.php
Original file line number Diff line number Diff line change
@@ -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;

@@ -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();
@@ -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();
@@ -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'];
7 changes: 3 additions & 4 deletions tests/Controller/UserControllerTest.php
Original file line number Diff line number Diff line change
@@ -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;

@@ -33,9 +34,7 @@
*/
final class UserControllerTest extends WebTestCase
{
/**
* @dataProvider getUrlsForAnonymousUsers
*/
#[DataProvider('getUrlsForAnonymousUsers')]
public function testAccessDeniedForAnonymousUsers(string $httpMethod, string $url): void
{
$client = static::createClient();
@@ -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'];