Skip to content

Commit d200b1a

Browse files
authored
Merge pull request #444 from ergebnis/fix/before
Fix: Sleep in method with `@before` annotation or `Before` attribute
2 parents 1ddf35f + 3309cd8 commit d200b1a

File tree

28 files changed

+809
-14
lines changed

28 files changed

+809
-14
lines changed

psalm-baseline.xml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<PossiblyUnusedMethod>
6565
<code>sleepWithAfterAnnotation</code>
6666
<code>sleepWithAfterClassAnnotation</code>
67+
<code>sleepWithBeforeAnnotation</code>
6768
</PossiblyUnusedMethod>
6869
</file>
6970
<file src="test/EndToEnd/Version08/TestCase/WithAfterAnnotation/SleeperTest.php">
@@ -76,16 +77,23 @@
7677
<code>sleepWithAfterClassAnnotation</code>
7778
</PossiblyUnusedMethod>
7879
</file>
80+
<file src="test/EndToEnd/Version08/TestCase/WithBeforeAnnotation/SleeperTest.php">
81+
<PossiblyUnusedMethod>
82+
<code>sleepWithBeforeAnnotation</code>
83+
</PossiblyUnusedMethod>
84+
</file>
7985
<file src="test/EndToEnd/Version08/TestMethod/WithRunInSeparateProcessAnnotation/SleeperTest.php">
8086
<PossiblyUnusedMethod>
8187
<code>sleepWithAfterAnnotation</code>
8288
<code>sleepWithAfterClassAnnotation</code>
89+
<code>sleepWithBeforeAnnotation</code>
8390
</PossiblyUnusedMethod>
8491
</file>
8592
<file src="test/EndToEnd/Version09/TestCase/Combination/SleeperTest.php">
8693
<PossiblyUnusedMethod>
8794
<code>sleepWithAfterAnnotation</code>
8895
<code>sleepWithAfterClassAnnotation</code>
96+
<code>sleepWithBeforeAnnotation</code>
8997
</PossiblyUnusedMethod>
9098
</file>
9199
<file src="test/EndToEnd/Version09/TestCase/WithAfterAnnotation/SleeperTest.php">
@@ -98,10 +106,16 @@
98106
<code>sleepWithAfterClassAnnotation</code>
99107
</PossiblyUnusedMethod>
100108
</file>
109+
<file src="test/EndToEnd/Version09/TestCase/WithBeforeAnnotation/SleeperTest.php">
110+
<PossiblyUnusedMethod>
111+
<code>sleepWithBeforeAnnotation</code>
112+
</PossiblyUnusedMethod>
113+
</file>
101114
<file src="test/EndToEnd/Version09/TestMethod/WithRunInSeparateProcessAnnotation/SleeperTest.php">
102115
<PossiblyUnusedMethod>
103116
<code>sleepWithAfterAnnotation</code>
104117
<code>sleepWithAfterClassAnnotation</code>
118+
<code>sleepWithBeforeAnnotation</code>
105119
</PossiblyUnusedMethod>
106120
</file>
107121
<file src="test/EndToEnd/Version10/Configuration/Defaults/SleeperTest.php">
@@ -131,6 +145,8 @@
131145
<code>sleepWithAfterAttribute</code>
132146
<code>sleepWithAfterClassAnnotation</code>
133147
<code>sleepWithAfterClassAttribute</code>
148+
<code>sleepWithBeforeAnnotation</code>
149+
<code>sleepWithBeforeAttribute</code>
134150
</PossiblyUnusedMethod>
135151
</file>
136152
<file src="test/EndToEnd/Version10/TestCase/WithAfterAnnotation/SleeperTest.php">
@@ -167,6 +183,18 @@
167183
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
168184
</PossiblyUnusedMethod>
169185
</file>
186+
<file src="test/EndToEnd/Version10/TestCase/WithBeforeAnnotation/SleeperTest.php">
187+
<PossiblyUnusedMethod>
188+
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
189+
<code>sleepWithBeforeAnnotation</code>
190+
</PossiblyUnusedMethod>
191+
</file>
192+
<file src="test/EndToEnd/Version10/TestCase/WithBeforeAttribute/SleeperTest.php">
193+
<PossiblyUnusedMethod>
194+
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
195+
<code>sleepWithBeforeAttribute</code>
196+
</PossiblyUnusedMethod>
197+
</file>
170198
<file src="test/EndToEnd/Version10/TestCase/WithDataProvider/SleeperTest.php">
171199
<PossiblyUnusedMethod>
172200
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
@@ -198,6 +226,8 @@
198226
<code>sleepWithAfterAttribute</code>
199227
<code>sleepWithAfterClassAnnotation</code>
200228
<code>sleepWithAfterClassAttribute</code>
229+
<code>sleepWithBeforeAnnotation</code>
230+
<code>sleepWithBeforeAttribute</code>
201231
</PossiblyUnusedMethod>
202232
</file>
203233
<file src="test/EndToEnd/Version10/TestMethod/WithRunInSeparateProcessAttribute/SleeperTest.php">
@@ -206,6 +236,8 @@
206236
<code>sleepWithAfterAttribute</code>
207237
<code>sleepWithAfterClassAnnotation</code>
208238
<code>sleepWithAfterClassAttribute</code>
239+
<code>sleepWithBeforeAnnotation</code>
240+
<code>sleepWithBeforeAttribute</code>
209241
</PossiblyUnusedMethod>
210242
</file>
211243
<file src="test/EndToEnd/Version11/Configuration/Defaults/SleeperTest.php">
@@ -233,6 +265,7 @@
233265
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
234266
<code>sleepWithAfterAttribute</code>
235267
<code>sleepWithAfterClassAttribute</code>
268+
<code>sleepWithBeforeAttribute</code>
236269
</PossiblyUnusedMethod>
237270
</file>
238271
<file src="test/EndToEnd/Version11/TestCase/WithAfterAttribute/SleeperTest.php">
@@ -257,6 +290,12 @@
257290
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
258291
</PossiblyUnusedMethod>
259292
</file>
293+
<file src="test/EndToEnd/Version11/TestCase/WithBeforeAttribute/SleeperTest.php">
294+
<PossiblyUnusedMethod>
295+
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
296+
<code>sleepWithBeforeAttribute</code>
297+
</PossiblyUnusedMethod>
298+
</file>
260299
<file src="test/EndToEnd/Version11/TestCase/WithDataProvider/SleeperTest.php">
261300
<PossiblyUnusedMethod>
262301
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>

