Test Failed
Push — master ( abf703...9671b2 )
by Jonathan
04:24 queued 01:11
created

ActionScheduler_QueueRunner_Test   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 256
Duplicated Lines 9.38 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
dl 24
loc 256
rs 10
c 0
b 0
f 0
wmc 18
lcom 0
cbo 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Class ActionScheduler_QueueRunner_Test
5
 * @group runners
6
 */
7
class ActionScheduler_QueueRunner_Test extends ActionScheduler_UnitTestCase {
8
	public function test_create_runner() {
9
		$store = ActionScheduler::store();
10
		$runner = new ActionScheduler_QueueRunner( $store );
11
		$actions_run = $runner->run();
12
13
		$this->assertEquals( 0, $actions_run );
14
	}
15
16
	public function test_run() {
17
		$store = ActionScheduler::store();
18
		$runner = new ActionScheduler_QueueRunner( $store );
19
20
		$mock = new MockAction();
21
		$random = md5(rand());
22
		add_action( $random, array( $mock, 'action' ) );
23
		$schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago'));
24
25
		for ( $i = 0 ; $i < 5 ; $i++ ) {
26
			$action = new ActionScheduler_Action( $random, array($random), $schedule );
27
			$store->save_action( $action );
28
		}
29
30
		$actions_run = $runner->run();
31
32
		remove_action( $random, array( $mock, 'action' ) );
33
34
		$this->assertEquals( 5, $mock->get_call_count() );
35
		$this->assertEquals( 5, $actions_run );
36
	}
37
38
	public function test_run_with_future_actions() {
39
		$store = ActionScheduler::store();
40
		$runner = new ActionScheduler_QueueRunner( $store );
41
42
		$mock = new MockAction();
43
		$random = md5(rand());
44
		add_action( $random, array( $mock, 'action' ) );
45
		$schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago'));
46
47
		for ( $i = 0 ; $i < 3 ; $i++ ) {
48
			$action = new ActionScheduler_Action( $random, array($random), $schedule );
49
			$store->save_action( $action );
50
		}
51
52
		$schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('tomorrow'));
53
		for ( $i = 0 ; $i < 3 ; $i++ ) {
54
			$action = new ActionScheduler_Action( $random, array($random), $schedule );
55
			$store->save_action( $action );
56
		}
57
58
		$actions_run = $runner->run();
59
60
		remove_action( $random, array( $mock, 'action' ) );
61
62
		$this->assertEquals( 3, $mock->get_call_count() );
63
		$this->assertEquals( 3, $actions_run );
64
	}
65
66
	public function test_completed_action_status() {
67
		$store = ActionScheduler::store();
68
		$runner = new ActionScheduler_QueueRunner( $store );
69
70
		$random = md5(rand());
71
		$schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('12 hours ago'));
72
73
		$action = new ActionScheduler_Action( $random, array(), $schedule );
74
		$action_id = $store->save_action( $action );
75
76
		$runner->run();
77
78
		$finished_action = $store->fetch_action( $action_id );
79
80
		$this->assertTrue( $finished_action->is_finished() );
81
	}
82
83
	public function test_next_instance_of_action() {
84
		$store = ActionScheduler::store();
85
		$runner = new ActionScheduler_QueueRunner( $store );
86
87
		$random = md5(rand());
88
		$schedule = new ActionScheduler_IntervalSchedule(as_get_datetime_object('12 hours ago'), DAY_IN_SECONDS);
89
90
		$action = new ActionScheduler_Action( $random, array(), $schedule );
91
		$store->save_action( $action );
92
93
		$runner->run();
94
95
		$claim = $store->stake_claim(10, as_get_datetime_object((DAY_IN_SECONDS - 60).' seconds'));
96
		$this->assertCount(0, $claim->get_actions());
97
98
		$claim = $store->stake_claim(10, as_get_datetime_object(DAY_IN_SECONDS.' seconds'));
99
		$actions = $claim->get_actions();
100
		$this->assertCount(1, $actions);
101
102
		$action_id = reset($actions);
103
		$new_action = $store->fetch_action($action_id);
104
105
106
		$this->assertEquals( $random, $new_action->get_hook() );
107
		$this->assertEquals( $schedule->next(as_get_datetime_object())->getTimestamp(), $new_action->get_schedule()->next(as_get_datetime_object())->getTimestamp() );
108
	}
109
110
	public function test_hooked_into_wp_cron() {
111
		$next = wp_next_scheduled( ActionScheduler_QueueRunner::WP_CRON_HOOK );
112
		$this->assertNotEmpty($next);
113
	}
