Page MenuHomeDevCentral

D1614.id.diff
No OneTemporary

D1614.id.diff

diff --git a/phpcs.xml b/phpcs.xml
--- a/phpcs.xml
+++ b/phpcs.xml
@@ -2,6 +2,11 @@
<ruleset name="Nasqueron">
<rule ref="vendor/nasqueron/codestyle/CodeSniffer/ruleset.xml" />
+ <!-- Allow dprint_r() and dieprint_r() legacy debug function names -->
+ <rule ref="Generic.NamingConventions.CamelCapsFunctionName.NotCamelCaps">
+ <exclude-pattern>*/_register_to_global_space.php</exclude-pattern>
+ </rule>
+
<file>src</file>
<file>tests</file>
</ruleset>
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 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Debug;
+
+class Debugger {
+
+ /**
+ * Prints human-readable information about a variable, wrapped in a <pre> block
+ *
+ * @param mixed $variable the variable to dump
+ */
+ public static function printVariable ($variable) : void {
+ echo "<pre class='debug'>";
+ print_r($variable);
+ echo "</pre>";
+ }
+
+ 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 @@
+<?php
+
+/* This code is intentionnally left in the global namespace. */
+
+use Keruald\OmniTools\Debug\Debugger;
+
+if (!function_exists("dprint_r")) {
+ function dprint_r ($variable) {
+ Debugger::printVariable($variable);
+ }
+}
+
+if (!function_exists("dieprint_r")) {
+ function dieprint_r ($variable) {
+ Debugger::printVariableAndDie($variable);
+ }
+}
diff --git a/tests/Debug/DebuggerTest.php b/tests/Debug/DebuggerTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Debug/DebuggerTest.php
@@ -0,0 +1,57 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Tests\Debug;
+
+use Keruald\OmniTools\Debug\Debugger;
+use PHPUnit\Framework\TestCase;
+
+class DebuggerTest extends TestCase {
+
+ ///
+ /// Unit tests
+ ///
+
+ public function testRegister () {
+ $this->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 @@
+<?php
+
+namespace Acme;
+
+/**
+ * Mock class to get an object to dump by dump_object.php
+ */
+class Counter {
+ private $counter = "4";
+}
diff --git a/tests/Debug/testers/check_die.php b/tests/Debug/testers/check_die.php
new file mode 100644
--- /dev/null
+++ b/tests/Debug/testers/check_die.php
@@ -0,0 +1,10 @@
+<?php
+
+use Keruald\OmniTools\Debug\Debugger;
+
+// Include Debugger class file
+$libDirectory = dirname(__DIR__, 3);
+require $libDirectory . "/src/Debug/Debugger.php";
+
+Debugger::printVariableAndDie("foo");
+echo "The script didn't die as expected.";
diff --git a/tests/Debug/testers/check_die.txt b/tests/Debug/testers/check_die.txt
new file mode 100644
--- /dev/null
+++ b/tests/Debug/testers/check_die.txt
@@ -0,0 +1 @@
+<pre class='debug'>foo</pre>
\ 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 @@
+<?php
+
+use Keruald\OmniTools\Debug\Debugger;
+
+// Include Debugger class file
+$libDirectory = dirname(__DIR__, 3);
+require $libDirectory . "/src/Debug/Debugger.php";
+
+Debugger::printVariableAndDie(["foo" => "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 @@
+<pre class='debug'>Array
+(
+ [foo] => bar
+)
+</pre>
\ 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 @@
+<?php
+
+use Keruald\OmniTools\Debug\Debugger;
+
+// Include Debugger class file
+$libDirectory = dirname(__DIR__, 3);
+require $libDirectory . "/src/Debug/Debugger.php";
+
+Debugger::printVariable(1 + 1);
diff --git a/tests/Debug/testers/dump_integer.txt b/tests/Debug/testers/dump_integer.txt
new file mode 100644
--- /dev/null
+++ b/tests/Debug/testers/dump_integer.txt
@@ -0,0 +1 @@
+<pre class='debug'>2</pre>
\ 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 @@
+<?php
+
+use Keruald\OmniTools\Debug\Debugger;
+use Acme\Counter;
+
+// Include Debugger class file
+$libDirectory = dirname(__DIR__, 3);
+require $libDirectory . "/src/Debug/Debugger.php";
+require "Counter.php";
+
+Debugger::printVariableAndDie(new Counter);
diff --git a/tests/Debug/testers/dump_object.txt b/tests/Debug/testers/dump_object.txt
new file mode 100644
--- /dev/null
+++ b/tests/Debug/testers/dump_object.txt
@@ -0,0 +1,5 @@
+<pre class='debug'>Acme\Counter Object
+(
+ [counter:Acme\Counter:private] => 4
+)
+</pre>
\ No newline at end of file

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 18, 23:17 (15 h, 45 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2251447
Default Alt Text
D1614.id.diff (6 KB)

Event Timeline