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 assertPreConditions() #425

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
13 changes: 13 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithAssertPreConditions/SleeperTest.php">
<UnusedClass>
<code>SleeperTest</code>
</UnusedClass>
<UnusedParam>
<code>$milliseconds</code>
</UnusedParam>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithSetUp/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
Expand Down Expand Up @@ -130,6 +138,11 @@
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/TestCase/WithAssertPreConditions/SleeperTest.php">
<DuplicateClass>
<code>SleeperTest</code>
</DuplicateClass>
</file>
<file src="test/EndToEnd/Version11/TestCase/WithSetUp/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
Expand Down
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
{
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/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>
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)
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
{
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>
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
Loading