diff --git a/tests/DbgTest.php b/tests/DbgTest.php index 03069d0..968a1fb 100644 --- a/tests/DbgTest.php +++ b/tests/DbgTest.php @@ -16,66 +16,35 @@ */ class DbgTest extends \PHPUnit\Framework\TestCase { - /** - * Test printing number. - * - * @return void - */ public function testPrintNumber() { - $var = 123; - $this->assertEquals('123', $this->captureOutput($var)); + $var = TestHelper::randomInt(); + + $this->assertEquals($var, $this->captureOutput($var)); } - /** - * Test printing string. - * - * @return void - */ public function testPrintString() { - $var = 'some string'; + $var = TestHelper::randomString(); + $this->assertEquals($var, $this->captureOutput($var)); } - /** - * Test printing output. - * - * @return void - */ public function testPrintArray() { - $var = ['first', 'second', 'third']; - $expected = <<<'EOT' -Array -( - [0] => first - [1] => second - [2] => third -) + $var = TestHelper::randomArray(); + $expected = TestHelper::makeArrayOutput($var); -EOT; $this->assertEquals($expected, $this->captureOutput($var)); } - /** - * Test printing output in non-CLI environment. - * - * @return void - */ public function testPrintArrayNonCli() { TestDebug::init(); - $var = ['first', 'second', 'third']; - $expected = <<<'EOT' -
Array -( - [0] => first - [1] => second - [2] => third -) --EOT; + + $var = TestHelper::randomArray(); + $expected = '
' . TestHelper::makeArrayOutput($var) . ''; + $this->assertEquals($expected, $this->captureOutput($var)); } diff --git a/tests/DbgThrowTest.php b/tests/DbgThrowTest.php index c25857a..8e4679c 100644 --- a/tests/DbgThrowTest.php +++ b/tests/DbgThrowTest.php @@ -18,45 +18,25 @@ */ class DbgThrowTest extends \PHPUnit\Framework\TestCase { - /** - * Test printing number. - * - * @return void - */ public function testThrowNumber() { - $var = 123; + $var = TestHelper::randomInt(); + $this->assertEquals($var, $this->captureException($var)); } - /** - * Test printing string. - * - * @return void - */ public function testThrowString() { - $var = 'some string'; + $var = TestHelper::randomString(); + $this->assertEquals($var, $this->captureException($var)); } - /** - * Test printing output. - * - * @return void - */ - public function testPrintArray() + public function testThrowArray() { - $var = ['first', 'second', 'third']; - $expected = <<<'EOT' -Array -( - [0] => first - [1] => second - [2] => third -) + $var = TestHelper::randomArray(); + $expected = TestHelper::makeArrayOutput($var); -EOT; $this->assertEquals($expected, $this->captureException($var)); } diff --git a/tests/DbglogTest.php b/tests/DbglogTest.php index a0f09ec..0e6c710 100644 --- a/tests/DbglogTest.php +++ b/tests/DbglogTest.php @@ -19,11 +19,6 @@ */ class DbglogTest extends \PHPUnit\Framework\TestCase { - /** - * Test printing number with log file. - * - * @return void - */ public function testLogNumberWithLogFile() { $logfile = TestHelper::createTempFile(); @@ -31,7 +26,7 @@ public function testLogNumberWithLogFile() 'log_file' => $logfile ]); - $var = random_int(PHP_INT_MIN, PHP_INT_MAX); + $var = TestHelper::randomInt(); dbglog($var); $this->assertRegExp( @@ -40,11 +35,6 @@ public function testLogNumberWithLogFile() ); } - /** - * Test printing string with log file. - * - * @return void - */ public function testLogStringWithLogFile() { $logfile = TestHelper::createTempFile(); @@ -52,7 +42,7 @@ public function testLogStringWithLogFile() 'log_file' => $logfile ]); - $var = base64_encode(random_bytes(24)); + $var = TestHelper::randomString(); dbglog($var); $this->assertRegExp( @@ -61,11 +51,6 @@ public function testLogStringWithLogFile() ); } - /** - * Test printing output with log file. - * - * @return void - */ public function testLogArrayWithLogFile() { $logfile = TestHelper::createTempFile(); @@ -73,29 +58,16 @@ public function testLogArrayWithLogFile() 'log_file' => $logfile ]); - $var = ['first', 'second', 'third']; + $var = TestHelper::randomArray(); dbglog($var); - $expected = <<<'EOT' -Array -( - [0] => first - [1] => second - [2] => third -) - -EOT; + $expected = TestHelper::makeArrayOutput($var); $this->assertRegExp( TestHelper::makePattern($expected), file_get_contents($logfile) ); } - /** - * Test printing data with no log file. - * - * @return void - */ public function testLogWithNoLogFile() { $logfile = TestHelper::createTempFile(); @@ -103,26 +75,20 @@ public function testLogWithNoLogFile() 'log_file' => null ]); - $var = random_int(PHP_INT_MIN, PHP_INT_MAX); + $var = TestHelper::randomInt(); dbglog($var); $this->assertEmpty(file_get_contents($logfile)); } - /** - * Test printing number with logger. - * - * @return void - */ public function testLogNumberWithLogger() { - $fp = tmpfile(); - $logfile = stream_get_meta_data($fp)['uri']; + $logfile = TestHelper::createTempFile(); dbginit([ 'logger' => new Logger($logfile) ]); - $var = random_int(PHP_INT_MIN, PHP_INT_MAX); + $var = TestHelper::randomInt(); dbglog($var); $this->assertRegExp( @@ -131,20 +97,14 @@ public function testLogNumberWithLogger() ); } - /** - * Test printing string with logger. - * - * @return void - */ public function testLogStringWithLogger() { - $fp = tmpfile(); - $logfile = stream_get_meta_data($fp)['uri']; + $logfile = TestHelper::createTempFile(); dbginit([ 'logger' => new Logger($logfile) ]); - $var = base64_encode(random_bytes(24)); + $var = TestHelper::randomString(); dbglog($var); $this->assertRegExp( @@ -153,52 +113,33 @@ public function testLogStringWithLogger() ); } - /** - * Test printing output with logger. - * - * @return void - */ public function testLogArrayWithLogger() { - $fp = tmpfile(); - $logfile = stream_get_meta_data($fp)['uri']; + $logfile = TestHelper::createTempFile(); dbginit([ 'logger' => new Logger($logfile) ]); - $var = ['first', 'second', 'third']; + $var = TestHelper::randomArray(); dbglog($var); - $expected = <<<'EOT' -Array -( - [0] => first - [1] => second - [2] => third -) + $expected = TestHelper::makeArrayOutput($var); -EOT; $this->assertRegExp( TestHelper::makePattern($expected), file_get_contents($logfile) ); } - /** - * Test printing data with custom date format. - * - * @return void - */ public function testLogWithLoggerAndCustomDateFormat() { - $fp = tmpfile(); - $logfile = stream_get_meta_data($fp)['uri']; + $logfile = TestHelper::createTempFile(); $dateFormat = 'Y/m/d H/i/s'; dbginit([ 'logger' => new Logger($logfile, $dateFormat) ]); - $var = random_int(PHP_INT_MIN, PHP_INT_MAX); + $var = TestHelper::randomInt(); dbglog($var); $this->assertRegExp( @@ -207,20 +148,14 @@ public function testLogWithLoggerAndCustomDateFormat() ); } - /** - * Test printing data with no logger. - * - * @return void - */ public function testLogWithNoLogger() { - $fp = tmpfile(); - $logfile = stream_get_meta_data($fp)['uri']; + $logfile = TestHelper::createTempFile(); dbginit([ 'logger' => null ]); - $var = random_int(PHP_INT_MIN, PHP_INT_MAX); + $var = TestHelper::randomInt(); dbglog($var); $this->assertEmpty(file_get_contents($logfile)); diff --git a/tests/DbgrTest.php b/tests/DbgrTest.php index ebbf2df..ee29f7a 100644 --- a/tests/DbgrTest.php +++ b/tests/DbgrTest.php @@ -18,97 +18,63 @@ */ class DbgrTest extends \PHPUnit\Framework\TestCase { - /** - * Test number output. - * - * @return void - */ public function testDebugNumber() { - $var = 123; - $this->assertSame('123', dbgr($var)); + $var = TestHelper::randomInt(); + + $this->assertEquals($var, dbgr($var)); } - /** - * Test string output. - * - * @return void - */ public function testDebugString() { - $var = 'some string'; + $var = TestHelper::randomString(); + $this->assertSame($var, dbgr($var)); } - /** - * Test array output. - * - * @return void - */ public function testDebugArray() { - $var = ['first', 'second', 'third']; - $expected = <<<'EOT' -Array -( - [0] => first - [1] => second - [2] => third -) - -EOT; + $var = TestHelper::randomArray(); + $expected = TestHelper::makeArrayOutput($var); + $this->assertEquals($expected, dbgr($var)); } - /** - * Test string output setting vardump by init. - * - * @return void - */ public function testDebugStringUsingVardumpByInit() { dbginit(['use_vardump' => true]); - $var = 'another string'; + + $var = TestHelper::randomString(); + $this->assertSame( $this->extractDumped($this->captureVardump($var), 'string'), $this->extractDumped(dbgr($var), 'string') ); } - /** - * Test string output setting vardump by argument. - * - * @return void - */ public function testDebugStringUsingVardumpByArg() { - $var = 'Some Third String'; + $var = TestHelper::randomString(); + $this->assertSame( $this->extractDumped($this->captureVardump($var), 'string'), $this->extractDumped(dbgr($var, Debug::USE_VARDUMP), 'string') ); } - /** - * Test string output setting htmlentities by init. - * - * @return void - */ public function testDebugStringUsingHtmlentitiesByInit() { dbginit(['use_htmlentities' => true]); - $var = 'Header'; + + $var = '' . TestHelper::randomInt() . ''; + $this->assertSame(htmlentities($var), dbgr($var)); } - /** - * Test string output setting htmlentities by argument. - * - * @return void - */ public function testDebugStringUsingHtmlentitiesByArg() { - $var = 'Footer'; + $var = '' . TestHelper::randomInt() . ''; + $this->assertSame(htmlentities($var), dbgr($var, Debug::USE_HTMLENTITIES)); } diff --git a/tests/TestHelper.php b/tests/TestHelper.php index dfc3204..6ce6ef5 100644 --- a/tests/TestHelper.php +++ b/tests/TestHelper.php @@ -25,6 +25,42 @@ class TestHelper */ private static $fp; + /** + * Generate a random int. + * + * @return int + */ + public static function randomInt(): int + { + return random_int(PHP_INT_MIN, PHP_INT_MAX); + } + + /** + * Generate a random string. + * + * @return string + */ + public static function randomString(): string + { + $numChars = random_int(1, 340); + return substr(base64_encode(random_bytes(256)), 0, $numChars); + } + + /** + * Generate a random array. + * + * @return array + */ + public static function randomArray(): array + { + $entries = []; + $numEntries = random_int(5, 25); + while ($numEntries-- > 0) { + $entries[self::randomInt()] = self::randomString(); + } + return $entries; + } + /** * Create temporary file. * @@ -38,7 +74,7 @@ public static function createTempFile(): string } /** - * Make regexp pattern for variable. + * Create regexp pattern for variable. * * @param mixed $var The variable to analyse. * @@ -62,4 +98,31 @@ public static function makePattern($var, string $dateFormat = Logger::DATE_FORMA ); return '/' . $datePattern . ': ' . preg_quote($var, '/') . '/'; } + + /** + * Create expected output for an array. + * + * @param array $entries The array to analyse. + * + * @return string + */ + public static function makeArrayOutput(array $entries): string + { + $output = ''; + foreach ($entries as $key => $value) { + $output .= <<