From c443387c5023e8c24495e84769d7dea35e8931b8 Mon Sep 17 00:00:00 2001 From: fabacino Date: Wed, 28 Mar 2018 22:56:18 +0200 Subject: [PATCH 1/2] Refactor CLI detection into a separate function to be able to mock it --- src/Debug.php | 12 +++++++++++- tests/DbgTest.php | 21 +++++++++++++++++++++ tests/TestDebug.php | 28 ++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/TestDebug.php diff --git a/src/Debug.php b/src/Debug.php index 382c16c..fdcb104 100644 --- a/src/Debug.php +++ b/src/Debug.php @@ -154,7 +154,7 @@ public function printValue($var, int $flags = null) } $output = static::debugValue($var, $flags); - if (PHP_SAPI !== 'cli') { + if (!$this->isCli()) { $output = "
{$output}
"; } echo $output; @@ -204,4 +204,14 @@ public function logValue($var, int $flags = null) { $this->Logger->addDebug(static::debugValue($var, $flags)); } + + /** + * Check whether we are in a CLI environment. + * + * @return bool + */ + protected function isCli(): bool + { + return substr(PHP_SAPI, 0, 3) === 'cli'; + } } diff --git a/tests/DbgTest.php b/tests/DbgTest.php index 30ebdf1..55fc4ff 100644 --- a/tests/DbgTest.php +++ b/tests/DbgTest.php @@ -45,6 +45,27 @@ public function testPrintArray() [2] => third ) +EOT; + $this->assertEquals($expected, $this->captureOutput($var)); + } + + /** + * Test printing output in non-CLI environment. + * + * @return void + */ + public function testPrintArrayNonCli() + { + TestDebug::init(); + $var = ['first', 'second', 'third']; + $expected = <<<'EOT' +
Array
+(
+    [0] => first
+    [1] => second
+    [2] => third
+)
+
EOT; $this->assertEquals($expected, $this->captureOutput($var)); } diff --git a/tests/TestDebug.php b/tests/TestDebug.php new file mode 100644 index 0000000..2bf65a8 --- /dev/null +++ b/tests/TestDebug.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Fabacino\Debug\Test; + +class TestDebug extends \Fabacino\Debug\Debug +{ + /** + * Check whether we are in a CLI environment. + * + * @return bool + */ + protected function isCli(): bool + { + // PHPUnit is always executed through CLI. In order to be able to check + // functionality which is working only in a non-CLI environment, we just + // pretend that PHP is running in a non-CLI environment. + return false; + } +} From 066b0b2c4fdb68effc1a565d1872abfbce72fc9f Mon Sep 17 00:00:00 2001 From: fabacino Date: Wed, 28 Mar 2018 22:58:44 +0200 Subject: [PATCH 2/2] Add file comments to test classes --- tests/DbgTest.php | 10 ++++++++-- tests/DbglogTest.php | 12 +++++++++--- tests/DbgrTest.php | 12 +++++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/tests/DbgTest.php b/tests/DbgTest.php index 55fc4ff..65c43d0 100644 --- a/tests/DbgTest.php +++ b/tests/DbgTest.php @@ -1,10 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. */ + +namespace Fabacino\Debug\Test; + class DbgTest extends \PHPUnit\Framework\TestCase { /** diff --git a/tests/DbglogTest.php b/tests/DbglogTest.php index 22b2475..dc1ae27 100644 --- a/tests/DbglogTest.php +++ b/tests/DbglogTest.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Fabacino\Debug\Test; use Fabacino\Debug\Debug; @@ -7,9 +16,6 @@ use Monolog\Formatter\LineFormatter; use Monolog\Handler\StreamHandler; -/** - * Tests for function `dbglog`. - */ class DbglogTest extends \PHPUnit\Framework\TestCase { /** diff --git a/tests/DbgrTest.php b/tests/DbgrTest.php index 74d4115..5b734e0 100644 --- a/tests/DbgrTest.php +++ b/tests/DbgrTest.php @@ -1,12 +1,18 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Fabacino\Debug\Test; use Fabacino\Debug\Debug; -/** - * Tests for function `dbgr`. - */ class DbgrTest extends \PHPUnit\Framework\TestCase { /**