Skip to content

Commit

Permalink
update API tests, back to 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
joemaller committed Feb 7, 2025
1 parent 3c02c3a commit c8464d8
Showing 1 changed file with 108 additions and 56 deletions.
164 changes: 108 additions & 56 deletions tests/PluginApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
if (!function_exists(__NAMESPACE__ . '\error_log')) {
function error_log($err)
{
// global $error_log;
// $error_log = $err;

global $error_log;
$error_log = $err;
$error_log = $error_log ?? [];
$error_log[] = $err;
}
}

Expand All @@ -28,7 +32,8 @@ protected function setUp(): void
{
global $flush_rewrite_rules, $error_log, $is_wp_error;
$flush_rewrite_rules = null;
$error_log = '';
// $error_log = '';
$error_log = [];
$is_wp_error = false;

/** @var \IdeasOnPurpose\WP\CPT $this->Taxonomy */
Expand Down Expand Up @@ -250,72 +255,119 @@ public function testUpdaterComplete()

public function testPluginInfo()
{
$this->Api->pluginInfo();
$this->assertObjectHasProperty('plugin_data', $this->Api);
$this->assertObjectHasProperty('plugin_id', $this->Api);
$this->assertObjectHasProperty('plugin_slug', $this->Api);
$this->assertObjectHasProperty('transient', $this->Api);
global $plugin_basename;
$reflection = new \ReflectionClass(Api::class);
$Api = $reflection->newInstanceWithoutConstructor();

$plugin_basename = 'fake-plugin_dir/main.php';
$Api->plugin = (object) ['__FILE__' => "fake/path/{$plugin_basename}"];

$Api->pluginInfo();
$this->assertIsArray($Api->plugin_data);
$this->assertObjectHasProperty('plugin_id', $Api);
$this->assertObjectHasProperty('plugin_slug', $Api);

$this->assertStringContainsString($Api->plugin_slug, $Api->plugin_id);
$this->assertStringContainsString($plugin_basename, $Api->transient);
}

// NOTE: Disabled because updateCheck is mocked
// public function testUpdateCheck_transientExists_debugTrue()
// {
// global $transients, $wp_remote_post, $error_log;
public function testUpdateCheck_transientExists_debugTrue()
{
global $get_transient, $transients, $set_transient, $error_log, $wp_remote_post;

// $expected = 'Response for testing';
// // $expected = (object) ['response' => ['key' => 'value']];
$ApiMock = $this->getMockBuilder(Api::class)
->disableOriginalConstructor()
->onlyMethods(['pluginInfo'])
->getMock();

// // TODO: This transient prefix is too buried, refactor it up into the constructor
// $transient_name = 'ideasonpurpose-update-check_mock-dir/plugin.php';
// $transients[$transient_name] = $expected;
// TODO: This transient prefix is too buried, refactor it up into the constructor
$expected = 'Response for testing';
$transient_name = 'ideasonpurpose-update-check_mock-dir/plugin.php';
$get_transient[$transient_name] = $expected;
$ApiMock->transient = $transient_name;

// $this->Api->is_debug = true;
// $this->Api->updateCheck();
$ApiMock->is_debug = true;
$ApiMock->plugin_data = get_plugin_data();

// // $this->assertFalse($this->Api->is_debug);
// $this->assertArrayHasKey($transient_name, $transients);
// // d($expected);
// // $this->assertEquals($expected, $this->Api->response);
// $this->assertStringContainsString('updateCheck', $error_log);
// }
$wp_remote_post = [
'body' => json_encode(['url' => '', 'args' => []]),
'response' => ['code' => 200],
];

// NOTE: Disabled because updateCheck is mocked
// public function testUpdateCheck_transientExists_debugFalse()
// {
// $this->Api->is_debug = false;
// $this->Api->updateCheck();
$ApiMock->updateCheck();
$this->assertContains($transient_name, end($transients));
}

// $this->assertIsObject($this->Api->response);
// $this->assertObjectHasProperty('url', $this->Api->response);
// $this->assertObjectHasProperty('args', $this->Api->response);
// $this->assertArrayHasKey('headers', $this->Api->response->args);
// }
public function testUpdateCheck_transientExists_debugFalse()
{
global $get_transient;

// NOTE: Disabled because updateCheck is mocked
// public function testUpdateCheck_transientExists_wpError()
// {
// global $wp_remote_post, $error_message, $error_log;
$ApiMock = $this->getMockBuilder(Api::class)
->disableOriginalConstructor()
->onlyMethods(['pluginInfo'])
->getMock();

// $wp_remote_post = new \WP_Error();
// $error_message = 'mock Error';
// $this->Api->updateCheck();
$expected = 'Response for testing';
$transient_name = 'ideasonpurpose-update-check_mock-dir/plugin.php';
$get_transient[$transient_name] = $expected;
$ApiMock->transient = $transient_name;

// $this->assertFalse($this->Api->response);
// $this->assertStringContainsString('Something went wrong', $error_log);
// }
$ApiMock->is_debug = false;
$ApiMock->plugin_data = get_plugin_data();
$ApiMock->updateCheck();

// NOTE: Disabled because updateCheck is mocked
// public function testUpdateCheck_transientExists_responseCodeNot200()
// {
// global $wp_remote_post, $error_log;
// $wp_remote_post = false;
// $wp_remote_post = wp_remote_post();
// $wp_remote_post['response']['code'] = 418; // I'm a teapot 🫖
// $wp_remote_post['body'] = "I'm a teapot";
$this->assertEquals($expected, $ApiMock->response);
}

// $this->Api->updateCheck();
public function testUpdateCheck_transientExists_wpError()
{
global $wp_remote_post, $error_message, $error_log;

// $this->assertFalse($this->Api->response);
// $this->assertStringContainsString('Something went wrong', $error_log);
// }
$ApiMock = $this->getMockBuilder(Api::class)
->disableOriginalConstructor()
->onlyMethods(['pluginInfo'])
->getMock();

$expected = 'Response for testing';
$transient_name = 'ideasonpurpose-update-check_mock-dir/plugin.php';
$ApiMock->transient = $transient_name;

$ApiMock->is_debug = true;
$ApiMock->plugin_data = get_plugin_data();

$wp_remote_post = new \WP_Error();
$error_message = 'mock Error';
$ApiMock->updateCheck();

$this->assertFalse($ApiMock->response);
$this->assertStringContainsString('Something went wrong', implode("\n", $error_log));
// $this->assertContains('Something went wrong', $error_log);
}

public function testUpdateCheck_transientExists_responseCodeNot200()
{
global $wp_remote_post, $error_log;

$ApiMock = $this->getMockBuilder(Api::class)
->disableOriginalConstructor()
->onlyMethods(['pluginInfo'])
->getMock();

$expected = 'Response for testing';
$transient_name = 'ideasonpurpose-update-check_mock-dir/plugin.php';
$ApiMock->transient = $transient_name;

$ApiMock->is_debug = true;
$ApiMock->plugin_data = get_plugin_data();

$wp_remote_post = false;
$wp_remote_post = wp_remote_post();
$wp_remote_post['response']['code'] = 418; // I'm a teapot 🫖
$wp_remote_post['body'] = "I'm a teapot";

$ApiMock->updateCheck();

$this->assertFalse($ApiMock->response);
$this->assertStringContainsString('Something went wrong', implode("\n", $error_log));
}
}

0 comments on commit c8464d8

Please sign in to comment.