Skip to content

Commit

Permalink
Merge pull request #437 from ergebnis/fix/data-provider
Browse files Browse the repository at this point in the history
Fix: Sleep in data provider
  • Loading branch information
localheinz authored Dec 20, 2023
2 parents 6036afc + ccf4076 commit 83fac72
Show file tree
Hide file tree
Showing 21 changed files with 550 additions and 4 deletions.
10 changes: 10 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithDataProvider/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/WithSetUp/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
Expand Down Expand Up @@ -150,6 +155,11 @@
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/TestCase/WithDataProvider/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/TestCase/WithSetUp/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
Expand Down
2 changes: 2 additions & 0 deletions test/EndToEnd/Version08/TestCase/Combination/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public static function provideMillisecondsGreaterThanMaximumDurationFromXmlConfi
);

foreach ($values as $value) {
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();

yield $value => [
$value,
];
Expand Down
2 changes: 1 addition & 1 deletion 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 sleeps in hook and test methods
With a test case that sleeps in data provider, hook, and test methods
--FILE--
<?php

Expand Down
66 changes: 66 additions & 0 deletions test/EndToEnd/Version08/TestCase/WithDataProvider/SleeperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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\WithDataProvider;

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

/**
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\Sleeper
*/
final class SleeperTest extends Framework\TestCase
{
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) {
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();

yield $value => [
$value,
];
}
}
}
36 changes: 36 additions & 0 deletions test/EndToEnd/Version08/TestCase/WithDataProvider/phpunit.xml
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>
32 changes: 32 additions & 0 deletions test/EndToEnd/Version08/TestCase/WithDataProvider/test.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
With a test case that sleeps in a data provider
--FILE--
<?php

declare(strict_types=1);

use PHPUnit\TextUI;

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

Time: %s, Memory: %s

OK (3 tests, 3 assertions)
2 changes: 2 additions & 0 deletions test/EndToEnd/Version09/TestCase/Combination/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public static function provideMillisecondsGreaterThanMaximumDurationFromXmlConfi
);

foreach ($values as $value) {
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();

yield $value => [
$value,
];
Expand Down
2 changes: 1 addition & 1 deletion test/EndToEnd/Version09/TestCase/Combination/test.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
With a test case that sleeps in hook and test methods
With a test case that sleeps in data provider, hook, and test methods
--FILE--
<?php

Expand Down
66 changes: 66 additions & 0 deletions test/EndToEnd/Version09/TestCase/WithDataProvider/SleeperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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\WithDataProvider;

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

/**
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\Sleeper
*/
final class SleeperTest extends Framework\TestCase
{
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) {
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();

yield $value => [
$value,
];
}
}
}
36 changes: 36 additions & 0 deletions test/EndToEnd/Version09/TestCase/WithDataProvider/phpunit.xml
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/9.0/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>
32 changes: 32 additions & 0 deletions test/EndToEnd/Version09/TestCase/WithDataProvider/test.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
With a test case that sleeps in a data provider
--FILE--
<?php

declare(strict_types=1);

use PHPUnit\TextUI;

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

Time: %s, Memory: %s

OK (3 tests, 3 assertions)
2 changes: 2 additions & 0 deletions test/EndToEnd/Version10/TestCase/Combination/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public static function provideMillisecondsGreaterThanMaximumDurationFromXmlConfi
);

foreach ($values as $value) {
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();

yield $value => [
$value,
];
Expand Down
2 changes: 1 addition & 1 deletion test/EndToEnd/Version10/TestCase/Combination/test.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
With a test case that sleeps in hook and test methods
With a test case that sleeps in data provider, hook, and test methods
--FILE--
<?php

Expand Down
Loading

0 comments on commit 83fac72

Please sign in to comment.