Verified Commit 67471b86 authored by Erick Hitter's avatar Erick Hitter
Browse files

Introduce utility methods to allow some basic reuse

parent e0b5a92b
......@@ -23,5 +23,8 @@ function _manually_load_plugin() {
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
// Utilities
require_once __DIR__ . '/utils.php';
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
......@@ -16,4 +16,25 @@ class WPCCR_Cron_Options_CPT_Test extends WP_UnitTestCase {
function test_cpt_exists() {
$this->assertTrue( post_type_exists( WP_Cron_Control_Revisited\Cron_Options_CPT::POST_TYPE ) );
}
/**
* Check that an event is stored properly in a CPT entry
*/
function test_events_exist() {
global $wpdb;
$event = WP_Cron_Control_Revisited_Tests\Utils::create_test_event();
$post_name = sprintf( '%s-%s-%s', $event['timestamp'], md5( $event['action'] ), md5( 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 ) );
$this->assertEquals( count( $entry ), 1 );
$entry = array_shift( $entry );
$instance = maybe_unserialize( $entry->post_content_filtered );
$this->assertEquals( $event['action'], $instance['action'] );
$this->assertEquals( md5( serialize( $event['args'] ) ), $instance['instance'] );
WP_Cron_Control_Revisited_Tests\Utils::compare_arrays( $event['args'], $instance['args'], $this );
}
}
......@@ -33,7 +33,7 @@ class WPCCR_REST_API_Test extends WP_UnitTestCase {
* Test that list endpoint returns expected format
*/
public function test_get_items() {
$ev = $this->create_test_event();
$ev = WP_Cron_Control_Revisited_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->set_body( wp_json_encode( array( 'secret' => WP_CRON_CONTROL_SECRET, ) ) );
......@@ -62,7 +62,7 @@ class WPCCR_REST_API_Test extends WP_UnitTestCase {
* Test that list endpoint returns expected format
*/
public function test_run_event() {
$ev = $this->create_test_event();
$ev = WP_Cron_Control_Revisited_Tests\Utils::create_test_event();
$ev['action'] = md5( $ev['action'] );
$ev['instance'] = md5( serialize( $ev['args'] ) );
$ev['secret'] = WP_CRON_CONTROL_SECRET;
......@@ -82,27 +82,6 @@ class WPCCR_REST_API_Test extends WP_UnitTestCase {
}
/**
* Build a test event
*/
protected function create_test_event() {
$event = array(
'timestamp' => time(),
'action' => 'wpccr_test_event',
'args' => array(),
);
$next = wp_next_scheduled( $event['action'], $event['args'] );
if ( $next ) {
$event['timestamp'] = $next;
} else {
wp_schedule_single_event( $event[ 'timestamp' ], $event[ 'action' ], $event[ 'args' ] );
}
return $event;
}
/**
* Check response code
*/
......@@ -114,16 +93,7 @@ class WPCCR_REST_API_Test extends WP_UnitTestCase {
* Ensure response includes the expected data
*/
protected function assertResponseData( $data, $response ) {
$response_data = $response->get_data();
$tested_data = array();
foreach( $data as $key => $value ) {
if ( isset( $response_data[ $key ] ) ) {
$tested_data[ $key ] = $response_data[ $key ];
} else {
$tested_data[ $key ] = null;
}
}
$this->assertEquals( $data, $tested_data );
WP_Cron_Control_Revisited_Tests\Utils::compare_arrays( $data, $response->get_data(), $this );
}
/**
......
<?php
namespace WP_Cron_Control_Revisited_Tests;
class Utils {
/**
* Build a test event
*/
static function create_test_event() {
$event = array(
'timestamp' => time(),
'action' => 'wpccr_test_event',
'args' => array(),
);
$next = wp_next_scheduled( $event['action'], $event['args'] );
if ( $next ) {
$event['timestamp'] = $next;
} else {
wp_schedule_single_event( $event[ 'timestamp' ], $event[ 'action' ], $event[ 'args' ] );
}
return $event;
}
/**
* Check that two arrays are equal
*/
static function compare_arrays( $expected, $test, $context ) {
$tested_data = array();
foreach( $expected as $key => $value ) {
if ( isset( $test[ $key ] ) ) {
$tested_data[ $key ] = $test[ $key ];
} else {
$tested_data[ $key ] = null;
}
}
$context->assertEquals( $expected, $tested_data );
}
}
Supports Markdown
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