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 |
||
7 | class ActionScheduler_wpPostStore_Test extends ActionScheduler_UnitTestCase { |
||
8 | |||
9 | public function test_create_action() { |
||
10 | $time = as_get_datetime_object(); |
||
11 | $schedule = new ActionScheduler_SimpleSchedule($time); |
||
12 | $action = new ActionScheduler_Action('my_hook', array(), $schedule); |
||
13 | $store = new ActionScheduler_wpPostStore(); |
||
14 | $action_id = $store->save_action($action); |
||
15 | |||
16 | $this->assertNotEmpty($action_id); |
||
17 | } |
||
18 | |||
19 | public function test_create_action_with_scheduled_date() { |
||
20 | $time = as_get_datetime_object( strtotime( '-1 week' ) ); |
||
21 | $action = new ActionScheduler_Action( 'my_hook', array(), new ActionScheduler_SimpleSchedule( $time ) ); |
||
22 | $store = new ActionScheduler_wpPostStore(); |
||
23 | |||
24 | $action_id = $store->save_action( $action, $time ); |
||
25 | $action_date = $store->get_date( $action_id ); |
||
26 | |||
27 | $this->assertEquals( $time->getTimestamp(), $action_date->getTimestamp() ); |
||
28 | } |
||
29 | |||
30 | public function test_retrieve_action() { |
||
31 | $time = as_get_datetime_object(); |
||
32 | $schedule = new ActionScheduler_SimpleSchedule($time); |
||
33 | $action = new ActionScheduler_Action('my_hook', array(), $schedule, 'my_group'); |
||
34 | $store = new ActionScheduler_wpPostStore(); |
||
35 | $action_id = $store->save_action($action); |
||
36 | |||
37 | $retrieved = $store->fetch_action($action_id); |
||
38 | $this->assertEquals($action->get_hook(), $retrieved->get_hook()); |
||
39 | $this->assertEqualSets($action->get_args(), $retrieved->get_args()); |
||
40 | $this->assertEquals($action->get_schedule()->next()->getTimestamp(), $retrieved->get_schedule()->next()->getTimestamp()); |
||
41 | $this->assertEquals($action->get_group(), $retrieved->get_group()); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @expectedException ActionScheduler_InvalidActionException |
||
46 | * @dataProvider provide_bad_args |
||
47 | * |
||
48 | * @param string $content |
||
49 | */ |
||
50 | public function test_action_bad_args( $content ) { |
||
51 | $store = new ActionScheduler_wpPostStore(); |
||
52 | $post_id = wp_insert_post( array( |
||
53 | 'post_type' => ActionScheduler_wpPostStore::POST_TYPE, |
||
54 | 'post_status' => ActionScheduler_Store::STATUS_PENDING, |
||
55 | 'post_content' => $content, |
||
56 | ) ); |
||
57 | |||
58 | $store->fetch_action( $post_id ); |
||
59 | } |
||
60 | |||
61 | public function provide_bad_args() { |
||
62 | return array( |
||
63 | array( '{"bad_json":true}}' ), |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | public function test_cancel_action() { |
||
68 | $time = as_get_datetime_object(); |
||
69 | $schedule = new ActionScheduler_SimpleSchedule($time); |
||
70 | $action = new ActionScheduler_Action('my_hook', array(), $schedule, 'my_group'); |
||
71 | $store = new ActionScheduler_wpPostStore(); |
||
72 | $action_id = $store->save_action($action); |
||
73 | $store->cancel_action( $action_id ); |
||
74 | |||
75 | $fetched = $store->fetch_action( $action_id ); |
||
76 | $this->assertInstanceOf( 'ActionScheduler_CanceledAction', $fetched ); |
||
77 | } |
||
78 | |||
79 | public function test_claim_actions() { |
||
80 | $created_actions = array(); |
||
81 | $store = new ActionScheduler_wpPostStore(); |
||
82 | for ( $i = 3 ; $i > -3 ; $i-- ) { |
||
83 | $time = as_get_datetime_object($i.' hours'); |
||
84 | $schedule = new ActionScheduler_SimpleSchedule($time); |
||
85 | $action = new ActionScheduler_Action('my_hook', array($i), $schedule, 'my_group'); |
||
86 | $created_actions[] = $store->save_action($action); |
||
87 | } |
||
88 | |||
89 | $claim = $store->stake_claim(); |
||
90 | $this->assertInstanceof( 'ActionScheduler_ActionClaim', $claim ); |
||
91 | |||
92 | $this->assertCount( 3, $claim->get_actions() ); |
||
93 | $this->assertEqualSets( array_slice( $created_actions, 3, 3 ), $claim->get_actions() ); |
||
94 | } |
||
95 | |||
96 | public function test_claim_actions_order() { |
||
97 | $store = new ActionScheduler_wpPostStore(); |
||
98 | $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '-1 hour' ) ); |
||
99 | $created_actions = array( |
||
100 | $store->save_action( new ActionScheduler_Action( 'my_hook', array( 1 ), $schedule, 'my_group' ) ), |
||
101 | $store->save_action( new ActionScheduler_Action( 'my_hook', array( 1 ), $schedule, 'my_group' ) ), |
||
102 | ); |
||
103 | |||
104 | $claim = $store->stake_claim(); |
||
105 | $this->assertInstanceof( 'ActionScheduler_ActionClaim', $claim ); |
||
106 | |||
107 | // Verify uniqueness of action IDs. |
||
108 | $this->assertEquals( 2, count( array_unique( $created_actions ) ) ); |
||
109 | |||
110 | // Verify the count and order of the actions. |
||
111 | $claimed_actions = $claim->get_actions(); |
||
112 | $this->assertCount( 2, $claimed_actions ); |
||
113 | $this->assertEquals( $created_actions, $claimed_actions ); |
||
114 | |||
115 | // Verify the reversed order doesn't pass. |
||
116 | $reversed_actions = array_reverse( $created_actions ); |
||
117 | $this->assertNotEquals( $reversed_actions, $claimed_actions ); |
||
118 | } |
||
119 | |||
120 | public function test_duplicate_claim() { |
||
121 | $created_actions = array(); |
||
122 | $store = new ActionScheduler_wpPostStore(); |
||
123 | for ( $i = 0 ; $i > -3 ; $i-- ) { |
||
124 | $time = as_get_datetime_object($i.' hours'); |
||
125 | $schedule = new ActionScheduler_SimpleSchedule($time); |
||
126 | $action = new ActionScheduler_Action('my_hook', array($i), $schedule, 'my_group'); |
||
127 | $created_actions[] = $store->save_action($action); |
||
128 | } |
||
129 | |||
130 | $claim1 = $store->stake_claim(); |
||
131 | $claim2 = $store->stake_claim(); |
||
132 | $this->assertCount( 3, $claim1->get_actions() ); |
||
133 | $this->assertCount( 0, $claim2->get_actions() ); |
||
134 | } |
||
135 | |||
136 | public function test_release_claim() { |
||
137 | $created_actions = array(); |
||
138 | $store = new ActionScheduler_wpPostStore(); |
||
139 | for ( $i = 0 ; $i > -3 ; $i-- ) { |
||
140 | $time = as_get_datetime_object($i.' hours'); |
||
141 | $schedule = new ActionScheduler_SimpleSchedule($time); |
||
142 | $action = new ActionScheduler_Action('my_hook', array($i), $schedule, 'my_group'); |
||
143 | $created_actions[] = $store->save_action($action); |
||
144 | } |
||
145 | |||
146 | $claim1 = $store->stake_claim(); |
||
147 | |||
148 | $store->release_claim( $claim1 ); |
||
149 | |||
150 | $claim2 = $store->stake_claim(); |
||
151 | $this->assertCount( 3, $claim2->get_actions() ); |
||
152 | } |
||
153 | |||
154 | public function test_search() { |
||
155 | $created_actions = array(); |
||
156 | $store = new ActionScheduler_wpPostStore(); |
||
157 | for ( $i = -3 ; $i <= 3 ; $i++ ) { |
||
158 | $time = as_get_datetime_object($i.' hours'); |
||
159 | $schedule = new ActionScheduler_SimpleSchedule($time); |
||
160 | $action = new ActionScheduler_Action('my_hook', array($i), $schedule, 'my_group'); |
||
161 | $created_actions[] = $store->save_action($action); |
||
162 | } |
||
163 | |||
164 | $next_no_args = $store->find_action( 'my_hook' ); |
||
165 | $this->assertEquals( $created_actions[0], $next_no_args ); |
||
166 | |||
167 | $next_with_args = $store->find_action( 'my_hook', array( 'args' => array( 1 ) ) ); |
||
168 | $this->assertEquals( $created_actions[4], $next_with_args ); |
||
169 | |||
170 | $non_existent = $store->find_action( 'my_hook', array( 'args' => array( 17 ) ) ); |
||
171 | $this->assertNull( $non_existent ); |
||
172 | } |
||
173 | |||
174 | public function test_search_by_group() { |
||
175 | $store = new ActionScheduler_wpPostStore(); |
||
176 | $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('tomorrow')); |
||
177 | $abc = $store->save_action(new ActionScheduler_Action('my_hook', array(1), $schedule, 'abc')); |
||
178 | $def = $store->save_action(new ActionScheduler_Action('my_hook', array(1), $schedule, 'def')); |
||
179 | $ghi = $store->save_action(new ActionScheduler_Action('my_hook', array(1), $schedule, 'ghi')); |
||
180 | |||
181 | $this->assertEquals( $abc, $store->find_action('my_hook', array('group' => 'abc'))); |
||
182 | $this->assertEquals( $def, $store->find_action('my_hook', array('group' => 'def'))); |
||
183 | $this->assertEquals( $ghi, $store->find_action('my_hook', array('group' => 'ghi'))); |
||
184 | } |
||
185 | |||
186 | public function test_post_author() { |
||
187 | $current_user = get_current_user_id(); |
||
188 | |||
189 | $time = as_get_datetime_object(); |
||
190 | $schedule = new ActionScheduler_SimpleSchedule($time); |
||
191 | $action = new ActionScheduler_Action('my_hook', array(), $schedule); |
||
192 | $store = new ActionScheduler_wpPostStore(); |
||
193 | $action_id = $store->save_action($action); |
||
194 | |||
195 | $post = get_post($action_id); |
||
196 | $this->assertEquals(0, $post->post_author); |
||
197 | |||
198 | $new_user = $this->factory->user->create_object(array( |
||
199 | 'user_login' => __FUNCTION__, |
||
200 | 'user_pass' => md5(rand()), |
||
201 | )); |
||
202 | wp_set_current_user( $new_user ); |
||
203 | |||
204 | |||
205 | $schedule = new ActionScheduler_SimpleSchedule($time); |
||
206 | $action = new ActionScheduler_Action('my_hook', array(), $schedule); |
||
207 | $action_id = $store->save_action($action); |
||
208 | $post = get_post($action_id); |
||
209 | $this->assertEquals(0, $post->post_author); |
||
210 | |||
211 | wp_set_current_user($current_user); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @issue 13 |
||
216 | */ |
||
217 | public function test_post_status_for_recurring_action() { |
||
218 | $time = as_get_datetime_object('10 minutes'); |
||
219 | $schedule = new ActionScheduler_IntervalSchedule($time, HOUR_IN_SECONDS); |
||
220 | $action = new ActionScheduler_Action('my_hook', array(), $schedule); |
||
221 | $store = new ActionScheduler_wpPostStore(); |
||
222 | $action_id = $store->save_action($action); |
||
223 | |||
224 | $action = $store->fetch_action($action_id); |
||
225 | $action->execute(); |
||
226 | $store->mark_complete( $action_id ); |
||
227 | |||
228 | $next = $action->get_schedule()->next( as_get_datetime_object() ); |
||
229 | $new_action_id = $store->save_action( $action, $next ); |
||
230 | |||
231 | $this->assertEquals('publish', get_post_status($action_id)); |
||
232 | $this->assertEquals('pending', get_post_status($new_action_id)); |
||
233 | } |
||
234 | |||
235 | public function test_get_run_date() { |
||
236 | $time = as_get_datetime_object('-10 minutes'); |
||
237 | $schedule = new ActionScheduler_IntervalSchedule($time, HOUR_IN_SECONDS); |
||
238 | $action = new ActionScheduler_Action('my_hook', array(), $schedule); |
||
239 | $store = new ActionScheduler_wpPostStore(); |
||
240 | $action_id = $store->save_action($action); |
||
241 | |||
242 | $this->assertEquals( $store->get_date($action_id)->getTimestamp(), $time->getTimestamp() ); |
||
243 | |||
244 | $action = $store->fetch_action($action_id); |
||
245 | $action->execute(); |
||
246 | $now = as_get_datetime_object(); |
||
247 | $store->mark_complete( $action_id ); |
||
248 | |||
249 | $this->assertEquals( $store->get_date($action_id)->getTimestamp(), $now->getTimestamp() ); |
||
250 | |||
251 | $next = $action->get_schedule()->next( $now ); |
||
252 | $new_action_id = $store->save_action( $action, $next ); |
||
253 | |||
254 | $this->assertEquals( (int)($now->getTimestamp()) + HOUR_IN_SECONDS, $store->get_date($new_action_id)->getTimestamp() ); |
||
255 | } |
||
256 | |||
257 | public function test_get_status() { |
||
258 | $time = as_get_datetime_object('-10 minutes'); |
||
259 | $schedule = new ActionScheduler_IntervalSchedule($time, HOUR_IN_SECONDS); |
||
260 | $action = new ActionScheduler_Action('my_hook', array(), $schedule); |
||
261 | $store = new ActionScheduler_wpPostStore(); |
||
262 | $action_id = $store->save_action($action); |
||
263 | |||
264 | $this->assertEquals( ActionScheduler_Store::STATUS_PENDING, $store->get_status( $action_id ) ); |
||
265 | |||
266 | $store->mark_complete( $action_id ); |
||
267 | $this->assertEquals( ActionScheduler_Store::STATUS_COMPLETE, $store->get_status( $action_id ) ); |
||
268 | |||
269 | $store->mark_failure( $action_id ); |
||
270 | $this->assertEquals( ActionScheduler_Store::STATUS_FAILED, $store->get_status( $action_id ) ); |
||
271 | } |
||
272 | |||
273 | public function test_claim_actions_by_hooks() { |
||
274 | $hook1 = __FUNCTION__ . '_hook_1'; |
||
275 | $hook2 = __FUNCTION__ . '_hook_2'; |
||
276 | $store = new ActionScheduler_wpPostStore(); |
||
277 | $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '-1 hour' ) ); |
||
278 | |||
279 | $action1 = $store->save_action( new ActionScheduler_Action( $hook1, array(), $schedule ) ); |
||
280 | $action2 = $store->save_action( new ActionScheduler_Action( $hook2, array(), $schedule ) ); |
||
281 | |||
282 | // Claiming no hooks should include all actions. |
||
283 | $claim = $store->stake_claim( 10 ); |
||
284 | $this->assertEquals( 2, count( $claim->get_actions() ) ); |
||
285 | $this->assertTrue( in_array( $action1, $claim->get_actions() ) ); |
||
286 | $this->assertTrue( in_array( $action2, $claim->get_actions() ) ); |
||
287 | $store->release_claim( $claim ); |
||
288 | |||
289 | // Claiming a hook should claim only actions with that hook |
||
290 | $claim = $store->stake_claim( 10, null, array( $hook1 ) ); |
||
291 | $this->assertEquals( 1, count( $claim->get_actions() ) ); |
||
292 | $this->assertTrue( in_array( $action1, $claim->get_actions() ) ); |
||
293 | $store->release_claim( $claim ); |
||
294 | |||
295 | // Claiming two hooks should claim actions with either of those hooks |
||
296 | $claim = $store->stake_claim( 10, null, array( $hook1, $hook2 ) ); |
||
297 | $this->assertEquals( 2, count( $claim->get_actions() ) ); |
||
298 | $this->assertTrue( in_array( $action1, $claim->get_actions() ) ); |
||
299 | $this->assertTrue( in_array( $action2, $claim->get_actions() ) ); |
||
300 | $store->release_claim( $claim ); |
||
301 | |||
302 | // Claiming two hooks should claim actions with either of those hooks |
||
303 | $claim = $store->stake_claim( 10, null, array( __METHOD__ . '_hook_3' ) ); |
||
304 | $this->assertEquals( 0, count( $claim->get_actions() ) ); |
||
305 | $this->assertFalse( in_array( $action1, $claim->get_actions() ) ); |
||
306 | $this->assertFalse( in_array( $action2, $claim->get_actions() ) ); |
||
307 | $store->release_claim( $claim ); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @issue 121 |
||
312 | */ |
||
313 | public function test_claim_actions_by_group() { |
||
314 | $group1 = md5( rand() ); |
||
315 | $store = new ActionScheduler_wpPostStore(); |
||
316 | $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '-1 hour' ) ); |
||
317 | |||
318 | $action1 = $store->save_action( new ActionScheduler_Action( __METHOD__, array(), $schedule, $group1 ) ); |
||
319 | $action2 = $store->save_action( new ActionScheduler_Action( __METHOD__, array(), $schedule ) ); |
||
320 | |||
321 | // Claiming no group should include all actions. |
||
322 | $claim = $store->stake_claim( 10 ); |
||
323 | $this->assertEquals( 2, count( $claim->get_actions() ) ); |
||
324 | $this->assertTrue( in_array( $action1, $claim->get_actions() ) ); |
||
325 | $this->assertTrue( in_array( $action2, $claim->get_actions() ) ); |
||
326 | $store->release_claim( $claim ); |
||
327 | |||
328 | // Claiming a group should claim only actions in that group. |
||
329 | $claim = $store->stake_claim( 10, null, array(), $group1 ); |
||
330 | $this->assertEquals( 1, count( $claim->get_actions() ) ); |
||
331 | $this->assertTrue( in_array( $action1, $claim->get_actions() ) ); |
||
332 | $store->release_claim( $claim ); |
||
333 | } |
||
334 | |||
335 | public function test_claim_actions_by_hook_and_group() { |
||
336 | $hook1 = __FUNCTION__ . '_hook_1'; |
||
337 | $hook2 = __FUNCTION__ . '_hook_2'; |
||
338 | $hook3 = __FUNCTION__ . '_hook_3'; |
||
339 | $group1 = 'group_' . md5( rand() ); |
||
340 | $group2 = 'group_' . md5( rand() ); |
||
341 | $store = new ActionScheduler_wpPostStore(); |
||
342 | $schedule = new ActionScheduler_SimpleSchedule( as_get_datetime_object( '-1 hour' ) ); |
||
343 | |||
344 | $action1 = $store->save_action( new ActionScheduler_Action( $hook1, array(), $schedule, $group1 ) ); |
||
345 | $action2 = $store->save_action( new ActionScheduler_Action( $hook2, array(), $schedule ) ); |
||
346 | $action3 = $store->save_action( new ActionScheduler_Action( $hook3, array(), $schedule, $group2 ) ); |
||
347 | |||
348 | // Claiming no hooks or group should include all actions. |
||
349 | $claim = $store->stake_claim( 10 ); |
||
350 | $this->assertEquals( 3, count( $claim->get_actions() ) ); |
||
351 | $this->assertTrue( in_array( $action1, $claim->get_actions() ) ); |
||
352 | $this->assertTrue( in_array( $action2, $claim->get_actions() ) ); |
||
353 | $store->release_claim( $claim ); |
||
354 | |||
355 | // Claiming a group and hook should claim only actions in that group. |
||
356 | $claim = $store->stake_claim( 10, null, array( $hook1 ), $group1 ); |
||
357 | $this->assertEquals( 1, count( $claim->get_actions() ) ); |
||
358 | $this->assertTrue( in_array( $action1, $claim->get_actions() ) ); |
||
359 | $store->release_claim( $claim ); |
||
360 | |||
361 | // Claiming a group and hook should claim only actions with that hook in that group. |
||
362 | $claim = $store->stake_claim( 10, null, array( $hook2 ), $group1 ); |
||
363 | $this->assertEquals( 0, count( $claim->get_actions() ) ); |
||
364 | $this->assertFalse( in_array( $action1, $claim->get_actions() ) ); |
||
365 | $this->assertFalse( in_array( $action2, $claim->get_actions() ) ); |
||
366 | $store->release_claim( $claim ); |
||
367 | |||
368 | // Claiming a group and hook should claim only actions with that hook in that group. |
||
369 | $claim = $store->stake_claim( 10, null, array( $hook1, $hook2 ), $group2 ); |
||
370 | $this->assertEquals( 0, count( $claim->get_actions() ) ); |
||
371 | $this->assertFalse( in_array( $action1, $claim->get_actions() ) ); |
||
372 | $this->assertFalse( in_array( $action2, $claim->get_actions() ) ); |
||
373 | $store->release_claim( $claim ); |
||
374 | } |
||
375 | } |
||
376 |