From 50068bda61d27ab60149a830cfbc984e39b3148a Mon Sep 17 00:00:00 2001 From: Joe Maller Date: Thu, 13 Feb 2025 16:11:15 -0500 Subject: [PATCH] update Error and tests, use Stubs::error_log --- src/Error.php | 13 +++++++++++-- tests/CPTTest.php | 8 -------- tests/DataModelLabelTest.php | 8 -------- tests/DataModelTest.php | 8 -------- tests/ErrorTest.php | 5 ++--- tests/LabelsTest.php | 3 +-- tests/PluginApiTest.php | 14 +++++--------- tests/RenameTest.php | 9 ++++----- 8 files changed, 23 insertions(+), 45 deletions(-) diff --git a/src/Error.php b/src/Error.php index a9325b2..246be26 100644 --- a/src/Error.php +++ b/src/Error.php @@ -30,6 +30,14 @@ public function __construct($msg) $this->msg = $msg; $this->trace = debug_backtrace(); error_log($this->msg); + error_log( + sprintf( + 'Error triggered in %s:%d [%s]', + $this->trace[1]['class'], + $this->trace[1]['line'], + basename($this->trace[1]['file']) + ) + ); add_action('wp_head', [$this, 'printInHead']); } @@ -40,9 +48,10 @@ public function printInHead() } printf( - "\n\n\n", + "\n\n\n", $this->trace[1]['class'], - $this->trace[1]['line'] + $this->trace[1]['line'], + basename($this->trace[1]['file']) ); echo "\n\n"; } diff --git a/tests/CPTTest.php b/tests/CPTTest.php index 9d065cb..d033542 100644 --- a/tests/CPTTest.php +++ b/tests/CPTTest.php @@ -8,14 +8,6 @@ Test\Stubs::init(); -if (!function_exists(__NAMESPACE__ . '\error_log')) { - function error_log($err) - { - global $error_log; - $error_log = $err; - } -} - /** * Empty class for mocking the abstract class */ diff --git a/tests/DataModelLabelTest.php b/tests/DataModelLabelTest.php index 77fa71c..8bc470e 100644 --- a/tests/DataModelLabelTest.php +++ b/tests/DataModelLabelTest.php @@ -12,14 +12,6 @@ Test\Stubs::init(); -if (!function_exists(__NAMESPACE__ . '\error_log')) { - function error_log($err) - { - global $error_log; - $error_log = $err; - } -} - #[CoversClass(\IdeasOnPurpose\WP\DataModel::class)] #[CoversClass(\IdeasOnPurpose\WP\DataModel\Labels::class)] #[CoversClass(\IdeasOnPurpose\WP\Error::class)] diff --git a/tests/DataModelTest.php b/tests/DataModelTest.php index 1579b7c..9017953 100644 --- a/tests/DataModelTest.php +++ b/tests/DataModelTest.php @@ -11,14 +11,6 @@ Test\Stubs::init(); -if (!function_exists(__NAMESPACE__ . '\error_log')) { - function error_log($err) - { - global $error_log; - $error_log = $err; - } -} - /** * Empty class for mocking the abstract class */ diff --git a/tests/ErrorTest.php b/tests/ErrorTest.php index ee81492..68b2ba6 100644 --- a/tests/ErrorTest.php +++ b/tests/ErrorTest.php @@ -11,8 +11,7 @@ if (!function_exists(__NAMESPACE__ . '\error_log')) { function error_log($err) { - global $error_log; - $error_log = $err; + Test\Stubs::error_log($err); } } @@ -22,7 +21,7 @@ final class ErrorTest extends TestCase public function testPrintError() { global $error_log; - $error_log = ''; + $error_log = 'aaa'; $msg = 'Test Error Message'; $Error = new Error($msg); $Error->is_debug = true; diff --git a/tests/LabelsTest.php b/tests/LabelsTest.php index ad582da..cbec6c5 100644 --- a/tests/LabelsTest.php +++ b/tests/LabelsTest.php @@ -12,8 +12,7 @@ if (!function_exists(__NAMESPACE__ . '\error_log')) { function error_log($err) { - global $error_log; - $error_log = $err; + Test\Stubs::error_log($err); } } diff --git a/tests/PluginApiTest.php b/tests/PluginApiTest.php index dab4044..ac7d6f3 100644 --- a/tests/PluginApiTest.php +++ b/tests/PluginApiTest.php @@ -13,12 +13,7 @@ if (!function_exists(__NAMESPACE__ . '\error_log')) { function error_log($err) { - // global $error_log; - // $error_log = $err; - - global $error_log; - $error_log = $error_log ?? []; - $error_log[] = $err; + Test\Stubs::error_log($err); } } @@ -33,7 +28,8 @@ protected function setUp(): void global $flush_rewrite_rules, $error_log, $is_wp_error; $flush_rewrite_rules = null; // $error_log = ''; - $error_log = []; + // $error_log = []; + $error_log = ''; $is_wp_error = false; /** @var \IdeasOnPurpose\WP\CPT $this->Taxonomy */ @@ -340,7 +336,7 @@ public function testUpdateCheck_transientExists_wpError() $ApiMock->updateCheck(); $this->assertFalse($ApiMock->response); - $this->assertStringContainsString('Something went wrong', implode("\n", $error_log)); + $this->assertStringContainsString('Something went wrong', $error_log); // $this->assertContains('Something went wrong', $error_log); } @@ -368,6 +364,6 @@ public function testUpdateCheck_transientExists_responseCodeNot200() $ApiMock->updateCheck(); $this->assertFalse($ApiMock->response); - $this->assertStringContainsString('Something went wrong', implode("\n", $error_log)); + $this->assertStringContainsString('Something went wrong', $error_log); } } diff --git a/tests/RenameTest.php b/tests/RenameTest.php index 6094867..ca4708f 100644 --- a/tests/RenameTest.php +++ b/tests/RenameTest.php @@ -14,8 +14,7 @@ if (!function_exists(__NAMESPACE__ . '\error_log')) { function error_log($err) { - global $error_log; - $error_log = $err; + Test\Stubs::error_log($err); } } @@ -25,8 +24,7 @@ function error_log($err) */ class RenameMock extends Rename { - // public function update($object, $singular, $plural, $overrides = []) -// {} + // public function update($object, $singular, $plural, $overrides = []) {} } #[CoversClass(\IdeasOnPurpose\WP\Error::class)] @@ -146,11 +144,12 @@ public function testUpdateError() { global $error_log; + $error_log = 'aaaa'; $reflection = new \ReflectionClass(RenameMock::class); $renameMock = $reflection->newInstanceWithoutConstructor(); + error_log('hi there'); $renameMock->update('nope', 'a', 'b'); - $this->assertStringContainsString('nope', $error_log); $this->assertStringContainsString('Unable to rename.', $error_log); }