114
115
	public function test_batch_count_limit() {
116
		$store = ActionScheduler::store();
117
		$runner = new ActionScheduler_QueueRunner( $store );
118
119
		$mock = new MockAction();
120
		$random = md5(rand());
121
		add_action( $random, array( $mock, 'action' ) );
122
		$schedule = new ActionScheduler_SimpleSchedule(new ActionScheduler_DateTime('1 day ago'));
123
124
		for ( $i = 0 ; $i < 30 ; $i++ ) {
125
			$action = new ActionScheduler_Action( $random, array($random), $schedule );
126
			$store->save_action( $action );
127
		}
128
129
		$claims = array();
130
131
		for ( $i = 0 ; $i < 5 ; $i++ ) {
132
			$claims[] = $store->stake_claim( 5 );
133
		}
134
135
		$actions_run = $runner->run();
136
137
138
		$this->assertEquals( 0, $mock->get_call_count() );
139
		$this->assertEquals( 0, $actions_run );
140
141
		$first = reset($claims);
142
		$store->release_claim( $first );
143
144
		$actions_run = $runner->run();
145
		$this->assertEquals( 10, $mock->get_call_count() );
146
		$this->assertEquals( 10, $actions_run );
147
148
		remove_action( $random, array( $mock, 'action' ) );
149
	}
150
151
	public function test_changing_batch_count_limit() {
152
		$store = ActionScheduler::store();
153
		$runner = new ActionScheduler_QueueRunner( $store );
154
155
		$random = md5(rand());
156
		$schedule = new ActionScheduler_SimpleSchedule(new ActionScheduler_DateTime('1 day ago'));
157
158
		for ( $i = 0 ; $i < 30 ; $i++ ) {
159
			$action = new ActionScheduler_Action( $random, array($random), $schedule );
160
			$store->save_action( $action );
161
		}
162
163
		$claims = array();
164
165
		for ( $i = 0 ; $i < 5 ; $i++ ) {
166
			$claims[] = $store->stake_claim( 5 );
167
		}
168
169
		$mock1 = new MockAction();
170
		add_action( $random, array( $mock1, 'action' ) );
171
		$actions_run = $runner->run();
172
		remove_action( $random, array( $mock1, 'action' ) );
173
174
		$this->assertEquals( 0, $mock1->get_call_count() );
175
		$this->assertEquals( 0, $actions_run );
176
177
178
		add_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'return_6' ) );
179
180
		$mock2 = new MockAction();
181
		add_action( $random, array( $mock2, 'action' ) );
182
		$actions_run = $runner->run();
183
		remove_action( $random, array( $mock2, 'action' ) );
184
185
		$this->assertEquals( 5, $mock2->get_call_count() );
186
		$this->assertEquals( 5, $actions_run );
187
188
		remove_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'return_6' ) );
189
190
		for ( $i = 0 ; $i < 5 ; $i++ ) { // to make up for the actions we just processed
191
			$action = new ActionScheduler_Action( $random, array($random), $schedule );
192
			$store->save_action( $action );
193
		}
194
195
		$mock3 = new MockAction();
196
		add_action( $random, array( $mock3, 'action' ) );
197
		$actions_run = $runner->run();
198
		remove_action( $random, array( $mock3, 'action' ) );
199
200
		$this->assertEquals( 0, $mock3->get_call_count() );
201
		$this->assertEquals( 0, $actions_run );
202
203
		remove_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'return_6' ) );
204
	}
205
206
	public function return_6() {
207
		return 6;
208
	}
209
210
	public function test_store_fetch_action_failure_schedule_next_instance() {
211
		$random    = md5( rand() );
212
		$schedule  = new ActionScheduler_IntervalSchedule( as_get_datetime_object( '12 hours ago' ), DAY_IN_SECONDS );
213
		$action    = new ActionScheduler_Action( $random, array(), $schedule );
214
		$action_id = ActionScheduler::store()->save_action( $action );
215
216
		// Set up a mock store that will throw an exception when fetching actions.
217
		$store = $this
218
					->getMockBuilder( 'ActionScheduler_wpPostStore' )
219
					->setMethods( array( 'fetch_action' ) )
220
					->getMock();
221
		$store
222
			->method( 'fetch_action' )
223
			->with( $action_id )
224
			->will( $this->throwException( new Exception() ) );
225
226
		// Set up a mock queue runner to verify that schedule_next_instance()
227
		// isn't called for an undefined $action.
228
		$runner = $this
229
					->getMockBuilder( 'ActionScheduler_QueueRunner' )
230
					->setConstructorArgs( array( $store ) )
231
					->setMethods( array( 'schedule_next_instance' ) )
232
					->getMock();
233
		$runner
234
			->expects( $this->never() )
235
			->method( 'schedule_next_instance' );
236
237
		$runner->run();
238
239
		// Set up a mock store that will throw an exception when fetching actions.
240
		$store2 = $this
241
					->getMockBuilder( 'ActionScheduler_wpPostStore' )
242
					->setMethods( array( 'fetch_action' ) )
243
					->getMock();
244
		$store2
245
			->method( 'fetch_action' )
246
			->with( $action_id )
247
			->willReturn( null );
248
249
		// Set up a mock queue runner to verify that schedule_next_instance()
250
		// isn't called for an undefined $action.
251
		$runner2 = $this
252
					->getMockBuilder( 'ActionScheduler_QueueRunner' )
253
					->setConstructorArgs( array( $store ) )
254
					->setMethods( array( 'schedule_next_instance' ) )
255
					->getMock();
256
		$runner2
257
			->expects( $this->never() )
258
			->method( 'schedule_next_instance' );
259
260
		$runner2->run();
261
	}
262
}
263