Skip to content

Commit b37f39d

Browse files
Allow to set options at runtime
1 parent 95f5540 commit b37f39d

11 files changed

+203
-24
lines changed

README.md

+28-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ This package requires:
2222

2323
## Configuration
2424

25-
Navigate to your `phpunit.xml.dist` file and following config:
25+
Navigate to your `phpunit.xml.dist` file and add following config to set default options
26+
(you can also set these options at run time):
2627

2728
```xml
2829
<extensions>
@@ -38,7 +39,7 @@ Navigate to your `phpunit.xml.dist` file and following config:
3839
```xml
3940
<extensions>
4041
<bootstrap class="RobinIngelbrecht\PHPUnitPrettyPrint\PhpUnitExtension">
41-
<parameter name="convertMethodNamesToSentences" value="true"/>
42+
<parameter name="prettifyMethodNames" value="true"/>
4243
</bootstrap>
4344
</extensions>
4445
```
@@ -71,7 +72,31 @@ Just run your testsuite like you normally would, but be sure to add `--no-output
7172
vendor/bin/phpunit --no-ouput
7273
```
7374

74-
* <sub>We'll need this until https://github.com/sebastianbergmann/phpunit/issues/5168 lands and gets released.</sub>
75+
*<sub>We'll need this until https://github.com/sebastianbergmann/phpunit/issues/5168 lands and gets released.</sub>
76+
77+
Prettify the method names
78+
79+
```bash
80+
vendor/bin/phpunit --no-output -d --prettify-method-names
81+
```
82+
83+
Use compact mode
84+
85+
```bash
86+
vendor/bin/phpunit --no-output -d --compact
87+
```
88+
89+
Display Chuck Norris quote
90+
91+
```bash
92+
vendor/bin/phpunit --no-output -d --display-quote
93+
```
94+
95+
Combine multiple options
96+
97+
```bash
98+
vendor/bin/phpunit --configuration=tests/phpunit.test.xml --no-output -d --compact -d --display-quote
99+
```
75100

76101
<p align="center">
77102
<img src="readme/example.png" alt="Example">

composer.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
},
4040
"scripts": {
4141
"lint:fix": " ./vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php",
42-
"phpunit:test": "vendor/bin/phpunit --configuration=tests/phpunit.test.xml --no-output",
43-
"phpunit:test:with-method-names": "vendor/bin/phpunit --configuration=tests/phpunit.test-with-method-name-conversion.xml --no-output",
44-
"phpunit:test:with-quotes": "vendor/bin/phpunit --configuration=tests/phpunit.test-with-quotes.xml --no-output",
45-
"phpunit:test:compact": "vendor/bin/phpunit --configuration=tests/phpunit.test-compact-mode.xml --no-output"
42+
"phpunit:test": "vendor/bin/phpunit --configuration=tests/phpunit.test.xml --no-output"
4643
}
4744
}

src/Configuration.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
class Configuration
88
{
99
private function __construct(
10-
private readonly bool $convertMethodNamesToSentences,
10+
private readonly bool $prettifyMethodNames,
1111
private readonly bool $displayQuote,
1212
private readonly bool $useCompactMode,
1313
) {
1414
}
1515

16-
public function convertMethodNamesToSentences(): bool
16+
public function prettifyMethodNames(): bool
1717
{
18-
return $this->convertMethodNamesToSentences;
18+
return $this->prettifyMethodNames;
1919
}
2020

2121
public function displayQuote(): bool
@@ -30,10 +30,20 @@ public function useCompactMode(): bool
3030

3131
public static function fromParameterCollection(ParameterCollection $parameters): self
3232
{
33+
if (!$prettifyMethodNames = in_array('--prettify-method-names', $_SERVER['argv'], true)) {
34+
$prettifyMethodNames = $parameters->has('prettifyMethodNames') && $parameters->get('prettifyMethodNames');
35+
}
36+
if (!$useCompactMode = in_array('--compact', $_SERVER['argv'], true)) {
37+
$useCompactMode = $parameters->has('useCompactMode') && $parameters->get('useCompactMode');
38+
}
39+
if (!$displayQuote = in_array('--display-quote', $_SERVER['argv'], true)) {
40+
$displayQuote = $parameters->has('displayQuote') && $parameters->get('displayQuote');
41+
}
42+
3343
return new self(
34-
$parameters->has('convertMethodNamesToSentences') && $parameters->get('convertMethodNamesToSentences'),
35-
$parameters->has('displayQuote') && $parameters->get('displayQuote'),
36-
$parameters->has('useCompactMode') && $parameters->get('useCompactMode'),
44+
$prettifyMethodNames,
45+
$displayQuote,
46+
$useCompactMode,
3747
);
3848
}
3949
}

src/Subscriber/Application/ApplicationStartedSubscriber.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class ApplicationStartedSubscriber implements StartedSubscriber
1111
{
1212
public function notify(Started $event): void
1313
{
14-
render('<div></div>');
14+
render('<br />');
1515
render(sprintf(
1616
'<div>Runtime:%s%s</div>',
1717
str_repeat('&nbsp;', 7),

src/Subscriber/TestRunner/TestRunnerConfiguredSubscriber.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public function notify(Configured $event): void
1515
'<div>Configuration: %s</div>',
1616
$event->configuration()->configurationFile()
1717
));
18-
render('<div></div>');
18+
render('<br />');
1919
}
2020
}

src/Subscriber/TestSuite/TestSuiteFinishedSubscriber.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ public function notify(Finished $event): void
5959
'<div class="ml-1"><span class="text-%s font-bold"> %s</span> <span class="text-neutral-400">%s [%ss]</span></div>',
6060
$unitTestOutcome->getIcon()->getColor(),
6161
$unitTestOutcome->getIcon()->value,
62-
$this->configuration->convertMethodNamesToSentences() ? $this->formatMethodName($methodName) : $methodName,
62+
$this->configuration->prettifyMethodNames() ? $this->prettifyMethodName($methodName) : $methodName,
6363
round($unitTestOutcome->getDuration()->asFloat(), 4)
6464
));
6565
}
6666

67-
render('<div></div>');
67+
render('<br />');
6868
}
6969

70-
private function formatMethodName(string $methodName): ?string
70+
private function prettifyMethodName(string $methodName): ?string
7171
{
7272
// Convert non-breaking method name to camelCase
7373
$methodName = str_replace(' ', '', ucwords($methodName, ' '));

tests/OutputTest.php

+56-4
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,30 @@ public function testPrintWithoutConfig(): void
2222
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
2323
}
2424

25-
public function testPrintWithMethodNameConversion(): void
25+
public function testPrettifyMethodNames(): void
2626
{
2727
$command = [
2828
'vendor/bin/phpunit',
29-
'--configuration=tests/phpunit.test-with-method-name-conversion.xml',
29+
'--configuration=tests/phpunit.test-prettify-method-names.xml',
3030
'--no-output',
3131
];
3232

3333
exec(implode(' ', $command), $out);
3434
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
3535
}
3636

37+
public function testPrettifyMethodNamesAtRunTime(): void
38+
{
39+
$command = [
40+
'vendor/bin/phpunit',
41+
'--configuration=tests/phpunit.test.xml',
42+
'--no-output',
43+
'-d --prettify-method-names',
44+
];
45+
exec(implode(' ', $command), $out);
46+
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
47+
}
48+
3749
public function testPrintCompactMode(): void
3850
{
3951
$command = [
@@ -46,7 +58,19 @@ public function testPrintCompactMode(): void
4658
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
4759
}
4860

49-
public function testPrintWithQuotes(): void
61+
public function testPrintCompactModeAtRunTime(): void
62+
{
63+
$command = [
64+
'vendor/bin/phpunit',
65+
'--configuration=tests/phpunit.test.xml',
66+
'--no-output',
67+
'-d --compact',
68+
];
69+
exec(implode(' ', $command), $out);
70+
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
71+
}
72+
73+
public function testPrintWithQuote(): void
5074
{
5175
$command = [
5276
'vendor/bin/phpunit',
@@ -73,11 +97,39 @@ public function testPrintWithQuotes(): void
7397
}
7498
}
7599

100+
public function testPrintWithQuoteAtRuntime(): void
101+
{
102+
$command = [
103+
'vendor/bin/phpunit',
104+
'--configuration=tests/phpunit.test.xml',
105+
'--no-output',
106+
'-d --display-quote',
107+
];
108+
109+
exec(implode(' ', $command), $out);
110+
111+
$print = implode(PHP_EOL, $out);
112+
113+
$printContainsQuote = false;
114+
foreach (Quotes::getAll() as $quote) {
115+
if (!str_contains($print, $quote)) {
116+
continue;
117+
}
118+
119+
$printContainsQuote = true;
120+
$this->addToAssertionCount(1);
121+
}
122+
123+
if (!$printContainsQuote) {
124+
$this->fail('Quote not found');
125+
}
126+
}
127+
76128
public function testPrintWhenNoOutputArgumentIsProvided(): void
77129
{
78130
$command = [
79131
'vendor/bin/phpunit',
80-
'--configuration=tests/phpunit.test-with-method-name-conversion.xml',
132+
'--configuration=tests/phpunit.test-prettify-method-names.xml',
81133
];
82134

83135
exec(implode(' ', $command), $out);

tests/__snapshots__/OutputTest__testPrintWithMethodNameConversion__1.txt renamed to tests/__snapshots__/OutputTest__testPrettifyMethodNamesAtRunTime__1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
Runtime:       PHPUnit SOME-PHPUNIT-VERSION using PHP SOME-PHP-VERSION (cli) on SOME-OS
3-
Configuration: tests/phpunit.test-with-method-name-conversion.xml
3+
Configuration: tests/phpunit.test.xml
44

55
FAIL Tests\ExampleTests\TestThatHasAllStatusesTest, 15 tests 88%
66
✓ success [DURATION-IN-SECONDS]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
Runtime:       PHPUnit SOME-PHPUNIT-VERSION using PHP SOME-PHP-VERSION (cli) on SOME-OS
3+
Configuration: tests/phpunit.test-prettify-method-names.xml
4+
5+
FAIL Tests\ExampleTests\TestThatHasAllStatusesTest, 15 tests 88%
6+
✓ success [DURATION-IN-SECONDS]
7+
⨯ fail [DURATION-IN-SECONDS]
8+
⨯ fail with diff [DURATION-IN-SECONDS]
9+
⨯ error [DURATION-IN-SECONDS]
10+
✓ risky [DURATION-IN-SECONDS]
11+
! skip [DURATION-IN-SECONDS]
12+
! incomplete [DURATION-IN-SECONDS]
13+
✓ should convert title case to lower cased words [DURATION-IN-SECONDS]
14+
✓ should convert snake case to lower cased words [DURATION-IN-SECONDS]
15+
✓ can contain 1 or 99 numbers [DURATION-IN-SECONDS]
16+
✓ 123 can start or end with numbers 456 [DURATION-IN-SECONDS]
17+
✓ should preserve and pa tia pita zed words [DURATION-IN-SECONDS]
18+
✓ with named datasets with data set [DURATION-IN-SECONDS]
19+
✓ with named datasets with data set data set 2 [DURATION-IN-SECONDS]
20+
✓ with named datasets with data set set 3 [DURATION-IN-SECONDS]
21+
22+
PASS Tests\ExampleTests\TestThatPassesTest, 2 tests 100%
23+
✓ do basic assertions [DURATION-IN-SECONDS]
24+
✓ do some more assertions [DURATION-IN-SECONDS]
25+
26+
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
27+
FAILED Tests\ExampleTests\TestThatHasAllStatusesTest::testFail PHPUnit\Framework\ExpectationFailedException
28+
Failed asserting that false is true.
29+
30+
1▕ /tests/ExampleTests/TestThatHasAllStatusesTest.php:19
31+
32+
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
33+
FAILED Tests\ExampleTests\TestThatHasAllStatusesTest::testFailWithDiff PHPUnit\Framework\ExpectationFailedException
34+
Failed asserting that two arrays are equal.
35+
36+
--- Expected
37+
+++ Actual
38+
@@ @@
39+
Array (
40+
- 0 => 'one'
41+
- 1 => 'two'
42+
+ 0 => 'two'
43+
+ 1 => 'one'
44+
)
45+
46+
47+
1▕ /tests/ExampleTests/TestThatHasAllStatusesTest.php:26
48+
49+
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
50+
FAILED Tests\ExampleTests\TestThatHasAllStatusesTest::testError Exception
51+
error
52+
53+
1▕ /tests/ExampleTests/TestThatHasAllStatusesTest.php:32
54+
55+
56+
Tests:    1 error(s), 2 failed, 1 incomplete, 1 skipped, 12 passed (17 tests, 15 assertions)
57+
Duration: DURATION-IN-SECONDS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
Runtime:       PHPUnit SOME-PHPUNIT-VERSION using PHP SOME-PHP-VERSION (cli) on SOME-OS
3+
Configuration: tests/phpunit.test.xml
4+
5+
FAIL Tests\ExampleTests\TestThatHasAllStatusesTest, 15 tests 88%
6+
PASS Tests\ExampleTests\TestThatPassesTest, 2 tests 100%
7+
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
8+
FAILED Tests\ExampleTests\TestThatHasAllStatusesTest::testFail PHPUnit\Framework\ExpectationFailedException
9+
Failed asserting that false is true.
10+
11+
1▕ /tests/ExampleTests/TestThatHasAllStatusesTest.php:19
12+
13+
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
14+
FAILED Tests\ExampleTests\TestThatHasAllStatusesTest::testFailWithDiff PHPUnit\Framework\ExpectationFailedException
15+
Failed asserting that two arrays are equal.
16+
17+
--- Expected
18+
+++ Actual
19+
@@ @@
20+
Array (
21+
- 0 => 'one'
22+
- 1 => 'two'
23+
+ 0 => 'two'
24+
+ 1 => 'one'
25+
)
26+
27+
28+
1▕ /tests/ExampleTests/TestThatHasAllStatusesTest.php:26
29+
30+
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
31+
FAILED Tests\ExampleTests\TestThatHasAllStatusesTest::testError Exception
32+
error
33+
34+
1▕ /tests/ExampleTests/TestThatHasAllStatusesTest.php:32
35+
36+
37+
Tests:    1 error(s), 2 failed, 1 incomplete, 1 skipped, 12 passed (17 tests, 15 assertions)
38+
Duration: DURATION-IN-SECONDS

tests/phpunit.test-with-method-name-conversion.xml renamed to tests/phpunit.test-prettify-method-names.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
cacheDirectory=".phpunit.cache">
1010
<extensions>
1111
<bootstrap class="RobinIngelbrecht\PHPUnitPrettyPrint\PhpUnitExtension">
12-
<parameter name="convertMethodNamesToSentences" value="true"/>
12+
<parameter name="prettifyMethodNames" value="true"/>
1313
</bootstrap>
1414
</extensions>
1515

0 commit comments

Comments
 (0)