Skip to content

Commit 1f5c46c

Browse files
committed
Repeat phpt tests
1 parent ec603b7 commit 1f5c46c

16 files changed

+173
-24
lines changed

src/Framework/RepeatTestSuite.php

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
namespace PHPUnit\Framework;
1111

12+
use PHPUnit\Event\Facade as EventFacade;
13+
use PHPUnit\Runner\Phpt\TestCase as PhptTestCase;
1214
use PHPUnit\TestRunner\TestResult\PassedTests;
1315
use PHPUnit\Event;
1416

@@ -20,14 +22,14 @@
2022
final class RepeatTestSuite implements Test, Reorderable
2123
{
2224
/**
23-
* @var non-empty-list<TestCase>
25+
* @var non-empty-list<TestCase>|non-empty-list<PhptTestCase>
2426
*/
2527
private array $tests;
2628

2729
/**
2830
* @param positive-int $times
2931
*/
30-
public function __construct(TestCase $test, int $times)
32+
public function __construct(TestCase|PhptTestCase $test, int $times)
3133
{
3234
$tests = [];
3335
for ($i = 0; $i < $times; $i++) {
@@ -43,6 +45,40 @@ public function count(): int
4345
}
4446

4547
public function run(): void
48+
{
49+
if ($this->isPhptTestCase()) {
50+
$this->runPhptTestCase();
51+
} else {
52+
$this->runTestCase();
53+
}
54+
}
55+
56+
public function sortId(): string
57+
{
58+
return $this->tests[0]->sortId();
59+
}
60+
61+
public function provides(): array
62+
{
63+
return $this->tests[0]->provides();
64+
}
65+
66+
public function requires(): array
67+
{
68+
return $this->tests[0]->requires();
69+
}
70+
71+
public function nameWithDataSet(): string
72+
{
73+
return $this->tests[0]->nameWithDataSet();
74+
}
75+
76+
public function valueObjectForEvents(): Event\Code\TestMethod|Event\Code\Phpt
77+
{
78+
return $this->tests[0]->valueObjectForEvents();
79+
}
80+
81+
private function runTestCase(): void
4682
{
4783
$defectOccurred = false;
4884

@@ -63,28 +99,33 @@ public function run(): void
6399
}
64100
}
65101

66-
public function sortId(): string
102+
private function runPhptTestCase(): void
67103
{
68-
return $this->tests[0]->sortId();
69-
}
104+
$defectOccurred = false;
70105

71-
public function provides(): array
72-
{
73-
return $this->tests[0]->provides();
74-
}
106+
foreach ($this->tests as $test) {
107+
if ($defectOccurred) {
108+
EventFacade::emitter()->testSkipped(
109+
$this->valueObjectForEvents(),
110+
'Test repetition failure',
111+
);
75112

76-
public function requires(): array
77-
{
78-
return $this->tests[0]->requires();
79-
}
113+
continue;
114+
}
80115

81-
public function nameWithDataSet(): string
82-
{
83-
return $this->tests[0]->nameWithDataSet();
116+
$test->run();
117+
118+
if (!$test->passed()) {
119+
$defectOccurred = true;
120+
}
121+
}
84122
}
85123

86-
public function valueObjectForEvents(): Event\Code\TestMethod
124+
/**
125+
* @phpstan-assert-if-true non-empty-list<PhptTestCase> $this->tests
126+
*/
127+
private function isPhptTestCase(): bool
87128
{
88-
return $this->tests[0]->valueObjectForEvents();
129+
return $this->tests[0] instanceof PhptTestCase;
89130
}
90131
}

src/Framework/TestSuite.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function addTest(Test $test, array $groups = [], int $repeat = 1): void
158158

159159
assert($test instanceof TestCase || $test instanceof PhptTestCase);
160160

161-
if ($test instanceof PhptTestCase || $repeat === 1) {
161+
if ($repeat === 1) {
162162
$this->tests[] = $test;
163163
} else {
164164
$this->tests[] = new RepeatTestSuite($test, $repeat);
@@ -231,11 +231,11 @@ public function addTestSuite(ReflectionClass $testClass, array $groups = [], int
231231
*
232232
* @throws Exception
233233
*/
234-
public function addTestFile(string $filename, array $groups = []): void
234+
public function addTestFile(string $filename, array $groups = [], int $repeat = 1): void
235235
{
236236
try {
237237
if (str_ends_with($filename, '.phpt') && is_file($filename)) {
238-
$this->addTest(new PhptTestCase($filename));
238+
$this->addTest(new PhptTestCase($filename), [], $repeat);
239239
} else {
240240
$this->addTestSuite(
241241
(new TestSuiteLoader)->load($filename),

src/Runner/Phpt/TestCase.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@
7070
*
7171
* @see https://qa.php.net/phpt_details.php
7272
*/
73-
final readonly class TestCase implements Reorderable, SelfDescribing, Test
73+
final class TestCase implements Reorderable, SelfDescribing, Test
7474
{
7575
/**
7676
* @var non-empty-string
7777
*/
78-
private string $filename;
78+
private readonly string $filename;
79+
80+
private bool $passed = false;
7981

8082
/**
8183
* @param non-empty-string $filename
@@ -242,6 +244,8 @@ public function run(): void
242244
$emitter->testPassed($this->valueObjectForEvents());
243245
}
244246

247+
$this->passed = $passed;
248+
245249
$this->runClean($sections, CodeCoverage::instance()->isActive());
246250

247251
$emitter->testFinished($this->valueObjectForEvents(), 1);
@@ -255,6 +259,11 @@ public function getName(): string
255259
return $this->toString();
256260
}
257261

262+
public function passed(): bool
263+
{
264+
return $this->passed;
265+
}
266+
258267
/**
259268
* Returns a string representation of the test case.
260269
*/

src/TextUI/Configuration/TestSuiteBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private function testSuiteFromPath(string $path, array $suffixes, int $repeat, ?
100100
$suite = TestSuite::empty($path);
101101
}
102102

103-
$suite->addTestFile($path);
103+
$suite->addTestFile($path, [], $repeat);
104104

105105
return $suite;
106106
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
Repeat option
3+
--FILE--
4+
<?php
5+
6+
echo 'ok';
7+
--EXPECTF--
8+
ko
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
Repeat option
3+
--FILE--
4+
<?php
5+
6+
echo 'ok';
7+
--EXPECTF--
8+
ok

0 commit comments

Comments
 (0)