test/EndToEnd/Version08/TestCase/Combination/SleeperTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ protected function tearDown(): void
5151
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
5252
}
5353

54+
/**
55+
* @before
56+
*/
57+
public function sleepWithBeforeAnnotation(): void
58+
{
59+
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
60+
}
61+
5462
/**
5563
* @after
5664
*/

test/EndToEnd/Version08/TestCase/Combination/test.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ Configuration: %s/EndToEnd/Version08/TestCase/Combination/phpunit.xml
2222

2323
Detected 3 tests that took longer than expected.
2424

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

2929
Time: %s, Memory: %s
3030

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2021-2023 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/phpunit-slow-test-detector
12+
*/
13+
14+
namespace Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithBeforeAnnotation;
15+
16+
use Ergebnis\PHPUnit\SlowTestDetector\Test;
17+
use PHPUnit\Framework;
18+
19+
/**
20+
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\Sleeper
21+
*/
22+
final class SleeperTest extends Framework\TestCase
23+
{
24+
/**
25+
* @before
26+
*/
27+
public function sleepWithBeforeAnnotation(): void
28+
{
29+
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
30+
}
31+
32+
public function testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration(): void
33+
{
34+
$milliseconds = 10;
35+
36+
$sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds);
37+
38+
$sleeper->sleep();
39+
40+
self::assertSame($milliseconds, $sleeper->milliseconds());
41+
}
42+
43+
/**
44+
* @dataProvider provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration
45+
*/
46+
public function testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider(int $milliseconds): void
47+
{
48+
$sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds);
49+
50+
$sleeper->sleep();
51+
52+
self::assertSame($milliseconds, $sleeper->milliseconds());
53+
}
54+
55+
/**
56+
* @return \Generator<int, array{0: int}>
57+
*/
58+
public static function provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration(): iterable
59+
{
60+
$values = \range(
61+
200,
62+
300,
63+
100,
64+
);
65+
66+
foreach ($values as $value) {
67+
yield $value => [
68+
$value,
69+
];
70+
}
71+
}
72+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<phpunit
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
4+
beStrictAboutChangesToGlobalState="true"
5+
beStrictAboutOutputDuringTests="true"
6+
beStrictAboutTestsThatDoNotTestAnything="true"
7+
beStrictAboutTodoAnnotatedTests="true"
8+
bootstrap="../../../../../vendor/autoload.php"
9+
cacheResult="false"
10+
colors="true"
11+
columns="max"
12+
executionOrder="default"
13+
forceCoversAnnotation="true"
14+
stopOnError="false"
15+
stopOnFailure="false"
16+
stopOnIncomplete="false"
17+
stopOnSkipped="false"
18+
verbose="true"
19+
>
20+
<extensions>
21+
<extension class="Ergebnis\PHPUnit\SlowTestDetector\Extension">
22+
<arguments>
23+
<array>
24+
<element key="maximum-duration">
25+
<integer>100</integer>
26+
</element>
27+
</array>
28+
</arguments>
29+
</extension>
30+
</extensions>
31+
<testsuites>
32+
<testsuite name="Unit Tests">
33+
<directory>.</directory>
34+
</testsuite>
35+
</testsuites>
36+
</phpunit>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
With a test case that sleeps in a method with @before annotation
3+
--FILE--
4+
<?php
5+
6+
declare(strict_types=1);
7+
8+
use PHPUnit\TextUI;
9+
10+
$_SERVER['argv'][] = '--configuration=test/EndToEnd/Version08/TestCase/WithBeforeAnnotation/phpunit.xml';
11+
12+
require_once __DIR__ . '/../../../../../vendor/autoload.php';
13+
14+
PHPUnit\TextUI\Command::main();
15+
--EXPECTF--
16+
PHPUnit %s
17+
18+
Runtime: %s
19+
Configuration: %s/EndToEnd/Version08/TestCase/WithBeforeAnnotation/phpunit.xml
20+
21+
... 3 / 3 (100%)
22+
23+
Detected 3 tests that took longer than expected.
24+
25+
1. 0.4%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithBeforeAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #1 (300)
26+
2. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithBeforeAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #0 (200)
27+
3. 0.1%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithBeforeAnnotation\SleeperTest::testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration
28+
29+
Time: %s, Memory: %s
30+
31+
OK (3 tests, 3 assertions)

