Skip to content

Commit 917e39d

Browse files
authored
Merge pull request #445 from ergebnis/fix/before-class
Fix: Sleep in method with `@beforeClass` annotation or `BeforeClass` attribute
2 parents ba0e16f + 126e358 commit 917e39d

File tree

27 files changed

+804
-4
lines changed

27 files changed

+804
-4
lines changed

psalm-baseline.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<code>sleepWithAfterAnnotation</code>
6666
<code>sleepWithAfterClassAnnotation</code>
6767
<code>sleepWithBeforeAnnotation</code>
68+
<code>sleepWithBeforeClassAnnotation</code>
6869
</PossiblyUnusedMethod>
6970
</file>
7071
<file src="test/EndToEnd/Version08/TestCase/WithAfterAnnotation/SleeperTest.php">
@@ -82,18 +83,25 @@
8283
<code>sleepWithBeforeAnnotation</code>
8384
</PossiblyUnusedMethod>
8485
</file>
86+
<file src="test/EndToEnd/Version08/TestCase/WithBeforeClassAnnotation/SleeperTest.php">
87+
<PossiblyUnusedMethod>
88+
<code>sleepWithBeforeClassAnnotation</code>
89+
</PossiblyUnusedMethod>
90+
</file>
8591
<file src="test/EndToEnd/Version08/TestMethod/WithRunInSeparateProcessAnnotation/SleeperTest.php">
8692
<PossiblyUnusedMethod>
8793
<code>sleepWithAfterAnnotation</code>
8894
<code>sleepWithAfterClassAnnotation</code>
8995
<code>sleepWithBeforeAnnotation</code>
96+
<code>sleepWithBeforeClassAnnotation</code>
9097
</PossiblyUnusedMethod>
9198
</file>
9299
<file src="test/EndToEnd/Version09/TestCase/Combination/SleeperTest.php">
93100
<PossiblyUnusedMethod>
94101
<code>sleepWithAfterAnnotation</code>
95102
<code>sleepWithAfterClassAnnotation</code>
96103
<code>sleepWithBeforeAnnotation</code>
104+
<code>sleepWithBeforeClassAnnotation</code>
97105
</PossiblyUnusedMethod>
98106
</file>
99107
<file src="test/EndToEnd/Version09/TestCase/WithAfterAnnotation/SleeperTest.php">
@@ -111,11 +119,17 @@
111119
<code>sleepWithBeforeAnnotation</code>
112120
</PossiblyUnusedMethod>
113121
</file>
122+
<file src="test/EndToEnd/Version09/TestCase/WithBeforeClassAnnotation/SleeperTest.php">
123+
<PossiblyUnusedMethod>
124+
<code>sleepWithBeforeClassAnnotation</code>
125+
</PossiblyUnusedMethod>
126+
</file>
114127
<file src="test/EndToEnd/Version09/TestMethod/WithRunInSeparateProcessAnnotation/SleeperTest.php">
115128
<PossiblyUnusedMethod>
116129
<code>sleepWithAfterAnnotation</code>
117130
<code>sleepWithAfterClassAnnotation</code>
118131
<code>sleepWithBeforeAnnotation</code>
132+
<code>sleepWithBeforeClassAnnotation</code>
119133
</PossiblyUnusedMethod>
120134
</file>
121135
<file src="test/EndToEnd/Version10/Configuration/Defaults/SleeperTest.php">
@@ -147,6 +161,8 @@
147161
<code>sleepWithAfterClassAttribute</code>
148162
<code>sleepWithBeforeAnnotation</code>
149163
<code>sleepWithBeforeAttribute</code>
164+
<code>sleepWithBeforeClassAnnotation</code>
165+
<code>sleepWithBeforeClassAttribute</code>
150166
</PossiblyUnusedMethod>
151167
</file>
152168
<file src="test/EndToEnd/Version10/TestCase/WithAfterAnnotation/SleeperTest.php">
@@ -195,6 +211,18 @@
195211
<code>sleepWithBeforeAttribute</code>
196212
</PossiblyUnusedMethod>
197213
</file>
214+
<file src="test/EndToEnd/Version10/TestCase/WithBeforeClassAnnotation/SleeperTest.php">
215+
<PossiblyUnusedMethod>
216+
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
217+
<code>sleepWithBeforeClassAnnotation</code>
218+
</PossiblyUnusedMethod>
219+
</file>
220+
<file src="test/EndToEnd/Version10/TestCase/WithBeforeClassAttribute/SleeperTest.php">
221+
<PossiblyUnusedMethod>
222+
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
223+
<code>sleepWithBeforeClassAttribute</code>
224+
</PossiblyUnusedMethod>
225+
</file>
198226
<file src="test/EndToEnd/Version10/TestCase/WithDataProvider/SleeperTest.php">
199227
<PossiblyUnusedMethod>
200228
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
@@ -228,6 +256,8 @@
228256
<code>sleepWithAfterClassAttribute</code>
229257
<code>sleepWithBeforeAnnotation</code>
230258
<code>sleepWithBeforeAttribute</code>
259+
<code>sleepWithBeforeClassAnnotation</code>
260+
<code>sleepWithBeforeClassAttribute</code>
231261
</PossiblyUnusedMethod>
232262
</file>
233263
<file src="test/EndToEnd/Version10/TestMethod/WithRunInSeparateProcessAttribute/SleeperTest.php">
@@ -238,6 +268,8 @@
238268
<code>sleepWithAfterClassAttribute</code>
239269
<code>sleepWithBeforeAnnotation</code>
240270
<code>sleepWithBeforeAttribute</code>
271+
<code>sleepWithBeforeClassAnnotation</code>
272+
<code>sleepWithBeforeClassAttribute</code>
241273
</PossiblyUnusedMethod>
242274
</file>
243275
<file src="test/EndToEnd/Version11/Configuration/Defaults/SleeperTest.php">
@@ -266,6 +298,7 @@
266298
<code>sleepWithAfterAttribute</code>
267299
<code>sleepWithAfterClassAttribute</code>
268300
<code>sleepWithBeforeAttribute</code>
301+
<code>sleepWithBeforeClassAttribute</code>
269302
</PossiblyUnusedMethod>
270303
</file>
271304
<file src="test/EndToEnd/Version11/TestCase/WithAfterAttribute/SleeperTest.php">
@@ -296,6 +329,12 @@
296329
<code>sleepWithBeforeAttribute</code>
297330
</PossiblyUnusedMethod>
298331
</file>
332+
<file src="test/EndToEnd/Version11/TestCase/WithBeforeClassAttribute/SleeperTest.php">
333+
<PossiblyUnusedMethod>
334+
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
335+
<code>sleepWithBeforeClassAttribute</code>
336+
</PossiblyUnusedMethod>
337+
</file>
299338
<file src="test/EndToEnd/Version11/TestCase/WithDataProvider/SleeperTest.php">
300339
<PossiblyUnusedMethod>
301340
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
@@ -326,6 +365,7 @@
326365
<code>sleepWithAfterAttribute</code>
327366
<code>sleepWithAfterClassAttribute</code>
328367
<code>sleepWithBeforeAttribute</code>
368+
<code>sleepWithBeforeClassAttribute</code>
329369
</PossiblyUnusedMethod>
330370
</file>
331371
<file src="test/Fixture/Sleeper.php">

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ public function sleepWithBeforeAnnotation(): void
5959
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
6060
}
6161

