Skip to content

Commit 76a7d84

Browse files
committed
CLI writes on STDERR
1 parent cd41b32 commit 76a7d84

File tree

4 files changed

+66
-32
lines changed

4 files changed

+66
-32
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"autoload": {
1414
"files": ["lib/r.php"]
1515
},
16+
"autoload-dev": {
17+
"classmap": ["tests/"]
18+
},
1619
"require": {
1720
"php": "^7.1"
1821
},

lib/r.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
{
77
use Slam\Debug\R as DebugR;
88

9-
function r($var, bool $exit = true, int $level = 0, bool $fullstack = false)
9+
function r($var, bool $exit = true, int $level = 0, bool $fullstack = false): void
1010
{
1111
DebugR::$db = \debug_backtrace();
1212
DebugR::debug($var, $exit, $level, $fullstack);
1313
}
1414

15-
function rq(string $query, array $params, bool $exit = true, int $level = 0, bool $fullstack = false)
15+
function rq(string $query, array $params, bool $exit = true, int $level = 0, bool $fullstack = false): void
1616
{
1717
\uksort($params, function (string $key1, string $key2) {
1818
return \strlen($key2) <=> \strlen($key1);
@@ -37,13 +37,13 @@ function rq(string $query, array $params, bool $exit = true, int $level = 0, boo
3737

3838
final class R
3939
{
40-
public static $db = array();
40+
public static $db = [];
4141

4242
private function __construct()
4343
{
4444
}
4545

46-
public static function debug($var, bool $exit = true, int $level = 0, bool $fullstack = false)
46+
public static function debug($var, bool $exit = true, int $level = 0, bool $fullstack = false): void
4747
{
4848
if (null === $var or \is_scalar($var)) {
4949
\ob_start();
@@ -56,7 +56,7 @@ public static function debug($var, bool $exit = true, int $level = 0, bool $full
5656
}
5757

5858
if (\PHP_SAPI === 'cli') {
59-
echo \PHP_EOL . self::formatDb($fullstack) . $output . \PHP_EOL;
59+
\fwrite(\STDERR, \PHP_EOL . self::formatDb($fullstack) . $output . \PHP_EOL);
6060
} else {
6161
echo '<pre><strong>' . self::formatDb($fullstack) . '</strong><br />' . \htmlspecialchars($output) . '</pre>';
6262
}
@@ -77,7 +77,7 @@ private static function formatDb(bool $fullstack): string
7777

7878
$output .= (isset($point['class']) ? $point['class'] . '->' : '') . $point['function'];
7979

80-
$args = array();
80+
$args = [];
8181
foreach ($point['args'] as $argument) {
8282
$args[] = (\is_object($argument)
8383
? \get_class($argument)

tests/MockStderr.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SlamTest\Debug;
6+
7+
use php_user_filter;
8+
9+
final class MockStderr extends php_user_filter
10+
{
11+
public static $output = '';
12+
13+
public function filter($in, $out, & $consumed, $closing)
14+
{
15+
while ($bucket = \stream_bucket_make_writeable($in)) {
16+
self::$output = $bucket->data;
17+
$consumed += $bucket->datalen;
18+
}
19+
20+
return \PSFS_PASS_ON;
21+
}
22+
}

tests/RTest.php

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,65 +9,74 @@
99

1010
final class RTest extends TestCase
1111
{
12+
const STREAM_FILTER_NAME = 'STDERR_MOCK';
13+
14+
private static $isStreamFilterRegistered;
15+
16+
private $registeredFilter;
17+
18+
protected function setUp()
19+
{
20+
if (true !== self::$isStreamFilterRegistered) {
21+
self::$isStreamFilterRegistered = \stream_filter_register(self::STREAM_FILTER_NAME, MockStderr::class);
22+
}
23+
24+
MockStderr::$output = '';
25+
$this->registeredFilter = \stream_filter_prepend(\STDERR, self::STREAM_FILTER_NAME, \STREAM_FILTER_WRITE);
26+
}
27+
28+
protected function tearDown()
29+
{
30+
\stream_filter_remove($this->registeredFilter);
31+
}
32+
1233
public function testScalar()
1334
{
14-
\ob_start();
1535
r(1, false);
16-
$output = \ob_get_clean();
1736

18-
$this->assertContains(__FILE__, $output);
19-
$this->assertContains('int(1)', $output);
37+
$this->assertContains(__FILE__, MockStderr::$output);
38+
$this->assertContains('int(1)', MockStderr::$output);
2039
}
2140

2241
public function testNonScalar()
2342
{
24-
\ob_start();
25-
r(array(1 => 2), false);
26-
$output = \ob_get_clean();
43+
r([1 => 2], false);
2744

28-
$this->assertContains(__FILE__, $output);
29-
$this->assertContains("Array\n(\n [1] => 2\n)", $output);
45+
$this->assertContains(__FILE__, MockStderr::$output);
46+
$this->assertContains("Array\n(\n [1] => 2\n)", MockStderr::$output);
3047
}
3148

3249
public function testFullstackOutput()
3350
{
34-
\ob_start();
3551
r(1, false, 0, true);
36-
$output = \ob_get_clean();
3752

38-
$this->assertContains(__FILE__, $output);
39-
$this->assertContains(__FUNCTION__, $output);
40-
$this->assertContains('TestCase', $output);
53+
$this->assertContains(__FILE__, MockStderr::$output);
54+
$this->assertContains(__FUNCTION__, MockStderr::$output);
55+
$this->assertContains('TestCase', MockStderr::$output);
4156
}
4257

4358
public function testQueryDebug()
4459
{
45-
\ob_start();
46-
rq('SELECT * FROM table WHERE c1 = :p1 AND c1 = :p11 AND c1 = :p2', array('p1' => 1, 'p11' => 2, 'p2' => '"'), false, 0, true);
47-
$output = \ob_get_clean();
60+
rq('SELECT * FROM table WHERE c1 = :p1 AND c1 = :p11 AND c1 = :p2', ['p1' => 1, 'p11' => 2, 'p2' => '"'], false, 0, true);
4861

49-
$this->assertContains('SELECT * FROM table WHERE c1 = "1" AND c1 = "2" AND c1 = "\\""', $output);
62+
$this->assertContains('SELECT * FROM table WHERE c1 = "1" AND c1 = "2" AND c1 = "\\""', MockStderr::$output);
5063
}
5164

5265
public function testDoctrine()
5366
{
54-
\ob_start();
5567
r(new stdClass(), false, 1);
56-
$output = \ob_get_clean();
5768

58-
$this->assertContains(__FILE__, $output);
59-
$this->assertContains('__CLASS__', $output);
69+
$this->assertContains(__FILE__, MockStderr::$output);
70+
$this->assertContains('__CLASS__', MockStderr::$output);
6071
}
6172

6273
public function testClearRootPath()
6374
{
6475
\define('ROOT_PATH', __DIR__);
6576

66-
\ob_start();
6777
r(1, false);
68-
$output = \ob_get_clean();
6978

70-
$this->assertContains(\basename(__FILE__), $output);
71-
$this->assertNotContains(__DIR__, $output);
79+
$this->assertContains(\basename(__FILE__), MockStderr::$output);
80+
$this->assertNotContains(__DIR__, MockStderr::$output);
7281
}
7382
}

0 commit comments

Comments
 (0)