Skip to content

Commit ab07345

Browse files
staabmsebastianbergmann
authored andcommitted
Fix order of tests which use data from data providers is not affected by test sorting
1 parent a97eacc commit ab07345

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

src/Runner/ResultCache/ResultCacheId.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHPUnit\Runner\ResultCache;
1111

1212
use PHPUnit\Event\Code\Test;
13+
use PHPUnit\Event\Code\TestMethod;
1314
use PHPUnit\Framework\Reorderable;
1415
use PHPUnit\Framework\TestCase;
1516

@@ -22,6 +23,10 @@
2223
{
2324
public static function fromTest(Test $test): self
2425
{
26+
if ($test instanceof TestMethod) {
27+
return new self($test->className() . '::' . $test->name());
28+
}
29+
2530
return new self($test->id());
2631
}
2732

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture;
11+
12+
use PHPUnit\Framework\Attributes\DataProvider;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class FaillingDataProviderTest extends TestCase
16+
{
17+
public static function provideData(): array
18+
{
19+
return [
20+
'good1' => [3, 1, 2],
21+
'good2' => [5, 2, 3],
22+
'fail1' => [10, 3, 4],
23+
'good3' => [10, 5, 5],
24+
'fail2' => [20, 3, 4],
25+
];
26+
}
27+
28+
public function testOne(): void
29+
{
30+
$this->assertTrue(true);
31+
}
32+
33+
#[DataProvider('provideData')]
34+
public function testWithProvider($sum, $summand, $summmand2): void
35+
{
36+
$this->assertSame($sum, $summand + $summmand2);
37+
}
38+
}

tests/unit/Runner/TestSuiteSorterTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPUnit\Framework\TestSuite;
1919
use PHPUnit\Runner\ResultCache\DefaultResultCache;
2020
use PHPUnit\Runner\ResultCache\ResultCacheId;
21+
use PHPUnit\TestFixture\FaillingDataProviderTest;
2122
use PHPUnit\TestFixture\MultiDependencyTest;
2223
use ReflectionClass;
2324

@@ -436,6 +437,74 @@ public static function suiteSorterOptionPermutationsProvider(): array
436437
return $data;
437438
}
438439

440+
/**
441+
* A data provider for testing defects execution reordering options based on FaillingDataProviderTest.
442+
*/
443+
public static function defectsSorterWithDataProviderProvider(): array
444+
{
445+
return [
446+
// The most simple situation should work as normal
447+
'default, no defects' => [
448+
TestSuiteSorter::ORDER_DEFAULT,
449+
self::IGNORE_DEPENDENCIES,
450+
[
451+
'testOne' => ['state' => TestStatus::success(), 'time' => 1],
452+
'testWithProvider with data set "good1"' => ['state' => TestStatus::success(), 'time' => 1],
453+
'testWithProvider with data set "good2"' => ['state' => TestStatus::success(), 'time' => 1],
454+
'testWithProvider with data set "good3"' => ['state' => TestStatus::success(), 'time' => 1],
455+
'testWithProvider with data set "fail1"' => ['state' => TestStatus::success(), 'time' => 1],
456+
'testWithProvider with data set "fail2"' => ['state' => TestStatus::success(), 'time' => 1],
457+
],
458+
[
459+
FaillingDataProviderTest::class . '::testWithProvider with data set "good1"',
460+
FaillingDataProviderTest::class . '::testWithProvider with data set "good2"',
461+
FaillingDataProviderTest::class . '::testWithProvider with data set "fail1"',
462+
FaillingDataProviderTest::class . '::testWithProvider with data set "good3"',
463+
FaillingDataProviderTest::class . '::testWithProvider with data set "fail2"',
464+
FaillingDataProviderTest::class . '::testOne',
465+
],
466+
],
467+
468+
// Running with an empty cache should not spook the TestSuiteSorter
469+
'default, empty result cache' => [
470+
TestSuiteSorter::ORDER_DEFAULT,
471+
self::IGNORE_DEPENDENCIES,
472+
[
473+
// empty result cache
474+
],
475+
[
476+
FaillingDataProviderTest::class . '::testWithProvider with data set "good1"',
477+
FaillingDataProviderTest::class . '::testWithProvider with data set "good2"',
478+
FaillingDataProviderTest::class . '::testWithProvider with data set "fail1"',
479+
FaillingDataProviderTest::class . '::testWithProvider with data set "good3"',
480+
FaillingDataProviderTest::class . '::testWithProvider with data set "fail2"',
481+
FaillingDataProviderTest::class . '::testOne',
482+
],
483+
],
484+
485+
'default, defects' => [
486+
TestSuiteSorter::ORDER_DEFAULT,
487+
self::IGNORE_DEPENDENCIES,
488+
[
489+
'testOne' => ['state' => TestStatus::success(), 'time' => 1],
490+
'testWithProvider with data set "good1"' => ['state' => TestStatus::success(), 'time' => 1],
491+
'testWithProvider with data set "good2"' => ['state' => TestStatus::success(), 'time' => 1],
492+
'testWithProvider with data set "good3"' => ['state' => TestStatus::success(), 'time' => 1],
493+
'testWithProvider with data set "fail1"' => ['state' => TestStatus::error(), 'time' => 1],
494+
'testWithProvider with data set "fail2"' => ['state' => TestStatus::error(), 'time' => 1],
495+
],
496+
[
497+
FaillingDataProviderTest::class . '::testWithProvider with data set "fail1"',
498+
FaillingDataProviderTest::class . '::testWithProvider with data set "fail2"',
499+
FaillingDataProviderTest::class . '::testWithProvider with data set "good1"',
500+
FaillingDataProviderTest::class . '::testWithProvider with data set "good2"',
501+
FaillingDataProviderTest::class . '::testWithProvider with data set "good3"',
502+
FaillingDataProviderTest::class . '::testOne',
503+
],
504+
],
505+
];
506+
}
507+
439508
public function testThrowsExceptionWhenUsingInvalidOrderOption(): void
440509
{
441510
$suite = TestSuite::empty('test suite name');
@@ -622,4 +691,23 @@ public function testSuiteSorterDefectsOptions(int $order, bool $resolveDependenc
622691

623692
$this->assertSame($expected, $sorter->getExecutionOrder());
624693
}
694+
695+
#[DataProvider('defectsSorterWithDataProviderProvider')]
696+
public function testSuiteSorterDefectsWithDataProviderTest(int $order, bool $resolveDependencies, array $runState, array $expected): void
697+
{
698+
$suite = TestSuite::empty('test suite name');
699+
$suite->addTestSuite(new ReflectionClass(FaillingDataProviderTest::class));
700+
701+
$cache = new DefaultResultCache;
702+
703+
foreach ($runState as $testName => $data) {
704+
$cache->setStatus(ResultCacheId::fromTestClassAndMethodName(FaillingDataProviderTest::class, $testName), $data['state']);
705+
$cache->setTime(ResultCacheId::fromTestClassAndMethodName(FaillingDataProviderTest::class, $testName), $data['time']);
706+
}
707+
708+
$sorter = new TestSuiteSorter($cache);
709+
$sorter->reorderTestsInSuite($suite, $order, $resolveDependencies, TestSuiteSorter::ORDER_DEFECTS_FIRST);
710+
711+
$this->assertSame($expected, $sorter->getExecutionOrder());
712+
}
625713
}

0 commit comments

Comments
 (0)