Skip to content

Commit b563551

Browse files
Emit test runner warning when #[Repeat] attribute is used on non-eligible test method
1 parent 49af7be commit b563551

8 files changed

Lines changed: 193 additions & 1 deletion

.php-cs-fixer.dist.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
->notName('TestAttributeOnHookMethodsTest.php')
2525
// InvokableConstraintAndPipeOperatorTest.php uses PHP 8.5 syntax
2626
->notName('InvokableConstraintAndPipeOperatorTest.php')
27-
// NoReturnTypeTest.php must have a method without a return type declaration
27+
// NoReturnTypeTest.php and RepeatAttributeWithoutReturnTypeTest.php must have a method without a return type declaration
2828
->notPath('repeat/_files/NoReturnTypeTest.php')
29+
->notPath('repeat/_files/RepeatAttributeWithoutReturnTypeTest.php')
2930
// FirstPartyClass.php and PhpDeprecationTest.php must not use declare(strict_types=1);
3031
->notPath('error-handler/_files/php-deprecation/src/FirstPartyClass.php')
3132
->notPath('error-handler/_files/php-deprecation/tests/PhpDeprecationTest.php')

src/Framework/TestBuilder.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use function assert;
1414
use function get_parent_class;
1515
use function range;
16+
use function sprintf;
17+
use PHPUnit\Event\Facade as EventFacade;
1618
use PHPUnit\Metadata\Api\DataProvider;
1719
use PHPUnit\Metadata\Api\Groups;
1820
use PHPUnit\Metadata\Api\ProvidedData;
@@ -64,6 +66,26 @@ public function build(ReflectionClass $theClass, string $methodName, array $grou
6466

6567
$numberOfRuns = $metadata->times();
6668
$failureThreshold = $metadata->failureThreshold();
69+
70+
if (!$this->hasVoidReturnType($theClass->getMethod($methodName))) {
71+
EventFacade::emitter()->testRunnerTriggeredPhpunitWarning(
72+
sprintf(
73+
'Method %s::%s is annotated with #[Repeat] but does not have a void return type declaration and will not be repeated',
74+
$className,
75+
$methodName,
76+
),
77+
);
78+
}
79+
80+
if (!$this->doesNotDependOnAnotherTest($className, $methodName)) {
81+
EventFacade::emitter()->testRunnerTriggeredPhpunitWarning(
82+
sprintf(
83+
'Method %s::%s is annotated with #[Repeat] but depends on another test and will not be repeated',
84+
$className,
85+
$methodName,
86+
),
87+
);
88+
}
6789
}
6890

