Skip to content

Commit

Permalink
Fix: Sleep in assertPreConditions()
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 20, 2023
1 parent 490815c commit 6b90b22
Show file tree
Hide file tree
Showing 22 changed files with 584 additions and 16 deletions.
5 changes: 5 additions & 0 deletions test/EndToEnd/Version10/TestCase/Combination/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ protected function setUp(): void
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

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

protected function tearDown(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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\Version11\TestCase\WithAssertPreConditions;

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

#[Framework\Attributes\CoversClass(Test\Fixture\Sleeper::class)]
final class SleeperTest extends Framework\TestCase

Check failure on line 20 in test/EndToEnd/Version10/TestCase/WithAssertPreConditions/SleeperTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

UnusedClass

test/EndToEnd/Version10/TestCase/WithAssertPreConditions/SleeperTest.php:20:13: UnusedClass: Class Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version11\TestCase\WithAssertPreConditions\SleeperTest is never used (see https://psalm.dev/075)
{
protected function assertPreConditions(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

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

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

$sleeper->sleep();

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

#[Framework\Attributes\DataProvider('provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration')]
public function testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider(int $milliseconds): void

Check failure on line 39 in test/EndToEnd/Version10/TestCase/WithAssertPreConditions/SleeperTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

UnusedParam

test/EndToEnd/Version10/TestCase/WithAssertPreConditions/SleeperTest.php:39:104: UnusedParam: Param #1 is never referenced in this method (see https://psalm.dev/135)
{
$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,34 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
bootstrap="../../../../../vendor/autoload.php"
cacheResult="false"
colors="true"
columns="max"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
executionOrder="random"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
>
<extensions>
<bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension">
<parameter name="maximum-duration" value="100"/>
</bootstrap>
</extensions>
<testsuites>
<testsuite name="Unit Tests">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
33 changes: 33 additions & 0 deletions test/EndToEnd/Version10/TestCase/WithAssertPreConditions/test.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
With a test case that has an assertPreConditions() method
--FILE--
<?php

declare(strict_types=1);

use PHPUnit\TextUI;

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

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

$application = new TextUI\Application();

$application->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s

Runtime: %s
Configuration: %s/EndToEnd/Version11/TestCase/WithAssertPreConditions/phpunit.xml
Random %seed: %s

... 3 / 3 (100%)

Detected 2 tests that took longer than expected.

1. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version11\TestCase\WithAssertPreConditions\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#1
2. 0.2%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version11\TestCase\WithAssertPreConditions\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#0

Time: %s, Memory: %s

OK (3 tests, 3 assertions)
5 changes: 5 additions & 0 deletions test/EndToEnd/Version11/TestCase/Combination/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ protected function setUp(): void
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

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

protected function tearDown(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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\Version11\TestCase\WithAssertPreConditions;

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

#[Framework\Attributes\CoversClass(Test\Fixture\Sleeper::class)]
final class SleeperTest extends Framework\TestCase

Check failure on line 20 in test/EndToEnd/Version11/TestCase/WithAssertPreConditions/SleeperTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

DuplicateClass

test/EndToEnd/Version11/TestCase/WithAssertPreConditions/SleeperTest.php:20:13: DuplicateClass: Class Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version11\TestCase\WithAssertPreConditions\SleeperTest has already been defined in /home/runner/work/phpunit-slow-test-detector/phpunit-slow-test-detector/test/EndToEnd/Version10/TestCase/WithAssertPreConditions/SleeperTest.php (see https://psalm.dev/071)
{
protected function assertPreConditions(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

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

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

$sleeper->sleep();

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

#[Framework\Attributes\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,34 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
bootstrap="../../../../../vendor/autoload.php"
cacheResult="false"
colors="true"
columns="max"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
executionOrder="random"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
>
<extensions>
<bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension">
<parameter name="maximum-duration" value="100"/>
</bootstrap>
</extensions>
<testsuites>
<testsuite name="Unit Tests">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
33 changes: 33 additions & 0 deletions test/EndToEnd/Version11/TestCase/WithAssertPreConditions/test.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
With a test case that has an assertPreConditions() method
--FILE--
<?php

declare(strict_types=1);

use PHPUnit\TextUI;

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

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

$application = new TextUI\Application();

$application->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s

Runtime: %s
Configuration: %s/EndToEnd/Version11/TestCase/WithAssertPreConditions/phpunit.xml
Random %seed: %s

... 3 / 3 (100%)

Detected 2 tests that took longer than expected.

1. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version11\TestCase\WithAssertPreConditions\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#1
2. 0.2%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version11\TestCase\WithAssertPreConditions\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider#0

Time: %s, Memory: %s

OK (3 tests, 3 assertions)
5 changes: 5 additions & 0 deletions test/EndToEnd/Version8/TestCase/Combination/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ protected function setUp(): void
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
}

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

protected function tearDown(): void
{
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
Expand Down
8 changes: 4 additions & 4 deletions test/EndToEnd/Version8/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(), setUp(), tearDown(), and tearDownAfterClass() methods
With a test case that has setUpBeforeClass(), setUp(), tearDown(), tearDownAfterClass(), and assertPreConditions() methods
--FILE--
<?php

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

Detected 3 tests that took longer than expected.

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

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

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

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

$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 6b90b22

Please sign in to comment.