test/EndToEnd/Version08/TestMethod/WithRunInSeparateProcessAnnotation/SleeperTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ protected function tearDown(): void
5151
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
5252
}
5353

54+
/**
55+
* @before
56+
*/
57+
public function sleepWithBeforeAnnotation(): void
58+
{
59+
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
60+
}
61+
5462
/**
5563
* @after
5664
*/

test/EndToEnd/Version08/TestMethod/WithRunInSeparateProcessAnnotation/test.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ Configuration: %s/EndToEnd/Version08/TestMethod/WithRunInSeparateProcessAnnotati
2727

2828
Detected 4 tests that took longer than expected.
2929

30-
1. 1.1%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation
31-
2. 0.8%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation
32-
3. 0.7%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfiguration
33-
4. 0.5%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromXmlConfiguration
30+
1. 1.2%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation
31+
2. 0.9%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation
32+
3. 0.8%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfiguration
33+
4. 0.6%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromXmlConfiguration
3434

3535
Time: %s, Memory: %s
3636

test/EndToEnd/Version09/TestCase/Combination/SleeperTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ protected function tearDown(): void
5151
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
5252
}
5353

54+
/**
55+
* @before
56+
*/
57+
public function sleepWithBeforeAnnotation(): void
58+
{
59+
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
60+
}
61+
5462
/**
5563
* @after
5664
*/

test/EndToEnd/Version09/TestCase/Combination/test.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ Configuration: %s/EndToEnd/Version09/TestCase/Combination/phpunit.xml
2222

2323
Detected 3 tests that took longer than expected.
2424

25-
1. 0.8%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #1 (300)
26-
2. 0.7%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #0 (200)
27-
3. 0.5%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\TestCase\Combination\SleeperTest::testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration
25+
1. 0.9%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #1 (300)
26+
2. 0.8%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\TestCase\Combination\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #0 (200)
27+
3. 0.6%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\TestCase\Combination\SleeperTest::testSleeperSleepsLessThanMaximumDurationFromXmlConfiguration
2828

2929
Time: %s, Memory: %s
3030

0 commit comments

Comments
 (0)