-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Warn when #[DataProvider*] attributes are mixed with #[TestWith*] attributes
#6273
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
Merged
sebastianbergmann
merged 6 commits into
sebastianbergmann:main
from
staabm:warn-mixed-dataprovider
Jul 30, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3bdb5b4
Warn on mix of different data providers
staabm f04b058
more generic error message and additional tests
staabm a0aa4d2
Cover #[DataProviderExternal] variant in a test
staabm 0e7c5ee
Test same-type providers don't warn
staabm b405a9d
Test same-type attribute providers don't warn
staabm cda60ae
fix test description typos
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
tests/end-to-end/_files/TestDataProviderExternalAndDataProviderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of PHPUnit. | ||
| * | ||
| * (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| namespace PHPUnit\TestFixture; | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\DataProviderExternal; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class TestDataProviderExternalAndDataProviderTest extends TestCase | ||
| { | ||
| public static function externalProvider(): iterable | ||
| { | ||
| yield 'foo' => ['bar', 'baz']; | ||
| } | ||
|
|
||
| public static function provider(): iterable | ||
| { | ||
| yield 'foo2' => ['bar', 'baz']; | ||
| } | ||
|
|
||
| #[DataProvider('provider')] | ||
| #[DataProviderExternal(self::class, 'externalProvider')] | ||
| public function testWithDifferentProviderTypes($one, $two): void | ||
| { | ||
| $this->assertTrue(true); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
tests/end-to-end/_files/TestWithAndTestWithJsonDataProviderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of PHPUnit. | ||
| * | ||
| * (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| namespace PHPUnit\TestFixture; | ||
|
|
||
| use PHPUnit\Framework\Attributes\TestWith; | ||
| use PHPUnit\Framework\Attributes\TestWithJson; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class TestWithAndTestWithJsonDataProviderTest extends TestCase | ||
| { | ||
| public static function provider(): iterable | ||
| { | ||
| yield 'foo' => ['bar', 'baz']; | ||
| } | ||
|
|
||
| #[TestWith(['a', 'b'], 'foo')] | ||
| #[TestWithJson('[1, 2]')] | ||
| public function testWithDifferentProviderTypes($one, $two): void | ||
| { | ||
| $this->assertTrue(true); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
tests/end-to-end/_files/TestWithAttributeAndDataProviderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of PHPUnit. | ||
| * | ||
| * (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| namespace PHPUnit\TestFixture; | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\TestWith; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class TestWithAttributeAndDataProviderTest extends TestCase | ||
| { | ||
| public static function provider(): iterable | ||
| { | ||
| yield 'foo' => ['bar', 'baz']; | ||
| } | ||
|
|
||
| #[TestWith(['a', 'b'], 'foo')] | ||
| #[DataProvider('provider')] | ||
| public function testWithDifferentProviderTypes($one, $two): void | ||
| { | ||
| $this->assertTrue(true); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
tests/end-to-end/_files/TestWithAttributeAndExternalDataProviderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of PHPUnit. | ||
| * | ||
| * (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| namespace PHPUnit\TestFixture; | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProviderExternal; | ||
| use PHPUnit\Framework\Attributes\TestWith; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class TestWithAttributeAndExternalDataProviderTest extends TestCase | ||
| { | ||
| public static function provider(): iterable | ||
| { | ||
| yield 'foo' => ['bar', 'baz']; | ||
| } | ||
|
|
||
| #[TestWith(['a', 'b'], 'foo')] | ||
| #[DataProviderExternal(self::class, 'provider')] | ||
| public function testWithDifferentProviderTypes($one, $two): void | ||
| { | ||
| $this->assertTrue(true); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
tests/end-to-end/_files/TestWithJsonAttributeAndDataProviderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of PHPUnit. | ||
| * | ||
| * (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| namespace PHPUnit\TestFixture; | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\TestWithJson; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class TestWithJsonAttributeAndDataProviderTest extends TestCase | ||
| { | ||
| public static function provider(): iterable | ||
| { | ||
| yield 'foo' => ['bar', 'baz']; | ||
| } | ||
|
|
||
| #[TestWithJson('[1, 2, 3]')] | ||
| #[DataProvider('provider')] | ||
| public function testWithDifferentProviderTypes($one, $two): void | ||
| { | ||
| $this->assertTrue(true); | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
tests/end-to-end/metadata/mix-attribute-dataproviders.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --TEST-- | ||
| phpunit ../_files/TestDataProviderExternalAndDataProviderTest.php | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
| $_SERVER['argv'][] = '--do-not-cache-result'; | ||
| $_SERVER['argv'][] = '--no-configuration'; | ||
| $_SERVER['argv'][] = __DIR__ . '/../_files/TestDataProviderExternalAndDataProviderTest.php'; | ||
|
|
||
| require __DIR__ . '/../../bootstrap.php'; | ||
|
|
||
| (new PHPUnit\TextUI\Application)->run($_SERVER['argv']); | ||
| --EXPECTF-- | ||
| PHPUnit %s by Sebastian Bergmann and contributors. | ||
|
|
||
| Runtime: %s | ||
|
|
||
| .. 2 / 2 (100%) | ||
|
|
||
| Time: %s, Memory: %s | ||
|
|
||
| OK (2 tests, 2 assertions) |
21 changes: 21 additions & 0 deletions
21
tests/end-to-end/metadata/mix-test-with-dataproviders.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --TEST-- | ||
| phpunit ../_files/TestWithAndTestWithJsonDataProviderTest.php | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
| $_SERVER['argv'][] = '--do-not-cache-result'; | ||
| $_SERVER['argv'][] = '--no-configuration'; | ||
| $_SERVER['argv'][] = __DIR__ . '/../_files/TestWithAndTestWithJsonDataProviderTest.php'; | ||
|
|
||
| require __DIR__ . '/../../bootstrap.php'; | ||
|
|
||
| (new PHPUnit\TextUI\Application)->run($_SERVER['argv']); | ||
| --EXPECTF-- | ||
| PHPUnit %s by Sebastian Bergmann and contributors. | ||
|
|
||
| Runtime: %s | ||
|
|
||
| .. 2 / 2 (100%) | ||
|
|
||
| Time: %s, Memory: %s | ||
|
|
||
| OK (2 tests, 2 assertions) |
29 changes: 29 additions & 0 deletions
29
tests/end-to-end/metadata/warning-mix-dataprovider-test-with-json-types.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --TEST-- | ||
| phpunit ../_files/TestWithJsonAttributeAndDataProviderTest.php | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
| $_SERVER['argv'][] = '--do-not-cache-result'; | ||
| $_SERVER['argv'][] = '--no-configuration'; | ||
| $_SERVER['argv'][] = __DIR__ . '/../_files/TestWithJsonAttributeAndDataProviderTest.php'; | ||
|
|
||
| require __DIR__ . '/../../bootstrap.php'; | ||
|
|
||
| (new PHPUnit\TextUI\Application)->run($_SERVER['argv']); | ||
| --EXPECTF-- | ||
| PHPUnit %s by Sebastian Bergmann and contributors. | ||
|
|
||
| Runtime: %s | ||
|
|
||
| W 1 / 1 (100%) | ||
|
|
||
| Time: %s, Memory: %s | ||
|
|
||
| 1 test triggered 1 PHPUnit warning: | ||
|
|
||
| 1) PHPUnit\TestFixture\TestWithJsonAttributeAndDataProviderTest::testWithDifferentProviderTypes | ||
| Mixing #[DataProvider*] and #[TestWith*] attributes is not supported, only the data provided by #[DataProvider*] will be used | ||
|
|
||
| %sTestWithJsonAttributeAndDataProviderTest.php:%d | ||
|
|
||
| OK, but there were issues! | ||
| Tests: 1, Assertions: 1, PHPUnit Warnings: 1. |
29 changes: 29 additions & 0 deletions
29
tests/end-to-end/metadata/warning-mix-dataprovider-test-with-types.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --TEST-- | ||
| phpunit ../_files/TestWithAttributeAndDataProviderTest.php | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
| $_SERVER['argv'][] = '--do-not-cache-result'; | ||
| $_SERVER['argv'][] = '--no-configuration'; | ||
| $_SERVER['argv'][] = __DIR__ . '/../_files/TestWithAttributeAndDataProviderTest.php'; | ||
|
|
||
| require __DIR__ . '/../../bootstrap.php'; | ||
|
|
||
| (new PHPUnit\TextUI\Application)->run($_SERVER['argv']); | ||
| --EXPECTF-- | ||
| PHPUnit %s by Sebastian Bergmann and contributors. | ||
|
|
||
| Runtime: %s | ||
|
|
||
| W 1 / 1 (100%) | ||
|
|
||
| Time: %s, Memory: %s | ||
|
|
||
| 1 test triggered 1 PHPUnit warning: | ||
|
|
||
| 1) PHPUnit\TestFixture\TestWithAttributeAndDataProviderTest::testWithDifferentProviderTypes | ||
| Mixing #[DataProvider*] and #[TestWith*] attributes is not supported, only the data provided by #[DataProvider*] will be used | ||
|
|
||
| %sTestWithAttributeAndDataProviderTest.php:%d | ||
|
|
||
| OK, but there were issues! | ||
| Tests: 1, Assertions: 1, PHPUnit Warnings: 1. |
29 changes: 29 additions & 0 deletions
29
tests/end-to-end/metadata/warning-mix-external-dataprovider-with-types.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --TEST-- | ||
| phpunit ../_files/TestWithAttributeAndExternalDataProviderTest.php | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
| $_SERVER['argv'][] = '--do-not-cache-result'; | ||
| $_SERVER['argv'][] = '--no-configuration'; | ||
| $_SERVER['argv'][] = __DIR__ . '/../_files/TestWithAttributeAndExternalDataProviderTest.php'; | ||
|
|
||
| require __DIR__ . '/../../bootstrap.php'; | ||
|
|
||
| (new PHPUnit\TextUI\Application)->run($_SERVER['argv']); | ||
| --EXPECTF-- | ||
| PHPUnit %s by Sebastian Bergmann and contributors. | ||
|
|
||
| Runtime: %s | ||
|
|
||
| W 1 / 1 (100%) | ||
|
|
||
| Time: %s, Memory: %s | ||
|
|
||
| 1 test triggered 1 PHPUnit warning: | ||
|
|
||
| 1) PHPUnit\TestFixture\TestWithAttributeAndExternalDataProviderTest::testWithDifferentProviderTypes | ||
| Mixing #[DataProvider*] and #[TestWith*] attributes is not supported, only the data provided by #[DataProvider*] will be used | ||
|
|
||
| %sTestWithAttributeAndExternalDataProviderTest.php:%d | ||
|
|
||
| OK, but there were issues! | ||
| Tests: 1, Assertions: 1, PHPUnit Warnings: 1. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.