|
| 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