Skip to content

Fix discovery of @maximumDuration annotation when using data providers #675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

For a full diff see [`2.19.0...main`][2.19.0...main].

### Fixed

- Fixed discovery of `@maximumDuration` annotation when using data providers ([#675]), by [@morgan-atproperties]

## [`2.19.0`][2.19.0]

For a full diff see [`2.18.0...2.19.0`][2.18.0...2.19.0].
Expand Down Expand Up @@ -406,4 +410,5 @@ For a full diff see [`7afa59c...1.0.0`][7afa59c...1.0.0].
[@dantleech]: https://github.com/dantleech
[@HypeMC]: https://github.com/HypeMC
[@localheinz]: https://github.com/localheinz
[@morgan-atproperties]: https://github.com/morgan-atproperties
[@mvorisek]: https://github.com/mvorisek
17 changes: 17 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,23 @@

private function resolveMaximumDuration(string $test): MaximumDuration
{
/**
* @see https://github.com/sebastianbergmann/phpunit/blob/6.5.0/src/Framework/TestCase.php#L352-L368
* @see https://github.com/sebastianbergmann/phpunit/blob/6.5.0/src/Framework/TestCase.php#L1966-L1992
*/
$dataSetPosition = \strpos(
$test,
' with data set'
);

Check warning on line 364 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L361-L364

Added lines #L361 - L364 were not covered by tests

if (false !== $dataSetPosition) {
$test = \substr(
$test,
0,
$dataSetPosition
);
}

Check warning on line 372 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L366-L372

Added lines #L366 - L372 were not covered by tests

list($testClassName, $testMethodName) = \explode(
'::',
$test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,62 @@ public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWhenTes
self::assertSame($milliseconds, $sleeper->milliseconds());
}

/**
* @dataProvider provideDataWhereDataNameIsInteger
*
* @maximumDuration 200
*/
public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWithNumericDataProvider()
{
$milliseconds = 150;

$sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds);

$sleeper->sleep();

self::assertSame($milliseconds, $sleeper->milliseconds());
}

/**
* @return list<array{0: string}>
*/
public static function provideDataWhereDataNameIsInteger(): array
{
return [
[
'bar',
],
];
}

/**
* @dataProvider provideDataWhereDataNameIsString
*
* @maximumDuration 200
*/
public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWithNamedDataProvider()
{
$milliseconds = 150;

$sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds);

$sleeper->sleep();

self::assertSame($milliseconds, $sleeper->milliseconds());
}

/**
* @return array<string, array{0: string}>
*/
public static function provideDataWhereDataNameIsString(): array
{
return [
'foo' => [
'bar',
],
];
}

/**
* @maximumDuration 200
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PHPUnit %s
Runtime: %s
Configuration: %s/EndToEnd/Version06/TestMethod/WithMaximumDurationAnnotation/phpunit.xml

.... 4 / 4 (100%)
...... 6 / 6 (100%)

Detected 2 tests where the duration exceeded the maximum duration.

Expand All @@ -27,4 +27,4 @@ Detected 2 tests where the duration exceeded the maximum duration.

Time: %s, Memory: %s

OK (4 tests, 4 assertions)
OK (6 tests, 6 assertions)
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,62 @@ public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWhenTes
self::assertSame($milliseconds, $sleeper->milliseconds());
}

/**
* @dataProvider provideDataWhereDataNameIsInteger
*
* @maximumDuration 200
*/
public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWithNumericDataProvider(): void
{
$milliseconds = 150;

$sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds);

$sleeper->sleep();

self::assertSame($milliseconds, $sleeper->milliseconds());
}

/**
* @return list<array{0: string}>
*/
public static function provideDataWhereDataNameIsInteger(): array
{
return [
[
'bar',
],
];
}

/**
* @dataProvider provideDataWhereDataNameIsString
*
* @maximumDuration 200
*/
public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWithNamedDataProvider(): void
{
$milliseconds = 150;

$sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds);

