Automattic\WP\Cron_Control\Tests\Utils::create_test_event PHP Method

create_test_event() static public method

Build a test event
static public create_test_event ( $allow_multiple = false )
    static function create_test_event($allow_multiple = false)
    {
        $event = array('timestamp' => time(), 'action' => 'a8c_cron_control_test_event', 'args' => array());
        // Plugin skips events with no callbacks
        add_action('a8c_cron_control_test_event', '__return_true');
        if ($allow_multiple) {
            $event['action'] .= '_' . rand(10, 100);
        }
        $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;
    }

Usage Example

 /**
  * Check that an event is stored properly in a CPT entry
  */
 function test_events_exist()
 {
     global $wpdb;
     $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", \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_TYPE, \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_STATUS_PENDING, $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(maybe_serialize($event['args'])), $instance['instance']);
     Utils::compare_arrays($event['args'], $instance['args'], $this);
 }