Skip to content

Commit

Permalink
eliminate code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
crstauf committed Oct 4, 2024
1 parent 10050f9 commit 129b490
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions tests/phpunit/migration/Scheduler_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Scheduler_Test extends ActionScheduler_UnitTestCase {
public function setUp() {
parent::setUp();
if ( ! taxonomy_exists( PostStore::GROUP_TAXONOMY ) ) {
// register the post type and taxonomy necessary for the store to work
// register the post type and taxonomy necessary for the store to work.
$store = new PostStore();
$store->init();
}
Expand Down Expand Up @@ -54,25 +54,25 @@ public function test_scheduler_runs_migration() {
};
add_filter( 'action_scheduler/migration_batch_size', $return_5 );

// Make sure successive migration actions are delayed so all actions aren't migrated at once on separate hooks
// Make sure successive migration actions are delayed so all actions aren't migrated at once on separate hooks.
$return_60 = function () {
return 60;
};
add_filter( 'action_scheduler/migration_interval', $return_60 );

for ( $i = 0; $i < 10; $i ++ ) {
for ( $i = 0; $i < 10; $i++ ) {
$time = as_get_datetime_object( $i + 1 . ' minutes' );
$schedule = new ActionScheduler_SimpleSchedule( $time );
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, [], $schedule );
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
$future[] = $source_store->save_action( $action );

$time = as_get_datetime_object( $i + 1 . ' minutes ago' );
$schedule = new ActionScheduler_SimpleSchedule( $time );
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, [], $schedule );
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
$due[] = $source_store->save_action( $action );
}

$this->assertCount( 20, $source_store->query_actions( [ 'per_page' => 0 ] ) );
$this->assertCount( 20, $source_store->query_actions( array( 'per_page' => 0 ) ) );

$scheduler = new Scheduler();
$scheduler->unschedule_migration();
Expand All @@ -81,8 +81,12 @@ public function test_scheduler_runs_migration() {
$queue_runner = ActionScheduler_Mocker::get_queue_runner( $destination_store );
$queue_runner->run();

// 5 actions should have moved from the source store when the queue runner triggered the migration action
$this->assertCount( 15, $source_store->query_actions( [ 'per_page' => 0, 'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ] ) );
// 5 actions should have moved from the source store when the queue runner triggered the migration action.
$args = array(
'per_page' => 0,
'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK,
);
$this->assertCount( 15, $source_store->query_actions( $args ) );

remove_filter( 'action_scheduler/migration_batch_size', $return_5 );
remove_filter( 'action_scheduler/migration_interval', $return_60 );
Expand All @@ -95,11 +99,11 @@ public function test_scheduler_marks_itself_complete() {
for ( $i = 0; $i < 5; $i ++ ) {
$time = as_get_datetime_object( $i + 1 . ' minutes ago' );
$schedule = new ActionScheduler_SimpleSchedule( $time );
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, [], $schedule );
$action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule );
$due[] = $source_store->save_action( $action );
}

$this->assertCount( 5, $source_store->query_actions( [ 'per_page' => 0 ] ) );
$this->assertCount( 5, $source_store->query_actions( array( 'per_page' => 0 ) ) );

$scheduler = new Scheduler();
$scheduler->unschedule_migration();
Expand All @@ -108,22 +112,26 @@ public function test_scheduler_marks_itself_complete() {
$queue_runner = ActionScheduler_Mocker::get_queue_runner( $destination_store );
$queue_runner->run();

// All actions should have moved from the source store when the queue runner triggered the migration action
$this->assertCount( 0, $source_store->query_actions( [ 'per_page' => 0, 'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ] ) );
// All actions should have moved from the source store when the queue runner triggered the migration action.
$args = array(
'per_page' => 0,
'hook' => ActionScheduler_Callbacks::HOOK_WITH_CALLBACK,
);
$this->assertCount( 0, $source_store->query_actions( $args ) );

// schedule another so we can get it to run immediately
// schedule another so we can get it to run immediately.
$scheduler->unschedule_migration();
$scheduler->schedule_migration( time() - 1 );

// run again so it knows that there's nothing left to process
// run again so it knows that there's nothing left to process.
$queue_runner->run();

$scheduler->unhook();

// ensure the flag is set marking migration as complete
// ensure the flag is set marking migration as complete.
$this->assertTrue( ActionScheduler_DataController::is_migration_complete() );

// ensure that another instance has not been scheduled
// ensure that another instance has not been scheduled.
$this->assertFalse( $scheduler->is_migration_scheduled() );

}
Expand Down

0 comments on commit 129b490

Please sign in to comment.