Skip to content

[Testing] Update tests to use attributes for data providers #20237

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions best_practices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,12 @@ checks that all application URLs load successfully::
// tests/ApplicationAvailabilityFunctionalTest.php
namespace App\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ApplicationAvailabilityFunctionalTest extends WebTestCase
{
/**
* @dataProvider urlProvider
*/
#[DataProvider('urlProvider')]
public function testPageIsSuccessful($url): void
{
$client = self::createClient();
Expand All @@ -433,6 +432,11 @@ checks that all application URLs load successfully::
}
}

.. note::

The ``#[DataProvider]`` attribute is available in PHPUnit 10 and later versions.
In previous versions, use the ``@dataProvider`` PHPDoc annotation.

Add this test while creating your application because it requires little effort
and checks that none of your pages returns an error. Later, you'll add more
specific tests for each page.
Expand Down
5 changes: 2 additions & 3 deletions validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ class to simplify writing unit tests for your custom constraints::

use App\Validator\ContainsAlphanumeric;
use App\Validator\ContainsAlphanumericValidator;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

Expand All @@ -530,9 +531,7 @@ class to simplify writing unit tests for your custom constraints::
$this->assertNoViolation();
}

/**
* @dataProvider provideInvalidConstraints
*/
#[DataProvider('provideInvalidConstraints')]
public function testTrueIsInvalid(ContainsAlphanumeric $constraint): void
{
$this->validator->validate('...', $constraint);
Expand Down
Loading