From 59ad9821e2dd85979c2d46201f6cac7e49f9fc9c Mon Sep 17 00:00:00 2001
From: robiningelbrecht
Date: Mon, 21 Aug 2023 09:28:35 +0200
Subject: [PATCH 1/3] Enable/disable at runtime
---
README.md | 17 +++
src/Configuration.php | 21 ++-
src/PhpUnitExtension.php | 27 ++++
tests/OutputTest.php | 25 ++++
tests/Unit/ConfigurationTest.php | 12 ++
tests/Unit/PhpUnitExtensionTest.php | 163 ++++++++++++++++++++++
tests/phpunit.test-disable-by-default.xml | 28 ++++
7 files changed, 290 insertions(+), 3 deletions(-)
create mode 100644 tests/phpunit.test-disable-by-default.xml
diff --git a/README.md b/README.md
index 6723392..f075dec 100644
--- a/README.md
+++ b/README.md
@@ -78,6 +78,16 @@ Also make sure the `color` attribute is set to `true`:
```
+* Disable pretty print. This can be useful when you only want to prettify the output when forced via CLI (see usage).
+
+```xml
+
+
+
+
+
+```
+
## Usage
```bash
@@ -118,6 +128,13 @@ Display Chuck Norris quote
+Enable/disable pretty print
+
+```bash
+> vendor/bin/phpunit -d --enable-pretty-print
+> vendor/bin/phpunit -d --disable-pretty-print
+```
+
Combine multiple options
```bash
diff --git a/src/Configuration.php b/src/Configuration.php
index 20fa5fa..9cde155 100644
--- a/src/Configuration.php
+++ b/src/Configuration.php
@@ -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(
@@ -46,4 +46,19 @@ 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;
+ }
+
+ return (int) $value ? 0 : 1;
+ }
}
diff --git a/src/PhpUnitExtension.php b/src/PhpUnitExtension.php
index 175bd98..a83972c 100644
--- a/src/PhpUnitExtension.php
+++ b/src/PhpUnitExtension.php
@@ -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();
@@ -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;
+ }
}
diff --git a/tests/OutputTest.php b/tests/OutputTest.php
index fbfd6f9..228c2aa 100644
--- a/tests/OutputTest.php
+++ b/tests/OutputTest.php
@@ -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);
+ }
}
diff --git a/tests/Unit/ConfigurationTest.php b/tests/Unit/ConfigurationTest.php
index 4adc153..35f2eff 100644
--- a/tests/Unit/ConfigurationTest.php
+++ b/tests/Unit/ConfigurationTest.php
@@ -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));
+ }
}
diff --git a/tests/Unit/PhpUnitExtensionTest.php b/tests/Unit/PhpUnitExtensionTest.php
index 6072de7..13b4c53 100644
--- a/tests/Unit/PhpUnitExtensionTest.php
+++ b/tests/Unit/PhpUnitExtensionTest.php
@@ -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));
+ }
}
diff --git a/tests/phpunit.test-disable-by-default.xml b/tests/phpunit.test-disable-by-default.xml
new file mode 100644
index 0000000..2c869aa
--- /dev/null
+++ b/tests/phpunit.test-disable-by-default.xml
@@ -0,0 +1,28 @@
+
+
+
+
+
+
+
+
+
+
+ ExampleTests
+
+
+
+
+
+ src
+
+
+
+
From 7a439203c8aa512310e675a6a0959e180e1a49ae Mon Sep 17 00:00:00 2001
From: robiningelbrecht
Date: Mon, 21 Aug 2023 09:31:41 +0200
Subject: [PATCH 2/3] Fix PHPStan errors
---
src/Configuration.php | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/src/Configuration.php b/src/Configuration.php
index 9cde155..784d5b7 100644
--- a/src/Configuration.php
+++ b/src/Configuration.php
@@ -58,7 +58,10 @@ public static function isFalsy(mixed $value): bool
if ('false' === $value) {
return true;
}
+ if (is_int($value)) {
+ return !$value;
+ }
- return (int) $value ? 0 : 1;
+ return false;
}
}
From c6a53f9d9c2da85dcdb5d7526a3275cf8d736e0b Mon Sep 17 00:00:00 2001
From: robiningelbrecht
Date: Mon, 21 Aug 2023 09:33:14 +0200
Subject: [PATCH 3/3] Made a woopsie
---
src/Configuration.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/Configuration.php b/src/Configuration.php
index 784d5b7..e09813d 100644
--- a/src/Configuration.php
+++ b/src/Configuration.php
@@ -62,6 +62,6 @@ public static function isFalsy(mixed $value): bool
return !$value;
}
- return false;
+ return true;
}
}