Skip to content

Commit 679b5e7

Browse files
authored
Merge pull request #2 from fabacino/feature/add-more-tests
Add more tests
2 parents 55deafb + 1a9b404 commit 679b5e7

File tree

3 files changed

+296
-9
lines changed

3 files changed

+296
-9
lines changed

tests/DbgTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
class DbgTest extends \PHPUnit\Framework\TestCase
99
{
1010
/**
11-
* Test printing number
11+
* Test printing number.
12+
*
13+
* @return void
1214
*/
1315
public function testPrintNumber()
1416
{
@@ -17,7 +19,9 @@ public function testPrintNumber()
1719
}
1820

1921
/**
20-
* Test printing string
22+
* Test printing string.
23+
*
24+
* @return void
2125
*/
2226
public function testPrintString()
2327
{
@@ -26,7 +30,9 @@ public function testPrintString()
2630
}
2731

2832
/**
29-
* Test printing output
33+
* Test printing output.
34+
*
35+
* @return void
3036
*/
3137
public function testPrintArray()
3238
{
@@ -47,11 +53,11 @@ public function testPrintArray()
4753
* Capture and return output of function `dbg`.
4854
*
4955
* @param mixed $var The variable to analyse.
50-
* @param mixed $flags Flags for tweaking the output.
56+
* @param int $flags Flags for tweaking the output.
5157
*
5258
* @return string
5359
*/
54-
private function captureOutput($var, $flags = null)
60+
private function captureOutput($var, int $flags = null)
5561
{
5662
ob_start();
5763
dbg($var, $flags);

tests/DbglogTest.php

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
3+
namespace Fabacino\Debug\Test;
4+
5+
use Fabacino\Debug\Debug;
6+
use Monolog\Logger;
7+
use Monolog\Formatter\LineFormatter;
8+
use Monolog\Handler\StreamHandler;
9+
10+
/**
11+
* Tests for function `dbglog`.
12+
*/
13+
class DbglogTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* Test printing number with log file.
17+
*
18+
* @return void
19+
*/
20+
public function testLogNumber()
21+
{
22+
$this->initWithLogFile();
23+
$this->execTestLogNumber();
24+
}
25+
26+
/**
27+
* Test printing string with log file.
28+
*
29+
* @return void
30+
*/
31+
public function testLogString()
32+
{
33+
$this->initWithLogFile();
34+
$this->execTestLogString();
35+
}
36+
37+
/**
38+
* Test printing output with log file.
39+
*
40+
* @return void
41+
*/
42+
public function testLogArray()
43+
{
44+
$this->initWithLogFile();
45+
$this->execTestLogArray();
46+
}
47+
48+
/**
49+
* Test printing number with logger.
50+
*
51+
* @return void
52+
*/
53+
public function testLogNumberWithLogger()
54+
{
55+
$this->initWithLogger();
56+
$this->execTestLogNumber();
57+
}
58+
59+
/**
60+
* Test printing string with logger.
61+
*
62+
* @return void
63+
*/
64+
public function testLogStringWithLogger()
65+
{
66+
$this->initWithLogger();
67+
$this->execTestLogString();
68+
}
69+
70+
/**
71+
* Test printing output with logger.
72+
*
73+
* @return void
74+
*/
75+
public function testLogArrayWithLogger()
76+
{
77+
$this->initWithLogger();
78+
$this->execTestLogArray();
79+
}
80+
81+
/**
82+
* Test printing number.
83+
*
84+
* @return void
85+
*/
86+
private function execTestLogNumber()
87+
{
88+
$var = 123;
89+
$this->assertRegExp($this->makePattern($var), $this->captureOutput($var));
90+
}
91+
92+
/**
93+
* Test printing string.
94+
*
95+
* @return void
96+
*/
97+
private function execTestLogString()
98+
{
99+
$var = 'some string';
100+
$this->assertRegExp($this->makePattern($var), $this->captureOutput($var));
101+
}
102+
103+
/**
104+
* Test printing output.
105+
*
106+
* @return void
107+
*/
108+
private function execTestLogArray()
109+
{
110+
$var = ['first', 'second', 'third'];
111+
$expected = <<<'EOT'
112+
Array
113+
(
114+
[0] => first
115+
[1] => second
116+
[2] => third
117+
)
118+
119+
EOT;
120+
$this->assertRegExp($this->makePattern($expected), $this->captureOutput($var));
121+
}
122+
123+
/**
124+
* Init debug settings with log file.
125+
*
126+
* @return void
127+
*/
128+
private function initWithLogFile()
129+
{
130+
dbginit([
131+
'log_file' => 'php://output'
132+
]);
133+
}
134+
135+
/**
136+
* Init debug settings with logger.
137+
*
138+
* @return void
139+
*/
140+
private function initWithLogger()
141+
{
142+
$Stream = new StreamHandler('php://output', Logger::DEBUG);
143+
$Stream->setFormatter(
144+
new LineFormatter(Debug::OUTPUT_FORMAT, Debug::DATE_FORMAT, true)
145+
);
146+
147+
$Logger = new Logger(Debug::CHANNEL_NAME);
148+
$Logger->pushHandler($Stream);
149+
150+
dbginit([
151+
'logger' => $Logger,
152+
]);
153+
}
154+
155+
/**
156+
* Capture and return output of function `dbglog`.
157+
*
158+
* @param mixed $var The variable to analyse.
159+
* @param int $flags Flags for tweaking the output.
160+
*
161+
* @return string
162+
*/
163+
private function captureOutput($var, int $flags = null)
164+
{
165+
ob_start();
166+
dbglog($var, $flags);
167+
$output = ob_get_contents();
168+
ob_end_clean();
169+
return $output;
170+
}
171+
172+
/**
173+
* Make regexp pattern for variable.
174+
*
175+
* @param mixed $var The variable to analyse.
176+
*
177+
* @return string
178+
*/
179+
private function makePattern($var)
180+
{
181+
$search = [
182+
'%datetime%',
183+
'%message%'
184+
];
185+
$replace = [
186+
'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}',
187+
preg_quote($var, '/')
188+
];
189+
return '/' . str_replace($search, $replace, Debug::OUTPUT_FORMAT) . '/';
190+
}
191+
}

tests/DbgrTest.php

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
namespace Fabacino\Debug\Test;
44

5+
use Fabacino\Debug\Debug;
6+
57
/**
68
* Tests for function `dbgr`.
79
*/
810
class DbgrTest extends \PHPUnit\Framework\TestCase
911
{
1012
/**
11-
* Test number output
13+
* Test number output.
14+
*
15+
* @return void
1216
*/
1317
public function testDebugNumber()
1418
{
@@ -17,16 +21,20 @@ public function testDebugNumber()
1721
}
1822

1923
/**
20-
* Test string output
24+
* Test string output.
25+
*
26+
* @return void
2127
*/
2228
public function testDebugString()
2329
{
24-
$var = "some string";
30+
$var = 'some string';
2531
$this->assertSame($var, dbgr($var));
2632
}
2733

2834
/**
29-
* Test array output
35+
* Test array output.
36+
*
37+
* @return void
3038
*/
3139
public function testDebugArray()
3240
{
@@ -42,4 +50,86 @@ public function testDebugArray()
4250
EOT;
4351
$this->assertEquals($expected, dbgr($var));
4452
}
53+
54+
/**
55+
* Test string output setting vardump by init.
56+
*
57+
* @return void
58+
*/
59+
public function testDebugStringUsingVardumpByInit()
60+
{
61+
dbginit(['use_vardump' => true]);
62+
$var = 'another string';
63+
$this->assertSame(
64+
$this->extractDumped($this->captureVardump($var), 'string'),
65+
$this->extractDumped(dbgr($var), 'string')
66+
);
67+
}
68+
69+
/**
70+
* Test string output setting vardump by argument.
71+
*
72+
* @return void
73+
*/
74+
public function testDebugStringUsingVardumpByArg()
75+
{
76+
$var = 'Some Third String';
77+
$this->assertSame(
78+
$this->extractDumped($this->captureVardump($var), 'string'),
79+
$this->extractDumped(dbgr($var, Debug::USE_VARDUMP), 'string')
80+
);
81+
}
82+
83+
/**
84+
* Test string output setting htmlentities by init.
85+
*
86+
* @return void
87+
*/
88+
public function testDebugStringUsingHtmlentitiesByInit()
89+
{
90+
dbginit(['use_htmlentities' => true]);
91+
$var = '<b>Header<b>';
92+
$this->assertSame(htmlentities($var), dbgr($var));
93+
}
94+
95+
/**
96+
* Test string output setting htmlentities by argument.
97+
*
98+
* @return void
99+
*/
100+
public function testDebugStringUsingHtmlentitiesByArg()
101+
{
102+
$var = '<b>Footer<b>';
103+
$this->assertSame(htmlentities($var), dbgr($var, Debug::USE_HTMLENTITIES));
104+
}
105+
106+
/**
107+
* Capture and return output of function `var_dump`.
108+
*
109+
* @param mixed $var The variable to analyse.
110+
*
111+
* @return string
112+
*/
113+
private function captureVardump($var)
114+
{
115+
ob_start();
116+
var_dump($var);
117+
$output = ob_get_contents();
118+
ob_end_clean();
119+
return $output;
120+
}
121+
122+
/**
123+
* Extract relevant information from vardump output.
124+
*
125+
* @param string $output The vardump output.
126+
* @param string $start The string the relevant info starts with.
127+
*
128+
* @return string
129+
*/
130+
private function extractDumped(string $output, string $start)
131+
{
132+
$pos = strpos($output, $start);
133+
return $pos !== false ? substr($output, $pos) : $output;
134+
}
45135
}

0 commit comments

Comments
 (0)