Skip to content

Commit

Permalink
Merge pull request #2 from robiningelbrecht/enable-disable-at-runtime
Browse files Browse the repository at this point in the history
Enable/disable at runtime
  • Loading branch information
robiningelbrecht committed Aug 21, 2023
2 parents 4681dcd + c6a53f9 commit edf25aa
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ Also make sure the `color` attribute is set to `true`:
</extensions>
```

* Disable pretty print. This can be useful when you only want to prettify the output when forced via CLI (see <a href="#usage">usage</a>).

```xml
<extensions>
<bootstrap class="RobinIngelbrecht\PHPUnitPrettyPrint\PhpUnitExtension">
<parameter name="enableByDefault" value="false"/>
</bootstrap>
</extensions>
```

## Usage

```bash
Expand Down Expand Up @@ -118,6 +128,13 @@ Display Chuck Norris quote
<img src="readme/example-quote.png" alt="Example quote">
</p>

Enable/disable pretty print

```bash
> vendor/bin/phpunit -d --enable-pretty-print
> vendor/bin/phpunit -d --disable-pretty-print
```

Combine multiple options

```bash
Expand Down
24 changes: 21 additions & 3 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public function useCompactMode(): bool
public static function fromParameterCollection(ParameterCollection $parameters): self
{
if (!$useProfiling = in_array('--profiling', $_SERVER['argv'], true)) {
$useProfiling = $parameters->has('displayProfiling') && $parameters->get('displayProfiling');
$useProfiling = $parameters->has('displayProfiling') && !self::isFalsy($parameters->get('displayProfiling'));
}
if (!$useCompactMode = in_array('--compact', $_SERVER['argv'], true)) {
$useCompactMode = $parameters->has('useCompactMode') && $parameters->get('useCompactMode');
$useCompactMode = $parameters->has('useCompactMode') && !self::isFalsy($parameters->get('useCompactMode'));
}
if (!$displayQuote = in_array('--display-quote', $_SERVER['argv'], true)) {
$displayQuote = $parameters->has('displayQuote') && $parameters->get('displayQuote');
$displayQuote = $parameters->has('displayQuote') && !self::isFalsy($parameters->get('displayQuote'));
}

return new self(
Expand All @@ -46,4 +46,22 @@ public static function fromParameterCollection(ParameterCollection $parameters):
$useCompactMode,
);
}

public static function isFalsy(mixed $value): bool
{
if (is_bool($value)) {
return !$value;
}
if ('true' === $value) {
return false;
}
if ('false' === $value) {
return true;
}
if (is_int($value)) {
return !$value;
}

return true;
}
}
27 changes: 27 additions & 0 deletions src/PhpUnitExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ final class PhpUnitExtension implements Extension
{
public function bootstrap(PHPUnitConfiguration $configuration, Facade $facade, ParameterCollection $parameters): void
{
if (!$this->isEnabled($parameters)) {
return;
}

$configuration = Configuration::fromParameterCollection($parameters);

$facade->replaceOutput();
Expand All @@ -34,4 +38,27 @@ public function bootstrap(PHPUnitConfiguration $configuration, Facade $facade, P
}
$facade->registerSubscriber(new ApplicationFinishedSubscriber());
}

private function isEnabled(ParameterCollection $parameters): bool
{
if (!$parameters->has('enableByDefault') &&
!in_array('--enable-pretty-print', $_SERVER['argv'], true) &&
!in_array('--disable-pretty-print', $_SERVER['argv'], true)) {
// Nothing has been set, assume the extension is enabled for backwards compatible reasons.
return true;
}

if (in_array('--enable-pretty-print', $_SERVER['argv'], true)) {
return true;
}
if (in_array('--disable-pretty-print', $_SERVER['argv'], true)) {
return false;
}

if ($parameters->has('enableByDefault') && !Configuration::isFalsy($parameters->get('enableByDefault'))) {
return true;
}

return false;
}
}
25 changes: 25 additions & 0 deletions tests/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,29 @@ public function testPrintWithQuoteAtRuntime(): void
$this->fail('Quote not found');
}
}

public function testItShouldBeEnabled(): void
{
$command = [
'vendor/bin/phpunit',
'tests/ExampleTests/TestThatPassesTest.php',
'--configuration=tests/phpunit.test-disable-by-default.xml',
'-d --enable-pretty-print',
];
exec(implode(' ', $command), $out);
$output = implode(PHP_EOL, $out);
$this->assertStringContainsString('Tests: 2 passed (4 assertions)', $output);
}

public function testItShouldBeDisabled(): void
{
$command = [
'vendor/bin/phpunit',
'tests/ExampleTests/TestThatPassesTest.php',
'--configuration=tests/phpunit.test-disable-by-default.xml',
];
exec(implode(' ', $command), $out);
$output = implode(PHP_EOL, $out);
$this->assertStringContainsString('OK (2 tests, 4 assertions)', $output);
}
}
12 changes: 12 additions & 0 deletions tests/Unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,16 @@ public function testFromParameterCollectionWithServerArguments(): void
$this->assertTrue($configuration->useCompactMode());
$this->assertTrue($configuration->displayQuote());
}

public function testIsFalsy(): void
{
$this->assertTrue(Configuration::isFalsy(0));
$this->assertTrue(Configuration::isFalsy('false'));
$this->assertTrue(Configuration::isFalsy(false));
$this->assertTrue(Configuration::isFalsy('test'));

$this->assertFalse(Configuration::isFalsy(1));
$this->assertFalse(Configuration::isFalsy('true'));
$this->assertFalse(Configuration::isFalsy(true));
}
}
163 changes: 163 additions & 0 deletions tests/Unit/PhpUnitExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,167 @@ public function testBootstrapWithDisplayQuote(): void
$this->assertNotContains('COLLISION_PRINTER_COMPACT', array_keys($_SERVER));
$this->assertNotContains('COLLISION_PRINTER_PROFILE', array_keys($_SERVER));
}

public function testItShouldBeEnabledThroughCli(): void
{
$facade = $this->createMock(Facade::class);
$configuration = (new Builder())->build([]);
$parameters = ParameterCollection::fromArray([
'displayProfiling' => 'true',
'useCompactMode' => 'true',
'displayQuote' => 'true',
'enableByDefault' => 'false',
]);

$extension = new PhpUnitExtension();

$facade
->expects($this->once())
->method('replaceOutput');

$facade
->expects($this->once())
->method('replaceProgressOutput');

$facade
->expects($this->once())
->method('replaceResultOutput');

$facade
->expects($this->once())
->method('registerSubscriber')
->with(new ApplicationFinishedSubscriber());

$_SERVER['argv'][] = '--enable-pretty-print';

$extension->bootstrap(
$configuration,
$facade,
$parameters
);

$this->assertContains('COLLISION_PRINTER_COMPACT', array_keys($_SERVER));
$this->assertContains('COLLISION_PRINTER_PROFILE', array_keys($_SERVER));
}

public function testItShouldBeDisabledThroughCli(): void
{
$facade = $this->createMock(Facade::class);
$configuration = (new Builder())->build([]);
$parameters = ParameterCollection::fromArray([
'displayProfiling' => 'true',
'useCompactMode' => 'true',
'displayQuote' => 'true',
]);

$extension = new PhpUnitExtension();

$facade
->expects($this->never())
->method('replaceOutput');

$facade
->expects($this->never())
->method('replaceProgressOutput');

$facade
->expects($this->never())
->method('replaceResultOutput');

$facade
->expects($this->never())
->method('registerSubscriber')
->with(new ApplicationFinishedSubscriber());

$_SERVER['argv'][] = '--disable-pretty-print';

$extension->bootstrap(
$configuration,
$facade,
$parameters
);

$this->assertNotContains('COLLISION_PRINTER_COMPACT', array_keys($_SERVER));
$this->assertNotContains('COLLISION_PRINTER_PROFILE', array_keys($_SERVER));
}

public function testItShouldBeEnabledWithParameter(): void
{
$facade = $this->createMock(Facade::class);
$configuration = (new Builder())->build([]);
$parameters = ParameterCollection::fromArray([
'displayProfiling' => 'true',
'useCompactMode' => 'true',
'displayQuote' => 'true',
'enableByDefault' => 'true',
]);

$extension = new PhpUnitExtension();

$facade
->expects($this->once())
->method('replaceOutput');

$facade
->expects($this->once())
->method('replaceProgressOutput');

$facade
->expects($this->once())
->method('replaceResultOutput');

$facade
->expects($this->once())
->method('registerSubscriber')
->with(new ApplicationFinishedSubscriber());

$extension->bootstrap(
$configuration,
$facade,
$parameters
);

$this->assertContains('COLLISION_PRINTER_COMPACT', array_keys($_SERVER));
$this->assertContains('COLLISION_PRINTER_PROFILE', array_keys($_SERVER));
}

public function testItShouldBeDisabledWithParameter(): void
{
$facade = $this->createMock(Facade::class);
$configuration = (new Builder())->build([]);
$parameters = ParameterCollection::fromArray([
'displayProfiling' => 'true',
'useCompactMode' => 'true',
'displayQuote' => 'true',
'enableByDefault' => 'false',
]);

$extension = new PhpUnitExtension();

$facade
->expects($this->never())
->method('replaceOutput');

$facade
->expects($this->never())
->method('replaceProgressOutput');

$facade
->expects($this->never())
->method('replaceResultOutput');

$facade
->expects($this->never())
->method('registerSubscriber')
->with(new ApplicationFinishedSubscriber());

$extension->bootstrap(
$configuration,
$facade,
$parameters
);

$this->assertNotContains('COLLISION_PRINTER_COMPACT', array_keys($_SERVER));
$this->assertNotContains('COLLISION_PRINTER_PROFILE', array_keys($_SERVER));
}
}
28 changes: 28 additions & 0 deletions tests/phpunit.test-disable-by-default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
colors="true"
defaultTestSuite="unit"
cacheDirectory=".phpunit.cache">
<extensions>
<bootstrap class="RobinIngelbrecht\PHPUnitPrettyPrint\PhpUnitExtension">
<parameter name="enableByDefault" value="false"/>
</bootstrap>
</extensions>

<testsuites>
<testsuite name="unit">
<directory>ExampleTests</directory>
</testsuite>
</testsuites>

<source>
<include>
<directory>src</directory>
</include>
</source>

</phpunit>

0 comments on commit edf25aa

Please sign in to comment.