diff --git a/src/Event/Value/TestSuite/TestSuiteBuilder.php b/src/Event/Value/TestSuite/TestSuiteBuilder.php index a1a5bed3bd4..6fa48c48343 100644 --- a/src/Event/Value/TestSuite/TestSuiteBuilder.php +++ b/src/Event/Value/TestSuite/TestSuiteBuilder.php @@ -11,17 +11,13 @@ use function assert; use function class_exists; -use function explode; -use function method_exists; use PHPUnit\Event\Code\Test; use PHPUnit\Event\Code\TestCollection; use PHPUnit\Event\RuntimeException; -use PHPUnit\Framework\DataProviderTestSuite; use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestSuite as FrameworkTestSuite; use PHPUnit\Runner\PhptTestCase; use ReflectionClass; -use ReflectionMethod; /** * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit @@ -39,31 +35,6 @@ public static function from(FrameworkTestSuite $testSuite): TestSuite self::process($testSuite, $tests); - if ($testSuite instanceof DataProviderTestSuite) { - [$className, $methodName] = explode('::', $testSuite->name()); - - assert(class_exists($className)); - assert($methodName !== '' && method_exists($className, $methodName)); - - $reflector = new ReflectionMethod($className, $methodName); - - $file = $reflector->getFileName(); - $line = $reflector->getStartLine(); - - assert($file !== false); - assert($line !== false); - - return new TestSuiteForTestMethodWithDataProvider( - $testSuite->name(), - $testSuite->count(), - TestCollection::fromArray($tests), - $className, - $methodName, - $file, - $line, - ); - } - if ($testSuite->isForTestClass()) { $testClassName = $testSuite->name(); diff --git a/src/Framework/DataProviderTestSuite.php b/src/Framework/DataProviderTestSuite.php deleted file mode 100644 index c984859e945..00000000000 --- a/src/Framework/DataProviderTestSuite.php +++ /dev/null @@ -1,85 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\Framework; - -use function assert; -use function class_exists; -use function explode; -use PHPUnit\Framework\TestSize\TestSize; -use PHPUnit\Metadata\Api\Groups; - -/** - * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit - * - * @internal This class is not covered by the backward compatibility promise for PHPUnit - */ -final class DataProviderTestSuite extends TestSuite -{ - /** - * @var list - */ - private array $dependencies = []; - - /** - * @var ?non-empty-list - */ - private ?array $providedTests = null; - - /** - * @param list $dependencies - */ - public function setDependencies(array $dependencies): void - { - $this->dependencies = $dependencies; - - foreach ($this->tests() as $test) { - if (!$test instanceof TestCase) { - continue; - } - - $test->setDependencies($dependencies); - } - } - - /** - * @return non-empty-list - */ - public function provides(): array - { - if ($this->providedTests === null) { - $this->providedTests = [new ExecutionOrderDependency($this->name())]; - } - - return $this->providedTests; - } - - /** - * @return list - */ - public function requires(): array - { - // A DataProviderTestSuite does not have to traverse its child tests - // as these are inherited and cannot reference dataProvider rows directly - return $this->dependencies; - } - - /** - * Returns the size of each test created using the data provider(s). - */ - public function size(): TestSize - { - [$className, $methodName] = explode('::', $this->name()); - - assert(class_exists($className)); - assert($methodName !== ''); - - return (new Groups)->size($className, $methodName); - } -} diff --git a/src/Framework/TestBuilder.php b/src/Framework/TestBuilder.php index 5c3effc7a35..afc3d05cd23 100644 --- a/src/Framework/TestBuilder.php +++ b/src/Framework/TestBuilder.php @@ -9,10 +9,8 @@ */ namespace PHPUnit\Framework; -use function array_merge; use function assert; use PHPUnit\Metadata\Api\DataProvider; -use PHPUnit\Metadata\Api\Groups; use PHPUnit\Metadata\Api\Requirements; use PHPUnit\Metadata\BackupGlobals; use PHPUnit\Metadata\BackupStaticProperties; @@ -36,8 +34,10 @@ * @param list $groups * * @throws InvalidDataProviderException + * + * @return list */ - public function build(ReflectionClass $theClass, string $methodName, array $groups = []): Test + public function build(ReflectionClass $theClass, string $methodName, array $groups = []): array { $className = $theClass->getName(); @@ -48,7 +48,7 @@ public function build(ReflectionClass $theClass, string $methodName, array $grou } if ($data !== null) { - return $this->buildDataProviderTestSuite( + return $this->buildDataProviderTests( $methodName, $className, $data, @@ -70,7 +70,7 @@ public function build(ReflectionClass $theClass, string $methodName, array $grou $this->backupSettings($className, $methodName), ); - return $test; + return [$test]; } /** @@ -79,35 +79,30 @@ public function build(ReflectionClass $theClass, string $methodName, array $grou * @param array> $data * @param array{backupGlobals: ?bool, backupGlobalsExcludeList: list, backupStaticProperties: ?bool, backupStaticPropertiesExcludeList: array>} $backupSettings * @param list $groups + * + * @return list */ - private function buildDataProviderTestSuite(string $methodName, string $className, array $data, bool $runTestInSeparateProcess, ?bool $preserveGlobalState, bool $runClassInSeparateProcess, array $backupSettings, array $groups): DataProviderTestSuite + private function buildDataProviderTests(string $methodName, string $className, array $data, bool $runTestInSeparateProcess, ?bool $preserveGlobalState, bool $runClassInSeparateProcess, array $backupSettings, array $groups): array { - $dataProviderTestSuite = DataProviderTestSuite::empty( - $className . '::' . $methodName, - ); - - $groups = array_merge( - $groups, - (new Groups)->groups($className, $methodName), - ); + $tests = []; - foreach ($data as $_dataName => $_data) { - $_test = new $className($methodName); + foreach ($data as $dataName => $_data) { + $test = new $className($methodName); - $_test->setData($_dataName, $_data); + $test->setData($dataName, $_data); $this->configureTestCase( - $_test, + $test, $runTestInSeparateProcess, $preserveGlobalState, $runClassInSeparateProcess, $backupSettings, ); - $dataProviderTestSuite->addTest($_test, $groups); + $tests[] = $test; } - return $dataProviderTestSuite; + return $tests; } /** diff --git a/src/Framework/TestSuite.php b/src/Framework/TestSuite.php index daf44a98643..f27dc37471f 100644 --- a/src/Framework/TestSuite.php +++ b/src/Framework/TestSuite.php @@ -511,7 +511,7 @@ protected function addTestMethod(ReflectionClass $class, ReflectionMethod $metho assert(!empty($methodName)); try { - $test = (new TestBuilder)->build($class, $methodName, $groups); + $tests = (new TestBuilder)->build($class, $methodName, $groups); } catch (InvalidDataProviderException $e) { Event\Facade::emitter()->testTriggeredPhpunitError( new TestMethod( @@ -537,19 +537,21 @@ protected function addTestMethod(ReflectionClass $class, ReflectionMethod $metho return; } - if ($test instanceof TestCase || $test instanceof DataProviderTestSuite) { - $test->setDependencies( - Dependencies::dependencies($class->getName(), $methodName), + foreach ($tests as $test) { + if ($test instanceof TestCase) { + $test->setDependencies( + Dependencies::dependencies($class->getName(), $methodName), + ); + } + + $this->addTest( + $test, + array_merge( + $groups, + (new Groups)->groups($class->getName(), $methodName), + ), ); } - - $this->addTest( - $test, - array_merge( - $groups, - (new Groups)->groups($class->getName(), $methodName), - ), - ); } private function clearCaches(): void diff --git a/src/Runner/TestSuiteSorter.php b/src/Runner/TestSuiteSorter.php index b2af1e65a9d..5f14a3f031a 100644 --- a/src/Runner/TestSuiteSorter.php +++ b/src/Runner/TestSuiteSorter.php @@ -18,7 +18,6 @@ use function max; use function shuffle; use function usort; -use PHPUnit\Framework\DataProviderTestSuite; use PHPUnit\Framework\Reorderable; use PHPUnit\Framework\Test; use PHPUnit\Framework\TestCase; @@ -174,7 +173,7 @@ private function sort(TestSuite $suite, int $order, bool $resolveDependencies, i $suite->setTests($this->sortDefectsFirst($suite->tests())); } - if ($resolveDependencies && !($suite instanceof DataProviderTestSuite)) { + if ($resolveDependencies) { $tests = $suite->tests(); /** @noinspection PhpParamsInspection */ @@ -314,10 +313,10 @@ private function cmpDuration(Test $a, Test $b): int */ private function cmpSize(Test $a, Test $b): int { - $sizeA = ($a instanceof TestCase || $a instanceof DataProviderTestSuite) + $sizeA = $a instanceof TestCase ? $a->size()->asString() : 'unknown'; - $sizeB = ($b instanceof TestCase || $b instanceof DataProviderTestSuite) + $sizeB = $b instanceof TestCase ? $b->size()->asString() : 'unknown'; @@ -335,9 +334,9 @@ private function cmpSize(Test $a, Test $b): int * 3. If the test has dependencies but none left to do: mark done, start again from the top * 4. When we reach the end add any leftover tests to the end. These will be marked 'skipped' during execution. * - * @param array $tests + * @param array $tests * - * @return array + * @return array */ private function resolveDependencies(array $tests): array { diff --git a/tests/end-to-end/data-provider/log-junit-isolation.phpt b/tests/end-to-end/data-provider/log-junit-isolation.phpt index 3156f6d6cf8..f7e71cdf076 100644 --- a/tests/end-to-end/data-provider/log-junit-isolation.phpt +++ b/tests/end-to-end/data-provider/log-junit-isolation.phpt @@ -23,16 +23,14 @@ unlink($logfile); - - - - - PHPUnit\TestFixture\DataProviderTest::testAdd with data set #2%A + + + + PHPUnit\TestFixture\DataProviderTest::testAdd with data set #2%A Failed asserting that 2 matches expected 3. %A %s:%i - - - + + diff --git a/tests/end-to-end/data-provider/log-junit.phpt b/tests/end-to-end/data-provider/log-junit.phpt index f38cfb07ad7..fbfcdf1e2d4 100644 --- a/tests/end-to-end/data-provider/log-junit.phpt +++ b/tests/end-to-end/data-provider/log-junit.phpt @@ -26,30 +26,26 @@ unlink($logfile); - - - - - PHPUnit\TestFixture\DataProviderTest::testAdd with data set #2%A + + + + PHPUnit\TestFixture\DataProviderTest::testAdd with data set #2%A Failed asserting that 2 matches expected 3. %A %sDataProviderTest.php:%d - - - + + - - - - - PHPUnit\TestFixture\DataProviderWithStringKeysTest::testAdd with data set "1 + 1 = 3"%A + + + + PHPUnit\TestFixture\DataProviderWithStringKeysTest::testAdd with data set "1 + 1 = 3"%A Failed asserting that 2 matches expected 3. %A %sDataProviderWithStringKeysTest.php:%d - - - + + diff --git a/tests/end-to-end/event/data-provider-external.phpt b/tests/end-to-end/event/data-provider-external.phpt index 03ff9791292..2525f69d573 100644 --- a/tests/end-to-end/event/data-provider-external.phpt +++ b/tests/end-to-end/event/data-provider-external.phpt @@ -25,7 +25,6 @@ Test Runner Started Test Suite Sorted Test Runner Execution Started (2 tests) Test Suite Started (PHPUnit\TestFixture\Event\DataProviderExternalTest, 2 tests) -Test Suite Started (PHPUnit\TestFixture\Event\DataProviderExternalTest::testSuccess, 2 tests) Test Preparation Started (PHPUnit\TestFixture\Event\DataProviderExternalTest::testSuccess#0) Test Prepared (PHPUnit\TestFixture\Event\DataProviderExternalTest::testSuccess#0) Test Passed (PHPUnit\TestFixture\Event\DataProviderExternalTest::testSuccess#0) @@ -34,7 +33,6 @@ Test Preparation Started (PHPUnit\TestFixture\Event\DataProviderExternalTest::te Test Prepared (PHPUnit\TestFixture\Event\DataProviderExternalTest::testSuccess#1) Test Passed (PHPUnit\TestFixture\Event\DataProviderExternalTest::testSuccess#1) Test Finished (PHPUnit\TestFixture\Event\DataProviderExternalTest::testSuccess#1) -Test Suite Finished (PHPUnit\TestFixture\Event\DataProviderExternalTest::testSuccess, 2 tests) Test Suite Finished (PHPUnit\TestFixture\Event\DataProviderExternalTest, 2 tests) Test Runner Execution Finished Test Runner Finished diff --git a/tests/end-to-end/event/data-provider-invalid-argument-name.phpt b/tests/end-to-end/event/data-provider-invalid-argument-name.phpt index d5b8b946f5b..3341f4fa1d4 100644 --- a/tests/end-to-end/event/data-provider-invalid-argument-name.phpt +++ b/tests/end-to-end/event/data-provider-invalid-argument-name.phpt @@ -22,7 +22,6 @@ Test Runner Started Test Suite Sorted Test Runner Execution Started (2 tests) Test Suite Started (PHPUnit\TestFixture\Event\InvalidParameterNameDataProviderTest, 2 tests) -Test Suite Started (PHPUnit\TestFixture\Event\InvalidParameterNameDataProviderTest::testSuccess, 2 tests) Test Preparation Started (PHPUnit\TestFixture\Event\InvalidParameterNameDataProviderTest::testSuccess#0) Test Prepared (PHPUnit\TestFixture\Event\InvalidParameterNameDataProviderTest::testSuccess#0) Test Passed (PHPUnit\TestFixture\Event\InvalidParameterNameDataProviderTest::testSuccess#0) @@ -32,7 +31,6 @@ Test Prepared (PHPUnit\TestFixture\Event\InvalidParameterNameDataProviderTest::t Test Errored (PHPUnit\TestFixture\Event\InvalidParameterNameDataProviderTest::testSuccess#1) Unknown named parameter $value3 Test Finished (PHPUnit\TestFixture\Event\InvalidParameterNameDataProviderTest::testSuccess#1) -Test Suite Finished (PHPUnit\TestFixture\Event\InvalidParameterNameDataProviderTest::testSuccess, 2 tests) Test Suite Finished (PHPUnit\TestFixture\Event\InvalidParameterNameDataProviderTest, 2 tests) Test Runner Execution Finished Test Runner Finished diff --git a/tests/end-to-end/event/data-provider.phpt b/tests/end-to-end/event/data-provider.phpt index f50444ec77a..f1aa0c68e01 100644 --- a/tests/end-to-end/event/data-provider.phpt +++ b/tests/end-to-end/event/data-provider.phpt @@ -22,7 +22,6 @@ Test Runner Started Test Suite Sorted Test Runner Execution Started (2 tests) Test Suite Started (PHPUnit\TestFixture\Event\DataProviderTest, 2 tests) -Test Suite Started (PHPUnit\TestFixture\Event\DataProviderTest::testSuccess, 2 tests) Test Preparation Started (PHPUnit\TestFixture\Event\DataProviderTest::testSuccess#0) Test Prepared (PHPUnit\TestFixture\Event\DataProviderTest::testSuccess#0) Test Passed (PHPUnit\TestFixture\Event\DataProviderTest::testSuccess#0) @@ -31,7 +30,6 @@ Test Preparation Started (PHPUnit\TestFixture\Event\DataProviderTest::testSucces Test Prepared (PHPUnit\TestFixture\Event\DataProviderTest::testSuccess#1) Test Passed (PHPUnit\TestFixture\Event\DataProviderTest::testSuccess#1) Test Finished (PHPUnit\TestFixture\Event\DataProviderTest::testSuccess#1) -Test Suite Finished (PHPUnit\TestFixture\Event\DataProviderTest::testSuccess, 2 tests) Test Suite Finished (PHPUnit\TestFixture\Event\DataProviderTest, 2 tests) Test Runner Execution Finished Test Runner Finished diff --git a/tests/end-to-end/event/testwith-attribute.phpt b/tests/end-to-end/event/testwith-attribute.phpt index d5761afdc7c..29a2196566a 100644 --- a/tests/end-to-end/event/testwith-attribute.phpt +++ b/tests/end-to-end/event/testwith-attribute.phpt @@ -19,30 +19,22 @@ Test Runner Started Test Suite Sorted Test Runner Execution Started (4 tests) Test Suite Started (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest, 4 tests) -Test Suite Started (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testOne, 1 test) Test Preparation Started (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testOne#0) Test Prepared (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testOne#0) Test Passed (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testOne#0) Test Finished (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testOne#0) -Test Suite Finished (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testOne, 1 test) -Test Suite Started (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testOneWithName, 1 test) Test Preparation Started (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testOneWithName#Name1) Test Prepared (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testOneWithName#Name1) Test Passed (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testOneWithName#Name1) Test Finished (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testOneWithName#Name1) -Test Suite Finished (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testOneWithName, 1 test) -Test Suite Started (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testTwo, 1 test) Test Preparation Started (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testTwo#0) Test Prepared (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testTwo#0) Test Passed (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testTwo#0) Test Finished (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testTwo#0) -Test Suite Finished (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testTwo, 1 test) -Test Suite Started (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testTwoWithName, 1 test) Test Preparation Started (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testTwoWithName#Name2) Test Prepared (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testTwoWithName#Name2) Test Passed (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testTwoWithName#Name2) Test Finished (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testTwoWithName#Name2) -Test Suite Finished (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest::testTwoWithName, 1 test) Test Suite Finished (PHPUnit\TestFixture\Metadata\Attribute\TestWithTest, 4 tests) Test Runner Execution Finished Test Runner Finished diff --git a/tests/end-to-end/logging/teamcity-data-provider.phpt b/tests/end-to-end/logging/teamcity-data-provider.phpt index 9675591718d..cc388d2aaab 100644 --- a/tests/end-to-end/logging/teamcity-data-provider.phpt +++ b/tests/end-to-end/logging/teamcity-data-provider.phpt @@ -20,8 +20,6 @@ Runtime: %s ##teamcity[testSuiteStarted name='PHPUnit\TestFixture\TeamCity\DataProviderTest' locationHint='php_qn://%sDataProviderTest.php::\PHPUnit\TestFixture\TeamCity\DataProviderTest' flowId='%d'] -##teamcity[testSuiteStarted name='testOne' locationHint='php_qn://%sDataProviderTest.php::\PHPUnit\TestFixture\TeamCity\DataProviderTest::testOne' flowId='%d'] - ##teamcity[testStarted name='testOne with data set #0' locationHint='php_qn://%sDataProviderTest.php::\PHPUnit\TestFixture\TeamCity\DataProviderTest::testOne with data set #0' flowId='%d'] ##teamcity[testFinished name='testOne with data set #0' duration='%d' flowId='%d'] @@ -32,8 +30,6 @@ Runtime: %s ##teamcity[testFinished name='testOne with data set #1' duration='%d' flowId='%d'] -##teamcity[testSuiteFinished name='testOne' flowId='%d'] - ##teamcity[testSuiteFinished name='PHPUnit\TestFixture\TeamCity\DataProviderTest' flowId='%d'] Time: %s, Memory: %s diff --git a/tests/end-to-end/regression/5178.phpt b/tests/end-to-end/regression/5178.phpt index af550887ba3..9d8e8e4b7f2 100644 --- a/tests/end-to-end/regression/5178.phpt +++ b/tests/end-to-end/regression/5178.phpt @@ -1,5 +1,7 @@ --TEST-- https://github.com/sebastianbergmann/phpunit/issues/5178 +--XFAIL-- +Depending on tests that use provided data stops working when DataProviderTestSuite is removed --FILE-- build( + $tests = (new TestBuilder)->build( new ReflectionClass(TestWithoutIsolationAttributes::class), 'testOne', ); + $this->assertCount(1, $tests); + + $test = $tests[0]; + $this->assertInstanceOf(TestWithoutIsolationAttributes::class, $test); $test = $test->valueObjectForEvents(); @@ -43,11 +46,15 @@ public function testBuildsTestWithoutMetadataForIsolation(): void public function testBuildsTestWithClassLevelMetadataForIsolation(): void { - $test = (new TestBuilder)->build( + $tests = (new TestBuilder)->build( new ReflectionClass(TestWithClassLevelIsolationAttributes::class), 'testOne', ); + $this->assertCount(1, $tests); + + $test = $tests[0]; + $this->assertInstanceOf(TestWithClassLevelIsolationAttributes::class, $test); $test = $test->valueObjectForEvents(); @@ -62,11 +69,15 @@ public function testBuildsTestWithClassLevelMetadataForIsolation(): void public function testBuildsTestWithMethodLevelMetadataForIsolation(): void { - $test = (new TestBuilder)->build( + $tests = (new TestBuilder)->build( new ReflectionClass(TestWithMethodLevelIsolationAttributes::class), 'testOne', ); + $this->assertCount(1, $tests); + + $test = $tests[0]; + $this->assertInstanceOf(TestWithMethodLevelIsolationAttributes::class, $test); $test = $test->valueObjectForEvents(); @@ -80,14 +91,14 @@ public function testBuildsTestWithMethodLevelMetadataForIsolation(): void public function testBuildsTestWithDataProvider(): void { - $test = (new TestBuilder)->build( + $tests = (new TestBuilder)->build( new ReflectionClass(TestWithDataProvider::class), 'testOne', ); - $this->assertInstanceOf(DataProviderTestSuite::class, $test); + $this->assertCount(1, $tests); - $test = iterator_to_array($test)[0]; + $test = $tests[0]; $this->assertInstanceOf(TestWithDataProvider::class, $test);