Skip to content

Commit

Permalink
Fix: Sleep in method with afterClass annotation or AfterClass attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 20, 2023
1 parent 4275c9d commit 2c01798
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
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 function sleepWithAfterClassAnnotation(): void

Check failure on line 65 in test/EndToEnd/Version08/TestCase/Combination/SleeperTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

PossiblyUnusedMethod

test/EndToEnd/Version08/TestCase/Combination/SleeperTest.php:65:21: PossiblyUnusedMethod: Cannot find any calls to method Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\Combination\SleeperTest::sleepWithAfterClassAnnotation (see https://psalm.dev/087)
{
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 function sleepWithAfterClassAnnotation(): void

Check failure on line 27 in test/EndToEnd/Version08/TestCase/WithAfterClassAnnotation/SleeperTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

PossiblyUnusedMethod

test/EndToEnd/Version08/TestCase/WithAfterClassAnnotation/SleeperTest.php:27:21: PossiblyUnusedMethod: Cannot find any calls to method Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithAfterClassAnnotation\SleeperTest::sleepWithAfterClassAnnotation (see https://psalm.dev/087)
{
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="random"
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,33 @@
--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/WithTearDown/phpunit.xml';
$_SERVER['argv'][] = '--random-order-seed=1234567890';

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

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

Runtime: %s
Configuration: %s/EndToEnd/Version08/TestCase/WithTearDown/phpunit.xml
Random %seed: %s

... 3 / 3 (100%)

Detected 3 tests that took longer than expected.

1. 0.4%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithTearDown\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #1 (300)
2. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithTearDown\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #0 (200)
3. 0.1%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithTearDown\SleeperTest::testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration

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 function sleepWithAfterClassAnnotation(): void

Check failure on line 65 in test/EndToEnd/Version08/TestMethod/WithRunInSeparateProcessAnnotation/SleeperTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

PossiblyUnusedMethod

test/EndToEnd/Version08/TestMethod/WithRunInSeparateProcessAnnotation/SleeperTest.php:65:21: PossiblyUnusedMethod: Cannot find any calls to method Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::sleepWithAfterClassAnnotation (see https://psalm.dev/087)
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

public function testSleeperSleepsShorterThanMaximumDurationFromXmlConfiguration(): void
{
$milliseconds = 10;
Expand Down

0 comments on commit 2c01798

Please sign in to comment.