Skip to content

Commit edf1f58

Browse files
authored
Merge pull request #3 from fabacino/feature/optimize-cli-detection
Feature/optimize cli detection
2 parents 679b5e7 + 066b0b2 commit edf1f58

File tree

5 files changed

+86
-9
lines changed

5 files changed

+86
-9
lines changed

src/Debug.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function printValue($var, int $flags = null)
154154
}
155155

156156
$output = static::debugValue($var, $flags);
157-
if (PHP_SAPI !== 'cli') {
157+
if (!$this->isCli()) {
158158
$output = "<pre>{$output}</pre>";
159159
}
160160
echo $output;
@@ -204,4 +204,14 @@ public function logValue($var, int $flags = null)
204204
{
205205
$this->Logger->addDebug(static::debugValue($var, $flags));
206206
}
207+
208+
/**
209+
* Check whether we are in a CLI environment.
210+
*
211+
* @return bool
212+
*/
213+
protected function isCli(): bool
214+
{
215+
return substr(PHP_SAPI, 0, 3) === 'cli';
216+
}
207217
}

tests/DbgTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
<?php
22

3-
namespace Fabacino\Debug\Test;
4-
53
/**
64
* Tests for function `dbg`.
5+
*
6+
* @copyright Fabian Wiget <fabacino@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
710
*/
11+
12+
namespace Fabacino\Debug\Test;
13+
814
class DbgTest extends \PHPUnit\Framework\TestCase
915
{
1016
/**
@@ -45,6 +51,27 @@ public function testPrintArray()
4551
[2] => third
4652
)
4753

54+
EOT;
55+
$this->assertEquals($expected, $this->captureOutput($var));
56+
}
57+
58+
/**
59+
* Test printing output in non-CLI environment.
60+
*
61+
* @return void
62+
*/
63+
public function testPrintArrayNonCli()
64+
{
65+
TestDebug::init();
66+
$var = ['first', 'second', 'third'];
67+
$expected = <<<'EOT'
68+
<pre>Array
69+
(
70+
[0] => first
71+
[1] => second
72+
[2] => third
73+
)
74+
</pre>
4875
EOT;
4976
$this->assertEquals($expected, $this->captureOutput($var));
5077
}

tests/DbglogTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
<?php
22

3+
/**
4+
* Tests for function `dbglog`.
5+
*
6+
* @copyright Fabian Wiget <fabacino@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Fabacino\Debug\Test;
413

514
use Fabacino\Debug\Debug;
615
use Monolog\Logger;
716
use Monolog\Formatter\LineFormatter;
817
use Monolog\Handler\StreamHandler;
918

10-
/**
11-
* Tests for function `dbglog`.
12-
*/
1319
class DbglogTest extends \PHPUnit\Framework\TestCase
1420
{
1521
/**

tests/DbgrTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
<?php
22

3+
/**
4+
* Tests for function `dbgr`.
5+
*
6+
* @copyright Fabian Wiget <fabacino@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Fabacino\Debug\Test;
413

514
use Fabacino\Debug\Debug;
615

7-
/**
8-
* Tests for function `dbgr`.
9-
*/
1016
class DbgrTest extends \PHPUnit\Framework\TestCase
1117
{
1218
/**

tests/TestDebug.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* Debug class for testing.
5+
*
6+
* @copyright Fabian Wiget <fabacino@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Fabacino\Debug\Test;
13+
14+
class TestDebug extends \Fabacino\Debug\Debug
15+
{
16+
/**
17+
* Check whether we are in a CLI environment.
18+
*
19+
* @return bool
20+
*/
21+
protected function isCli(): bool
22+
{
23+
// PHPUnit is always executed through CLI. In order to be able to check
24+
// functionality which is working only in a non-CLI environment, we just
25+
// pretend that PHP is running in a non-CLI environment.
26+
return false;
27+
}
28+
}

0 commit comments

Comments
 (0)