From e6eadd1fbf2fb12424a07a45c77481ef41264445 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Fri, 23 Aug 2024 17:35:46 -0700 Subject: [PATCH] Add unit tests for the has_pending_actions_due method In #1077 we're making some performance improvements to the `has_pending_actions_due` method, but there are no unit tests to protect against regressions, so this is simply adding some. Because the tests are in the abstract class, they will get run for each data store type. --- tests/phpunit/jobstore/AbstractStoreTest.php | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/phpunit/jobstore/AbstractStoreTest.php b/tests/phpunit/jobstore/AbstractStoreTest.php index a29897630..8b7035c7c 100644 --- a/tests/phpunit/jobstore/AbstractStoreTest.php +++ b/tests/phpunit/jobstore/AbstractStoreTest.php @@ -5,6 +5,7 @@ use ActionScheduler_Action; use ActionScheduler_Callbacks; use ActionScheduler_IntervalSchedule; +use ActionScheduler_Mocker; use ActionScheduler_SimpleSchedule; use ActionScheduler_Store; use ActionScheduler_UnitTestCase; @@ -115,4 +116,49 @@ public function test_query_actions_by_array_status() { /* End tests for \ActionScheduler_Store::query_actions() */ + /** + * The `has_pending_actions_due` method should return a boolean value depending on whether there are + * pending actions. + * + * @return void + */ + public function test_has_pending_actions_due() { + $store = $this->get_store(); + $runner = ActionScheduler_Mocker::get_queue_runner( $store ); + + for ( $i = - 3; $i <= 3; $i ++ ) { + // Some past actions, some future actions. + $time = as_get_datetime_object( $i . ' hours' ); + $schedule = new ActionScheduler_SimpleSchedule( $time ); + $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, [ $i ], $schedule, 'my_group' ); + + $store->save_action( $action ); + } + + $this->assertTrue( $store->has_pending_actions_due() ); + + $runner->run(); + + $this->assertFalse( $store->has_pending_actions_due() ); + } + + /** + * The `has_pending_actions_due` method should return false when all pending actions are in the future. + * + * @return void + */ + public function test_has_pending_actions_due_only_future_actions() { + $store = $this->get_store(); + + for ( $i = 1; $i <= 3; $i ++ ) { + // Only future actions. + $time = as_get_datetime_object( $i . ' hours' ); + $schedule = new ActionScheduler_SimpleSchedule( $time ); + $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, [ $i ], $schedule, 'my_group' ); + + $store->save_action( $action ); + } + + $this->assertFalse( $store->has_pending_actions_due() ); + } }