62+
/**
63+
* @beforeClass
64+
*/
65+
public function sleepWithBeforeClassAnnotation(): void
66+
{
67+
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
68+
}
69+
6270
/**
6371
* @after
6472
*/
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\WithBeforeClassAnnotation;
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+
* @beforeClass
26+
*/
27+
public static function sleepWithBeforeClassAnnotation(): 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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
With a test case that sleeps in a method with @beforeClass 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/WithBeforeClassAnnotation/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/WithBeforeClassAnnotation/phpunit.xml
20+
21+
... 3 / 3 (100%)
22+
23+
Detected 2 tests that took longer than expected.
24+
25+
1. 0.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithBeforeClassAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #1 (300)
26+
2. 0.2%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestCase\WithBeforeClassAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWithDataProvider with data set #0 (200)
27+
28+
Time: %s, Memory: %s
29+
30+
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
@@ -59,6 +59,14 @@ public function sleepWithBeforeAnnotation(): void
5959
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
6060
}
6161

62+
/**
63+
* @beforeClass
64+
*/
65+
public static function sleepWithBeforeClassAnnotation(): void
66+
{
67+
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
68+
}
69+
6270
/**
6371
* @after
6472
*/

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

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

2828
Detected 4 tests that took longer than expected.
2929

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
30+
1. 1.3%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation
31+
2. 1.0%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromXmlConfigurationWhenMethodHasRunInSeparateProcessAnnotation
3232
3. 0.8%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromXmlConfiguration
3333
4. 0.6%s (0.100) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\TestMethod\WithRunInSeparateProcessAnnotation\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromXmlConfiguration
3434

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ public function sleepWithBeforeAnnotation(): void
5959
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
6060
}
6161

62+
/**
63+
* @beforeClass
64+
*/
65+
public static function sleepWithBeforeClassAnnotation(): void
66+
{
67+
Test\Fixture\Sleeper::fromMilliseconds(100)->sleep();
68+
}
69+
6270
/**
6371
* @after
6472
*/
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\Version09\TestCase\WithBeforeClassAnnotation;
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+
* @beforeClass
26+
*/
27+
public static function sleepWithBeforeClassAnnotation(): 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+
}

0 commit comments

Comments
 (0)