public function test_next_instance_of_action()
 {
     $store = new ActionScheduler_wpPostStore();
     $runner = new ActionScheduler_QueueRunner($store);
     $random = md5(rand());
     $schedule = new ActionScheduler_IntervalSchedule(new DateTime('12 hours ago'), DAY_IN_SECONDS);
     $action = new ActionScheduler_Action($random, array(), $schedule);
     $store->save_action($action);
     $runner->run();
     $claim = $store->stake_claim(10, new DateTime(DAY_IN_SECONDS - 60 . ' seconds'));
     $this->assertCount(0, $claim->get_actions());
     $claim = $store->stake_claim(10, new DateTime(DAY_IN_SECONDS . ' seconds'));
     $actions = $claim->get_actions();
     $this->assertCount(1, $actions);
     $action_id = reset($actions);
     $new_action = $store->fetch_action($action_id);
     $this->assertEquals($random, $new_action->get_hook());
     $this->assertEquals($schedule->next(new DateTime()), $new_action->get_schedule()->next(new DateTime()));
 }
 public function test_do_not_delete_recent_actions()
 {
     $store = new ActionScheduler_wpPostStore();
     $runner = new ActionScheduler_QueueRunner($store);
     $random = md5(rand());
     $schedule = new ActionScheduler_SimpleSchedule(new DateTime('1 day ago'));
     $created_actions = array();
     for ($i = 0; $i < 5; $i++) {
         $action = new ActionScheduler_Action($random, array($random), $schedule);
         $created_actions[] = $store->save_action($action);
     }
     $runner->run();
     $cleaner = new ActionScheduler_QueueCleaner($store);
     $cleaner->delete_old_actions();
     foreach ($created_actions as $action_id) {
         $action = $store->fetch_action($action_id);
         $this->assertTrue($action->is_finished());
         // It's a FinishedAction
     }
 }
 public function test_get_run_date()
 {
     $time = new DateTime('-10 minutes');
     $schedule = new ActionScheduler_IntervalSchedule($time, HOUR_IN_SECONDS);
     $action = new ActionScheduler_Action('my_hook', array(), $schedule);
     $store = new ActionScheduler_wpPostStore();
     $action_id = $store->save_action($action);
     $this->assertEquals($store->get_date($action_id)->format('U'), $time->format('U'));
     $action = $store->fetch_action($action_id);
     $action->execute();
     $now = new DateTime();
     $store->mark_complete($action_id);
     $this->assertEquals($store->get_date($action_id)->format('U'), $now->format('U'));
     $next = $action->get_schedule()->next($now);
     $new_action_id = $store->save_action($action, $next);
     $this->assertEquals((int) $now->format('U') + HOUR_IN_SECONDS, $store->get_date($new_action_id)->format('U'));
 }