Skip to content

Commit

Permalink
Allow to set options at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
robiningelbrecht committed Apr 2, 2023
1 parent 95f5540 commit b37f39d
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 24 deletions.
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ This package requires:

## Configuration

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

```xml
<extensions>
Expand All @@ -38,7 +39,7 @@ Navigate to your `phpunit.xml.dist` file and following config:
```xml
<extensions>
<bootstrap class="RobinIngelbrecht\PHPUnitPrettyPrint\PhpUnitExtension">
<parameter name="convertMethodNamesToSentences" value="true"/>
<parameter name="prettifyMethodNames" value="true"/>
</bootstrap>
</extensions>
```
Expand Down Expand Up @@ -71,7 +72,31 @@ Just run your testsuite like you normally would, but be sure to add `--no-output
vendor/bin/phpunit --no-ouput
```

* <sub>We'll need this until https://github.com/sebastianbergmann/phpunit/issues/5168 lands and gets released.</sub>
*<sub>We'll need this until https://github.com/sebastianbergmann/phpunit/issues/5168 lands and gets released.</sub>

Prettify the method names

```bash
vendor/bin/phpunit --no-output -d --prettify-method-names
```

Use compact mode

```bash
vendor/bin/phpunit --no-output -d --compact
```

Display Chuck Norris quote

```bash
vendor/bin/phpunit --no-output -d --display-quote
```

Combine multiple options

```bash
vendor/bin/phpunit --configuration=tests/phpunit.test.xml --no-output -d --compact -d --display-quote
```

<p align="center">
<img src="readme/example.png" alt="Example">
Expand Down
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
},
"scripts": {
"lint:fix": " ./vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php",
"phpunit:test": "vendor/bin/phpunit --configuration=tests/phpunit.test.xml --no-output",
"phpunit:test:with-method-names": "vendor/bin/phpunit --configuration=tests/phpunit.test-with-method-name-conversion.xml --no-output",
"phpunit:test:with-quotes": "vendor/bin/phpunit --configuration=tests/phpunit.test-with-quotes.xml --no-output",
"phpunit:test:compact": "vendor/bin/phpunit --configuration=tests/phpunit.test-compact-mode.xml --no-output"
"phpunit:test": "vendor/bin/phpunit --configuration=tests/phpunit.test.xml --no-output"
}
}
22 changes: 16 additions & 6 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
class Configuration
{
private function __construct(
private readonly bool $convertMethodNamesToSentences,
private readonly bool $prettifyMethodNames,
private readonly bool $displayQuote,
private readonly bool $useCompactMode,
) {
}

public function convertMethodNamesToSentences(): bool
public function prettifyMethodNames(): bool
{
return $this->convertMethodNamesToSentences;
return $this->prettifyMethodNames;
}

public function displayQuote(): bool
Expand All @@ -30,10 +30,20 @@ public function useCompactMode(): bool

public static function fromParameterCollection(ParameterCollection $parameters): self
{
if (!$prettifyMethodNames = in_array('--prettify-method-names', $_SERVER['argv'], true)) {
$prettifyMethodNames = $parameters->has('prettifyMethodNames') && $parameters->get('prettifyMethodNames');
}
if (!$useCompactMode = in_array('--compact', $_SERVER['argv'], true)) {
$useCompactMode = $parameters->has('useCompactMode') && $parameters->get('useCompactMode');
}
if (!$displayQuote = in_array('--display-quote', $_SERVER['argv'], true)) {
$displayQuote = $parameters->has('displayQuote') && $parameters->get('displayQuote');
}

return new self(
$parameters->has('convertMethodNamesToSentences') && $parameters->get('convertMethodNamesToSentences'),
$parameters->has('displayQuote') && $parameters->get('displayQuote'),
$parameters->has('useCompactMode') && $parameters->get('useCompactMode'),
$prettifyMethodNames,
$displayQuote,
$useCompactMode,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class ApplicationStartedSubscriber implements StartedSubscriber
{
public function notify(Started $event): void
{
render('<div></div>');
render('<br />');
render(sprintf(
'<div>Runtime:%s%s</div>',
str_repeat('&nbsp;', 7),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function notify(Configured $event): void
'<div>Configuration: %s</div>',
$event->configuration()->configurationFile()
));
render('<div></div>');
render('<br />');
}
}
6 changes: 3 additions & 3 deletions src/Subscriber/TestSuite/TestSuiteFinishedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public function notify(Finished $event): void
'<div class="ml-1"><span class="text-%s font-bold"> %s</span> <span class="text-neutral-400">%s [%ss]</span></div>',
$unitTestOutcome->getIcon()->getColor(),
$unitTestOutcome->getIcon()->value,
$this->configuration->convertMethodNamesToSentences() ? $this->formatMethodName($methodName) : $methodName,
$this->configuration->prettifyMethodNames() ? $this->prettifyMethodName($methodName) : $methodName,
round($unitTestOutcome->getDuration()->asFloat(), 4)
));
}

render('<div></div>');
render('<br />');
}

private function formatMethodName(string $methodName): ?string
private function prettifyMethodName(string $methodName): ?string
{
// Convert non-breaking method name to camelCase
$methodName = str_replace(' ', '', ucwords($methodName, ' '));
Expand Down
60 changes: 56 additions & 4 deletions tests/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,30 @@ public function testPrintWithoutConfig(): void
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
}

public function testPrintWithMethodNameConversion(): void
public function testPrettifyMethodNames(): void
{
$command = [
'vendor/bin/phpunit',
'--configuration=tests/phpunit.test-with-method-name-conversion.xml',
'--configuration=tests/phpunit.test-prettify-method-names.xml',
'--no-output',
];

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

public function testPrettifyMethodNamesAtRunTime(): void
{
$command = [
'vendor/bin/phpunit',
'--configuration=tests/phpunit.test.xml',
'--no-output',
'-d --prettify-method-names',
];
exec(implode(' ', $command), $out);
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
}

public function testPrintCompactMode(): void
{
$command = [
Expand All @@ -46,7 +58,19 @@ public function testPrintCompactMode(): void
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
}

public function testPrintWithQuotes(): void
public function testPrintCompactModeAtRunTime(): void
{
$command = [
'vendor/bin/phpunit',
'--configuration=tests/phpunit.test.xml',
'--no-output',
'-d --compact',
];
exec(implode(' ', $command), $out);
$this->assertMatchesSnapshot(implode(PHP_EOL, $out), new SnapshotTextDriver());
}

public function testPrintWithQuote(): void
{
$command = [
'vendor/bin/phpunit',
Expand All @@ -73,11 +97,39 @@ public function testPrintWithQuotes(): void
}
}

public function testPrintWithQuoteAtRuntime(): void
{
$command = [
'vendor/bin/phpunit',
'--configuration=tests/phpunit.test.xml',
'--no-output',
'-d --display-quote',
];

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

$print = implode(PHP_EOL, $out);

$printContainsQuote = false;
foreach (Quotes::getAll() as $quote) {
if (!str_contains($print, $quote)) {
continue;
}

$printContainsQuote = true;
$this->addToAssertionCount(1);
}

if (!$printContainsQuote) {
$this->fail('Quote not found');
}
}

public function testPrintWhenNoOutputArgumentIsProvided(): void
{
$command = [
'vendor/bin/phpunit',
'--configuration=tests/phpunit.test-with-method-name-conversion.xml',
'--configuration=tests/phpunit.test-prettify-method-names.xml',
];

exec(implode(' ', $command), $out);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

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

FAIL Tests\ExampleTests\TestThatHasAllStatusesTest, 15 tests 88%
✓ success [DURATION-IN-SECONDS]
Expand Down
57 changes: 57 additions & 0 deletions tests/__snapshots__/OutputTest__testPrettifyMethodNames__1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

Runtime:       PHPUnit SOME-PHPUNIT-VERSION using PHP SOME-PHP-VERSION (cli) on SOME-OS
Configuration: tests/phpunit.test-prettify-method-names.xml

FAIL Tests\ExampleTests\TestThatHasAllStatusesTest, 15 tests 88%
✓ success [DURATION-IN-SECONDS]
⨯ fail [DURATION-IN-SECONDS]
⨯ fail with diff [DURATION-IN-SECONDS]
⨯ error [DURATION-IN-SECONDS]
✓ risky [DURATION-IN-SECONDS]
! skip [DURATION-IN-SECONDS]
! incomplete [DURATION-IN-SECONDS]
✓ should convert title case to lower cased words [DURATION-IN-SECONDS]
✓ should convert snake case to lower cased words [DURATION-IN-SECONDS]
✓ can contain 1 or 99 numbers [DURATION-IN-SECONDS]
✓ 123 can start or end with numbers 456 [DURATION-IN-SECONDS]
✓ should preserve and pa tia pita zed words [DURATION-IN-SECONDS]
✓ with named datasets with data set [DURATION-IN-SECONDS]
✓ with named datasets with data set data set 2 [DURATION-IN-SECONDS]
✓ with named datasets with data set set 3 [DURATION-IN-SECONDS]

PASS Tests\ExampleTests\TestThatPassesTest, 2 tests 100%
✓ do basic assertions [DURATION-IN-SECONDS]
✓ do some more assertions [DURATION-IN-SECONDS]

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
FAILED Tests\ExampleTests\TestThatHasAllStatusesTest::testFail PHPUnit\Framework\ExpectationFailedException
Failed asserting that false is true.

1▕ /tests/ExampleTests/TestThatHasAllStatusesTest.php:19

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
FAILED Tests\ExampleTests\TestThatHasAllStatusesTest::testFailWithDiff PHPUnit\Framework\ExpectationFailedException
Failed asserting that two arrays are equal.

--- Expected
+++ Actual
@@ @@
Array (
- 0 => 'one'
- 1 => 'two'
+ 0 => 'two'
+ 1 => 'one'
)


1▕ /tests/ExampleTests/TestThatHasAllStatusesTest.php:26

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
FAILED Tests\ExampleTests\TestThatHasAllStatusesTest::testError Exception
error

1▕ /tests/ExampleTests/TestThatHasAllStatusesTest.php:32


Tests:    1 error(s), 2 failed, 1 incomplete, 1 skipped, 12 passed (17 tests, 15 assertions)
Duration: DURATION-IN-SECONDS
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

Runtime:       PHPUnit SOME-PHPUNIT-VERSION using PHP SOME-PHP-VERSION (cli) on SOME-OS
Configuration: tests/phpunit.test.xml

FAIL Tests\ExampleTests\TestThatHasAllStatusesTest, 15 tests 88%
PASS Tests\ExampleTests\TestThatPassesTest, 2 tests 100%
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
FAILED Tests\ExampleTests\TestThatHasAllStatusesTest::testFail PHPUnit\Framework\ExpectationFailedException
Failed asserting that false is true.

1▕ /tests/ExampleTests/TestThatHasAllStatusesTest.php:19

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
FAILED Tests\ExampleTests\TestThatHasAllStatusesTest::testFailWithDiff PHPUnit\Framework\ExpectationFailedException
Failed asserting that two arrays are equal.

--- Expected
+++ Actual
@@ @@
Array (
- 0 => 'one'
- 1 => 'two'
+ 0 => 'two'
+ 1 => 'one'
)


1▕ /tests/ExampleTests/TestThatHasAllStatusesTest.php:26

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
FAILED Tests\ExampleTests\TestThatHasAllStatusesTest::testError Exception
error

1▕ /tests/ExampleTests/TestThatHasAllStatusesTest.php:32


Tests:    1 error(s), 2 failed, 1 incomplete, 1 skipped, 12 passed (17 tests, 15 assertions)
Duration: DURATION-IN-SECONDS
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
cacheDirectory=".phpunit.cache">
<extensions>
<bootstrap class="RobinIngelbrecht\PHPUnitPrettyPrint\PhpUnitExtension">
<parameter name="convertMethodNamesToSentences" value="true"/>
<parameter name="prettifyMethodNames" value="true"/>
</bootstrap>
</extensions>

Expand Down

0 comments on commit b37f39d

Please sign in to comment.