Skip to content

Commit

Permalink
update Error and tests, use Stubs::error_log
Browse files Browse the repository at this point in the history
  • Loading branch information
joemaller committed Feb 13, 2025
1 parent 42a4714 commit 50068bd
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 45 deletions.
13 changes: 11 additions & 2 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand All @@ -40,9 +48,10 @@ public function printInHead()
}

printf(
"\n\n<!-- Error triggered in %s:%d -->\n",
"\n\n<!-- Error triggered in %s:%d [%s] -->\n",
$this->trace[1]['class'],
$this->trace[1]['line']
$this->trace[1]['line'],
basename($this->trace[1]['file'])
);
echo "<!-- {$this->msg} -->\n\n";
}
Expand Down
8 changes: 0 additions & 8 deletions tests/CPTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
8 changes: 0 additions & 8 deletions tests/DataModelLabelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
8 changes: 0 additions & 8 deletions tests/DataModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
5 changes: 2 additions & 3 deletions tests/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions tests/LabelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
14 changes: 5 additions & 9 deletions tests/PluginApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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 */
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
9 changes: 4 additions & 5 deletions tests/RenameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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)]
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 50068bd

Please sign in to comment.