Skip to content

Commit 6f5a87e

Browse files
committed
Added customization support for performance thresholds.
1 parent d9a3951 commit 6f5a87e

27 files changed

+113
-31
lines changed

README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The following preview is somewhat atypical but shows all supported output cases
2525

2626
![Preview image][]
2727

28-
This printer makes no attempt to modify the test summary; only runtime output is changed.
28+
Pip makes no attempt to modify the test summary; only runtime output is changed.
2929

3030
## Usage
3131

@@ -51,13 +51,22 @@ This printer makes no attempt to modify the test summary; only runtime output is
5151

5252
4. Enjoy immediate test execution feedback.
5353

54+
### Configuration
55+
56+
Pip's behaviour can be customized by adding `<parameter>` nodes as children of the `<bootstrap>` node in `phpunit.xml`, with `name` and `value` attributes corresponding to the table below.
57+
58+
| Parameter name | Default value | Comments |
59+
|----------------|---------------|--------------------------------------------------|
60+
| perf.slow | 200 (ms) | _Slow_ performance threshold (shown in yellow) |
61+
| perf.vslow | 1000 (ms) | _Very slow_ performance threshold (shown in red) |
62+
5463
## Requirements
5564
56-
| Pip version | PHPUnit versions | Minimum PHP |
57-
|:-----------:|:----------------:|:-----------:|
58-
| 3 | 10 / 11 | 8.1 / 8.2 |
59-
| 2 | *yanked* | - |
60-
| 1 | 5 / 6 | 5.6 / 7.0 |
65+
| Pip version | PHPUnit versions | Minimum PHP version |
66+
|:-----------:|:----------------:|:-------------------:|
67+
| 3 | 10 / 11 | 8.1 / 8.2 |
68+
| 2 | *yanked* | - |
69+
| 1 | 5 / 6 | 5.6 / 7.0 |
6170
6271
## Testing
6372
@@ -94,8 +103,8 @@ Thanks to the following open source projects that inspired this project. Keep be
94103
[Version image]: https://poser.pugx.org/scriptfusion/pip/version "Latest version"
95104
[Downloads]: https://packagist.org/packages/scriptfusion/pip
96105
[Downloads image]: https://poser.pugx.org/scriptfusion/pip/downloads "Total downloads"
97-
[Build]: https://github.com/ScriptFUSION/PHPUnit-Immediate-Printer/actions/workflows/Test.yaml
98-
[Build image]: https://github.com/ScriptFUSION/PHPUnit-Immediate-Printer/actions/workflows/Test.yaml/badge.svg "Build status"
106+
[Build]: https://github.com/ScriptFUSION/PHPUnit-Immediate-Printer/actions/workflows/Tests.yaml
107+
[Build image]: https://github.com/ScriptFUSION/PHPUnit-Immediate-Printer/actions/workflows/Tests.yaml/badge.svg "Build status"
99108
[Coverage]: https://codecov.io/gh/ScriptFUSION/PHPUnit-Immediate-Printer
100109
[Coverage image]: https://codecov.io/github/ScriptFUSION/PHPUnit-Immediate-Printer/graph/badge.svg "Test coverage"
101110

src/PipConfig.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSION\Pip;
5+
6+
final class PipConfig
7+
{
8+
public int $perfSlow = 200;
9+
public int $perfVslow = 1_000;
10+
}

src/PipExtension.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ final class PipExtension implements Extension
1212
{
1313
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
1414
{
15-
$facade->registerTracer(new Printer());
15+
$config = new PipConfig();
16+
$parameters->has('perf.slow') && $config->perfSlow = +$parameters->get('perf.slow');
17+
$parameters->has('perf.vslow') && $config->perfVslow = +$parameters->get('perf.vslow');
18+
19+
$facade->registerTracer(new Printer($config));
1620
$facade->replaceProgressOutput();
1721
}
1822
}

src/Printer.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@
2323

2424
final class Printer implements Tracer
2525
{
26-
private array $performanceThresholds = [
27-
'red' => 1000,
28-
'yellow' => 200,
29-
'green' => 0,
30-
];
26+
private readonly array $performanceThresholds;
3127

3228
private int $totalTests;
3329

@@ -41,6 +37,15 @@ final class Printer implements Tracer
4137

4238
private bool $flawless = true;
4339

40+
public function __construct(private readonly PipConfig $config)
41+
{
42+
$this->performanceThresholds = [
43+
'red' => $config->perfVslow,
44+
'yellow' => $config->perfSlow,
45+
'green' => 0,
46+
];
47+
}
48+
4449
public function trace(Event $event): void
4550
{
4651
if ($event instanceof ExecutionStarted) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<phpunit>
2+
<extensions>
3+
<bootstrap class="ScriptFUSION\Pip\PipExtension">
4+
<parameter name="perf.slow" value="300"/>
5+
<parameter name="perf.vslow" value="600"/>
6+
</bootstrap>
7+
</extensions>
8+
</phpunit>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSIONTest\Pip\config;
5+
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class PerformanceThresholdsTest extends TestCase
9+
{
10+
public function testSlow(): void
11+
{
12+
usleep(300_000);
13+
14+
self::assertTrue(true);
15+
}
16+
17+
public function testVslow(): void
18+
{
19+
usleep(600_000);
20+
21+
self::assertTrue(true);
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Tests that when custom performance thresholds are specified, they overrides the defaults.
3+
4+
--ARGS--
5+
-c test/config/PerformanceThresholds.xml --colors=always test/config/PerformanceThresholdsTest.php
6+
7+
--FILE_EXTERNAL--
8+
../PHPUnit runner.php
9+
10+
--EXPECTF--
11+
PHPUnit %s
12+
13+
Runtime: %s
14+
Configuration: %s
15+
16+
50% . ScriptFUSIONTest\Pip\config\PerformanceThresholdsTest::testSlow (%d ms)
17+
100% . ScriptFUSIONTest\Pip\config\PerformanceThresholdsTest::testVslow (%d ms)
18+
19+
20+
Time: %s
21+
22+
OK (2 tests, 2 assertions)

test/functional/data provider.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A successful test is fed two cases by a data provider.
55
-c test --colors=always test/CapabilitiesTest.php --filter '::testDataProvider\h'
66

77
--FILE_EXTERNAL--
8-
PHPUnit runner.php
8+
../PHPUnit runner.php
99

1010
--EXPECTF--
1111
PHPUnit %s

0 commit comments

Comments
 (0)