Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function printValue($var, int $flags = null)
}

$output = static::debugValue($var, $flags);
if (PHP_SAPI !== 'cli') {
if (!$this->isCli()) {
$output = "<pre>{$output}</pre>";
}
echo $output;
Expand Down Expand Up @@ -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';
}
}
31 changes: 29 additions & 2 deletions tests/DbgTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?php

namespace Fabacino\Debug\Test;

/**
* Tests for function `dbg`.
*
* @copyright Fabian Wiget <fabacino@gmail.com>
*
* 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
{
/**
Expand Down Expand Up @@ -45,6 +51,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'
<pre>Array
(
[0] => first
[1] => second
[2] => third
)
</pre>
EOT;
$this->assertEquals($expected, $this->captureOutput($var));
}
Expand Down
12 changes: 9 additions & 3 deletions tests/DbglogTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?php

/**
* Tests for function `dbglog`.
*
* @copyright Fabian Wiget <fabacino@gmail.com>
*
* 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;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;

/**
* Tests for function `dbglog`.
*/
class DbglogTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down
12 changes: 9 additions & 3 deletions tests/DbgrTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<?php

/**
* Tests for function `dbgr`.
*
* @copyright Fabian Wiget <fabacino@gmail.com>
*
* 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
{
/**
Expand Down
28 changes: 28 additions & 0 deletions tests/TestDebug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* Debug class for testing.
*
* @copyright Fabian Wiget <fabacino@gmail.com>
*
* 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;
}
}