diff --git a/phpcs.xml b/phpcs.xml --- a/phpcs.xml +++ b/phpcs.xml @@ -2,6 +2,11 @@ + + + */_register_to_global_space.php + + src tests diff --git a/src/Debug/Debugger.php b/src/Debug/Debugger.php new file mode 100644 --- /dev/null +++ b/src/Debug/Debugger.php @@ -0,0 +1,32 @@ + block + * + * @param mixed $variable the variable to dump + */ + public static function printVariable ($variable) : void { + echo "
";
+        print_r($variable);
+        echo "
"; + } + + public static function printVariableAndDie ($variable) : void { + static::printVariable($variable); + die; + } + + /// + /// Comfort debug helper to register debug method in global space + /// + + public static function register () : void { + require_once '_register_to_global_space.php'; + } + +} diff --git a/src/Debug/_register_to_global_space.php b/src/Debug/_register_to_global_space.php new file mode 100644 --- /dev/null +++ b/src/Debug/_register_to_global_space.php @@ -0,0 +1,17 @@ +assertTestSuiteStateIsValid(); + + Debugger::register(); + + $this->assertTrue(function_exists("dprint_r")); + } + + private function assertTestSuiteStateIsValid() : void { + $this->assertFalse( + function_exists("dprint_r"), + "Configure the test suite so dprint_r isn't in global space first." + ); + } + + /// + /// Integration tests + /// + + /** + * @dataProvider provideDebuggerScripts + */ + public function testDebuggerScript ($script, $message) : void { + $this->assertProgramMatchesOutput($script, $message); + } + + private function assertProgramMatchesOutput(string $script, string $message = "") : void { + $filename = __DIR__ . "/testers/$script"; + + $expected = file_get_contents($filename . ".txt"); + $actual = `php $filename.php`; + + $this->assertSame($expected, $actual, $message); + } + + public function provideDebuggerScripts () : iterable { + yield ["dump_integer", "Can't dump a variable"]; + yield ["dump_array", "Can't dump an array"]; + yield ["dump_object", "Can't dump an object"]; + yield ["check_die", "printVariableAndDie doesn't die"]; + } + +} diff --git a/tests/Debug/testers/Counter.php b/tests/Debug/testers/Counter.php new file mode 100644 --- /dev/null +++ b/tests/Debug/testers/Counter.php @@ -0,0 +1,10 @@ +foo \ No newline at end of file diff --git a/tests/Debug/testers/dump_array.php b/tests/Debug/testers/dump_array.php new file mode 100644 --- /dev/null +++ b/tests/Debug/testers/dump_array.php @@ -0,0 +1,10 @@ + "bar"]); +echo "The script didn't die as expected."; diff --git a/tests/Debug/testers/dump_array.txt b/tests/Debug/testers/dump_array.txt new file mode 100644 --- /dev/null +++ b/tests/Debug/testers/dump_array.txt @@ -0,0 +1,5 @@ +
Array
+(
+    [foo] => bar
+)
+
\ No newline at end of file diff --git a/tests/Debug/testers/dump_integer.php b/tests/Debug/testers/dump_integer.php new file mode 100644 --- /dev/null +++ b/tests/Debug/testers/dump_integer.php @@ -0,0 +1,9 @@ +2 \ No newline at end of file diff --git a/tests/Debug/testers/dump_object.php b/tests/Debug/testers/dump_object.php new file mode 100644 --- /dev/null +++ b/tests/Debug/testers/dump_object.php @@ -0,0 +1,11 @@ +Acme\Counter Object +( + [counter:Acme\Counter:private] => 4 +) + \ No newline at end of file