Skip to content

Commit 5dbe5b4

Browse files
authored
Merge pull request #5 from fabacino/feature/add-dbgthrow-dbgdie
Add dbgthrow() and dbgdie()
2 parents db5da17 + e9c5fb7 commit 5dbe5b4

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

src/debug-functions.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,31 @@ function dbglog($var, int $flags = null)
6363
{
6464
Debug::getInstance()->logValue($var, $flags);
6565
}
66+
67+
/**
68+
* Print debug value and die.
69+
*
70+
* @param mixed $var The variable to analyse.
71+
* @param int|null $flags Flags for tweaking the output.
72+
*
73+
* @return void
74+
* @codeCoverageIgnore
75+
*/
76+
function dbgdie($var, int $flags = null)
77+
{
78+
dbg($var, $flags);
79+
exit;
80+
}
81+
82+
/**
83+
* Throw exception with debug value.
84+
*
85+
* @param mixed $var The variable to analyse.
86+
* @param int|null $flags Flags for tweaking the output.
87+
*
88+
* @return void
89+
*/
90+
function dbgthrow($var, int $flags = null)
91+
{
92+
throw new \Exception(dbgr($var, $flags));
93+
}

tests/DbgThrowTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/*
3+
* © Fabian Wiget <fabacino@gmail.com>
4+
*
5+
* For the full copyright and license information, please view
6+
* the LICENSE file that was distributed with this source code.
7+
*/
8+
9+
namespace Fabacino\Debug\Test;
10+
11+
use Fabacino\Debug\Debug;
12+
13+
/**
14+
* Tests for function `dbgthrow`.
15+
*/
16+
class DbgThrowTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* Test printing number.
20+
*
21+
* @return void
22+
*/
23+
public function testThrowNumber()
24+
{
25+
$var = 123;
26+
$this->assertEquals($var, $this->captureException($var));
27+
}
28+
29+
/**
30+
* Test printing string.
31+
*
32+
* @return void
33+
*/
34+
public function testThrowString()
35+
{
36+
$var = 'some string';
37+
$this->assertEquals($var, $this->captureException($var));
38+
}
39+
40+
/**
41+
* Test printing output.
42+
*
43+
* @return void
44+
*/
45+
public function testPrintArray()
46+
{
47+
$var = ['first', 'second', 'third'];
48+
$expected = <<<'EOT'
49+
Array
50+
(
51+
[0] => first
52+
[1] => second
53+
[2] => third
54+
)
55+
56+
EOT;
57+
$this->assertEquals($expected, $this->captureException($var));
58+
}
59+
60+
/**
61+
* Capture and return output of function `dbg`.
62+
*
63+
* @param mixed $var The variable to analyse.
64+
* @param int $flags Flags for tweaking the output.
65+
*
66+
* @return string|null
67+
*/
68+
private function captureException($var, int $flags = null)
69+
{
70+
try {
71+
dbgthrow($var, $flags);
72+
} catch (\Exception $Exception) {
73+
return $Exception->getMessage();
74+
}
75+
return null;
76+
}
77+
}

0 commit comments

Comments
 (0)