6991
$repeat = $numberOfRuns > 1 &&
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
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\Repeat;
11+
12+
use PHPUnit\Framework\Attributes\Depends;
13+
use PHPUnit\Framework\Attributes\Repeat;
14+
use PHPUnit\Framework\TestCase;
15+
16+
final class RepeatAttributeOnDependentTest extends TestCase
17+
{
18+
public function testOne(): void
19+
{
20+
$this->assertTrue(true);
21+
}
22+
23+
#[Depends('testOne')]
24+
#[Repeat(3)]
25+
public function testTwo(): void
26+
{
27+
$this->assertTrue(true);
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
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\Repeat;
11+
12+
use PHPUnit\Framework\Attributes\Repeat;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class RepeatAttributeOnNonVoidReturnTest extends TestCase
16+
{
17+
#[Repeat(3)]
18+
public function testWithReturnValue(): int
19+
{
20+
$this->assertTrue(true);
21+
22+
return 1;
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
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\Repeat;
11+
12+
use PHPUnit\Framework\Attributes\Repeat;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class RepeatAttributeWithoutReturnTypeTest extends TestCase
16+
{
17+
#[Repeat(3)]
18+
public function testWithoutReturnType()
19+
{
20+
$this->assertTrue(true);
21+
}
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
#[Repeat] on method that depends on another test triggers a test runner warning
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--no-configuration';
6+
$_SERVER['argv'][] = '--do-not-cache-result';
7+
$_SERVER['argv'][] = '--debug';
8+
$_SERVER['argv'][] = __DIR__ . '/_files/RepeatAttributeOnDependentTest.php';
9+
10+
require __DIR__ . '/../../bootstrap.php';
11+
12+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
13+
--EXPECTF--
14+
PHPUnit Started (PHPUnit %s using %s)
15+
Test Runner Configured
16+
Event Facade Sealed
17+
Test Runner Triggered PHPUnit Warning (Method PHPUnit\TestFixture\Repeat\RepeatAttributeOnDependentTest::testTwo is annotated with #[Repeat] but depends on another test and will not be repeated)
18+
Test Suite Loaded (2 tests)
19+
Test Runner Started
20+
Test Suite Sorted
21+
Test Runner Execution Started (2 tests)
22+
Test Suite Started (PHPUnit\TestFixture\Repeat\RepeatAttributeOnDependentTest, 2 tests)
23+
Test Preparation Started (PHPUnit\TestFixture\Repeat\RepeatAttributeOnDependentTest::testOne)
24+
Test Prepared (PHPUnit\TestFixture\Repeat\RepeatAttributeOnDependentTest::testOne)
25+
Test Passed (PHPUnit\TestFixture\Repeat\RepeatAttributeOnDependentTest::testOne)
26+
Test Finished (PHPUnit\TestFixture\Repeat\RepeatAttributeOnDependentTest::testOne)
27+
Test Preparation Started (PHPUnit\TestFixture\Repeat\RepeatAttributeOnDependentTest::testTwo)
28+
Test Prepared (PHPUnit\TestFixture\Repeat\RepeatAttributeOnDependentTest::testTwo)
29+
Test Passed (PHPUnit\TestFixture\Repeat\RepeatAttributeOnDependentTest::testTwo)
30+
Test Finished (PHPUnit\TestFixture\Repeat\RepeatAttributeOnDependentTest::testTwo)
31+
Test Suite Finished (PHPUnit\TestFixture\Repeat\RepeatAttributeOnDependentTest, 2 tests)
32+
Test Runner Execution Finished
33+
Test Runner Finished
34+
PHPUnit Finished (Shell Exit Code: 1)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
#[Repeat] on method without return type declaration triggers a test runner warning
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--no-configuration';
6+
$_SERVER['argv'][] = '--do-not-cache-result';
7+
$_SERVER['argv'][] = '--debug';
8+
$_SERVER['argv'][] = __DIR__ . '/_files/RepeatAttributeWithoutReturnTypeTest.php';
9+
10+
require __DIR__ . '/../../bootstrap.php';
11+
12+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
13+
--EXPECTF--
14+
PHPUnit Started (PHPUnit %s using %s)
15+
Test Runner Configured
16+
Event Facade Sealed
17+
Test Runner Triggered PHPUnit Warning (Method PHPUnit\TestFixture\Repeat\RepeatAttributeWithoutReturnTypeTest::testWithoutReturnType is annotated with #[Repeat] but does not have a void return type declaration and will not be repeated)
18+
Test Suite Loaded (1 test)
19+
Test Runner Started
20+
Test Suite Sorted
21+
Test Runner Execution Started (1 test)
22+
Test Suite Started (PHPUnit\TestFixture\Repeat\RepeatAttributeWithoutReturnTypeTest, 1 test)
23+
Test Preparation Started (PHPUnit\TestFixture\Repeat\RepeatAttributeWithoutReturnTypeTest::testWithoutReturnType)
24+
Test Prepared (PHPUnit\TestFixture\Repeat\RepeatAttributeWithoutReturnTypeTest::testWithoutReturnType)
25+
Test Passed (PHPUnit\TestFixture\Repeat\RepeatAttributeWithoutReturnTypeTest::testWithoutReturnType)
26+
Test Finished (PHPUnit\TestFixture\Repeat\RepeatAttributeWithoutReturnTypeTest::testWithoutReturnType)
27+
Test Suite Finished (PHPUnit\TestFixture\Repeat\RepeatAttributeWithoutReturnTypeTest, 1 test)
28+
Test Runner Execution Finished
29+
Test Runner Finished
30+
PHPUnit Finished (Shell Exit Code: 1)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
#[Repeat] on method with non-void return type triggers a test runner warning
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--no-configuration';
6+
$_SERVER['argv'][] = '--do-not-cache-result';
7+
$_SERVER['argv'][] = '--debug';
8+
$_SERVER['argv'][] = __DIR__ . '/_files/RepeatAttributeOnNonVoidReturnTest.php';
9+
10+
require __DIR__ . '/../../bootstrap.php';
11+
12+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
13+
--EXPECTF--
14+
PHPUnit Started (PHPUnit %s using %s)
15+
Test Runner Configured
16+
Event Facade Sealed
17+
Test Runner Triggered PHPUnit Warning (Method PHPUnit\TestFixture\Repeat\RepeatAttributeOnNonVoidReturnTest::testWithReturnValue is annotated with #[Repeat] but does not have a void return type declaration and will not be repeated)
18+
Test Suite Loaded (1 test)
19+
Test Runner Started
20+
Test Suite Sorted
21+
Test Runner Execution Started (1 test)
22+
Test Suite Started (PHPUnit\TestFixture\Repeat\RepeatAttributeOnNonVoidReturnTest, 1 test)
23+
Test Preparation Started (PHPUnit\TestFixture\Repeat\RepeatAttributeOnNonVoidReturnTest::testWithReturnValue)
24+
Test Prepared (PHPUnit\TestFixture\Repeat\RepeatAttributeOnNonVoidReturnTest::testWithReturnValue)
25+
Test Passed (PHPUnit\TestFixture\Repeat\RepeatAttributeOnNonVoidReturnTest::testWithReturnValue)
26+
Test Finished (PHPUnit\TestFixture\Repeat\RepeatAttributeOnNonVoidReturnTest::testWithReturnValue)
27+
Test Suite Finished (PHPUnit\TestFixture\Repeat\RepeatAttributeOnNonVoidReturnTest, 1 test)
28+
Test Runner Execution Finished
29+
Test Runner Finished
30+
PHPUnit Finished (Shell Exit Code: 1)

0 commit comments

Comments
 (0)