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 2 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 @@
// 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 @@
}
}

.. note::

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

Check failure on line 438 in best_practices.rst

View workflow job for this annotation

GitHub Actions / Lint (DOCtor-RST)

Please replace "PHPdoc" with "PHPDoc"

Check failure on line 438 in best_practices.rst

View workflow job for this annotation

GitHub Actions / Lint (DOCtor-RST)

Please replace "PHPdoc" with "PHPDoc"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In previous versions, use the ``@dataProvider`` PHPdoc annotation.
In previous versions, use the ``@dataProvider`` PHPDoc annotation.

To make DOCtor-RST happy :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Fixed. Thanks.


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