Verified Commit 0c4f6e54 authored by Erick Hitter's avatar Erick Hitter
Browse files

Review feedback: better namespacing

parent 985790db
<?php
namespace WP_Cron_Control_Revisited;
namespace Automattic\WP\Cron_Control;
abstract class Singleton {
/**
......
<?php
namespace WP_Cron_Control_Revisited;
namespace Automattic\WP\Cron_Control;
class Cron_Options_CPT extends Singleton {
/**
......
<?php
namespace WP_Cron_Control_Revisited;
namespace Automattic\WP\Cron_Control;
class Events extends Singleton {
/**
......
<?php
namespace WP_Cron_Control_Revisited;
namespace Automattic\WP\Cron_Control;
class Internal_Events extends Singleton {
/**
......
<?php
namespace WP_Cron_Control_Revisited;
namespace Automattic\WP\Cron_Control;
class Lock {
/**
......
<?php
namespace WP_Cron_Control_Revisited;
namespace Automattic\WP\Cron_Control;
class Main extends Singleton {
/**
......
<?php
namespace WP_Cron_Control_Revisited;
namespace Automattic\WP\Cron_Control;
class REST_API extends Singleton {
/**
......
<?php
namespace WP_Cron_Control_Revisited;
namespace Automattic\WP\Cron_Control;
/**
* Job queues
......
<?php
namespace WP_Cron_Control_Revisited;
namespace Automattic\WP\Cron_Control;
/**
* Check if an event is an internal one that the plugin will always run
......
<?php
namespace WP_Cron_Control_Revisited;
namespace Automattic\WP\Cron_Control;
/**
* Produce a simplified version of the cron events array
......
<?php
namespace WP_Cron_Control_Revisited_Tests;
namespace Automattic\WP\Cron_Control\Tests;
class Utils {
/**
......@@ -33,8 +33,8 @@ class Utils {
*/
static function get_events_from_post_objects() {
$events = get_posts( array(
'post_type' => \WP_Cron_Control_Revisited\Cron_Options_CPT::POST_TYPE,
'post_status' => \WP_Cron_Control_Revisited\Cron_Options_CPT::POST_STATUS,
'post_type' => \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_TYPE,
'post_status' => \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_STATUS,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'ASC',
......
......@@ -14,7 +14,7 @@ class WPCCR_Cron_Options_CPT_Test extends WP_UnitTestCase {
* Custom post type exists
*/
function test_cpt_exists() {
$this->assertTrue( post_type_exists( WP_Cron_Control_Revisited\Cron_Options_CPT::POST_TYPE ) );
$this->assertTrue( post_type_exists( Automattic\WP\Cron_Control\Cron_Options_CPT::POST_TYPE ) );
}
/**
......@@ -23,10 +23,10 @@ class WPCCR_Cron_Options_CPT_Test extends WP_UnitTestCase {
function test_events_exist() {
global $wpdb;
$event = WP_Cron_Control_Revisited_Tests\Utils::create_test_event();
$event = Automattic\WP\Cron_Control\Tests\Utils::create_test_event();
$post_name = sprintf( '%s-%s-%s', $event['timestamp'], md5( $event['action'] ), md5( maybe_serialize( $event['args'] ) ) );
$entry = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = %s AND post_status = %s AND post_name = %s LIMIT 1", WP_Cron_Control_Revisited\Cron_Options_CPT::POST_TYPE, WP_Cron_Control_Revisited\Cron_Options_CPT::POST_STATUS, $post_name ) );
$entry = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = %s AND post_status = %s AND post_name = %s LIMIT 1", Automattic\WP\Cron_Control\Cron_Options_CPT::POST_TYPE, Automattic\WP\Cron_Control\Cron_Options_CPT::POST_STATUS, $post_name ) );
$this->assertEquals( count( $entry ), 1 );
......@@ -35,14 +35,14 @@ class WPCCR_Cron_Options_CPT_Test extends WP_UnitTestCase {
$this->assertEquals( $event['action'], $instance['action'] );
$this->assertEquals( md5( maybe_serialize( $event['args'] ) ), $instance['instance'] );
WP_Cron_Control_Revisited_Tests\Utils::compare_arrays( $event['args'], $instance['args'], $this );
Automattic\WP\Cron_Control\Tests\Utils::compare_arrays( $event['args'], $instance['args'], $this );
}
/**
* Check format of filtered array returned from CPT
*/
function test_filter_cron_option_get() {
$event = WP_Cron_Control_Revisited_Tests\Utils::create_test_event();
$event = Automattic\WP\Cron_Control\Tests\Utils::create_test_event();
$cron = get_option( 'cron' );
......@@ -52,7 +52,7 @@ class WPCCR_Cron_Options_CPT_Test extends WP_UnitTestCase {
$this->assertEquals( $cron['version'], 2 );
// Validate the remaining structure
$cron = \WP_Cron_Control_Revisited\collapse_events_array( $cron );
$cron = \Automattic\WP\Cron_Control\collapse_events_array( $cron );
foreach ( $cron as $single_cron ) {
$this->assertEquals( $single_cron['timestamp'], $event['timestamp'] );
......@@ -68,8 +68,8 @@ class WPCCR_Cron_Options_CPT_Test extends WP_UnitTestCase {
* Test that events are unscheduled correctly using Core functions
*/
function test_event_unscheduling_using_core_functions() {
$first_event = WP_Cron_Control_Revisited_Tests\Utils::create_test_event();
$second_event = WP_Cron_Control_Revisited_Tests\Utils::create_test_event( true );
$first_event = Automattic\WP\Cron_Control\Tests\Utils::create_test_event();
$second_event = Automattic\WP\Cron_Control\Tests\Utils::create_test_event( true );
$first_event_ts = wp_next_scheduled( $first_event['action'], $first_event['args'] );
......@@ -95,29 +95,29 @@ class WPCCR_Cron_Options_CPT_Test extends WP_UnitTestCase {
*/
function test_event_unscheduling_against_cpt() {
// Schedule two events and prepare their data a bit for further testing
$first_event = WP_Cron_Control_Revisited_Tests\Utils::create_test_event();
$first_event = Automattic\WP\Cron_Control\Tests\Utils::create_test_event();
$first_event['instance'] = md5( maybe_serialize( $first_event['args'] ) );
$first_event_args = $first_event['args'];
unset( $first_event['args'] );
sleep( 2 ); // More-thorough to test with events that don't have matching timestamps
$second_event = WP_Cron_Control_Revisited_Tests\Utils::create_test_event( true );
$second_event = Automattic\WP\Cron_Control\Tests\Utils::create_test_event( true );
$second_event['instance'] = md5( maybe_serialize( $second_event['args'] ) );
$second_event_args = $second_event['args'];
unset( $second_event['args'] );
// First, check that posts were created for the two events
WP_Cron_Control_Revisited_Tests\Utils::compare_arrays( array( $first_event, $second_event ), WP_Cron_Control_Revisited_Tests\Utils::get_events_from_post_objects(), $this );
Automattic\WP\Cron_Control\Tests\Utils::compare_arrays( array( $first_event, $second_event ), Automattic\WP\Cron_Control\Tests\Utils::get_events_from_post_objects(), $this );
// Second, unschedule an event and confirm that the post is removed
wp_unschedule_event( $first_event['timestamp'], $first_event['action'], $first_event_args );
WP_Cron_Control_Revisited_Tests\Utils::compare_arrays( array( $second_event ), WP_Cron_Control_Revisited_Tests\Utils::get_events_from_post_objects(), $this );
Automattic\WP\Cron_Control\Tests\Utils::compare_arrays( array( $second_event ), Automattic\WP\Cron_Control\Tests\Utils::get_events_from_post_objects(), $this );
// Finally, unschedule the second event and confirm its post is also deleted
wp_unschedule_event( $second_event['timestamp'], $second_event['action'], $second_event_args );
$this->assertEmpty( WP_Cron_Control_Revisited_Tests\Utils::get_events_from_post_objects() );
$this->assertEmpty( Automattic\WP\Cron_Control\Tests\Utils::get_events_from_post_objects() );
}
}
......@@ -14,16 +14,16 @@ class WPCCR_Internal_Events_Test extends WP_UnitTestCase {
* Internal events should be scheduled
*/
function test_events() {
WP_Cron_Control_Revisited\Internal_Events::instance()->schedule_internal_events();
Automattic\WP\Cron_Control\Internal_Events::instance()->schedule_internal_events();
$events = \WP_Cron_Control_Revisited\collapse_events_array( get_option( 'cron' ) );
$events = \Automattic\WP\Cron_Control\collapse_events_array( get_option( 'cron' ) );
// Check that the plugin scheduled the expected number of events
$this->assertEquals( count( $events ), 3 );
// Confirm that the scheduled jobs came from the Internal Events class
foreach ( $events as $event ) {
$this->assertTrue( \WP_Cron_Control_Revisited\is_internal_event( $event['action'] ) );
$this->assertTrue( \Automattic\WP\Cron_Control\is_internal_event( $event['action'] ) );
}
}
}
......@@ -24,7 +24,7 @@ class WPCCR_REST_API_Test extends WP_UnitTestCase {
* Verify that GET requests to the endpoint fail
*/
public function test_invalid_request() {
$request = new WP_REST_Request( 'GET', '/' . WP_Cron_Control_Revisited\REST_API_NAMESPACE . '/' . WP_Cron_Control_Revisited\REST_API_ENDPOINT_LIST );
$request = new WP_REST_Request( 'GET', '/' . Automattic\WP\Cron_Control\REST_API_NAMESPACE . '/' . Automattic\WP\Cron_Control\REST_API_ENDPOINT_LIST );
$response = $this->server->dispatch( $request );
$this->assertResponseStatus( 404, $response );
}
......@@ -33,9 +33,9 @@ class WPCCR_REST_API_Test extends WP_UnitTestCase {
* Test that list endpoint returns expected format
*/
public function test_get_items() {
$ev = WP_Cron_Control_Revisited_Tests\Utils::create_test_event();
$ev = Automattic\WP\Cron_Control\Tests\Utils::create_test_event();
$request = new WP_REST_Request( 'POST', '/' . WP_Cron_Control_Revisited\REST_API_NAMESPACE . '/' . WP_Cron_Control_Revisited\REST_API_ENDPOINT_LIST );
$request = new WP_REST_Request( 'POST', '/' . Automattic\WP\Cron_Control\REST_API_NAMESPACE . '/' . Automattic\WP\Cron_Control\REST_API_ENDPOINT_LIST );
$request->set_body( wp_json_encode( array( 'secret' => WP_CRON_CONTROL_SECRET, ) ) );
$request->set_header( 'content-type', 'application/json' );
......@@ -54,7 +54,7 @@ class WPCCR_REST_API_Test extends WP_UnitTestCase {
'instance' => md5( maybe_serialize( $ev['args'] ) ),
),
),
'endpoint' => get_rest_url( null, WP_Cron_Control_Revisited\REST_API_NAMESPACE . '/' . WP_Cron_Control_Revisited\REST_API_ENDPOINT_RUN ),
'endpoint' => get_rest_url( null, Automattic\WP\Cron_Control\REST_API_NAMESPACE . '/' . Automattic\WP\Cron_Control\REST_API_ENDPOINT_RUN ),
), $response );
}
......@@ -62,13 +62,13 @@ class WPCCR_REST_API_Test extends WP_UnitTestCase {
* Test that list endpoint returns expected format
*/
public function test_run_event() {
$ev = WP_Cron_Control_Revisited_Tests\Utils::create_test_event();
$ev = Automattic\WP\Cron_Control\Tests\Utils::create_test_event();
$ev['action'] = md5( $ev['action'] );
$ev['instance'] = md5( maybe_serialize( $ev['args'] ) );
$ev['secret'] = WP_CRON_CONTROL_SECRET;
unset( $ev['args'] );
$request = new WP_REST_Request( 'PUT', '/' . WP_Cron_Control_Revisited\REST_API_NAMESPACE . '/' . WP_Cron_Control_Revisited\REST_API_ENDPOINT_RUN );
$request = new WP_REST_Request( 'PUT', '/' . Automattic\WP\Cron_Control\REST_API_NAMESPACE . '/' . Automattic\WP\Cron_Control\REST_API_ENDPOINT_RUN );
$request->set_body( wp_json_encode( $ev ) );
$request->set_header( 'content-type', 'application/json' );
......@@ -93,7 +93,7 @@ class WPCCR_REST_API_Test extends WP_UnitTestCase {
* Ensure response includes the expected data
*/
protected function assertResponseData( $data, $response ) {
WP_Cron_Control_Revisited_Tests\Utils::compare_arrays( $data, $response->get_data(), $this );
Automattic\WP\Cron_Control\Tests\Utils::compare_arrays( $data, $response->get_data(), $this );
}
/**
......
......@@ -8,7 +8,7 @@
Text Domain: wp-cron-control-revisited
*/
namespace WP_Cron_Control_Revisited;
namespace Automattic\WP\Cron_Control;
// Load basics needed to instantiate plugin
require __DIR__ . '/includes/constants.php';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment