Skip to content

Commit

Permalink
Merge pull request #439 from ergebnis/fix/after-class
Browse files Browse the repository at this point in the history
Fix: Sleep in method with `@afterClass` annotation or `AfterClass` attribute
  • Loading branch information
localheinz authored Dec 21, 2023
2 parents 223c6cf + 6af1b2a commit 71ba320
Show file tree
Hide file tree
Showing 27 changed files with 804 additions and 4 deletions.
40 changes: 40 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,45 @@
<file src="test/EndToEnd/Version08/TestCase/Combination/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
<code>sleepWithAfterClassAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version08/TestCase/WithAfterAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version08/TestCase/WithAfterClassAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterClassAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version08/TestMethod/WithRunInSeparateProcessAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
<code>sleepWithAfterClassAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version09/TestCase/Combination/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
<code>sleepWithAfterClassAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version09/TestCase/WithAfterAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version09/TestCase/WithAfterClassAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterClassAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version09/TestMethod/WithRunInSeparateProcessAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
<code>sleepWithAfterClassAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/Configuration/Defaults/SleeperTest.php">
Expand Down Expand Up @@ -115,6 +129,8 @@
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
<code>sleepWithAfterAnnotation</code>
<code>sleepWithAfterAttribute</code>
<code>sleepWithAfterClassAnnotation</code>
<code>sleepWithAfterClassAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithAfterAnnotation/SleeperTest.php">
Expand All @@ -129,6 +145,18 @@
<code>sleepWithAfterAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithAfterClassAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
<code>sleepWithAfterClassAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithAfterClassAttribute/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
<code>sleepWithAfterClassAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithAssertPostConditions/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
Expand Down Expand Up @@ -168,12 +196,16 @@
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
<code>sleepWithAfterAttribute</code>
<code>sleepWithAfterClassAnnotation</code>
<code>sleepWithAfterClassAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestMethod/WithRunInSeparateProcessAttribute/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
<code>sleepWithAfterAttribute</code>
<code>sleepWithAfterClassAnnotation</code>
<code>sleepWithAfterClassAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/Configuration/Defaults/SleeperTest.php">
Expand All @@ -200,6 +232,7 @@
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
<code>sleepWithAfterAttribute</code>
<code>sleepWithAfterClassAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/TestCase/WithAfterAttribute/SleeperTest.php">
Expand All @@ -208,6 +241,12 @@
<code>sleepWithAfterAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/TestCase/WithAfterClassAttribute/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
<code>sleepWithAfterClassAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/TestCase/WithAssertPostConditions/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
Expand Down Expand Up @@ -246,6 +285,7 @@
<file src="test/EndToEnd/Version11/TestMethod/WithRunInSeparateProcessAttribute/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAttribute</code>
<code>sleepWithAfterClassAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Fixture/Sleeper.php">
Expand Down
8 changes: 8 additions & 0 deletions test/EndToEnd/Version08/TestCase/Combination/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public function sleepWithAfterAnnotation(): void
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

/**
* @afterClass
*/
public static function sleepWithAfterClassAnnotation(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

public function testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration(): void
{
$milliseconds = 10;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithAfterClassAnnotation;

use Ergebnis\PHPUnit\SlowTestDetector\Test;
use PHPUnit\Framework;

/**
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\Sleeper
*/
final class SleeperTest extends Framework\TestCase
{
/**
* @afterClass
*/
public static function sleepWithAfterClassAnnotation(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

public function testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration(): void
{
$milliseconds = 10;

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

$sleeper->sleep();

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

/**
* @dataProvider provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration
*/
public function testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider(int $milliseconds): void
{
$sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds);

$sleeper->sleep();

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

/**
* @return \Generator<int, array{0: int}>
*/
public static function provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration(): iterable
{
$values = \range(
200,
300,
100,
);

foreach ($values as $value) {
yield $value => [
$value,
];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
bootstrap="../../../../../vendor/autoload.php"
cacheResult="false"
colors="true"
columns="max"
executionOrder="default"
forceCoversAnnotation="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="true"
>
<extensions>
<extension class="Ergebnis\PHPUnit\SlowTestDetector\Extension">
<arguments>
<array>
<element key="maximum-duration">
<integer>100</integer>
</element>
</array>
</arguments>
</extension>
</extensions>
<testsuites>
<testsuite name="Unit Tests">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
With a test case that sleeps in a method with @afterClass annotation
--FILE--
<?php

declare(strict_types=1);

use PHPUnit\TextUI;

$_SERVER['argv'][] = '--configuration=test/EndToEnd/Version08/TestCase/WithAfterClassAnnotation/phpunit.xml';

require_once __DIR__ . '/../../../../../vendor/autoload.php';

PHPUnit\TextUI\Command::main();
--EXPECTF--
PHPUnit %s

Runtime: %s
Configuration: %s/EndToEnd/Version08/TestCase/WithAfterClassAnnotation/phpunit.xml

... 3 / 3 (100%)

Detected 2 tests that took longer than expected.

1. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithAfterClassAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #1 (300)
2. 0.2%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithAfterClassAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #0 (200)

Time: %s, Memory: %s

OK (3 tests, 3 assertions)
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public function sleepWithAfterAnnotation(): void
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

/**
* @afterClass
*/
public static function sleepWithAfterClassAnnotation(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

public function testSleeperSleepsShorterThanMaximumDurationFromXmlConfiguration(): void
{
$milliseconds = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Configuration: %s/EndToEnd/Version08/TestMethod/WithRunInSeparateProcessAnnotati

Detected 4 tests that took longer than expected.

1. 1.0%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation
2. 0.7%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation
1. 1.1%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation
2. 0.8%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation
3. 0.7%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfiguration
4. 0.5%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromXmlConfiguration

Expand Down
8 changes: 8 additions & 0 deletions test/EndToEnd/Version09/TestCase/Combination/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public function sleepWithAfterAnnotation(): void
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

/**
* @afterClass
*/
public static function sleepWithAfterClassAnnotation(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

public function testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration(): void
{
$milliseconds = 10;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\TestCase\WithAfterClassAnnotation;

use Ergebnis\PHPUnit\SlowTestDetector\Test;
use PHPUnit\Framework;

/**
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\Sleeper
*/
final class SleeperTest extends Framework\TestCase
{
/**
* @afterClass
*/
public static function sleepWithAfterClassAnnotation(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

public function testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration(): void
{
$milliseconds = 10;

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

$sleeper->sleep();

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

/**
* @dataProvider provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration
*/
public function testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider(int $milliseconds): void
{
$sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds);

$sleeper->sleep();

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

/**
* @return \Generator<int, array{0: int}>
*/
public static function provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration(): iterable
{
$values = \range(
200,
300,
100,
);

foreach ($values as $value) {
yield $value => [
$value,
];
}
}
}
Loading

0 comments on commit 71ba320

Please sign in to comment.