$sleeper->sleep();

self::assertSame($milliseconds, $sleeper->milliseconds());
}

/**
* @return array<string, array{0: string}>
*/
public static function provideDataWhereDataNameIsString(): array
{
return [
'foo' => [
'bar',
],
];
}

/**
* @maximumDuration 200
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PHPUnit %s
Runtime: %s
Configuration: %s/EndToEnd/Version07/TestMethod/WithMaximumDurationAnnotation/phpunit.xml

.... 4 / 4 (100%)
...... 6 / 6 (100%)

Detected 2 tests where the duration exceeded the maximum duration.

Expand All @@ -27,4 +27,4 @@ Detected 2 tests where the duration exceeded the maximum duration.

Time: %s, Memory: %s

OK (4 tests, 4 assertions)
OK (6 tests, 6 assertions)
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,62 @@ public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWhenTes
self::assertSame($milliseconds, $sleeper->milliseconds());
}

/**
* @dataProvider provideDataWhereDataNameIsInteger
*
* @maximumDuration 200
*/
public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWithNumericDataProvider(): void
{
$milliseconds = 150;

$sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds);

$sleeper->sleep();

self::assertSame($milliseconds, $sleeper->milliseconds());
}

/**
* @return list<array{0: string}>
*/
public static function provideDataWhereDataNameIsInteger(): array
{
return [
[
'bar',
],
];
}

/**
* @dataProvider provideDataWhereDataNameIsString
*
* @maximumDuration 200
*/
public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWithNamedDataProvider(): void
{
$milliseconds = 150;

$sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds);

$sleeper->sleep();

self::assertSame($milliseconds, $sleeper->milliseconds());
}

/**
* @return array<string, array{0: string}>
*/
public static function provideDataWhereDataNameIsString(): array
{
return [
'foo' => [
'bar',
],
];
}

/**
* @maximumDuration 200
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PHPUnit %s
Runtime: %s
Configuration: %s/EndToEnd/Version08/TestMethod/WithMaximumDurationAnnotation/phpunit.xml

.... 4 / 4 (100%)
...... 6 / 6 (100%)

Detected 2 tests where the duration exceeded the maximum duration.

Expand All @@ -27,4 +27,4 @@ Detected 2 tests where the duration exceeded the maximum duration.

Time: %s, Memory: %s

OK (4 tests, 4 assertions)
OK (6 tests, 6 assertions)
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,62 @@ public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWhenTes
self::assertSame($milliseconds, $sleeper->milliseconds());
}

/**
* @dataProvider provideDataWhereDataNameIsInteger
*
* @maximumDuration 200
*/
public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWithNumericDataProvider(): void
{
$milliseconds = 150;

$sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds);

$sleeper->sleep();

self::assertSame($milliseconds, $sleeper->milliseconds());
}

/**
* @return list<array{0: string}>
*/
public static function provideDataWhereDataNameIsInteger(): array
{
return [
[
'bar',
],
];
}

/**
* @dataProvider provideDataWhereDataNameIsString
*
* @maximumDuration 200
*/
public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWithNamedDataProvider(): void
{
$milliseconds = 150;

$sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds);

$sleeper->sleep();

self::assertSame($milliseconds, $sleeper->milliseconds());
}

/**
* @return array<string, array{0: string}>
*/
public static function provideDataWhereDataNameIsString(): array
{
return [
'foo' => [
'bar',
],
];
}

/**
* @maximumDuration 200
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PHPUnit %s
Runtime: %s
Configuration: %s/EndToEnd/Version09/TestMethod/WithMaximumDurationAnnotation/phpunit.xml

.... 4 / 4 (100%)
...... 6 / 6 (100%)

Detected 2 tests where the duration exceeded the maximum duration.

Expand All @@ -27,4 +27,4 @@ Detected 2 tests where the duration exceeded the maximum duration.

Time: %s, Memory: %s

OK (4 tests, 4 assertions)
OK (6 tests, 6 assertions)
Loading