@@ -6,33 +6,33 @@ |
||
6 | 6 | * @group schedules |
7 | 7 | */ |
8 | 8 | class ActionScheduler_IntervalSchedule_Test extends ActionScheduler_UnitTestCase { |
9 | - public function test_creation() { |
|
10 | - $time = as_get_datetime_object(); |
|
11 | - $schedule = new ActionScheduler_IntervalSchedule( $time, HOUR_IN_SECONDS ); |
|
12 | - $this->assertEquals( $time, $schedule->get_date() ); |
|
13 | - $this->assertEquals( $time, $schedule->get_first_date() ); |
|
14 | - } |
|
9 | + public function test_creation() { |
|
10 | + $time = as_get_datetime_object(); |
|
11 | + $schedule = new ActionScheduler_IntervalSchedule( $time, HOUR_IN_SECONDS ); |
|
12 | + $this->assertEquals( $time, $schedule->get_date() ); |
|
13 | + $this->assertEquals( $time, $schedule->get_first_date() ); |
|
14 | + } |
|
15 | 15 | |
16 | - public function test_creation_with_first_date() { |
|
17 | - $first = as_get_datetime_object(); |
|
18 | - $time = as_get_datetime_object( '+12 hours' ); |
|
19 | - $schedule = new ActionScheduler_IntervalSchedule( $time, HOUR_IN_SECONDS, $first ); |
|
20 | - $this->assertEquals( $time, $schedule->get_date() ); |
|
21 | - $this->assertEquals( $first, $schedule->get_first_date() ); |
|
22 | - } |
|
16 | + public function test_creation_with_first_date() { |
|
17 | + $first = as_get_datetime_object(); |
|
18 | + $time = as_get_datetime_object( '+12 hours' ); |
|
19 | + $schedule = new ActionScheduler_IntervalSchedule( $time, HOUR_IN_SECONDS, $first ); |
|
20 | + $this->assertEquals( $time, $schedule->get_date() ); |
|
21 | + $this->assertEquals( $first, $schedule->get_first_date() ); |
|
22 | + } |
|
23 | 23 | |
24 | - public function test_next() { |
|
25 | - $now = time(); |
|
26 | - $start = $now - 30; |
|
27 | - $schedule = new ActionScheduler_IntervalSchedule( as_get_datetime_object( "@$start" ), MINUTE_IN_SECONDS ); |
|
28 | - $this->assertEquals( $start, $schedule->get_date()->getTimestamp() ); |
|
29 | - $this->assertEquals( $now + MINUTE_IN_SECONDS, $schedule->get_next( as_get_datetime_object() )->getTimestamp() ); |
|
30 | - $this->assertEquals( $start, $schedule->get_next( as_get_datetime_object( "@$start" ) )->getTimestamp() ); |
|
31 | - } |
|
24 | + public function test_next() { |
|
25 | + $now = time(); |
|
26 | + $start = $now - 30; |
|
27 | + $schedule = new ActionScheduler_IntervalSchedule( as_get_datetime_object( "@$start" ), MINUTE_IN_SECONDS ); |
|
28 | + $this->assertEquals( $start, $schedule->get_date()->getTimestamp() ); |
|
29 | + $this->assertEquals( $now + MINUTE_IN_SECONDS, $schedule->get_next( as_get_datetime_object() )->getTimestamp() ); |
|
30 | + $this->assertEquals( $start, $schedule->get_next( as_get_datetime_object( "@$start" ) )->getTimestamp() ); |
|
31 | + } |
|
32 | 32 | |
33 | - public function test_is_recurring() { |
|
34 | - $start = time() - 30; |
|
35 | - $schedule = new ActionScheduler_IntervalSchedule( as_get_datetime_object( "@$start" ), MINUTE_IN_SECONDS ); |
|
36 | - $this->assertTrue( $schedule->is_recurring() ); |
|
37 | - } |
|
33 | + public function test_is_recurring() { |
|
34 | + $start = time() - 30; |
|
35 | + $schedule = new ActionScheduler_IntervalSchedule( as_get_datetime_object( "@$start" ), MINUTE_IN_SECONDS ); |
|
36 | + $this->assertTrue( $schedule->is_recurring() ); |
|
37 | + } |
|
38 | 38 | } |
@@ -8,31 +8,31 @@ |
||
8 | 8 | class ActionScheduler_IntervalSchedule_Test extends ActionScheduler_UnitTestCase { |
9 | 9 | public function test_creation() { |
10 | 10 | $time = as_get_datetime_object(); |
11 | - $schedule = new ActionScheduler_IntervalSchedule( $time, HOUR_IN_SECONDS ); |
|
12 | - $this->assertEquals( $time, $schedule->get_date() ); |
|
13 | - $this->assertEquals( $time, $schedule->get_first_date() ); |
|
11 | + $schedule = new ActionScheduler_IntervalSchedule($time, HOUR_IN_SECONDS); |
|
12 | + $this->assertEquals($time, $schedule->get_date()); |
|
13 | + $this->assertEquals($time, $schedule->get_first_date()); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | public function test_creation_with_first_date() { |
17 | 17 | $first = as_get_datetime_object(); |
18 | - $time = as_get_datetime_object( '+12 hours' ); |
|
19 | - $schedule = new ActionScheduler_IntervalSchedule( $time, HOUR_IN_SECONDS, $first ); |
|
20 | - $this->assertEquals( $time, $schedule->get_date() ); |
|
21 | - $this->assertEquals( $first, $schedule->get_first_date() ); |
|
18 | + $time = as_get_datetime_object('+12 hours'); |
|
19 | + $schedule = new ActionScheduler_IntervalSchedule($time, HOUR_IN_SECONDS, $first); |
|
20 | + $this->assertEquals($time, $schedule->get_date()); |
|
21 | + $this->assertEquals($first, $schedule->get_first_date()); |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | public function test_next() { |
25 | 25 | $now = time(); |
26 | 26 | $start = $now - 30; |
27 | - $schedule = new ActionScheduler_IntervalSchedule( as_get_datetime_object( "@$start" ), MINUTE_IN_SECONDS ); |
|
28 | - $this->assertEquals( $start, $schedule->get_date()->getTimestamp() ); |
|
29 | - $this->assertEquals( $now + MINUTE_IN_SECONDS, $schedule->get_next( as_get_datetime_object() )->getTimestamp() ); |
|
30 | - $this->assertEquals( $start, $schedule->get_next( as_get_datetime_object( "@$start" ) )->getTimestamp() ); |
|
27 | + $schedule = new ActionScheduler_IntervalSchedule(as_get_datetime_object("@$start"), MINUTE_IN_SECONDS); |
|
28 | + $this->assertEquals($start, $schedule->get_date()->getTimestamp()); |
|
29 | + $this->assertEquals($now + MINUTE_IN_SECONDS, $schedule->get_next(as_get_datetime_object())->getTimestamp()); |
|
30 | + $this->assertEquals($start, $schedule->get_next(as_get_datetime_object("@$start"))->getTimestamp()); |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | public function test_is_recurring() { |
34 | 34 | $start = time() - 30; |
35 | - $schedule = new ActionScheduler_IntervalSchedule( as_get_datetime_object( "@$start" ), MINUTE_IN_SECONDS ); |
|
36 | - $this->assertTrue( $schedule->is_recurring() ); |
|
35 | + $schedule = new ActionScheduler_IntervalSchedule(as_get_datetime_object("@$start"), MINUTE_IN_SECONDS); |
|
36 | + $this->assertTrue($schedule->is_recurring()); |
|
37 | 37 | } |
38 | 38 | } |
@@ -6,14 +6,14 @@ |
||
6 | 6 | * @group schedules |
7 | 7 | */ |
8 | 8 | class ActionScheduler_NullSchedule_Test extends ActionScheduler_UnitTestCase { |
9 | - public function test_null_schedule() { |
|
10 | - $schedule = new ActionScheduler_NullSchedule(); |
|
11 | - $this->assertNull( $schedule->get_date() ); |
|
12 | - } |
|
9 | + public function test_null_schedule() { |
|
10 | + $schedule = new ActionScheduler_NullSchedule(); |
|
11 | + $this->assertNull( $schedule->get_date() ); |
|
12 | + } |
|
13 | 13 | |
14 | - public function test_is_recurring() { |
|
15 | - $schedule = new ActionScheduler_NullSchedule(); |
|
16 | - $this->assertFalse( $schedule->is_recurring() ); |
|
17 | - } |
|
14 | + public function test_is_recurring() { |
|
15 | + $schedule = new ActionScheduler_NullSchedule(); |
|
16 | + $this->assertFalse( $schedule->is_recurring() ); |
|
17 | + } |
|
18 | 18 | } |
19 | 19 |
@@ -8,12 +8,12 @@ |
||
8 | 8 | class ActionScheduler_NullSchedule_Test extends ActionScheduler_UnitTestCase { |
9 | 9 | public function test_null_schedule() { |
10 | 10 | $schedule = new ActionScheduler_NullSchedule(); |
11 | - $this->assertNull( $schedule->get_date() ); |
|
11 | + $this->assertNull($schedule->get_date()); |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | public function test_is_recurring() { |
15 | 15 | $schedule = new ActionScheduler_NullSchedule(); |
16 | - $this->assertFalse( $schedule->is_recurring() ); |
|
16 | + $this->assertFalse($schedule->is_recurring()); |
|
17 | 17 | } |
18 | 18 | } |
19 | 19 |
@@ -3,44 +3,44 @@ |
||
3 | 3 | * @group helpers |
4 | 4 | */ |
5 | 5 | class ActionScheduler_Compatibility_Test extends ActionScheduler_UnitTestCase { |
6 | - /** |
|
7 | - * Test the logic relating to ActionScheduler_Compatibility::raise_time_limit(). |
|
8 | - */ |
|
9 | - public function test_raise_time_limit() { |
|
10 | - // We'll want to restore things after this test. |
|
11 | - $default_max_execution_time = ini_get( 'max_execution_time' ); |
|
6 | + /** |
|
7 | + * Test the logic relating to ActionScheduler_Compatibility::raise_time_limit(). |
|
8 | + */ |
|
9 | + public function test_raise_time_limit() { |
|
10 | + // We'll want to restore things after this test. |
|
11 | + $default_max_execution_time = ini_get( 'max_execution_time' ); |
|
12 | 12 | |
13 | - ini_set( 'max_execution_time', 0 ); |
|
14 | - ActionScheduler_Compatibility::raise_time_limit( 10 ); |
|
15 | - $this->assertSame( |
|
16 | - '0', |
|
17 | - ini_get( 'max_execution_time' ), |
|
18 | - 'If the max_execution_time was already zero (unlimited), then it will not be changed.' |
|
19 | - ); |
|
13 | + ini_set( 'max_execution_time', 0 ); |
|
14 | + ActionScheduler_Compatibility::raise_time_limit( 10 ); |
|
15 | + $this->assertSame( |
|
16 | + '0', |
|
17 | + ini_get( 'max_execution_time' ), |
|
18 | + 'If the max_execution_time was already zero (unlimited), then it will not be changed.' |
|
19 | + ); |
|
20 | 20 | |
21 | - ini_set( 'max_execution_time', 60 ); |
|
22 | - ActionScheduler_Compatibility::raise_time_limit( 30 ); |
|
23 | - $this->assertSame( |
|
24 | - '60', |
|
25 | - ini_get( 'max_execution_time' ), |
|
26 | - 'If the max_execution_time was already a higher value than we specify, then it will not be changed.' |
|
27 | - ); |
|
21 | + ini_set( 'max_execution_time', 60 ); |
|
22 | + ActionScheduler_Compatibility::raise_time_limit( 30 ); |
|
23 | + $this->assertSame( |
|
24 | + '60', |
|
25 | + ini_get( 'max_execution_time' ), |
|
26 | + 'If the max_execution_time was already a higher value than we specify, then it will not be changed.' |
|
27 | + ); |
|
28 | 28 | |
29 | - ActionScheduler_Compatibility::raise_time_limit( 200 ); |
|
30 | - $this->assertSame( |
|
31 | - '200', |
|
32 | - ini_get( 'max_execution_time' ), |
|
33 | - 'If the max_execution_time was a lower value than we specify, but was above zero, then it will be updated to the new value.' |
|
34 | - ); |
|
29 | + ActionScheduler_Compatibility::raise_time_limit( 200 ); |
|
30 | + $this->assertSame( |
|
31 | + '200', |
|
32 | + ini_get( 'max_execution_time' ), |
|
33 | + 'If the max_execution_time was a lower value than we specify, but was above zero, then it will be updated to the new value.' |
|
34 | + ); |
|
35 | 35 | |
36 | - ActionScheduler_Compatibility::raise_time_limit( 0 ); |
|
37 | - $this->assertSame( |
|
38 | - '0', |
|
39 | - ini_get( 'max_execution_time' ), |
|
40 | - 'If the max_execution_time was a positive, non-zero value and we then specify zero (unlimited) as the new value, then it will be updated.' |
|
41 | - ); |
|
36 | + ActionScheduler_Compatibility::raise_time_limit( 0 ); |
|
37 | + $this->assertSame( |
|
38 | + '0', |
|
39 | + ini_get( 'max_execution_time' ), |
|
40 | + 'If the max_execution_time was a positive, non-zero value and we then specify zero (unlimited) as the new value, then it will be updated.' |
|
41 | + ); |
|
42 | 42 | |
43 | - // Cleanup. |
|
44 | - ini_set( 'max_execution_time', $default_max_execution_time ); |
|
45 | - } |
|
43 | + // Cleanup. |
|
44 | + ini_set( 'max_execution_time', $default_max_execution_time ); |
|
45 | + } |
|
46 | 46 | } |
@@ -8,39 +8,39 @@ |
||
8 | 8 | */ |
9 | 9 | public function test_raise_time_limit() { |
10 | 10 | // We'll want to restore things after this test. |
11 | - $default_max_execution_time = ini_get( 'max_execution_time' ); |
|
11 | + $default_max_execution_time = ini_get('max_execution_time'); |
|
12 | 12 | |
13 | - ini_set( 'max_execution_time', 0 ); |
|
14 | - ActionScheduler_Compatibility::raise_time_limit( 10 ); |
|
13 | + ini_set('max_execution_time', 0); |
|
14 | + ActionScheduler_Compatibility::raise_time_limit(10); |
|
15 | 15 | $this->assertSame( |
16 | 16 | '0', |
17 | - ini_get( 'max_execution_time' ), |
|
17 | + ini_get('max_execution_time'), |
|
18 | 18 | 'If the max_execution_time was already zero (unlimited), then it will not be changed.' |
19 | 19 | ); |
20 | 20 | |
21 | - ini_set( 'max_execution_time', 60 ); |
|
22 | - ActionScheduler_Compatibility::raise_time_limit( 30 ); |
|
21 | + ini_set('max_execution_time', 60); |
|
22 | + ActionScheduler_Compatibility::raise_time_limit(30); |
|
23 | 23 | $this->assertSame( |
24 | 24 | '60', |
25 | - ini_get( 'max_execution_time' ), |
|
25 | + ini_get('max_execution_time'), |
|
26 | 26 | 'If the max_execution_time was already a higher value than we specify, then it will not be changed.' |
27 | 27 | ); |
28 | 28 | |
29 | - ActionScheduler_Compatibility::raise_time_limit( 200 ); |
|
29 | + ActionScheduler_Compatibility::raise_time_limit(200); |
|
30 | 30 | $this->assertSame( |
31 | 31 | '200', |
32 | - ini_get( 'max_execution_time' ), |
|
32 | + ini_get('max_execution_time'), |
|
33 | 33 | 'If the max_execution_time was a lower value than we specify, but was above zero, then it will be updated to the new value.' |
34 | 34 | ); |
35 | 35 | |
36 | - ActionScheduler_Compatibility::raise_time_limit( 0 ); |
|
36 | + ActionScheduler_Compatibility::raise_time_limit(0); |
|
37 | 37 | $this->assertSame( |
38 | 38 | '0', |
39 | - ini_get( 'max_execution_time' ), |
|
39 | + ini_get('max_execution_time'), |
|
40 | 40 | 'If the max_execution_time was a positive, non-zero value and we then specify zero (unlimited) as the new value, then it will be updated.' |
41 | 41 | ); |
42 | 42 | |
43 | 43 | // Cleanup. |
44 | - ini_set( 'max_execution_time', $default_max_execution_time ); |
|
44 | + ini_set('max_execution_time', $default_max_execution_time); |
|
45 | 45 | } |
46 | 46 | } |
@@ -1,31 +1,31 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | class ActionScheduler_Callbacks { |
4 | - /** |
|
5 | - * Scheduled action hook that can be used when we want to simulate an action |
|
6 | - * with a registered callback. |
|
7 | - */ |
|
8 | - const HOOK_WITH_CALLBACK = 'hook_with_callback'; |
|
4 | + /** |
|
5 | + * Scheduled action hook that can be used when we want to simulate an action |
|
6 | + * with a registered callback. |
|
7 | + */ |
|
8 | + const HOOK_WITH_CALLBACK = 'hook_with_callback'; |
|
9 | 9 | |
10 | - /** |
|
11 | - * Setup callbacks for different types of hook. |
|
12 | - */ |
|
13 | - public static function add_callbacks() { |
|
14 | - add_action( self::HOOK_WITH_CALLBACK, array( __CLASS__, 'empty_callback' ) ); |
|
15 | - } |
|
10 | + /** |
|
11 | + * Setup callbacks for different types of hook. |
|
12 | + */ |
|
13 | + public static function add_callbacks() { |
|
14 | + add_action( self::HOOK_WITH_CALLBACK, array( __CLASS__, 'empty_callback' ) ); |
|
15 | + } |
|
16 | 16 | |
17 | - /** |
|
18 | - * Remove callbacks. |
|
19 | - */ |
|
20 | - public static function remove_callbacks() { |
|
21 | - remove_action( self::HOOK_WITH_CALLBACK, array( __CLASS__, 'empty_callback' ) ); |
|
22 | - } |
|
17 | + /** |
|
18 | + * Remove callbacks. |
|
19 | + */ |
|
20 | + public static function remove_callbacks() { |
|
21 | + remove_action( self::HOOK_WITH_CALLBACK, array( __CLASS__, 'empty_callback' ) ); |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * This stub is used as the callback function for the ActionScheduler_Callbacks::HOOK_WITH_CALLBACK hook. |
|
26 | - * |
|
27 | - * Action Scheduler will mark actions as 'failed' if a callback does not exist, this |
|
28 | - * simply serves to act as the callback for various test scenarios in child classes. |
|
29 | - */ |
|
30 | - public static function empty_callback() {} |
|
24 | + /** |
|
25 | + * This stub is used as the callback function for the ActionScheduler_Callbacks::HOOK_WITH_CALLBACK hook. |
|
26 | + * |
|
27 | + * Action Scheduler will mark actions as 'failed' if a callback does not exist, this |
|
28 | + * simply serves to act as the callback for various test scenarios in child classes. |
|
29 | + */ |
|
30 | + public static function empty_callback() {} |
|
31 | 31 | } |
@@ -11,14 +11,14 @@ |
||
11 | 11 | * Setup callbacks for different types of hook. |
12 | 12 | */ |
13 | 13 | public static function add_callbacks() { |
14 | - add_action( self::HOOK_WITH_CALLBACK, array( __CLASS__, 'empty_callback' ) ); |
|
14 | + add_action(self::HOOK_WITH_CALLBACK, array(__CLASS__, 'empty_callback')); |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * Remove callbacks. |
19 | 19 | */ |
20 | 20 | public static function remove_callbacks() { |
21 | - remove_action( self::HOOK_WITH_CALLBACK, array( __CLASS__, 'empty_callback' ) ); |
|
21 | + remove_action(self::HOOK_WITH_CALLBACK, array(__CLASS__, 'empty_callback')); |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
@@ -5,56 +5,56 @@ |
||
5 | 5 | */ |
6 | 6 | class ActionScheduler_UnitTestCase extends WP_UnitTestCase { |
7 | 7 | |
8 | - protected $existing_timezone; |
|
9 | - |
|
10 | - /** |
|
11 | - * Perform test set-up work. |
|
12 | - */ |
|
13 | - public function set_up() { |
|
14 | - ActionScheduler_Callbacks::add_callbacks(); |
|
15 | - parent::set_up(); |
|
16 | - } |
|
17 | - |
|
18 | - /** |
|
19 | - * Perform test tear-down work. |
|
20 | - */ |
|
21 | - public function tear_down() { |
|
22 | - ActionScheduler_Callbacks::remove_callbacks(); |
|
23 | - parent::tear_down(); |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * Counts the number of test cases executed by run(TestResult result). |
|
28 | - * |
|
29 | - * @return int |
|
30 | - */ |
|
31 | - public function count() { |
|
32 | - return 'UTC' == date_default_timezone_get() ? 2 : 3; |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * We want to run every test multiple times using a different timezone to make sure |
|
37 | - * that they are unaffected by changes to PHP's timezone. |
|
38 | - */ |
|
39 | - public function run( PHPUnit_Framework_TestResult $result = null ) { |
|
40 | - |
|
41 | - if ( $result === null ) { |
|
42 | - $result = $this->createResult(); |
|
43 | - } |
|
44 | - |
|
45 | - if ( 'UTC' != ( $this->existing_timezone = date_default_timezone_get() ) ) { |
|
46 | - date_default_timezone_set( 'UTC' ); |
|
47 | - $result->run( $this ); |
|
48 | - } |
|
49 | - |
|
50 | - date_default_timezone_set( 'Pacific/Fiji' ); // UTC+12 |
|
51 | - $result->run( $this ); |
|
52 | - |
|
53 | - date_default_timezone_set( 'Pacific/Tahiti' ); // UTC-10: it's a magical place |
|
54 | - $result->run( $this ); |
|
55 | - |
|
56 | - date_default_timezone_set( $this->existing_timezone ); |
|
57 | - |
|
58 | - return $result; |
|
59 | - } |
|
8 | + protected $existing_timezone; |
|
9 | + |
|
10 | + /** |
|
11 | + * Perform test set-up work. |
|
12 | + */ |
|
13 | + public function set_up() { |
|
14 | + ActionScheduler_Callbacks::add_callbacks(); |
|
15 | + parent::set_up(); |
|
16 | + } |
|
17 | + |
|
18 | + /** |
|
19 | + * Perform test tear-down work. |
|
20 | + */ |
|
21 | + public function tear_down() { |
|
22 | + ActionScheduler_Callbacks::remove_callbacks(); |
|
23 | + parent::tear_down(); |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * Counts the number of test cases executed by run(TestResult result). |
|
28 | + * |
|
29 | + * @return int |
|
30 | + */ |
|
31 | + public function count() { |
|
32 | + return 'UTC' == date_default_timezone_get() ? 2 : 3; |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * We want to run every test multiple times using a different timezone to make sure |
|
37 | + * that they are unaffected by changes to PHP's timezone. |
|
38 | + */ |
|
39 | + public function run( PHPUnit_Framework_TestResult $result = null ) { |
|
40 | + |
|
41 | + if ( $result === null ) { |
|
42 | + $result = $this->createResult(); |
|
43 | + } |
|
44 | + |
|
45 | + if ( 'UTC' != ( $this->existing_timezone = date_default_timezone_get() ) ) { |
|
46 | + date_default_timezone_set( 'UTC' ); |
|
47 | + $result->run( $this ); |
|
48 | + } |
|
49 | + |
|
50 | + date_default_timezone_set( 'Pacific/Fiji' ); // UTC+12 |
|
51 | + $result->run( $this ); |
|
52 | + |
|
53 | + date_default_timezone_set( 'Pacific/Tahiti' ); // UTC-10: it's a magical place |
|
54 | + $result->run( $this ); |
|
55 | + |
|
56 | + date_default_timezone_set( $this->existing_timezone ); |
|
57 | + |
|
58 | + return $result; |
|
59 | + } |
|
60 | 60 | } |
@@ -36,24 +36,24 @@ |
||
36 | 36 | * We want to run every test multiple times using a different timezone to make sure |
37 | 37 | * that they are unaffected by changes to PHP's timezone. |
38 | 38 | */ |
39 | - public function run( PHPUnit_Framework_TestResult $result = null ) { |
|
39 | + public function run(PHPUnit_Framework_TestResult $result = null) { |
|
40 | 40 | |
41 | - if ( $result === null ) { |
|
41 | + if ($result === null) { |
|
42 | 42 | $result = $this->createResult(); |
43 | 43 | } |
44 | 44 | |
45 | - if ( 'UTC' != ( $this->existing_timezone = date_default_timezone_get() ) ) { |
|
46 | - date_default_timezone_set( 'UTC' ); |
|
47 | - $result->run( $this ); |
|
45 | + if ('UTC' != ($this->existing_timezone = date_default_timezone_get())) { |
|
46 | + date_default_timezone_set('UTC'); |
|
47 | + $result->run($this); |
|
48 | 48 | } |
49 | 49 | |
50 | - date_default_timezone_set( 'Pacific/Fiji' ); // UTC+12 |
|
51 | - $result->run( $this ); |
|
50 | + date_default_timezone_set('Pacific/Fiji'); // UTC+12 |
|
51 | + $result->run($this); |
|
52 | 52 | |
53 | - date_default_timezone_set( 'Pacific/Tahiti' ); // UTC-10: it's a magical place |
|
54 | - $result->run( $this ); |
|
53 | + date_default_timezone_set('Pacific/Tahiti'); // UTC-10: it's a magical place |
|
54 | + $result->run($this); |
|
55 | 55 | |
56 | - date_default_timezone_set( $this->existing_timezone ); |
|
56 | + date_default_timezone_set($this->existing_timezone); |
|
57 | 57 | |
58 | 58 | return $result; |
59 | 59 | } |
@@ -6,50 +6,50 @@ |
||
6 | 6 | * @group actions |
7 | 7 | */ |
8 | 8 | class ActionScheduler_Action_Test extends ActionScheduler_UnitTestCase { |
9 | - public function test_set_schedule() { |
|
10 | - $time = as_get_datetime_object(); |
|
11 | - $schedule = new ActionScheduler_SimpleSchedule( $time ); |
|
12 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule ); |
|
13 | - $this->assertEquals( $schedule, $action->get_schedule() ); |
|
14 | - } |
|
15 | - |
|
16 | - public function test_null_schedule() { |
|
17 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ); |
|
18 | - $this->assertInstanceOf( 'ActionScheduler_NullSchedule', $action->get_schedule() ); |
|
19 | - } |
|
20 | - |
|
21 | - public function test_set_hook() { |
|
22 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ); |
|
23 | - $this->assertEquals( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, $action->get_hook() ); |
|
24 | - } |
|
25 | - |
|
26 | - public function test_args() { |
|
27 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ); |
|
28 | - $this->assertEmpty( $action->get_args() ); |
|
29 | - |
|
30 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array( 5, 10, 15 ) ); |
|
31 | - $this->assertEqualSets( array( 5, 10, 15 ), $action->get_args() ); |
|
32 | - } |
|
33 | - |
|
34 | - public function test_set_group() { |
|
35 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), null, 'my_group' ); |
|
36 | - $this->assertEquals( 'my_group', $action->get_group() ); |
|
37 | - } |
|
38 | - |
|
39 | - public function test_execute() { |
|
40 | - $mock = new MockAction(); |
|
41 | - |
|
42 | - $random = md5( rand() ); |
|
43 | - add_action( $random, array( $mock, 'action' ) ); |
|
44 | - |
|
45 | - $action = new ActionScheduler_Action( $random, array( $random ) ); |
|
46 | - $action->execute(); |
|
47 | - |
|
48 | - remove_action( $random, array( $mock, 'action' ) ); |
|
49 | - |
|
50 | - $this->assertSame( 1, $mock->get_call_count() ); |
|
51 | - $events = $mock->get_events(); |
|
52 | - $event = reset( $events ); |
|
53 | - $this->assertEquals( $random, reset( $event['args'] ) ); |
|
54 | - } |
|
9 | + public function test_set_schedule() { |
|
10 | + $time = as_get_datetime_object(); |
|
11 | + $schedule = new ActionScheduler_SimpleSchedule( $time ); |
|
12 | + $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule ); |
|
13 | + $this->assertEquals( $schedule, $action->get_schedule() ); |
|
14 | + } |
|
15 | + |
|
16 | + public function test_null_schedule() { |
|
17 | + $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ); |
|
18 | + $this->assertInstanceOf( 'ActionScheduler_NullSchedule', $action->get_schedule() ); |
|
19 | + } |
|
20 | + |
|
21 | + public function test_set_hook() { |
|
22 | + $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ); |
|
23 | + $this->assertEquals( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, $action->get_hook() ); |
|
24 | + } |
|
25 | + |
|
26 | + public function test_args() { |
|
27 | + $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ); |
|
28 | + $this->assertEmpty( $action->get_args() ); |
|
29 | + |
|
30 | + $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array( 5, 10, 15 ) ); |
|
31 | + $this->assertEqualSets( array( 5, 10, 15 ), $action->get_args() ); |
|
32 | + } |
|
33 | + |
|
34 | + public function test_set_group() { |
|
35 | + $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), null, 'my_group' ); |
|
36 | + $this->assertEquals( 'my_group', $action->get_group() ); |
|
37 | + } |
|
38 | + |
|
39 | + public function test_execute() { |
|
40 | + $mock = new MockAction(); |
|
41 | + |
|
42 | + $random = md5( rand() ); |
|
43 | + add_action( $random, array( $mock, 'action' ) ); |
|
44 | + |
|
45 | + $action = new ActionScheduler_Action( $random, array( $random ) ); |
|
46 | + $action->execute(); |
|
47 | + |
|
48 | + remove_action( $random, array( $mock, 'action' ) ); |
|
49 | + |
|
50 | + $this->assertSame( 1, $mock->get_call_count() ); |
|
51 | + $events = $mock->get_events(); |
|
52 | + $event = reset( $events ); |
|
53 | + $this->assertEquals( $random, reset( $event['args'] ) ); |
|
54 | + } |
|
55 | 55 | } |
@@ -8,48 +8,48 @@ |
||
8 | 8 | class ActionScheduler_Action_Test extends ActionScheduler_UnitTestCase { |
9 | 9 | public function test_set_schedule() { |
10 | 10 | $time = as_get_datetime_object(); |
11 | - $schedule = new ActionScheduler_SimpleSchedule( $time ); |
|
12 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule ); |
|
13 | - $this->assertEquals( $schedule, $action->get_schedule() ); |
|
11 | + $schedule = new ActionScheduler_SimpleSchedule($time); |
|
12 | + $action = new ActionScheduler_Action(ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), $schedule); |
|
13 | + $this->assertEquals($schedule, $action->get_schedule()); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | public function test_null_schedule() { |
17 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ); |
|
18 | - $this->assertInstanceOf( 'ActionScheduler_NullSchedule', $action->get_schedule() ); |
|
17 | + $action = new ActionScheduler_Action(ActionScheduler_Callbacks::HOOK_WITH_CALLBACK); |
|
18 | + $this->assertInstanceOf('ActionScheduler_NullSchedule', $action->get_schedule()); |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | public function test_set_hook() { |
22 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ); |
|
23 | - $this->assertEquals( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, $action->get_hook() ); |
|
22 | + $action = new ActionScheduler_Action(ActionScheduler_Callbacks::HOOK_WITH_CALLBACK); |
|
23 | + $this->assertEquals(ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, $action->get_hook()); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | public function test_args() { |
27 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ); |
|
28 | - $this->assertEmpty( $action->get_args() ); |
|
27 | + $action = new ActionScheduler_Action(ActionScheduler_Callbacks::HOOK_WITH_CALLBACK); |
|
28 | + $this->assertEmpty($action->get_args()); |
|
29 | 29 | |
30 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array( 5, 10, 15 ) ); |
|
31 | - $this->assertEqualSets( array( 5, 10, 15 ), $action->get_args() ); |
|
30 | + $action = new ActionScheduler_Action(ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(5, 10, 15)); |
|
31 | + $this->assertEqualSets(array(5, 10, 15), $action->get_args()); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | public function test_set_group() { |
35 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), null, 'my_group' ); |
|
36 | - $this->assertEquals( 'my_group', $action->get_group() ); |
|
35 | + $action = new ActionScheduler_Action(ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array(), null, 'my_group'); |
|
36 | + $this->assertEquals('my_group', $action->get_group()); |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | public function test_execute() { |
40 | 40 | $mock = new MockAction(); |
41 | 41 | |
42 | - $random = md5( rand() ); |
|
43 | - add_action( $random, array( $mock, 'action' ) ); |
|
42 | + $random = md5(rand()); |
|
43 | + add_action($random, array($mock, 'action')); |
|
44 | 44 | |
45 | - $action = new ActionScheduler_Action( $random, array( $random ) ); |
|
45 | + $action = new ActionScheduler_Action($random, array($random)); |
|
46 | 46 | $action->execute(); |
47 | 47 | |
48 | - remove_action( $random, array( $mock, 'action' ) ); |
|
48 | + remove_action($random, array($mock, 'action')); |
|
49 | 49 | |
50 | - $this->assertSame( 1, $mock->get_call_count() ); |
|
50 | + $this->assertSame(1, $mock->get_call_count()); |
|
51 | 51 | $events = $mock->get_events(); |
52 | - $event = reset( $events ); |
|
53 | - $this->assertEquals( $random, reset( $event['args'] ) ); |
|
52 | + $event = reset($events); |
|
53 | + $this->assertEquals($random, reset($event['args'])); |
|
54 | 54 | } |
55 | 55 | } |
@@ -6,12 +6,12 @@ |
||
6 | 6 | * @group actions |
7 | 7 | */ |
8 | 8 | class ActionScheduler_NullAction_Test extends ActionScheduler_UnitTestCase { |
9 | - public function test_null_action() { |
|
10 | - $action = new ActionScheduler_NullAction(); |
|
9 | + public function test_null_action() { |
|
10 | + $action = new ActionScheduler_NullAction(); |
|
11 | 11 | |
12 | - $this->assertEmpty( $action->get_hook() ); |
|
13 | - $this->assertEmpty( $action->get_args() ); |
|
14 | - $this->assertNull( $action->get_schedule()->get_date() ); |
|
15 | - } |
|
12 | + $this->assertEmpty( $action->get_hook() ); |
|
13 | + $this->assertEmpty( $action->get_args() ); |
|
14 | + $this->assertNull( $action->get_schedule()->get_date() ); |
|
15 | + } |
|
16 | 16 | } |
17 | 17 |
@@ -9,9 +9,9 @@ |
||
9 | 9 | public function test_null_action() { |
10 | 10 | $action = new ActionScheduler_NullAction(); |
11 | 11 | |
12 | - $this->assertEmpty( $action->get_hook() ); |
|
13 | - $this->assertEmpty( $action->get_args() ); |
|
14 | - $this->assertNull( $action->get_schedule()->get_date() ); |
|
12 | + $this->assertEmpty($action->get_hook()); |
|
13 | + $this->assertEmpty($action->get_args()); |
|
14 | + $this->assertNull($action->get_schedule()->get_date()); |
|
15 | 15 | } |
16 | 16 | } |
17 | 17 |
@@ -5,149 +5,149 @@ |
||
5 | 5 | */ |
6 | 6 | class ActionScheduler_QueueCleaner_Test extends ActionScheduler_UnitTestCase { |
7 | 7 | |
8 | - public function test_delete_old_actions() { |
|
9 | - $store = ActionScheduler::store(); |
|
10 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
8 | + public function test_delete_old_actions() { |
|
9 | + $store = ActionScheduler::store(); |
|
10 | + $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
11 | 11 | |
12 | - $random = md5( rand() ); |
|
13 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
12 | + $random = md5( rand() ); |
|
13 | + $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
14 | 14 | |
15 | - $created_actions = array(); |
|
16 | - for ( $i = 0; $i < 5; $i++ ) { |
|
17 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array( $random ), $schedule ); |
|
18 | - $created_actions[] = $store->save_action( $action ); |
|
19 | - } |
|
15 | + $created_actions = array(); |
|
16 | + for ( $i = 0; $i < 5; $i++ ) { |
|
17 | + $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array( $random ), $schedule ); |
|
18 | + $created_actions[] = $store->save_action( $action ); |
|
19 | + } |
|
20 | 20 | |
21 | - $runner->run(); |
|
21 | + $runner->run(); |
|
22 | 22 | |
23 | - add_filter( 'action_scheduler_retention_period', '__return_zero' ); // delete any finished job |
|
24 | - $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
25 | - $cleaner->delete_old_actions(); |
|
26 | - remove_filter( 'action_scheduler_retention_period', '__return_zero' ); |
|
23 | + add_filter( 'action_scheduler_retention_period', '__return_zero' ); // delete any finished job |
|
24 | + $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
25 | + $cleaner->delete_old_actions(); |
|
26 | + remove_filter( 'action_scheduler_retention_period', '__return_zero' ); |
|
27 | 27 | |
28 | - foreach ( $created_actions as $action_id ) { |
|
29 | - $action = $store->fetch_action( $action_id ); |
|
30 | - $this->assertFalse( $action->is_finished() ); // it's a NullAction |
|
31 | - } |
|
32 | - } |
|
28 | + foreach ( $created_actions as $action_id ) { |
|
29 | + $action = $store->fetch_action( $action_id ); |
|
30 | + $this->assertFalse( $action->is_finished() ); // it's a NullAction |
|
31 | + } |
|
32 | + } |
|
33 | 33 | |
34 | - public function test_delete_canceled_actions() { |
|
35 | - $store = ActionScheduler::store(); |
|
34 | + public function test_delete_canceled_actions() { |
|
35 | + $store = ActionScheduler::store(); |
|
36 | 36 | |
37 | - $random = md5( rand() ); |
|
38 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
39 | - |
|
40 | - $created_actions = array(); |
|
41 | - for ( $i = 0; $i < 5; $i++ ) { |
|
42 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
43 | - $action_id = $store->save_action( $action ); |
|
44 | - $store->cancel_action( $action_id ); |
|
45 | - $created_actions[] = $action_id; |
|
46 | - } |
|
37 | + $random = md5( rand() ); |
|
38 | + $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
39 | + |
|
40 | + $created_actions = array(); |
|
41 | + for ( $i = 0; $i < 5; $i++ ) { |
|
42 | + $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
43 | + $action_id = $store->save_action( $action ); |
|
44 | + $store->cancel_action( $action_id ); |
|
45 | + $created_actions[] = $action_id; |
|
46 | + } |
|
47 | 47 | |
48 | - // track the actions that are deleted |
|
49 | - $mock_action = new MockAction(); |
|
50 | - add_action( 'action_scheduler_deleted_action', array( $mock_action, 'action' ), 10, 1 ); |
|
51 | - add_filter( 'action_scheduler_retention_period', '__return_zero' ); // delete any finished job |
|
48 | + // track the actions that are deleted |
|
49 | + $mock_action = new MockAction(); |
|
50 | + add_action( 'action_scheduler_deleted_action', array( $mock_action, 'action' ), 10, 1 ); |
|
51 | + add_filter( 'action_scheduler_retention_period', '__return_zero' ); // delete any finished job |
|
52 | 52 | |
53 | - $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
54 | - $cleaner->delete_old_actions(); |
|
53 | + $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
54 | + $cleaner->delete_old_actions(); |
|
55 | 55 | |
56 | - remove_filter( 'action_scheduler_retention_period', '__return_zero' ); |
|
57 | - remove_action( 'action_scheduler_deleted_action', array( $mock_action, 'action' ), 10 ); |
|
56 | + remove_filter( 'action_scheduler_retention_period', '__return_zero' ); |
|
57 | + remove_action( 'action_scheduler_deleted_action', array( $mock_action, 'action' ), 10 ); |
|
58 | 58 | |
59 | - $deleted_actions = array(); |
|
60 | - foreach ( $mock_action->get_args() as $action ) { |
|
61 | - $deleted_actions[] = reset( $action ); |
|
62 | - } |
|
59 | + $deleted_actions = array(); |
|
60 | + foreach ( $mock_action->get_args() as $action ) { |
|
61 | + $deleted_actions[] = reset( $action ); |
|
62 | + } |
|
63 | 63 | |
64 | - $this->assertEqualSets( $created_actions, $deleted_actions ); |
|
65 | - } |
|
64 | + $this->assertEqualSets( $created_actions, $deleted_actions ); |
|
65 | + } |
|
66 | 66 | |
67 | - public function test_do_not_delete_recent_actions() { |
|
68 | - $store = ActionScheduler::store(); |
|
69 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
67 | + public function test_do_not_delete_recent_actions() { |
|
68 | + $store = ActionScheduler::store(); |
|
69 | + $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
70 | 70 | |
71 | - $random = md5( rand() ); |
|
72 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
71 | + $random = md5( rand() ); |
|
72 | + $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
73 | 73 | |
74 | - $created_actions = array(); |
|
75 | - for ( $i = 0; $i < 5; $i++ ) { |
|
76 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
77 | - $created_actions[] = $store->save_action( $action ); |
|
78 | - } |
|
74 | + $created_actions = array(); |
|
75 | + for ( $i = 0; $i < 5; $i++ ) { |
|
76 | + $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
77 | + $created_actions[] = $store->save_action( $action ); |
|
78 | + } |
|
79 | 79 | |
80 | - $runner->run(); |
|
80 | + $runner->run(); |
|
81 | 81 | |
82 | - $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
83 | - $cleaner->delete_old_actions(); |
|
82 | + $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
83 | + $cleaner->delete_old_actions(); |
|
84 | 84 | |
85 | - foreach ( $created_actions as $action_id ) { |
|
86 | - $action = $store->fetch_action( $action_id ); |
|
87 | - $this->assertTrue( $action->is_finished() ); // It's a FinishedAction |
|
88 | - } |
|
89 | - } |
|
85 | + foreach ( $created_actions as $action_id ) { |
|
86 | + $action = $store->fetch_action( $action_id ); |
|
87 | + $this->assertTrue( $action->is_finished() ); // It's a FinishedAction |
|
88 | + } |
|
89 | + } |
|
90 | 90 | |
91 | - public function test_reset_unrun_actions() { |
|
92 | - $store = ActionScheduler::store(); |
|
91 | + public function test_reset_unrun_actions() { |
|
92 | + $store = ActionScheduler::store(); |
|
93 | 93 | |
94 | - $random = md5( rand() ); |
|
95 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
94 | + $random = md5( rand() ); |
|
95 | + $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
96 | 96 | |
97 | - $created_actions = array(); |
|
98 | - for ( $i = 0; $i < 5; $i++ ) { |
|
99 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
100 | - $created_actions[] = $store->save_action( $action ); |
|
101 | - } |
|
97 | + $created_actions = array(); |
|
98 | + for ( $i = 0; $i < 5; $i++ ) { |
|
99 | + $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
100 | + $created_actions[] = $store->save_action( $action ); |
|
101 | + } |
|
102 | 102 | |
103 | - $store->stake_claim( 10 ); |
|
103 | + $store->stake_claim( 10 ); |
|
104 | 104 | |
105 | - // don't actually process the jobs, to simulate a request that timed out |
|
105 | + // don't actually process the jobs, to simulate a request that timed out |
|
106 | 106 | |
107 | - add_filter( 'action_scheduler_timeout_period', '__return_zero' ); // delete any finished job |
|
108 | - $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
109 | - $cleaner->reset_timeouts(); |
|
107 | + add_filter( 'action_scheduler_timeout_period', '__return_zero' ); // delete any finished job |
|
108 | + $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
109 | + $cleaner->reset_timeouts(); |
|
110 | 110 | |
111 | - remove_filter( 'action_scheduler_timeout_period', '__return_zero' ); |
|
111 | + remove_filter( 'action_scheduler_timeout_period', '__return_zero' ); |
|
112 | 112 | |
113 | - $claim = $store->stake_claim( 10 ); |
|
114 | - $this->assertEqualSets( $created_actions, $claim->get_actions() ); |
|
115 | - } |
|
113 | + $claim = $store->stake_claim( 10 ); |
|
114 | + $this->assertEqualSets( $created_actions, $claim->get_actions() ); |
|
115 | + } |
|
116 | 116 | |
117 | - public function test_do_not_reset_failed_action() { |
|
118 | - $store = ActionScheduler::store(); |
|
117 | + public function test_do_not_reset_failed_action() { |
|
118 | + $store = ActionScheduler::store(); |
|
119 | 119 | |
120 | - $random = md5( rand() ); |
|
121 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
120 | + $random = md5( rand() ); |
|
121 | + $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
122 | 122 | |
123 | - $created_actions = array(); |
|
124 | - for ( $i = 0; $i < 5; $i++ ) { |
|
125 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
126 | - $created_actions[] = $store->save_action( $action ); |
|
127 | - } |
|
123 | + $created_actions = array(); |
|
124 | + for ( $i = 0; $i < 5; $i++ ) { |
|
125 | + $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
126 | + $created_actions[] = $store->save_action( $action ); |
|
127 | + } |
|
128 | 128 | |
129 | - $claim = $store->stake_claim( 10 ); |
|
130 | - foreach ( $claim->get_actions() as $action_id ) { |
|
131 | - // simulate the first action interrupted by an uncatchable fatal error |
|
132 | - $store->log_execution( $action_id ); |
|
133 | - break; |
|
134 | - } |
|
129 | + $claim = $store->stake_claim( 10 ); |
|
130 | + foreach ( $claim->get_actions() as $action_id ) { |
|
131 | + // simulate the first action interrupted by an uncatchable fatal error |
|
132 | + $store->log_execution( $action_id ); |
|
133 | + break; |
|
134 | + } |
|
135 | 135 | |
136 | - add_filter( 'action_scheduler_timeout_period', '__return_zero' ); // delete any finished job |
|
137 | - $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
138 | - $cleaner->reset_timeouts(); |
|
139 | - remove_filter( 'action_scheduler_timeout_period', '__return_zero' ); |
|
136 | + add_filter( 'action_scheduler_timeout_period', '__return_zero' ); // delete any finished job |
|
137 | + $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
138 | + $cleaner->reset_timeouts(); |
|
139 | + remove_filter( 'action_scheduler_timeout_period', '__return_zero' ); |
|
140 | 140 | |
141 | - $new_claim = $store->stake_claim( 10 ); |
|
142 | - $this->assertCount( 4, $new_claim->get_actions() ); |
|
141 | + $new_claim = $store->stake_claim( 10 ); |
|
142 | + $this->assertCount( 4, $new_claim->get_actions() ); |
|
143 | 143 | |
144 | - add_filter( 'action_scheduler_failure_period', '__return_zero' ); |
|
145 | - $cleaner->mark_failures(); |
|
146 | - remove_filter( 'action_scheduler_failure_period', '__return_zero' ); |
|
144 | + add_filter( 'action_scheduler_failure_period', '__return_zero' ); |
|
145 | + $cleaner->mark_failures(); |
|
146 | + remove_filter( 'action_scheduler_failure_period', '__return_zero' ); |
|
147 | 147 | |
148 | - $failed = $store->query_actions( array( 'status' => ActionScheduler_Store::STATUS_FAILED ) ); |
|
149 | - $this->assertEquals( $created_actions[0], $failed[0] ); |
|
150 | - $this->assertCount( 1, $failed ); |
|
148 | + $failed = $store->query_actions( array( 'status' => ActionScheduler_Store::STATUS_FAILED ) ); |
|
149 | + $this->assertEquals( $created_actions[0], $failed[0] ); |
|
150 | + $this->assertCount( 1, $failed ); |
|
151 | 151 | |
152 | - } |
|
152 | + } |
|
153 | 153 | } |
@@ -7,147 +7,147 @@ |
||
7 | 7 | |
8 | 8 | public function test_delete_old_actions() { |
9 | 9 | $store = ActionScheduler::store(); |
10 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
10 | + $runner = ActionScheduler_Mocker::get_queue_runner($store); |
|
11 | 11 | |
12 | - $random = md5( rand() ); |
|
13 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
12 | + $random = md5(rand()); |
|
13 | + $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago')); |
|
14 | 14 | |
15 | 15 | $created_actions = array(); |
16 | - for ( $i = 0; $i < 5; $i++ ) { |
|
17 | - $action = new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array( $random ), $schedule ); |
|
18 | - $created_actions[] = $store->save_action( $action ); |
|
16 | + for ($i = 0; $i < 5; $i++) { |
|
17 | + $action = new ActionScheduler_Action(ActionScheduler_Callbacks::HOOK_WITH_CALLBACK, array($random), $schedule); |
|
18 | + $created_actions[] = $store->save_action($action); |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | $runner->run(); |
22 | 22 | |
23 | - add_filter( 'action_scheduler_retention_period', '__return_zero' ); // delete any finished job |
|
24 | - $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
23 | + add_filter('action_scheduler_retention_period', '__return_zero'); // delete any finished job |
|
24 | + $cleaner = new ActionScheduler_QueueCleaner($store); |
|
25 | 25 | $cleaner->delete_old_actions(); |
26 | - remove_filter( 'action_scheduler_retention_period', '__return_zero' ); |
|
26 | + remove_filter('action_scheduler_retention_period', '__return_zero'); |
|
27 | 27 | |
28 | - foreach ( $created_actions as $action_id ) { |
|
29 | - $action = $store->fetch_action( $action_id ); |
|
30 | - $this->assertFalse( $action->is_finished() ); // it's a NullAction |
|
28 | + foreach ($created_actions as $action_id) { |
|
29 | + $action = $store->fetch_action($action_id); |
|
30 | + $this->assertFalse($action->is_finished()); // it's a NullAction |
|
31 | 31 | } |
32 | 32 | } |
33 | 33 | |
34 | 34 | public function test_delete_canceled_actions() { |
35 | 35 | $store = ActionScheduler::store(); |
36 | 36 | |
37 | - $random = md5( rand() ); |
|
38 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
37 | + $random = md5(rand()); |
|
38 | + $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago')); |
|
39 | 39 | |
40 | 40 | $created_actions = array(); |
41 | - for ( $i = 0; $i < 5; $i++ ) { |
|
42 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
43 | - $action_id = $store->save_action( $action ); |
|
44 | - $store->cancel_action( $action_id ); |
|
41 | + for ($i = 0; $i < 5; $i++) { |
|
42 | + $action = new ActionScheduler_Action($random, array($random), $schedule); |
|
43 | + $action_id = $store->save_action($action); |
|
44 | + $store->cancel_action($action_id); |
|
45 | 45 | $created_actions[] = $action_id; |
46 | 46 | } |
47 | 47 | |
48 | 48 | // track the actions that are deleted |
49 | 49 | $mock_action = new MockAction(); |
50 | - add_action( 'action_scheduler_deleted_action', array( $mock_action, 'action' ), 10, 1 ); |
|
51 | - add_filter( 'action_scheduler_retention_period', '__return_zero' ); // delete any finished job |
|
50 | + add_action('action_scheduler_deleted_action', array($mock_action, 'action'), 10, 1); |
|
51 | + add_filter('action_scheduler_retention_period', '__return_zero'); // delete any finished job |
|
52 | 52 | |
53 | - $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
53 | + $cleaner = new ActionScheduler_QueueCleaner($store); |
|
54 | 54 | $cleaner->delete_old_actions(); |
55 | 55 | |
56 | - remove_filter( 'action_scheduler_retention_period', '__return_zero' ); |
|
57 | - remove_action( 'action_scheduler_deleted_action', array( $mock_action, 'action' ), 10 ); |
|
56 | + remove_filter('action_scheduler_retention_period', '__return_zero'); |
|
57 | + remove_action('action_scheduler_deleted_action', array($mock_action, 'action'), 10); |
|
58 | 58 | |
59 | 59 | $deleted_actions = array(); |
60 | - foreach ( $mock_action->get_args() as $action ) { |
|
61 | - $deleted_actions[] = reset( $action ); |
|
60 | + foreach ($mock_action->get_args() as $action) { |
|
61 | + $deleted_actions[] = reset($action); |
|
62 | 62 | } |
63 | 63 | |
64 | - $this->assertEqualSets( $created_actions, $deleted_actions ); |
|
64 | + $this->assertEqualSets($created_actions, $deleted_actions); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | public function test_do_not_delete_recent_actions() { |
68 | 68 | $store = ActionScheduler::store(); |
69 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
69 | + $runner = ActionScheduler_Mocker::get_queue_runner($store); |
|
70 | 70 | |
71 | - $random = md5( rand() ); |
|
72 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
71 | + $random = md5(rand()); |
|
72 | + $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago')); |
|
73 | 73 | |
74 | 74 | $created_actions = array(); |
75 | - for ( $i = 0; $i < 5; $i++ ) { |
|
76 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
77 | - $created_actions[] = $store->save_action( $action ); |
|
75 | + for ($i = 0; $i < 5; $i++) { |
|
76 | + $action = new ActionScheduler_Action($random, array($random), $schedule); |
|
77 | + $created_actions[] = $store->save_action($action); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | $runner->run(); |
81 | 81 | |
82 | - $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
82 | + $cleaner = new ActionScheduler_QueueCleaner($store); |
|
83 | 83 | $cleaner->delete_old_actions(); |
84 | 84 | |
85 | - foreach ( $created_actions as $action_id ) { |
|
86 | - $action = $store->fetch_action( $action_id ); |
|
87 | - $this->assertTrue( $action->is_finished() ); // It's a FinishedAction |
|
85 | + foreach ($created_actions as $action_id) { |
|
86 | + $action = $store->fetch_action($action_id); |
|
87 | + $this->assertTrue($action->is_finished()); // It's a FinishedAction |
|
88 | 88 | } |
89 | 89 | } |
90 | 90 | |
91 | 91 | public function test_reset_unrun_actions() { |
92 | 92 | $store = ActionScheduler::store(); |
93 | 93 | |
94 | - $random = md5( rand() ); |
|
95 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
94 | + $random = md5(rand()); |
|
95 | + $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago')); |
|
96 | 96 | |
97 | 97 | $created_actions = array(); |
98 | - for ( $i = 0; $i < 5; $i++ ) { |
|
99 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
100 | - $created_actions[] = $store->save_action( $action ); |
|
98 | + for ($i = 0; $i < 5; $i++) { |
|
99 | + $action = new ActionScheduler_Action($random, array($random), $schedule); |
|
100 | + $created_actions[] = $store->save_action($action); |
|
101 | 101 | } |
102 | 102 | |
103 | - $store->stake_claim( 10 ); |
|
103 | + $store->stake_claim(10); |
|
104 | 104 | |
105 | 105 | // don't actually process the jobs, to simulate a request that timed out |
106 | 106 | |
107 | - add_filter( 'action_scheduler_timeout_period', '__return_zero' ); // delete any finished job |
|
108 | - $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
107 | + add_filter('action_scheduler_timeout_period', '__return_zero'); // delete any finished job |
|
108 | + $cleaner = new ActionScheduler_QueueCleaner($store); |
|
109 | 109 | $cleaner->reset_timeouts(); |
110 | 110 | |
111 | - remove_filter( 'action_scheduler_timeout_period', '__return_zero' ); |
|
111 | + remove_filter('action_scheduler_timeout_period', '__return_zero'); |
|
112 | 112 | |
113 | - $claim = $store->stake_claim( 10 ); |
|
114 | - $this->assertEqualSets( $created_actions, $claim->get_actions() ); |
|
113 | + $claim = $store->stake_claim(10); |
|
114 | + $this->assertEqualSets($created_actions, $claim->get_actions()); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | public function test_do_not_reset_failed_action() { |
118 | 118 | $store = ActionScheduler::store(); |
119 | 119 | |
120 | - $random = md5( rand() ); |
|
121 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
120 | + $random = md5(rand()); |
|
121 | + $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago')); |
|
122 | 122 | |
123 | 123 | $created_actions = array(); |
124 | - for ( $i = 0; $i < 5; $i++ ) { |
|
125 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
126 | - $created_actions[] = $store->save_action( $action ); |
|
124 | + for ($i = 0; $i < 5; $i++) { |
|
125 | + $action = new ActionScheduler_Action($random, array($random), $schedule); |
|
126 | + $created_actions[] = $store->save_action($action); |
|
127 | 127 | } |
128 | 128 | |
129 | - $claim = $store->stake_claim( 10 ); |
|
130 | - foreach ( $claim->get_actions() as $action_id ) { |
|
129 | + $claim = $store->stake_claim(10); |
|
130 | + foreach ($claim->get_actions() as $action_id) { |
|
131 | 131 | // simulate the first action interrupted by an uncatchable fatal error |
132 | - $store->log_execution( $action_id ); |
|
132 | + $store->log_execution($action_id); |
|
133 | 133 | break; |
134 | 134 | } |
135 | 135 | |
136 | - add_filter( 'action_scheduler_timeout_period', '__return_zero' ); // delete any finished job |
|
137 | - $cleaner = new ActionScheduler_QueueCleaner( $store ); |
|
136 | + add_filter('action_scheduler_timeout_period', '__return_zero'); // delete any finished job |
|
137 | + $cleaner = new ActionScheduler_QueueCleaner($store); |
|
138 | 138 | $cleaner->reset_timeouts(); |
139 | - remove_filter( 'action_scheduler_timeout_period', '__return_zero' ); |
|
139 | + remove_filter('action_scheduler_timeout_period', '__return_zero'); |
|
140 | 140 | |
141 | - $new_claim = $store->stake_claim( 10 ); |
|
142 | - $this->assertCount( 4, $new_claim->get_actions() ); |
|
141 | + $new_claim = $store->stake_claim(10); |
|
142 | + $this->assertCount(4, $new_claim->get_actions()); |
|
143 | 143 | |
144 | - add_filter( 'action_scheduler_failure_period', '__return_zero' ); |
|
144 | + add_filter('action_scheduler_failure_period', '__return_zero'); |
|
145 | 145 | $cleaner->mark_failures(); |
146 | - remove_filter( 'action_scheduler_failure_period', '__return_zero' ); |
|
146 | + remove_filter('action_scheduler_failure_period', '__return_zero'); |
|
147 | 147 | |
148 | - $failed = $store->query_actions( array( 'status' => ActionScheduler_Store::STATUS_FAILED ) ); |
|
149 | - $this->assertEquals( $created_actions[0], $failed[0] ); |
|
150 | - $this->assertCount( 1, $failed ); |
|
148 | + $failed = $store->query_actions(array('status' => ActionScheduler_Store::STATUS_FAILED)); |
|
149 | + $this->assertEquals($created_actions[0], $failed[0]); |
|
150 | + $this->assertCount(1, $failed); |
|
151 | 151 | |
152 | 152 | } |
153 | 153 | } |
@@ -6,387 +6,387 @@ |
||
6 | 6 | * @group runners |
7 | 7 | */ |
8 | 8 | class ActionScheduler_QueueRunner_Test extends ActionScheduler_UnitTestCase { |
9 | - public function test_create_runner() { |
|
10 | - $store = ActionScheduler::store(); |
|
11 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
12 | - $actions_run = $runner->run(); |
|
9 | + public function test_create_runner() { |
|
10 | + $store = ActionScheduler::store(); |
|
11 | + $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
12 | + $actions_run = $runner->run(); |
|
13 | 13 | |
14 | - $this->assertSame( 0, $actions_run ); |
|
15 | - } |
|
14 | + $this->assertSame( 0, $actions_run ); |
|
15 | + } |
|
16 | 16 | |
17 | - public function test_run() { |
|
18 | - $store = ActionScheduler::store(); |
|
19 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
17 | + public function test_run() { |
|
18 | + $store = ActionScheduler::store(); |
|
19 | + $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
20 | 20 | |
21 | - $mock = new MockAction(); |
|
22 | - $random = md5( rand() ); |
|
23 | - add_action( $random, array( $mock, 'action' ) ); |
|
24 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
21 | + $mock = new MockAction(); |
|
22 | + $random = md5( rand() ); |
|
23 | + add_action( $random, array( $mock, 'action' ) ); |
|
24 | + $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
25 | 25 | |
26 | - for ( $i = 0; $i < 5; $i++ ) { |
|
27 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
28 | - $store->save_action( $action ); |
|
29 | - } |
|
26 | + for ( $i = 0; $i < 5; $i++ ) { |
|
27 | + $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
28 | + $store->save_action( $action ); |
|
29 | + } |
|
30 | 30 | |
31 | - $actions_run = $runner->run(); |
|
31 | + $actions_run = $runner->run(); |
|
32 | 32 | |
33 | - remove_action( $random, array( $mock, 'action' ) ); |
|
33 | + remove_action( $random, array( $mock, 'action' ) ); |
|
34 | 34 | |
35 | - $this->assertEquals( 5, $mock->get_call_count() ); |
|
36 | - $this->assertEquals( 5, $actions_run ); |
|
37 | - } |
|
35 | + $this->assertEquals( 5, $mock->get_call_count() ); |
|
36 | + $this->assertEquals( 5, $actions_run ); |
|
37 | + } |
|
38 | 38 | |
39 | - public function test_run_with_future_actions() { |
|
40 | - $store = ActionScheduler::store(); |
|
41 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
39 | + public function test_run_with_future_actions() { |
|
40 | + $store = ActionScheduler::store(); |
|
41 | + $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
42 | 42 | |
43 | - $mock = new MockAction(); |
|
44 | - $random = md5( rand() ); |
|
45 | - add_action( $random, array( $mock, 'action' ) ); |
|
46 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
43 | + $mock = new MockAction(); |
|
44 | + $random = md5( rand() ); |
|
45 | + add_action( $random, array( $mock, 'action' ) ); |
|
46 | + $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
47 | 47 | |
48 | - for ( $i = 0; $i < 3; $i++ ) { |
|
49 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
50 | - $store->save_action( $action ); |
|
51 | - } |
|
48 | + for ( $i = 0; $i < 3; $i++ ) { |
|
49 | + $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
50 | + $store->save_action( $action ); |
|
51 | + } |
|
52 | 52 | |
53 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( 'tomorrow' ) ); |
|
54 | - for ( $i = 0; $i < 3; $i++ ) { |
|
55 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
56 | - $store->save_action( $action ); |
|
57 | - } |
|
53 | + $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( 'tomorrow' ) ); |
|
54 | + for ( $i = 0; $i < 3; $i++ ) { |
|
55 | + $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
56 | + $store->save_action( $action ); |
|
57 | + } |
|
58 | 58 | |
59 | - $actions_run = $runner->run(); |
|
59 | + $actions_run = $runner->run(); |
|
60 | 60 | |
61 | - remove_action( $random, array( $mock, 'action' ) ); |
|
61 | + remove_action( $random, array( $mock, 'action' ) ); |
|
62 | 62 | |
63 | - $this->assertEquals( 3, $mock->get_call_count() ); |
|
64 | - $this->assertEquals( 3, $actions_run ); |
|
65 | - } |
|
63 | + $this->assertEquals( 3, $mock->get_call_count() ); |
|
64 | + $this->assertEquals( 3, $actions_run ); |
|
65 | + } |
|
66 | 66 | |
67 | - public function test_completed_action_status() { |
|
68 | - $store = ActionScheduler::store(); |
|
69 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
67 | + public function test_completed_action_status() { |
|
68 | + $store = ActionScheduler::store(); |
|
69 | + $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
70 | 70 | |
71 | - $random = md5( rand() ); |
|
72 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '12 hours ago' ) ); |
|
71 | + $random = md5( rand() ); |
|
72 | + $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '12 hours ago' ) ); |
|
73 | 73 | |
74 | - $action = new ActionScheduler_Action( $random, array(), $schedule ); |
|
75 | - $action_id = $store->save_action( $action ); |
|
74 | + $action = new ActionScheduler_Action( $random, array(), $schedule ); |
|
75 | + $action_id = $store->save_action( $action ); |
|
76 | 76 | |
77 | - $runner->run(); |
|
77 | + $runner->run(); |
|
78 | 78 | |
79 | - $finished_action = $store->fetch_action( $action_id ); |
|
79 | + $finished_action = $store->fetch_action( $action_id ); |
|
80 | 80 | |
81 | - $this->assertTrue( $finished_action->is_finished() ); |
|
82 | - } |
|
81 | + $this->assertTrue( $finished_action->is_finished() ); |
|
82 | + } |
|
83 | 83 | |
84 | - public function test_next_instance_of_cron_action() { |
|
85 | - // Create an action with daily Cron expression (i.e. midnight each day) |
|
86 | - $random = md5( rand() ); |
|
87 | - $action_id = ActionScheduler::factory()->cron( $random, array(), null, '0 0 * * *' ); |
|
88 | - $store = ActionScheduler::store(); |
|
89 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
84 | + public function test_next_instance_of_cron_action() { |
|
85 | + // Create an action with daily Cron expression (i.e. midnight each day) |
|
86 | + $random = md5( rand() ); |
|
87 | + $action_id = ActionScheduler::factory()->cron( $random, array(), null, '0 0 * * *' ); |
|
88 | + $store = ActionScheduler::store(); |
|
89 | + $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
90 | 90 | |
91 | - // Make sure the 1st instance of the action is scheduled to occur tomorrow |
|
92 | - $date = as_get_datetime_object( 'tomorrow' ); |
|
93 | - $date->modify( '-1 minute' ); |
|
94 | - $claim = $store->stake_claim( 10, $date ); |
|
95 | - $this->assertCount( 0, $claim->get_actions() ); |
|
91 | + // Make sure the 1st instance of the action is scheduled to occur tomorrow |
|
92 | + $date = as_get_datetime_object( 'tomorrow' ); |
|
93 | + $date->modify( '-1 minute' ); |
|
94 | + $claim = $store->stake_claim( 10, $date ); |
|
95 | + $this->assertCount( 0, $claim->get_actions() ); |
|
96 | 96 | |
97 | - $store->release_claim( $claim ); |
|
97 | + $store->release_claim( $claim ); |
|
98 | 98 | |
99 | - $date->modify( '+1 minute' ); |
|
99 | + $date->modify( '+1 minute' ); |
|
100 | 100 | |
101 | - $claim = $store->stake_claim( 10, $date ); |
|
102 | - $actions = $claim->get_actions(); |
|
103 | - $this->assertCount( 1, $actions ); |
|
104 | - |
|
105 | - $fetched_action_id = reset( $actions ); |
|
106 | - $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
107 | - |
|
108 | - $this->assertEquals( $fetched_action_id, $action_id ); |
|
109 | - $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
110 | - $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
111 | - |
|
112 | - $store->release_claim( $claim ); |
|
113 | - |
|
114 | - // Make sure the 2nd instance of the cron action is scheduled to occur tomorrow still |
|
115 | - $runner->process_action( $action_id ); |
|
116 | - |
|
117 | - $claim = $store->stake_claim( 10, $date ); |
|
118 | - $actions = $claim->get_actions(); |
|
119 | - $this->assertCount( 1, $actions ); |
|
120 | - |
|
121 | - $fetched_action_id = reset( $actions ); |
|
122 | - $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
123 | - |
|
124 | - $this->assertNotEquals( $fetched_action_id, $action_id ); |
|
125 | - $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
126 | - $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
127 | - } |
|
128 | - |
|
129 | - public function test_next_instance_of_interval_action() { |
|
130 | - // Create an action to recur every 24 hours, with the first instance scheduled to run 12 hours ago |
|
131 | - $random = md5( rand() ); |
|
132 | - $date = as_get_datetime_object( '12 hours ago' ); |
|
133 | - $action_id = ActionScheduler::factory()->recurring( $random, array(), $date->getTimestamp(), DAY_IN_SECONDS ); |
|
134 | - $store = ActionScheduler::store(); |
|
135 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
136 | - |
|
137 | - // Make sure the 1st instance of the action is scheduled to occur 12 hours ago |
|
138 | - $claim = $store->stake_claim( 10, $date ); |
|
139 | - $actions = $claim->get_actions(); |
|
140 | - $this->assertCount( 1, $actions ); |
|
141 | - |
|
142 | - $fetched_action_id = reset( $actions ); |
|
143 | - $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
144 | - |
|
145 | - $this->assertEquals( $fetched_action_id, $action_id ); |
|
146 | - $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
147 | - $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
148 | - |
|
149 | - $store->release_claim( $claim ); |
|
150 | - |
|
151 | - // Make sure after the queue is run, the 2nd instance of the action is scheduled to occur in 24 hours |
|
152 | - $runner->run(); |
|
153 | - |
|
154 | - $date = as_get_datetime_object( '+1 day' ); |
|
155 | - $claim = $store->stake_claim( 10, $date ); |
|
156 | - $actions = $claim->get_actions(); |
|
157 | - $this->assertCount( 1, $actions ); |
|
158 | - |
|
159 | - $fetched_action_id = reset( $actions ); |
|
160 | - $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
161 | - |
|
162 | - $this->assertNotEquals( $fetched_action_id, $action_id ); |
|
163 | - $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
164 | - $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
165 | - |
|
166 | - $store->release_claim( $claim ); |
|
167 | - |
|
168 | - // Make sure the 3rd instance of the cron action is scheduled for 24 hours from now, as the action was run early, ahead of schedule |
|
169 | - $runner->process_action( $fetched_action_id ); |
|
170 | - $date = as_get_datetime_object( '+1 day' ); |
|
171 | - |
|
172 | - $claim = $store->stake_claim( 10, $date ); |
|
173 | - $actions = $claim->get_actions(); |
|
174 | - $this->assertCount( 1, $actions ); |
|
175 | - |
|
176 | - $fetched_action_id = reset( $actions ); |
|
177 | - $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
178 | - |
|
179 | - $this->assertNotEquals( $fetched_action_id, $action_id ); |
|
180 | - $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
181 | - $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * As soon as one recurring action has been executed its replacement will be scheduled. |
|
186 | - * |
|
187 | - * This is true even if the current action fails. This makes sense, since a failure may be temporary in nature. |
|
188 | - * However, if the same recurring action consistently fails then it is likely that there is a problem and we should |
|
189 | - * stop creating new instances. This test outlines the expected behavior in this regard. |
|
190 | - * |
|
191 | - * @return void |
|
192 | - */ |
|
193 | - public function test_failing_recurring_actions_are_not_rescheduled_when_threshold_met() { |
|
194 | - $store = ActionScheduler_Store::instance(); |
|
195 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
196 | - $created_actions = array(); |
|
197 | - |
|
198 | - // Create 4 failed actions (one below the threshold of what counts as 'consistently failing'). |
|
199 | - for ( $i = 0; $i < 3; $i++ ) { |
|
200 | - // We give each action a unique set of args, this illustrates that in the context of determining consistent |
|
201 | - // failure we care only about the hook and not other properties of the action. |
|
202 | - $args = array( 'unique-' . $i => hash( 'md5', $i ) ); |
|
203 | - $hook = 'will-fail'; |
|
204 | - $date = as_get_datetime_object( 12 - $i . ' hours ago' ); |
|
205 | - $action_id = as_schedule_recurring_action( $date->getTimestamp(), HOUR_IN_SECONDS, $hook, $args ); |
|
206 | - $store->mark_failure( $action_id ); |
|
207 | - $created_actions[] = $action_id; |
|
208 | - } |
|
209 | - |
|
210 | - // Now create a further action using the same hook, that is also destined to fail. |
|
211 | - $date = as_get_datetime_object( '6 hours ago' ); |
|
212 | - $pending_action_id = as_schedule_recurring_action( $date->getTimestamp(), HOUR_IN_SECONDS, $hook, $args ); |
|
213 | - $created_actions[] = $pending_action_id; |
|
214 | - |
|
215 | - // Process the queue! |
|
216 | - $runner->run(); |
|
217 | - $pending_actions = $store->query_actions( |
|
218 | - array( |
|
219 | - 'hook' => $hook, |
|
220 | - 'args' => $args, |
|
221 | - 'status' => ActionScheduler_Store::STATUS_PENDING, |
|
222 | - ) |
|
223 | - ); |
|
224 | - $new_pending_action_id = current( $pending_actions ); |
|
225 | - |
|
226 | - // We now have 5 instances of the same recurring action. 4 have already failed, one is pending. |
|
227 | - $this->assertCount( 1, $pending_actions, 'If the threshold for consistent failure has not been met, a replacement action should have been scheduled.' ); |
|
228 | - $this->assertNotContains( $new_pending_action_id, $created_actions, 'Confirm that the replacement action is new, and not one of those we created manually earlier in the test.' ); |
|
229 | - |
|
230 | - // Process the pending action (we do this directly instead of via `$runner->run()` because it won't actually |
|
231 | - // become due for another hour). |
|
232 | - $runner->process_action( $new_pending_action_id ); |
|
233 | - $pending_actions = $store->query_actions( |
|
234 | - array( |
|
235 | - 'hook' => $hook, |
|
236 | - 'args' => $args, |
|
237 | - 'status' => ActionScheduler_Store::STATUS_PENDING, |
|
238 | - ) |
|
239 | - ); |
|
240 | - |
|
241 | - // Now 5 instances of the same recurring action have all failed, therefore the threshold for consistent failure |
|
242 | - // has been met and, this time, a new action should *not* have been scheduled. |
|
243 | - $this->assertCount( 0, $pending_actions, 'The failure threshold (5 consecutive fails for recurring actions with the same signature) having been met, no further actions were scheduled.' ); |
|
244 | - } |
|
245 | - |
|
246 | - public function test_hooked_into_wp_cron() { |
|
247 | - $next = wp_next_scheduled( ActionScheduler_QueueRunner::WP_CRON_HOOK, array( 'WP Cron' ) ); |
|
248 | - $this->assertNotEmpty( $next ); |
|
249 | - } |
|
250 | - |
|
251 | - public function test_batch_count_limit() { |
|
252 | - $store = ActionScheduler::store(); |
|
253 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
254 | - |
|
255 | - $mock = new MockAction(); |
|
256 | - $random = md5( rand() ); |
|
257 | - add_action( $random, array( $mock, 'action' ) ); |
|
258 | - $schedule = new ActionScheduler_SimpleSchedule( new ActionScheduler_DateTime( '1 day ago' ) ); |
|
259 | - |
|
260 | - for ( $i = 0; $i < 2; $i++ ) { |
|
261 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
262 | - $store->save_action( $action ); |
|
263 | - } |
|
264 | - |
|
265 | - $claim = $store->stake_claim(); |
|
266 | - |
|
267 | - $actions_run = $runner->run(); |
|
268 | - |
|
269 | - $this->assertSame( 0, $mock->get_call_count() ); |
|
270 | - $this->assertSame( 0, $actions_run ); |
|
271 | - |
|
272 | - $store->release_claim( $claim ); |
|
273 | - |
|
274 | - $actions_run = $runner->run(); |
|
275 | - |
|
276 | - $this->assertEquals( 2, $mock->get_call_count() ); |
|
277 | - $this->assertEquals( 2, $actions_run ); |
|
278 | - |
|
279 | - remove_action( $random, array( $mock, 'action' ) ); |
|
280 | - } |
|
281 | - |
|
282 | - public function test_changing_batch_count_limit() { |
|
283 | - $store = ActionScheduler::store(); |
|
284 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
285 | - |
|
286 | - $random = md5( rand() ); |
|
287 | - $schedule = new ActionScheduler_SimpleSchedule( new ActionScheduler_DateTime( '1 day ago' ) ); |
|
288 | - |
|
289 | - for ( $i = 0; $i < 30; $i++ ) { |
|
290 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
291 | - $store->save_action( $action ); |
|
292 | - } |
|
293 | - |
|
294 | - $claims = array(); |
|
295 | - |
|
296 | - for ( $i = 0; $i < 5; $i++ ) { |
|
297 | - $claims[] = $store->stake_claim( 5 ); |
|
298 | - } |
|
299 | - |
|
300 | - $mock1 = new MockAction(); |
|
301 | - add_action( $random, array( $mock1, 'action' ) ); |
|
302 | - $actions_run = $runner->run(); |
|
303 | - remove_action( $random, array( $mock1, 'action' ) ); |
|
304 | - |
|
305 | - $this->assertSame( 0, $mock1->get_call_count() ); |
|
306 | - $this->assertSame( 0, $actions_run ); |
|
307 | - |
|
308 | - add_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'return_6' ) ); |
|
309 | - |
|
310 | - $mock2 = new MockAction(); |
|
311 | - add_action( $random, array( $mock2, 'action' ) ); |
|
312 | - $actions_run = $runner->run(); |
|
313 | - remove_action( $random, array( $mock2, 'action' ) ); |
|
314 | - |
|
315 | - $this->assertEquals( 5, $mock2->get_call_count() ); |
|
316 | - $this->assertEquals( 5, $actions_run ); |
|
317 | - |
|
318 | - remove_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'return_6' ) ); |
|
319 | - |
|
320 | - for ( $i = 0; $i < 5; $i++ ) { // to make up for the actions we just processed |
|
321 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
322 | - $store->save_action( $action ); |
|
323 | - } |
|
324 | - |
|
325 | - $mock3 = new MockAction(); |
|
326 | - add_action( $random, array( $mock3, 'action' ) ); |
|
327 | - $actions_run = $runner->run(); |
|
328 | - remove_action( $random, array( $mock3, 'action' ) ); |
|
329 | - |
|
330 | - $this->assertSame( 0, $mock3->get_call_count() ); |
|
331 | - $this->assertSame( 0, $actions_run ); |
|
332 | - |
|
333 | - remove_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'return_6' ) ); |
|
334 | - } |
|
335 | - |
|
336 | - public function return_6() { |
|
337 | - return 6; |
|
338 | - } |
|
339 | - |
|
340 | - public function test_store_fetch_action_failure_schedule_next_instance() { |
|
341 | - $random = md5( rand() ); |
|
342 | - $schedule = new ActionScheduler_IntervalSchedule( as_get_datetime_object( '12 hours ago' ), DAY_IN_SECONDS ); |
|
343 | - $action = new ActionScheduler_Action( $random, array(), $schedule ); |
|
344 | - $action_id = ActionScheduler::store()->save_action( $action ); |
|
345 | - |
|
346 | - // Set up a mock store that will throw an exception when fetching actions. |
|
347 | - $store = $this |
|
348 | - ->getMockBuilder( 'ActionScheduler_wpPostStore' ) |
|
349 | - ->setMethods( array( 'fetch_action' ) ) |
|
350 | - ->getMock(); |
|
351 | - $store |
|
352 | - ->method( 'fetch_action' ) |
|
353 | - ->with( array( $action_id ) ) |
|
354 | - ->will( $this->throwException( new Exception() ) ); |
|
355 | - |
|
356 | - // Set up a mock queue runner to verify that schedule_next_instance() |
|
357 | - // isn't called for an undefined $action. |
|
358 | - $runner = $this |
|
359 | - ->getMockBuilder( 'ActionScheduler_QueueRunner' ) |
|
360 | - ->setConstructorArgs( array( $store ) ) |
|
361 | - ->setMethods( array( 'schedule_next_instance' ) ) |
|
362 | - ->getMock(); |
|
363 | - $runner |
|
364 | - ->expects( $this->never() ) |
|
365 | - ->method( 'schedule_next_instance' ); |
|
366 | - |
|
367 | - $runner->run(); |
|
368 | - |
|
369 | - // Set up a mock store that will throw an exception when fetching actions. |
|
370 | - $store2 = $this |
|
371 | - ->getMockBuilder( 'ActionScheduler_wpPostStore' ) |
|
372 | - ->setMethods( array( 'fetch_action' ) ) |
|
373 | - ->getMock(); |
|
374 | - $store2 |
|
375 | - ->method( 'fetch_action' ) |
|
376 | - ->with( array( $action_id ) ) |
|
377 | - ->willReturn( null ); |
|
378 | - |
|
379 | - // Set up a mock queue runner to verify that schedule_next_instance() |
|
380 | - // isn't called for an undefined $action. |
|
381 | - $runner2 = $this |
|
382 | - ->getMockBuilder( 'ActionScheduler_QueueRunner' ) |
|
383 | - ->setConstructorArgs( array( $store ) ) |
|
384 | - ->setMethods( array( 'schedule_next_instance' ) ) |
|
385 | - ->getMock(); |
|
386 | - $runner2 |
|
387 | - ->expects( $this->never() ) |
|
388 | - ->method( 'schedule_next_instance' ); |
|
389 | - |
|
390 | - $runner2->run(); |
|
391 | - } |
|
101 | + $claim = $store->stake_claim( 10, $date ); |
|
102 | + $actions = $claim->get_actions(); |
|
103 | + $this->assertCount( 1, $actions ); |
|
104 | + |
|
105 | + $fetched_action_id = reset( $actions ); |
|
106 | + $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
107 | + |
|
108 | + $this->assertEquals( $fetched_action_id, $action_id ); |
|
109 | + $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
110 | + $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
111 | + |
|
112 | + $store->release_claim( $claim ); |
|
113 | + |
|
114 | + // Make sure the 2nd instance of the cron action is scheduled to occur tomorrow still |
|
115 | + $runner->process_action( $action_id ); |
|
116 | + |
|
117 | + $claim = $store->stake_claim( 10, $date ); |
|
118 | + $actions = $claim->get_actions(); |
|
119 | + $this->assertCount( 1, $actions ); |
|
120 | + |
|
121 | + $fetched_action_id = reset( $actions ); |
|
122 | + $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
123 | + |
|
124 | + $this->assertNotEquals( $fetched_action_id, $action_id ); |
|
125 | + $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
126 | + $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
127 | + } |
|
128 | + |
|
129 | + public function test_next_instance_of_interval_action() { |
|
130 | + // Create an action to recur every 24 hours, with the first instance scheduled to run 12 hours ago |
|
131 | + $random = md5( rand() ); |
|
132 | + $date = as_get_datetime_object( '12 hours ago' ); |
|
133 | + $action_id = ActionScheduler::factory()->recurring( $random, array(), $date->getTimestamp(), DAY_IN_SECONDS ); |
|
134 | + $store = ActionScheduler::store(); |
|
135 | + $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
136 | + |
|
137 | + // Make sure the 1st instance of the action is scheduled to occur 12 hours ago |
|
138 | + $claim = $store->stake_claim( 10, $date ); |
|
139 | + $actions = $claim->get_actions(); |
|
140 | + $this->assertCount( 1, $actions ); |
|
141 | + |
|
142 | + $fetched_action_id = reset( $actions ); |
|
143 | + $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
144 | + |
|
145 | + $this->assertEquals( $fetched_action_id, $action_id ); |
|
146 | + $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
147 | + $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
148 | + |
|
149 | + $store->release_claim( $claim ); |
|
150 | + |
|
151 | + // Make sure after the queue is run, the 2nd instance of the action is scheduled to occur in 24 hours |
|
152 | + $runner->run(); |
|
153 | + |
|
154 | + $date = as_get_datetime_object( '+1 day' ); |
|
155 | + $claim = $store->stake_claim( 10, $date ); |
|
156 | + $actions = $claim->get_actions(); |
|
157 | + $this->assertCount( 1, $actions ); |
|
158 | + |
|
159 | + $fetched_action_id = reset( $actions ); |
|
160 | + $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
161 | + |
|
162 | + $this->assertNotEquals( $fetched_action_id, $action_id ); |
|
163 | + $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
164 | + $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
165 | + |
|
166 | + $store->release_claim( $claim ); |
|
167 | + |
|
168 | + // Make sure the 3rd instance of the cron action is scheduled for 24 hours from now, as the action was run early, ahead of schedule |
|
169 | + $runner->process_action( $fetched_action_id ); |
|
170 | + $date = as_get_datetime_object( '+1 day' ); |
|
171 | + |
|
172 | + $claim = $store->stake_claim( 10, $date ); |
|
173 | + $actions = $claim->get_actions(); |
|
174 | + $this->assertCount( 1, $actions ); |
|
175 | + |
|
176 | + $fetched_action_id = reset( $actions ); |
|
177 | + $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
178 | + |
|
179 | + $this->assertNotEquals( $fetched_action_id, $action_id ); |
|
180 | + $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
181 | + $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * As soon as one recurring action has been executed its replacement will be scheduled. |
|
186 | + * |
|
187 | + * This is true even if the current action fails. This makes sense, since a failure may be temporary in nature. |
|
188 | + * However, if the same recurring action consistently fails then it is likely that there is a problem and we should |
|
189 | + * stop creating new instances. This test outlines the expected behavior in this regard. |
|
190 | + * |
|
191 | + * @return void |
|
192 | + */ |
|
193 | + public function test_failing_recurring_actions_are_not_rescheduled_when_threshold_met() { |
|
194 | + $store = ActionScheduler_Store::instance(); |
|
195 | + $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
196 | + $created_actions = array(); |
|
197 | + |
|
198 | + // Create 4 failed actions (one below the threshold of what counts as 'consistently failing'). |
|
199 | + for ( $i = 0; $i < 3; $i++ ) { |
|
200 | + // We give each action a unique set of args, this illustrates that in the context of determining consistent |
|
201 | + // failure we care only about the hook and not other properties of the action. |
|
202 | + $args = array( 'unique-' . $i => hash( 'md5', $i ) ); |
|
203 | + $hook = 'will-fail'; |
|
204 | + $date = as_get_datetime_object( 12 - $i . ' hours ago' ); |
|
205 | + $action_id = as_schedule_recurring_action( $date->getTimestamp(), HOUR_IN_SECONDS, $hook, $args ); |
|
206 | + $store->mark_failure( $action_id ); |
|
207 | + $created_actions[] = $action_id; |
|
208 | + } |
|
209 | + |
|
210 | + // Now create a further action using the same hook, that is also destined to fail. |
|
211 | + $date = as_get_datetime_object( '6 hours ago' ); |
|
212 | + $pending_action_id = as_schedule_recurring_action( $date->getTimestamp(), HOUR_IN_SECONDS, $hook, $args ); |
|
213 | + $created_actions[] = $pending_action_id; |
|
214 | + |
|
215 | + // Process the queue! |
|
216 | + $runner->run(); |
|
217 | + $pending_actions = $store->query_actions( |
|
218 | + array( |
|
219 | + 'hook' => $hook, |
|
220 | + 'args' => $args, |
|
221 | + 'status' => ActionScheduler_Store::STATUS_PENDING, |
|
222 | + ) |
|
223 | + ); |
|
224 | + $new_pending_action_id = current( $pending_actions ); |
|
225 | + |
|
226 | + // We now have 5 instances of the same recurring action. 4 have already failed, one is pending. |
|
227 | + $this->assertCount( 1, $pending_actions, 'If the threshold for consistent failure has not been met, a replacement action should have been scheduled.' ); |
|
228 | + $this->assertNotContains( $new_pending_action_id, $created_actions, 'Confirm that the replacement action is new, and not one of those we created manually earlier in the test.' ); |
|
229 | + |
|
230 | + // Process the pending action (we do this directly instead of via `$runner->run()` because it won't actually |
|
231 | + // become due for another hour). |
|
232 | + $runner->process_action( $new_pending_action_id ); |
|
233 | + $pending_actions = $store->query_actions( |
|
234 | + array( |
|
235 | + 'hook' => $hook, |
|
236 | + 'args' => $args, |
|
237 | + 'status' => ActionScheduler_Store::STATUS_PENDING, |
|
238 | + ) |
|
239 | + ); |
|
240 | + |
|
241 | + // Now 5 instances of the same recurring action have all failed, therefore the threshold for consistent failure |
|
242 | + // has been met and, this time, a new action should *not* have been scheduled. |
|
243 | + $this->assertCount( 0, $pending_actions, 'The failure threshold (5 consecutive fails for recurring actions with the same signature) having been met, no further actions were scheduled.' ); |
|
244 | + } |
|
245 | + |
|
246 | + public function test_hooked_into_wp_cron() { |
|
247 | + $next = wp_next_scheduled( ActionScheduler_QueueRunner::WP_CRON_HOOK, array( 'WP Cron' ) ); |
|
248 | + $this->assertNotEmpty( $next ); |
|
249 | + } |
|
250 | + |
|
251 | + public function test_batch_count_limit() { |
|
252 | + $store = ActionScheduler::store(); |
|
253 | + $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
254 | + |
|
255 | + $mock = new MockAction(); |
|
256 | + $random = md5( rand() ); |
|
257 | + add_action( $random, array( $mock, 'action' ) ); |
|
258 | + $schedule = new ActionScheduler_SimpleSchedule( new ActionScheduler_DateTime( '1 day ago' ) ); |
|
259 | + |
|
260 | + for ( $i = 0; $i < 2; $i++ ) { |
|
261 | + $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
262 | + $store->save_action( $action ); |
|
263 | + } |
|
264 | + |
|
265 | + $claim = $store->stake_claim(); |
|
266 | + |
|
267 | + $actions_run = $runner->run(); |
|
268 | + |
|
269 | + $this->assertSame( 0, $mock->get_call_count() ); |
|
270 | + $this->assertSame( 0, $actions_run ); |
|
271 | + |
|
272 | + $store->release_claim( $claim ); |
|
273 | + |
|
274 | + $actions_run = $runner->run(); |
|
275 | + |
|
276 | + $this->assertEquals( 2, $mock->get_call_count() ); |
|
277 | + $this->assertEquals( 2, $actions_run ); |
|
278 | + |
|
279 | + remove_action( $random, array( $mock, 'action' ) ); |
|
280 | + } |
|
281 | + |
|
282 | + public function test_changing_batch_count_limit() { |
|
283 | + $store = ActionScheduler::store(); |
|
284 | + $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
285 | + |
|
286 | + $random = md5( rand() ); |
|
287 | + $schedule = new ActionScheduler_SimpleSchedule( new ActionScheduler_DateTime( '1 day ago' ) ); |
|
288 | + |
|
289 | + for ( $i = 0; $i < 30; $i++ ) { |
|
290 | + $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
291 | + $store->save_action( $action ); |
|
292 | + } |
|
293 | + |
|
294 | + $claims = array(); |
|
295 | + |
|
296 | + for ( $i = 0; $i < 5; $i++ ) { |
|
297 | + $claims[] = $store->stake_claim( 5 ); |
|
298 | + } |
|
299 | + |
|
300 | + $mock1 = new MockAction(); |
|
301 | + add_action( $random, array( $mock1, 'action' ) ); |
|
302 | + $actions_run = $runner->run(); |
|
303 | + remove_action( $random, array( $mock1, 'action' ) ); |
|
304 | + |
|
305 | + $this->assertSame( 0, $mock1->get_call_count() ); |
|
306 | + $this->assertSame( 0, $actions_run ); |
|
307 | + |
|
308 | + add_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'return_6' ) ); |
|
309 | + |
|
310 | + $mock2 = new MockAction(); |
|
311 | + add_action( $random, array( $mock2, 'action' ) ); |
|
312 | + $actions_run = $runner->run(); |
|
313 | + remove_action( $random, array( $mock2, 'action' ) ); |
|
314 | + |
|
315 | + $this->assertEquals( 5, $mock2->get_call_count() ); |
|
316 | + $this->assertEquals( 5, $actions_run ); |
|
317 | + |
|
318 | + remove_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'return_6' ) ); |
|
319 | + |
|
320 | + for ( $i = 0; $i < 5; $i++ ) { // to make up for the actions we just processed |
|
321 | + $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
322 | + $store->save_action( $action ); |
|
323 | + } |
|
324 | + |
|
325 | + $mock3 = new MockAction(); |
|
326 | + add_action( $random, array( $mock3, 'action' ) ); |
|
327 | + $actions_run = $runner->run(); |
|
328 | + remove_action( $random, array( $mock3, 'action' ) ); |
|
329 | + |
|
330 | + $this->assertSame( 0, $mock3->get_call_count() ); |
|
331 | + $this->assertSame( 0, $actions_run ); |
|
332 | + |
|
333 | + remove_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'return_6' ) ); |
|
334 | + } |
|
335 | + |
|
336 | + public function return_6() { |
|
337 | + return 6; |
|
338 | + } |
|
339 | + |
|
340 | + public function test_store_fetch_action_failure_schedule_next_instance() { |
|
341 | + $random = md5( rand() ); |
|
342 | + $schedule = new ActionScheduler_IntervalSchedule( as_get_datetime_object( '12 hours ago' ), DAY_IN_SECONDS ); |
|
343 | + $action = new ActionScheduler_Action( $random, array(), $schedule ); |
|
344 | + $action_id = ActionScheduler::store()->save_action( $action ); |
|
345 | + |
|
346 | + // Set up a mock store that will throw an exception when fetching actions. |
|
347 | + $store = $this |
|
348 | + ->getMockBuilder( 'ActionScheduler_wpPostStore' ) |
|
349 | + ->setMethods( array( 'fetch_action' ) ) |
|
350 | + ->getMock(); |
|
351 | + $store |
|
352 | + ->method( 'fetch_action' ) |
|
353 | + ->with( array( $action_id ) ) |
|
354 | + ->will( $this->throwException( new Exception() ) ); |
|
355 | + |
|
356 | + // Set up a mock queue runner to verify that schedule_next_instance() |
|
357 | + // isn't called for an undefined $action. |
|
358 | + $runner = $this |
|
359 | + ->getMockBuilder( 'ActionScheduler_QueueRunner' ) |
|
360 | + ->setConstructorArgs( array( $store ) ) |
|
361 | + ->setMethods( array( 'schedule_next_instance' ) ) |
|
362 | + ->getMock(); |
|
363 | + $runner |
|
364 | + ->expects( $this->never() ) |
|
365 | + ->method( 'schedule_next_instance' ); |
|
366 | + |
|
367 | + $runner->run(); |
|
368 | + |
|
369 | + // Set up a mock store that will throw an exception when fetching actions. |
|
370 | + $store2 = $this |
|
371 | + ->getMockBuilder( 'ActionScheduler_wpPostStore' ) |
|
372 | + ->setMethods( array( 'fetch_action' ) ) |
|
373 | + ->getMock(); |
|
374 | + $store2 |
|
375 | + ->method( 'fetch_action' ) |
|
376 | + ->with( array( $action_id ) ) |
|
377 | + ->willReturn( null ); |
|
378 | + |
|
379 | + // Set up a mock queue runner to verify that schedule_next_instance() |
|
380 | + // isn't called for an undefined $action. |
|
381 | + $runner2 = $this |
|
382 | + ->getMockBuilder( 'ActionScheduler_QueueRunner' ) |
|
383 | + ->setConstructorArgs( array( $store ) ) |
|
384 | + ->setMethods( array( 'schedule_next_instance' ) ) |
|
385 | + ->getMock(); |
|
386 | + $runner2 |
|
387 | + ->expects( $this->never() ) |
|
388 | + ->method( 'schedule_next_instance' ); |
|
389 | + |
|
390 | + $runner2->run(); |
|
391 | + } |
|
392 | 392 | } |
@@ -8,177 +8,177 @@ discard block |
||
8 | 8 | class ActionScheduler_QueueRunner_Test extends ActionScheduler_UnitTestCase { |
9 | 9 | public function test_create_runner() { |
10 | 10 | $store = ActionScheduler::store(); |
11 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
11 | + $runner = ActionScheduler_Mocker::get_queue_runner($store); |
|
12 | 12 | $actions_run = $runner->run(); |
13 | 13 | |
14 | - $this->assertSame( 0, $actions_run ); |
|
14 | + $this->assertSame(0, $actions_run); |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | public function test_run() { |
18 | 18 | $store = ActionScheduler::store(); |
19 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
19 | + $runner = ActionScheduler_Mocker::get_queue_runner($store); |
|
20 | 20 | |
21 | 21 | $mock = new MockAction(); |
22 | - $random = md5( rand() ); |
|
23 | - add_action( $random, array( $mock, 'action' ) ); |
|
24 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
22 | + $random = md5(rand()); |
|
23 | + add_action($random, array($mock, 'action')); |
|
24 | + $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago')); |
|
25 | 25 | |
26 | - for ( $i = 0; $i < 5; $i++ ) { |
|
27 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
28 | - $store->save_action( $action ); |
|
26 | + for ($i = 0; $i < 5; $i++) { |
|
27 | + $action = new ActionScheduler_Action($random, array($random), $schedule); |
|
28 | + $store->save_action($action); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | $actions_run = $runner->run(); |
32 | 32 | |
33 | - remove_action( $random, array( $mock, 'action' ) ); |
|
33 | + remove_action($random, array($mock, 'action')); |
|
34 | 34 | |
35 | - $this->assertEquals( 5, $mock->get_call_count() ); |
|
36 | - $this->assertEquals( 5, $actions_run ); |
|
35 | + $this->assertEquals(5, $mock->get_call_count()); |
|
36 | + $this->assertEquals(5, $actions_run); |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | public function test_run_with_future_actions() { |
40 | 40 | $store = ActionScheduler::store(); |
41 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
41 | + $runner = ActionScheduler_Mocker::get_queue_runner($store); |
|
42 | 42 | |
43 | 43 | $mock = new MockAction(); |
44 | - $random = md5( rand() ); |
|
45 | - add_action( $random, array( $mock, 'action' ) ); |
|
46 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '1 day ago' ) ); |
|
44 | + $random = md5(rand()); |
|
45 | + add_action($random, array($mock, 'action')); |
|
46 | + $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago')); |
|
47 | 47 | |
48 | - for ( $i = 0; $i < 3; $i++ ) { |
|
49 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
50 | - $store->save_action( $action ); |
|
48 | + for ($i = 0; $i < 3; $i++) { |
|
49 | + $action = new ActionScheduler_Action($random, array($random), $schedule); |
|
50 | + $store->save_action($action); |
|
51 | 51 | } |
52 | 52 | |
53 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( 'tomorrow' ) ); |
|
54 | - for ( $i = 0; $i < 3; $i++ ) { |
|
55 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
56 | - $store->save_action( $action ); |
|
53 | + $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('tomorrow')); |
|
54 | + for ($i = 0; $i < 3; $i++) { |
|
55 | + $action = new ActionScheduler_Action($random, array($random), $schedule); |
|
56 | + $store->save_action($action); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | $actions_run = $runner->run(); |
60 | 60 | |
61 | - remove_action( $random, array( $mock, 'action' ) ); |
|
61 | + remove_action($random, array($mock, 'action')); |
|
62 | 62 | |
63 | - $this->assertEquals( 3, $mock->get_call_count() ); |
|
64 | - $this->assertEquals( 3, $actions_run ); |
|
63 | + $this->assertEquals(3, $mock->get_call_count()); |
|
64 | + $this->assertEquals(3, $actions_run); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | public function test_completed_action_status() { |
68 | 68 | $store = ActionScheduler::store(); |
69 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
69 | + $runner = ActionScheduler_Mocker::get_queue_runner($store); |
|
70 | 70 | |
71 | - $random = md5( rand() ); |
|
72 | - $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '12 hours ago' ) ); |
|
71 | + $random = md5(rand()); |
|
72 | + $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('12 hours ago')); |
|
73 | 73 | |
74 | - $action = new ActionScheduler_Action( $random, array(), $schedule ); |
|
75 | - $action_id = $store->save_action( $action ); |
|
74 | + $action = new ActionScheduler_Action($random, array(), $schedule); |
|
75 | + $action_id = $store->save_action($action); |
|
76 | 76 | |
77 | 77 | $runner->run(); |
78 | 78 | |
79 | - $finished_action = $store->fetch_action( $action_id ); |
|
79 | + $finished_action = $store->fetch_action($action_id); |
|
80 | 80 | |
81 | - $this->assertTrue( $finished_action->is_finished() ); |
|
81 | + $this->assertTrue($finished_action->is_finished()); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | public function test_next_instance_of_cron_action() { |
85 | 85 | // Create an action with daily Cron expression (i.e. midnight each day) |
86 | - $random = md5( rand() ); |
|
87 | - $action_id = ActionScheduler::factory()->cron( $random, array(), null, '0 0 * * *' ); |
|
86 | + $random = md5(rand()); |
|
87 | + $action_id = ActionScheduler::factory()->cron($random, array(), null, '0 0 * * *'); |
|
88 | 88 | $store = ActionScheduler::store(); |
89 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
89 | + $runner = ActionScheduler_Mocker::get_queue_runner($store); |
|
90 | 90 | |
91 | 91 | // Make sure the 1st instance of the action is scheduled to occur tomorrow |
92 | - $date = as_get_datetime_object( 'tomorrow' ); |
|
93 | - $date->modify( '-1 minute' ); |
|
94 | - $claim = $store->stake_claim( 10, $date ); |
|
95 | - $this->assertCount( 0, $claim->get_actions() ); |
|
92 | + $date = as_get_datetime_object('tomorrow'); |
|
93 | + $date->modify('-1 minute'); |
|
94 | + $claim = $store->stake_claim(10, $date); |
|
95 | + $this->assertCount(0, $claim->get_actions()); |
|
96 | 96 | |
97 | - $store->release_claim( $claim ); |
|
97 | + $store->release_claim($claim); |
|
98 | 98 | |
99 | - $date->modify( '+1 minute' ); |
|
99 | + $date->modify('+1 minute'); |
|
100 | 100 | |
101 | - $claim = $store->stake_claim( 10, $date ); |
|
101 | + $claim = $store->stake_claim(10, $date); |
|
102 | 102 | $actions = $claim->get_actions(); |
103 | - $this->assertCount( 1, $actions ); |
|
103 | + $this->assertCount(1, $actions); |
|
104 | 104 | |
105 | - $fetched_action_id = reset( $actions ); |
|
106 | - $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
105 | + $fetched_action_id = reset($actions); |
|
106 | + $fetched_action = $store->fetch_action($fetched_action_id); |
|
107 | 107 | |
108 | - $this->assertEquals( $fetched_action_id, $action_id ); |
|
109 | - $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
110 | - $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
108 | + $this->assertEquals($fetched_action_id, $action_id); |
|
109 | + $this->assertEquals($random, $fetched_action->get_hook()); |
|
110 | + $this->assertEquals($date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1); |
|
111 | 111 | |
112 | - $store->release_claim( $claim ); |
|
112 | + $store->release_claim($claim); |
|
113 | 113 | |
114 | 114 | // Make sure the 2nd instance of the cron action is scheduled to occur tomorrow still |
115 | - $runner->process_action( $action_id ); |
|
115 | + $runner->process_action($action_id); |
|
116 | 116 | |
117 | - $claim = $store->stake_claim( 10, $date ); |
|
117 | + $claim = $store->stake_claim(10, $date); |
|
118 | 118 | $actions = $claim->get_actions(); |
119 | - $this->assertCount( 1, $actions ); |
|
119 | + $this->assertCount(1, $actions); |
|
120 | 120 | |
121 | - $fetched_action_id = reset( $actions ); |
|
122 | - $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
121 | + $fetched_action_id = reset($actions); |
|
122 | + $fetched_action = $store->fetch_action($fetched_action_id); |
|
123 | 123 | |
124 | - $this->assertNotEquals( $fetched_action_id, $action_id ); |
|
125 | - $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
126 | - $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
124 | + $this->assertNotEquals($fetched_action_id, $action_id); |
|
125 | + $this->assertEquals($random, $fetched_action->get_hook()); |
|
126 | + $this->assertEquals($date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | public function test_next_instance_of_interval_action() { |
130 | 130 | // Create an action to recur every 24 hours, with the first instance scheduled to run 12 hours ago |
131 | - $random = md5( rand() ); |
|
132 | - $date = as_get_datetime_object( '12 hours ago' ); |
|
133 | - $action_id = ActionScheduler::factory()->recurring( $random, array(), $date->getTimestamp(), DAY_IN_SECONDS ); |
|
131 | + $random = md5(rand()); |
|
132 | + $date = as_get_datetime_object('12 hours ago'); |
|
133 | + $action_id = ActionScheduler::factory()->recurring($random, array(), $date->getTimestamp(), DAY_IN_SECONDS); |
|
134 | 134 | $store = ActionScheduler::store(); |
135 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
135 | + $runner = ActionScheduler_Mocker::get_queue_runner($store); |
|
136 | 136 | |
137 | 137 | // Make sure the 1st instance of the action is scheduled to occur 12 hours ago |
138 | - $claim = $store->stake_claim( 10, $date ); |
|
138 | + $claim = $store->stake_claim(10, $date); |
|
139 | 139 | $actions = $claim->get_actions(); |
140 | - $this->assertCount( 1, $actions ); |
|
140 | + $this->assertCount(1, $actions); |
|
141 | 141 | |
142 | - $fetched_action_id = reset( $actions ); |
|
143 | - $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
142 | + $fetched_action_id = reset($actions); |
|
143 | + $fetched_action = $store->fetch_action($fetched_action_id); |
|
144 | 144 | |
145 | - $this->assertEquals( $fetched_action_id, $action_id ); |
|
146 | - $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
147 | - $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
145 | + $this->assertEquals($fetched_action_id, $action_id); |
|
146 | + $this->assertEquals($random, $fetched_action->get_hook()); |
|
147 | + $this->assertEquals($date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1); |
|
148 | 148 | |
149 | - $store->release_claim( $claim ); |
|
149 | + $store->release_claim($claim); |
|
150 | 150 | |
151 | 151 | // Make sure after the queue is run, the 2nd instance of the action is scheduled to occur in 24 hours |
152 | 152 | $runner->run(); |
153 | 153 | |
154 | - $date = as_get_datetime_object( '+1 day' ); |
|
155 | - $claim = $store->stake_claim( 10, $date ); |
|
154 | + $date = as_get_datetime_object('+1 day'); |
|
155 | + $claim = $store->stake_claim(10, $date); |
|
156 | 156 | $actions = $claim->get_actions(); |
157 | - $this->assertCount( 1, $actions ); |
|
157 | + $this->assertCount(1, $actions); |
|
158 | 158 | |
159 | - $fetched_action_id = reset( $actions ); |
|
160 | - $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
159 | + $fetched_action_id = reset($actions); |
|
160 | + $fetched_action = $store->fetch_action($fetched_action_id); |
|
161 | 161 | |
162 | - $this->assertNotEquals( $fetched_action_id, $action_id ); |
|
163 | - $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
164 | - $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
162 | + $this->assertNotEquals($fetched_action_id, $action_id); |
|
163 | + $this->assertEquals($random, $fetched_action->get_hook()); |
|
164 | + $this->assertEquals($date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1); |
|
165 | 165 | |
166 | - $store->release_claim( $claim ); |
|
166 | + $store->release_claim($claim); |
|
167 | 167 | |
168 | 168 | // Make sure the 3rd instance of the cron action is scheduled for 24 hours from now, as the action was run early, ahead of schedule |
169 | - $runner->process_action( $fetched_action_id ); |
|
170 | - $date = as_get_datetime_object( '+1 day' ); |
|
169 | + $runner->process_action($fetched_action_id); |
|
170 | + $date = as_get_datetime_object('+1 day'); |
|
171 | 171 | |
172 | - $claim = $store->stake_claim( 10, $date ); |
|
172 | + $claim = $store->stake_claim(10, $date); |
|
173 | 173 | $actions = $claim->get_actions(); |
174 | - $this->assertCount( 1, $actions ); |
|
174 | + $this->assertCount(1, $actions); |
|
175 | 175 | |
176 | - $fetched_action_id = reset( $actions ); |
|
177 | - $fetched_action = $store->fetch_action( $fetched_action_id ); |
|
176 | + $fetched_action_id = reset($actions); |
|
177 | + $fetched_action = $store->fetch_action($fetched_action_id); |
|
178 | 178 | |
179 | - $this->assertNotEquals( $fetched_action_id, $action_id ); |
|
180 | - $this->assertEquals( $random, $fetched_action->get_hook() ); |
|
181 | - $this->assertEquals( $date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1 ); |
|
179 | + $this->assertNotEquals($fetched_action_id, $action_id); |
|
180 | + $this->assertEquals($random, $fetched_action->get_hook()); |
|
181 | + $this->assertEquals($date->getTimestamp(), $fetched_action->get_schedule()->get_date()->getTimestamp(), '', 1); |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | /** |
@@ -192,44 +192,44 @@ discard block |
||
192 | 192 | */ |
193 | 193 | public function test_failing_recurring_actions_are_not_rescheduled_when_threshold_met() { |
194 | 194 | $store = ActionScheduler_Store::instance(); |
195 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
195 | + $runner = ActionScheduler_Mocker::get_queue_runner($store); |
|
196 | 196 | $created_actions = array(); |
197 | 197 | |
198 | 198 | // Create 4 failed actions (one below the threshold of what counts as 'consistently failing'). |
199 | - for ( $i = 0; $i < 3; $i++ ) { |
|
199 | + for ($i = 0; $i < 3; $i++) { |
|
200 | 200 | // We give each action a unique set of args, this illustrates that in the context of determining consistent |
201 | 201 | // failure we care only about the hook and not other properties of the action. |
202 | - $args = array( 'unique-' . $i => hash( 'md5', $i ) ); |
|
202 | + $args = array('unique-'.$i => hash('md5', $i)); |
|
203 | 203 | $hook = 'will-fail'; |
204 | - $date = as_get_datetime_object( 12 - $i . ' hours ago' ); |
|
205 | - $action_id = as_schedule_recurring_action( $date->getTimestamp(), HOUR_IN_SECONDS, $hook, $args ); |
|
206 | - $store->mark_failure( $action_id ); |
|
204 | + $date = as_get_datetime_object(12 - $i.' hours ago'); |
|
205 | + $action_id = as_schedule_recurring_action($date->getTimestamp(), HOUR_IN_SECONDS, $hook, $args); |
|
206 | + $store->mark_failure($action_id); |
|
207 | 207 | $created_actions[] = $action_id; |
208 | 208 | } |
209 | 209 | |
210 | 210 | // Now create a further action using the same hook, that is also destined to fail. |
211 | - $date = as_get_datetime_object( '6 hours ago' ); |
|
212 | - $pending_action_id = as_schedule_recurring_action( $date->getTimestamp(), HOUR_IN_SECONDS, $hook, $args ); |
|
211 | + $date = as_get_datetime_object('6 hours ago'); |
|
212 | + $pending_action_id = as_schedule_recurring_action($date->getTimestamp(), HOUR_IN_SECONDS, $hook, $args); |
|
213 | 213 | $created_actions[] = $pending_action_id; |
214 | 214 | |
215 | 215 | // Process the queue! |
216 | 216 | $runner->run(); |
217 | - $pending_actions = $store->query_actions( |
|
217 | + $pending_actions = $store->query_actions( |
|
218 | 218 | array( |
219 | 219 | 'hook' => $hook, |
220 | 220 | 'args' => $args, |
221 | 221 | 'status' => ActionScheduler_Store::STATUS_PENDING, |
222 | 222 | ) |
223 | 223 | ); |
224 | - $new_pending_action_id = current( $pending_actions ); |
|
224 | + $new_pending_action_id = current($pending_actions); |
|
225 | 225 | |
226 | 226 | // We now have 5 instances of the same recurring action. 4 have already failed, one is pending. |
227 | - $this->assertCount( 1, $pending_actions, 'If the threshold for consistent failure has not been met, a replacement action should have been scheduled.' ); |
|
228 | - $this->assertNotContains( $new_pending_action_id, $created_actions, 'Confirm that the replacement action is new, and not one of those we created manually earlier in the test.' ); |
|
227 | + $this->assertCount(1, $pending_actions, 'If the threshold for consistent failure has not been met, a replacement action should have been scheduled.'); |
|
228 | + $this->assertNotContains($new_pending_action_id, $created_actions, 'Confirm that the replacement action is new, and not one of those we created manually earlier in the test.'); |
|
229 | 229 | |
230 | 230 | // Process the pending action (we do this directly instead of via `$runner->run()` because it won't actually |
231 | 231 | // become due for another hour). |
232 | - $runner->process_action( $new_pending_action_id ); |
|
232 | + $runner->process_action($new_pending_action_id); |
|
233 | 233 | $pending_actions = $store->query_actions( |
234 | 234 | array( |
235 | 235 | 'hook' => $hook, |
@@ -240,97 +240,97 @@ discard block |
||
240 | 240 | |
241 | 241 | // Now 5 instances of the same recurring action have all failed, therefore the threshold for consistent failure |
242 | 242 | // has been met and, this time, a new action should *not* have been scheduled. |
243 | - $this->assertCount( 0, $pending_actions, 'The failure threshold (5 consecutive fails for recurring actions with the same signature) having been met, no further actions were scheduled.' ); |
|
243 | + $this->assertCount(0, $pending_actions, 'The failure threshold (5 consecutive fails for recurring actions with the same signature) having been met, no further actions were scheduled.'); |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | public function test_hooked_into_wp_cron() { |
247 | - $next = wp_next_scheduled( ActionScheduler_QueueRunner::WP_CRON_HOOK, array( 'WP Cron' ) ); |
|
248 | - $this->assertNotEmpty( $next ); |
|
247 | + $next = wp_next_scheduled(ActionScheduler_QueueRunner::WP_CRON_HOOK, array('WP Cron')); |
|
248 | + $this->assertNotEmpty($next); |
|
249 | 249 | } |
250 | 250 | |
251 | 251 | public function test_batch_count_limit() { |
252 | 252 | $store = ActionScheduler::store(); |
253 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
253 | + $runner = ActionScheduler_Mocker::get_queue_runner($store); |
|
254 | 254 | |
255 | 255 | $mock = new MockAction(); |
256 | - $random = md5( rand() ); |
|
257 | - add_action( $random, array( $mock, 'action' ) ); |
|
258 | - $schedule = new ActionScheduler_SimpleSchedule( new ActionScheduler_DateTime( '1 day ago' ) ); |
|
256 | + $random = md5(rand()); |
|
257 | + add_action($random, array($mock, 'action')); |
|
258 | + $schedule = new ActionScheduler_SimpleSchedule(new ActionScheduler_DateTime('1 day ago')); |
|
259 | 259 | |
260 | - for ( $i = 0; $i < 2; $i++ ) { |
|
261 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
262 | - $store->save_action( $action ); |
|
260 | + for ($i = 0; $i < 2; $i++) { |
|
261 | + $action = new ActionScheduler_Action($random, array($random), $schedule); |
|
262 | + $store->save_action($action); |
|
263 | 263 | } |
264 | 264 | |
265 | 265 | $claim = $store->stake_claim(); |
266 | 266 | |
267 | 267 | $actions_run = $runner->run(); |
268 | 268 | |
269 | - $this->assertSame( 0, $mock->get_call_count() ); |
|
270 | - $this->assertSame( 0, $actions_run ); |
|
269 | + $this->assertSame(0, $mock->get_call_count()); |
|
270 | + $this->assertSame(0, $actions_run); |
|
271 | 271 | |
272 | - $store->release_claim( $claim ); |
|
272 | + $store->release_claim($claim); |
|
273 | 273 | |
274 | 274 | $actions_run = $runner->run(); |
275 | 275 | |
276 | - $this->assertEquals( 2, $mock->get_call_count() ); |
|
277 | - $this->assertEquals( 2, $actions_run ); |
|
276 | + $this->assertEquals(2, $mock->get_call_count()); |
|
277 | + $this->assertEquals(2, $actions_run); |
|
278 | 278 | |
279 | - remove_action( $random, array( $mock, 'action' ) ); |
|
279 | + remove_action($random, array($mock, 'action')); |
|
280 | 280 | } |
281 | 281 | |
282 | 282 | public function test_changing_batch_count_limit() { |
283 | 283 | $store = ActionScheduler::store(); |
284 | - $runner = ActionScheduler_Mocker::get_queue_runner( $store ); |
|
284 | + $runner = ActionScheduler_Mocker::get_queue_runner($store); |
|
285 | 285 | |
286 | - $random = md5( rand() ); |
|
287 | - $schedule = new ActionScheduler_SimpleSchedule( new ActionScheduler_DateTime( '1 day ago' ) ); |
|
286 | + $random = md5(rand()); |
|
287 | + $schedule = new ActionScheduler_SimpleSchedule(new ActionScheduler_DateTime('1 day ago')); |
|
288 | 288 | |
289 | - for ( $i = 0; $i < 30; $i++ ) { |
|
290 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
291 | - $store->save_action( $action ); |
|
289 | + for ($i = 0; $i < 30; $i++) { |
|
290 | + $action = new ActionScheduler_Action($random, array($random), $schedule); |
|
291 | + $store->save_action($action); |
|
292 | 292 | } |
293 | 293 | |
294 | 294 | $claims = array(); |
295 | 295 | |
296 | - for ( $i = 0; $i < 5; $i++ ) { |
|
297 | - $claims[] = $store->stake_claim( 5 ); |
|
296 | + for ($i = 0; $i < 5; $i++) { |
|
297 | + $claims[] = $store->stake_claim(5); |
|
298 | 298 | } |
299 | 299 | |
300 | 300 | $mock1 = new MockAction(); |
301 | - add_action( $random, array( $mock1, 'action' ) ); |
|
301 | + add_action($random, array($mock1, 'action')); |
|
302 | 302 | $actions_run = $runner->run(); |
303 | - remove_action( $random, array( $mock1, 'action' ) ); |
|
303 | + remove_action($random, array($mock1, 'action')); |
|
304 | 304 | |
305 | - $this->assertSame( 0, $mock1->get_call_count() ); |
|
306 | - $this->assertSame( 0, $actions_run ); |
|
305 | + $this->assertSame(0, $mock1->get_call_count()); |
|
306 | + $this->assertSame(0, $actions_run); |
|
307 | 307 | |
308 | - add_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'return_6' ) ); |
|
308 | + add_filter('action_scheduler_queue_runner_concurrent_batches', array($this, 'return_6')); |
|
309 | 309 | |
310 | 310 | $mock2 = new MockAction(); |
311 | - add_action( $random, array( $mock2, 'action' ) ); |
|
311 | + add_action($random, array($mock2, 'action')); |
|
312 | 312 | $actions_run = $runner->run(); |
313 | - remove_action( $random, array( $mock2, 'action' ) ); |
|
313 | + remove_action($random, array($mock2, 'action')); |
|
314 | 314 | |
315 | - $this->assertEquals( 5, $mock2->get_call_count() ); |
|
316 | - $this->assertEquals( 5, $actions_run ); |
|
315 | + $this->assertEquals(5, $mock2->get_call_count()); |
|
316 | + $this->assertEquals(5, $actions_run); |
|
317 | 317 | |
318 | - remove_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'return_6' ) ); |
|
318 | + remove_filter('action_scheduler_queue_runner_concurrent_batches', array($this, 'return_6')); |
|
319 | 319 | |
320 | - for ( $i = 0; $i < 5; $i++ ) { // to make up for the actions we just processed |
|
321 | - $action = new ActionScheduler_Action( $random, array( $random ), $schedule ); |
|
322 | - $store->save_action( $action ); |
|
320 | + for ($i = 0; $i < 5; $i++) { // to make up for the actions we just processed |
|
321 | + $action = new ActionScheduler_Action($random, array($random), $schedule); |
|
322 | + $store->save_action($action); |
|
323 | 323 | } |
324 | 324 | |
325 | 325 | $mock3 = new MockAction(); |
326 | - add_action( $random, array( $mock3, 'action' ) ); |
|
326 | + add_action($random, array($mock3, 'action')); |
|
327 | 327 | $actions_run = $runner->run(); |
328 | - remove_action( $random, array( $mock3, 'action' ) ); |
|
328 | + remove_action($random, array($mock3, 'action')); |
|
329 | 329 | |
330 | - $this->assertSame( 0, $mock3->get_call_count() ); |
|
331 | - $this->assertSame( 0, $actions_run ); |
|
330 | + $this->assertSame(0, $mock3->get_call_count()); |
|
331 | + $this->assertSame(0, $actions_run); |
|
332 | 332 | |
333 | - remove_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'return_6' ) ); |
|
333 | + remove_filter('action_scheduler_queue_runner_concurrent_batches', array($this, 'return_6')); |
|
334 | 334 | } |
335 | 335 | |
336 | 336 | public function return_6() { |
@@ -338,54 +338,54 @@ discard block |
||
338 | 338 | } |
339 | 339 | |
340 | 340 | public function test_store_fetch_action_failure_schedule_next_instance() { |
341 | - $random = md5( rand() ); |
|
342 | - $schedule = new ActionScheduler_IntervalSchedule( as_get_datetime_object( '12 hours ago' ), DAY_IN_SECONDS ); |
|
343 | - $action = new ActionScheduler_Action( $random, array(), $schedule ); |
|
344 | - $action_id = ActionScheduler::store()->save_action( $action ); |
|
341 | + $random = md5(rand()); |
|
342 | + $schedule = new ActionScheduler_IntervalSchedule(as_get_datetime_object('12 hours ago'), DAY_IN_SECONDS); |
|
343 | + $action = new ActionScheduler_Action($random, array(), $schedule); |
|
344 | + $action_id = ActionScheduler::store()->save_action($action); |
|
345 | 345 | |
346 | 346 | // Set up a mock store that will throw an exception when fetching actions. |
347 | 347 | $store = $this |
348 | - ->getMockBuilder( 'ActionScheduler_wpPostStore' ) |
|
349 | - ->setMethods( array( 'fetch_action' ) ) |
|
348 | + ->getMockBuilder('ActionScheduler_wpPostStore') |
|
349 | + ->setMethods(array('fetch_action')) |
|
350 | 350 | ->getMock(); |
351 | 351 | $store |
352 | - ->method( 'fetch_action' ) |
|
353 | - ->with( array( $action_id ) ) |
|
354 | - ->will( $this->throwException( new Exception() ) ); |
|
352 | + ->method('fetch_action') |
|
353 | + ->with(array($action_id)) |
|
354 | + ->will($this->throwException(new Exception())); |
|
355 | 355 | |
356 | 356 | // Set up a mock queue runner to verify that schedule_next_instance() |
357 | 357 | // isn't called for an undefined $action. |
358 | 358 | $runner = $this |
359 | - ->getMockBuilder( 'ActionScheduler_QueueRunner' ) |
|
360 | - ->setConstructorArgs( array( $store ) ) |
|
361 | - ->setMethods( array( 'schedule_next_instance' ) ) |
|
359 | + ->getMockBuilder('ActionScheduler_QueueRunner') |
|
360 | + ->setConstructorArgs(array($store)) |
|
361 | + ->setMethods(array('schedule_next_instance')) |
|
362 | 362 | ->getMock(); |
363 | 363 | $runner |
364 | - ->expects( $this->never() ) |
|
365 | - ->method( 'schedule_next_instance' ); |
|
364 | + ->expects($this->never()) |
|
365 | + ->method('schedule_next_instance'); |
|
366 | 366 | |
367 | 367 | $runner->run(); |
368 | 368 | |
369 | 369 | // Set up a mock store that will throw an exception when fetching actions. |
370 | 370 | $store2 = $this |
371 | - ->getMockBuilder( 'ActionScheduler_wpPostStore' ) |
|
372 | - ->setMethods( array( 'fetch_action' ) ) |
|
371 | + ->getMockBuilder('ActionScheduler_wpPostStore') |
|
372 | + ->setMethods(array('fetch_action')) |
|
373 | 373 | ->getMock(); |
374 | 374 | $store2 |
375 | - ->method( 'fetch_action' ) |
|
376 | - ->with( array( $action_id ) ) |
|
377 | - ->willReturn( null ); |
|
375 | + ->method('fetch_action') |
|
376 | + ->with(array($action_id)) |
|
377 | + ->willReturn(null); |
|
378 | 378 | |
379 | 379 | // Set up a mock queue runner to verify that schedule_next_instance() |
380 | 380 | // isn't called for an undefined $action. |
381 | 381 | $runner2 = $this |
382 | - ->getMockBuilder( 'ActionScheduler_QueueRunner' ) |
|
383 | - ->setConstructorArgs( array( $store ) ) |
|
384 | - ->setMethods( array( 'schedule_next_instance' ) ) |
|
382 | + ->getMockBuilder('ActionScheduler_QueueRunner') |
|
383 | + ->setConstructorArgs(array($store)) |
|
384 | + ->setMethods(array('schedule_next_instance')) |
|
385 | 385 | ->getMock(); |
386 | 386 | $runner2 |
387 | - ->expects( $this->never() ) |
|
388 | - ->method( 'schedule_next_instance' ); |
|
387 | + ->expects($this->never()) |
|
388 | + ->method('schedule_next_instance'); |
|
389 | 389 | |
390 | 390 | $runner2->run(); |
391 | 391 | } |