Skip to content

Commit 6bab737

Browse files
staabmsebastianbergmann
authored andcommitted
Warn on mix of different data providers
1 parent 172492c commit 6bab737

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/Metadata/Api/DataProvider.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ public function providedData(string $className, string $methodName): ?array
5252
}
5353

5454
if ($dataProvider->isNotEmpty()) {
55+
if ($testWith->isNotEmpty()) {
56+
$method = new ReflectionMethod($className, $methodName);
57+
58+
Event\Facade::emitter()->testTriggeredPhpunitWarning(
59+
new TestMethod(
60+
$className,
61+
$methodName,
62+
$method->getFileName(),
63+
$method->getStartLine(),
64+
Event\Code\TestDoxBuilder::fromClassNameAndMethodName(
65+
$className,
66+
$methodName,
67+
),
68+
MetadataCollection::fromArray([]),
69+
Event\TestData\TestDataCollection::fromArray([]),
70+
),
71+
'Mixing #[DataProvider] and #[TestWith] attributes is not supported, only the data provided by #[DataProvider] will be used',
72+
);
73+
}
5574
$data = $this->dataProvidedByMethods($className, $methodName, $dataProvider);
5675
} else {
5776
$data = $this->dataProvidedByMetadata($testWith);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of PHPUnit.
5+
*
6+
* (c) Sebastian Bergmann <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace PHPUnit\TestFixture;
12+
13+
use PHPUnit\Framework\Attributes\DataProvider;
14+
use PHPUnit\Framework\Attributes\TestWith;
15+
use PHPUnit\Framework\TestCase;
16+
17+
final class TestWithAttributeAndDataProviderTest extends TestCase
18+
{
19+
public static function provider(): iterable
20+
{
21+
yield 'foo' => ['bar', 'baz'];
22+
}
23+
24+
#[TestWith(['a', 'b'], 'foo')]
25+
#[DataProvider('provider')]
26+
public function testWithDifferentProviderTypes($one, $two): void
27+
{
28+
$this->assertTrue(true);
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
phpunit ../_files/TestWithAttributeAndDataProviderTest.php
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = __DIR__ . '/../_files/TestWithAttributeAndDataProviderTest.php';
8+
9+
require __DIR__ . '/../../bootstrap.php';
10+
11+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
12+
--EXPECTF--
13+
PHPUnit %s by Sebastian Bergmann and contributors.
14+
15+
Runtime: %s
16+
17+
W 1 / 1 (100%)
18+
19+
Time: %s, Memory: %s
20+
21+
1 test triggered 1 PHPUnit warning:
22+
23+
1) PHPUnit\TestFixture\TestWithAttributeAndDataProviderTest::testWithDifferentProviderTypes
24+
Mixing #[DataProvider] and #[TestWith] attributes is not supported, only the data provided by #[DataProvider] will be used
25+
26+
%sTestWithAttributeAndDataProviderTest.php:%d
27+
28+
OK, but there were issues!
29+
Tests: 1, Assertions: 1, PHPUnit Warnings: 1.

0 commit comments

Comments
 (0)