Skip to content

Commit

Permalink
Merge pull request #436 from ergebnis/fix/assert-post-conditions
Browse files Browse the repository at this point in the history
Fix: Sleep in `assertPostConditions()`
  • Loading branch information
localheinz authored Dec 20, 2023
2 parents 400d6f6 + 5c2151c commit b370f7d
Show file tree
Hide file tree
Showing 77 changed files with 713 additions and 106 deletions.
10 changes: 10 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithAssertPostConditions/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithAssertPreConditions/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
Expand Down Expand Up @@ -135,6 +140,11 @@
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/TestCase/WithAssertPostConditions/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/TestCase/WithAssertPreConditions/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class SleeperTest extends Framework\TestCase
{
public function testSleeperSleepsLessThanDefaultMaximumDuration(): void
{
$milliseconds = 1;
$milliseconds = 10;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class SleeperTest extends Framework\TestCase
{
public function testSleeperSleepsLessThanDefaultMaximumDuration(): void
{
$milliseconds = 1;
$milliseconds = 10;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class SleeperTest extends Framework\TestCase
{
public function testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration(): void
{
$milliseconds = 1;
$milliseconds = 10;

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

Expand Down
2 changes: 1 addition & 1 deletion test/EndToEnd/Version08/TestCase/Bare/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class SleeperTest extends Framework\TestCase
{
public function testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration(): void
{
$milliseconds = 1;
$milliseconds = 10;

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

Expand Down
7 changes: 6 additions & 1 deletion test/EndToEnd/Version08/TestCase/Combination/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ protected function assertPreConditions(): void
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

protected function assertPostConditions(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

protected function tearDown(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

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

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

Expand Down
8 changes: 4 additions & 4 deletions test/EndToEnd/Version08/TestCase/Combination/test.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
With a test case that has setUpBeforeClass(), tearDownAfterClass(), setUp(), assertPreConditions(), and tearDown() methods
With a test case that has setUpBeforeClass(), tearDownAfterClass(), setUp(), assertPreConditions(), assertPostConditions(), and tearDown() methods
--FILE--
<?php

Expand All @@ -24,9 +24,9 @@ Random %seed: %s

Detected 3 tests that took longer than expected.

1. 0.6%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #1 (300)
2. 0.5%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #0 (200)
3. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\Combination\SleeperTest::testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration
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

Time: %s, Memory: %s

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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\WithAssertPostConditions;

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

/**
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\Sleeper
*/
final class SleeperTest extends Framework\TestCase
{
protected function assertPostConditions(): 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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
With a test case that has an assertPostConditions() method
--FILE--
<?php

declare(strict_types=1);

use PHPUnit\TextUI;

$_SERVER['argv'][] = '--configuration=test/EndToEnd/Version08/TestCase/WithAssertPostConditions/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/WithAssertPostConditions/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\WithAssertPostConditions\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #1 (300)
2. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithAssertPostConditions\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #0 (200)
3. 0.1%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithAssertPostConditions\SleeperTest::testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration

Time: %s, Memory: %s

OK (3 tests, 3 assertions)
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function assertPreConditions(): void

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

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

Expand Down
2 changes: 1 addition & 1 deletion test/EndToEnd/Version08/TestCase/WithSetUp/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function setUp(): void

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function setUpBeforeClass(): void

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function tearDown(): void

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function tearDownAfterClass(): void

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class SleeperTest extends Framework\TestCase
*/
public function testSleeperSleepsShorterThanMaximumDurationFromXmlConfigurationWhenTestMethodHasInvalidMaximumDurationAnnotation(): void
{
$milliseconds = 1;
$milliseconds = 10;

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

Expand Down Expand Up @@ -54,7 +54,7 @@ public function testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWh
*/
public function testSleeperSleepsShorterThanMaximumDurationFromAnnotationWhenTestMethodHasValidMaximumDurationAnnotation(): void
{
$milliseconds = 1;
$milliseconds = 10;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ protected function assertPreConditions(): void
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

protected function assertPostConditions(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

protected function tearDown(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

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

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

Expand All @@ -62,7 +67,7 @@ public function testSleeperSleepsShorterThanMaximumDurationFromXmlConfiguration(
*/
public function testSleeperSleepsShorterThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation(): void
{
$milliseconds = 1;
$milliseconds = 50;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
With a test case that has setUpBeforeClass(), tearDownAfterClass(), setUp(), assertPreConditions(), tearDown() methods and test methods with @runInSeparateProcess annotation
With a test case that has setUpBeforeClass(), tearDownAfterClass(), setUp(), assertPreConditions(), assertPostConditions(), tearDown() methods and test methods with @runInSeparateProcess annotation
--FILE--
<?php

Expand Down Expand Up @@ -29,10 +29,10 @@ Random %seed: %s

Detected 4 tests that took longer than expected.

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

Time: %s, Memory: %s

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class SleeperTest extends Framework\TestCase
*/
public function testSleeperSleepsShorterThanMaximumDurationFromXmlConfigurationWhenTestMethodHasInvalidSlowThresholdAnnotation(): void
{
$milliseconds = 1;
$milliseconds = 10;

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

Expand Down Expand Up @@ -60,7 +60,7 @@ public function testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWh
*/
public function testSleeperSleepsShorterThanSlowThresholdFromAnnotationWhenTestMethodHasValidSlowThresholdAnnotation(): void
{
$milliseconds = 1;
$milliseconds = 10;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class SleeperTest extends Framework\TestCase
{
public function testSleeperSleepsLessThanDefaultMaximumDuration(): void
{
$milliseconds = 1;
$milliseconds = 10;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class SleeperTest extends Framework\TestCase
{
public function testSleeperSleepsLessThanDefaultMaximumDuration(): void
{
$milliseconds = 1;
$milliseconds = 10;

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

Expand Down
Loading

0 comments on commit b370f7d

Please sign in to comment.