Skip to content

Commit cfd8940

Browse files
committed
add test
1 parent 6fc008b commit cfd8940

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

src/Traits/ConsolePrint.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Traits;
3+
namespace Php\Support\Traits;
44

55
/**
66
* Trait ConsolePrintTrait
@@ -11,17 +11,19 @@ trait ConsolePrint
1111
{
1212
/**
1313
* @param mixed $msg
14+
* @param bool $newLine
1415
*/
15-
public function print($msg): void
16+
public function print($msg, bool $newLine = true): void
1617
{
17-
fwrite(STDOUT, print_r($msg, true) . PHP_EOL);
18+
fwrite(STDOUT, print_r($msg, true) . ($newLine ? PHP_EOL : ''));
1819
}
1920

2021
/**
2122
* @param mixed $msg
23+
* @param bool $newLine
2224
*/
23-
public function printError($msg): void
25+
public function printError($msg, bool $newLine = true): void
2426
{
25-
fwrite(STDERR, print_r($msg, true) . PHP_EOL);
27+
fwrite(STDERR, print_r($msg, true) . ($newLine ? PHP_EOL : ''));
2628
}
2729
}

tests/traits/ConsolePrintTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
/**
7+
* Class ConsolePrint
8+
*/
9+
final class ConsolePrint extends TestCase
10+
{
11+
public function testStdOut(): void
12+
{
13+
stream_filter_register("intercept", "Intercept");
14+
15+
stream_filter_append(STDOUT, "intercept");
16+
17+
$str1 = 'Test message';
18+
$this->cls()->print($str1);
19+
$this->assertEquals($str1 . PHP_EOL, Intercept::$cache);
20+
21+
$this->cls()->print($str1, false);
22+
$this->assertEquals($str1, Intercept::$cache);
23+
24+
$array = [
25+
'key' => 'value',
26+
'int' => 323,
27+
'float' => 3.12,
28+
];
29+
30+
$this->cls()->print($array, false);
31+
$this->assertEquals(print_r($array, true), Intercept::$cache);
32+
33+
$this->cls()->print($array);
34+
$this->assertEquals(print_r($array, true) . PHP_EOL, Intercept::$cache);
35+
}
36+
37+
public function testErrOut(): void
38+
{
39+
stream_filter_register("intercept", "Intercept");
40+
41+
stream_filter_append(STDERR, "intercept");
42+
43+
$str1 = 'Error message';
44+
$this->cls()->print($str1);
45+
$this->assertEquals($str1 . PHP_EOL, Intercept::$cache);
46+
47+
$this->cls()->print($str1, false);
48+
$this->assertEquals($str1, Intercept::$cache);
49+
}
50+
51+
private function cls()
52+
{
53+
return new class()
54+
{
55+
use \Php\Support\Traits\ConsolePrint;
56+
};
57+
}
58+
}
59+
60+
/**
61+
* Class Intercept
62+
*/
63+
class Intercept extends \php_user_filter
64+
{
65+
public static $cache = '';
66+
67+
public function filter($in, $out, &$consumed, $closing)
68+
{
69+
while ($bucket = stream_bucket_make_writeable($in)) {
70+
self::$cache = $bucket->data;
71+
$consumed += $bucket->datalen;
72+
stream_bucket_append($out, $bucket);
73+
}
74+
75+
return PSFS_PASS_ON;
76+
}
77+
}

0 commit comments

Comments
 (0)