Skip to content

Commit

Permalink
Merge pull request #217 from ergebnis/feature/maximum-count
Browse files Browse the repository at this point in the history
Enhancement: Allow configuring the maximum count via `maximum-count` parameter
  • Loading branch information
localheinz committed Feb 10, 2023
2 parents bb48067 + a1df9b2 commit 0e9fa24
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For a full diff see [`1.0.0...main`][1.0.0...main].
### Changed

- Allowed configuring the maximum duration via `maximum-duration` parameter ([#212]), by [@localheinz]
- Allowed configuring the maximum count via `maximum-count` parameter ([#217]), by [@localheinz]

### Fixed

Expand Down Expand Up @@ -78,5 +79,6 @@ For a full diff see [`7afa59c...1.0.0`][7afa59c...1.0.0].
[#49]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/49
[#211]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/211
[#212]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/212
[#217]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/217

[@localheinz]: https://github.com/localheinz
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ To bootstrap the extension, adjust your `phpunit.xml` configuration file:

You can configure the extension with the following parameters in your `phpunit.xml` configuration file:

- `maximum-count`, an `int`, the maximum count of slow test that should be listed, defaults to `3`
- `maximum-duration`, an `int`, the maximum duration in milliseconds for all tests, defaults to `250`

The following example configures the maximum duration for all tests to 250ms:
The following example configures the maximum count of slow tests to three, and the maximum duration for all tests to 250 milliseconds:

```xml
<phpunit
Expand Down
4 changes: 4 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function bootstrap(
): void {
$maximumCount = MaximumCount::fromInt(3);

if ($parameters->has('maximum-count')) {
$maximumCount = MaximumCount::fromInt((int) $parameters->get('maximum-count'));
}

$maximumDuration = MaximumDuration::fromMilliseconds(125);

if ($parameters->has('maximum-duration')) {
Expand Down
32 changes: 32 additions & 0 deletions test/EndToEnd/MaximumCount/Default/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../vendor/phpunit/phpunit/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"/>
</extensions>
<testsuites>
<testsuite name="unit">
<directory>../../../Fixture/</directory>
</testsuite>
</testsuites>
</phpunit>
36 changes: 36 additions & 0 deletions test/EndToEnd/MaximumCount/Default/test.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
With default configuration of extension
--FILE--
<?php

declare(strict_types=1);

use PHPUnit\TextUI;

$_SERVER['argv'][] = '--configuration=test/EndToEnd/MaximumDuration/Default/phpunit.xml';

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

$application = new TextUI\Application();

$application->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s
Configuration: test/EndToEnd/MaximumDuration/Default/phpunit.xml
Random Seed: %s

......... 9 / 9 (100%)

Detected 8 tests that took longer than expected.

1,0%s ms (125 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\SleeperTest::testSleeperSleepsOneSecond
5%s ms (125 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\SleeperTest::testSleeperSleepsWithSlowThresholdAnnotation#1
4%s ms (125 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\SleeperTest::testSleeperSleepsWithDocBlockWithSlowThresholdAnnotationWhereValueIsNotAnInt

There are 5 additional slow tests that are not listed here.

Time: %s, Memory: %s

OK (9 tests, 9 assertions)
28 changes: 28 additions & 0 deletions test/EndToEnd/MaximumCount/Five/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../vendor/phpunit/phpunit/phpunit.xsd"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
bootstrap="../../../../vendor/autoload.php"
cacheResult="false"
colors="true"
columns="max"
executionOrder="random"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
>
<extensions>
<bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension">
<parameter name="maximum-count" value="5"/>
</bootstrap>
</extensions>
<testsuites>
<testsuite name="unit">
<directory>../../../Fixture/</directory>
</testsuite>
</testsuites>
</phpunit>
38 changes: 38 additions & 0 deletions test/EndToEnd/MaximumCount/Five/test.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Configuring "maximum-duration" parameter to 50 milliseconds
--FILE--
<?php

declare(strict_types=1);

use PHPUnit\TextUI;

$_SERVER['argv'][] = '--configuration=test/EndToEnd/MaximumCount/Five/phpunit.xml';

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

$application = new TextUI\Application();

$application->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s
Configuration: test/EndToEnd/MaximumCount/Five/phpunit.xml
Random Seed: %s

......... 9 / 9 (100%)

Detected 8 tests that took longer than expected.

1,0%s ms (125 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\SleeperTest::testSleeperSleepsOneSecond
5%s ms (125 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\SleeperTest::testSleeperSleepsWithSlowThresholdAnnotation#1
4%s ms (125 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\SleeperTest::testSleeperSleepsWithDocBlockWithSlowThresholdAnnotationWhereValueIsNotAnInt
4%s ms (125 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\SleeperTest::testSleeperSleepsWithDocBlockWithoutSlowThresholdAnnotation
3%s ms (125 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture\SleeperTest::testSleeperSleepsThreeHundredMilliseconds

There are 3 additional slow tests that are not listed here.

Time: %s, Memory: %s

OK (9 tests, 9 assertions)

0 comments on commit 0e9fa24

Please sign in to comment.