Skip to content
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

Fix: Sleep in method with @after annotation or After attribute #438

Merged
merged 1 commit into from
Dec 20, 2023
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
63 changes: 63 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@
<code>$reporter</code>
</UnusedParam>
</file>
<file src="test/EndToEnd/Version08/TestCase/Combination/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version08/TestCase/WithAfterAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version08/TestMethod/WithRunInSeparateProcessAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version09/TestCase/WithAfterAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version09/TestMethod/WithRunInSeparateProcessAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/Configuration/Defaults/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanDefaultMaximumDuration</code>
Expand All @@ -83,6 +108,20 @@
<file src="test/EndToEnd/Version10/TestCase/Combination/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
<code>sleepWithAfterAnnotation</code>
<code>sleepWithAfterAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithAfterAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
<code>sleepWithAfterAnnotation</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithAfterAttribute/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
<code>sleepWithAfterAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithAssertPostConditions/SleeperTest.php">
Expand Down Expand Up @@ -120,6 +159,18 @@
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestMethod/WithRunInSeparateProcessAnnotation/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
<code>sleepWithAfterAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestMethod/WithRunInSeparateProcessAttribute/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAnnotation</code>
<code>sleepWithAfterAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/Configuration/Defaults/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanDefaultMaximumDuration</code>
Expand All @@ -143,6 +194,13 @@
<file src="test/EndToEnd/Version11/TestCase/Combination/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
<code>sleepWithAfterAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/TestCase/WithAfterAttribute/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
<code>sleepWithAfterAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/TestCase/WithAssertPostConditions/SleeperTest.php">
Expand Down Expand Up @@ -180,6 +238,11 @@
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/TestMethod/WithRunInSeparateProcessAttribute/SleeperTest.php">
<PossiblyUnusedMethod>
<code>sleepWithAfterAttribute</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Fixture/Sleeper.php">
<ArgumentTypeCoercion>
<code><![CDATA[$this->milliseconds * 1000]]></code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ protected function tearDown(): void
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

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

public function testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration(): void
{
$milliseconds = 10;
Expand Down
6 changes: 3 additions & 3 deletions test/EndToEnd/Version08/TestCase/Combination/test.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Random %seed: %s

Detected 3 tests that took longer than expected.

1. 0.7%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #1 (300)
2. 0.6%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #0 (200)
3. 0.4%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\Combination\SleeperTest::testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration
1. 0.8%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #1 (300)
2. 0.7%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #0 (200)
3. 0.5%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\Combination\SleeperTest::testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration

Time: %s, Memory: %s

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\WithAfterAnnotation;

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

/**
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\Sleeper
*/
final class SleeperTest extends Framework\TestCase
{
/**
* @after
*/
public function sleepWithAfterAnnotation(): 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="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>
33 changes: 33 additions & 0 deletions test/EndToEnd/Version08/TestCase/WithAfterAnnotation/test.phpt
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 @after 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 @@ -51,6 +51,14 @@ protected function tearDown(): void
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

/**
* @after
*/
public function sleepWithAfterAnnotation(): 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 @@ -29,10 +29,10 @@ Random %seed: %s

Detected 4 tests that took longer than expected.

1. 0.9%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation
2. 0.6%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation
3. 0.6%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfiguration
4. 0.4%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromXmlConfiguration
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
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

Time: %s, Memory: %s

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\WithAfterAnnotation;

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

/**
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\Sleeper
*/
final class SleeperTest extends Framework\TestCase
{
/**
* @after
*/
public function sleepWithAfterAnnotation(): 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