Skip to content

Commit d88f9a6

Browse files
committed
Refactor withConsecutive()
1 parent fb944cd commit d88f9a6

File tree

3 files changed

+86
-40
lines changed

3 files changed

+86
-40
lines changed

tests/controller/acp_controller_test.php

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,32 @@ public function test_action_add($id, $data)
300300
{
301301
$controller = $this->get_controller();
302302

303+
$variable_expectations = [
304+
['action', '', 'add'],
305+
['id', 0, $id]
306+
];
303307
$this->request
304308
->expects(self::exactly(2))
305309
->method('variable')
306-
->withConsecutive(['action', ''], ['id', 0])
307-
->willReturnOnConsecutiveCalls('add', $id);
308-
310+
->willReturnCallback(function($arg1, $arg2) use (&$variable_expectations) {
311+
$expectation = array_shift($variable_expectations);
312+
self::assertEquals($expectation[0], $arg1);
313+
self::assertEquals($expectation[1], $arg2);
314+
return $expectation[2];
315+
});
316+
317+
$post_expectations = [
318+
['submit', false],
319+
['preview', false]
320+
];
309321
$this->request
310322
->expects(self::exactly(2))
311323
->method('is_set_post')
312-
->withConsecutive(['submit'], ['preview'])
313-
->willReturnOnConsecutiveCalls(false, false);
324+
->willReturnCallback(function($arg) use (&$post_expectations) {
325+
$expectation = array_shift($post_expectations);
326+
self::assertEquals($expectation[0], $arg);
327+
return $expectation[1];
328+
});
314329

315330
$this->manager->expects(self::once())
316331
->method('announcement_columns')
@@ -384,31 +399,34 @@ public function test_action_add_submit($id, $form, $preview, $submit, $valid_for
384399
$this->setExpectedTriggerError(E_USER_NOTICE, 'BOARD_ANNOUNCEMENTS_UPDATED');
385400
}
386401

402+
$variable_invocation = 0;
403+
$expected_args = [
404+
['action', ''], ['id', 0], ['board_announcements_text', ''], ['board_announcements_description', ''],
405+
['board_announcements_bgcolor', ''], ['board_announcements_enabled', true], ['board_announcements_users', 0],
406+
['board_announcements_locations', [0]], ['board_announcements_dismiss', true], ['board_announcements_expiry', ''],
407+
['disable_bbcode', false], ['disable_magic_url', false], ['disable_smilies', false]
408+
];
387409
$this->request
388410
->expects(self::exactly(13))
389411
->method('variable')
390-
->withConsecutive(
391-
['action', ''],
392-
['id', 0],
393-
['board_announcements_text', ''],
394-
['board_announcements_description', ''],
395-
['board_announcements_bgcolor', ''],
396-
['board_announcements_enabled', true],
397-
['board_announcements_users', 0],
398-
['board_announcements_locations', [0]],
399-
['board_announcements_dismiss', true],
400-
['board_announcements_expiry', ''],
401-
['disable_bbcode', false],
402-
['disable_magic_url', false],
403-
['disable_smilies', false]
404-
)
405-
->willReturnOnConsecutiveCalls(...$form);
406-
412+
->willReturnCallback(function($arg1, $arg2) use (&$variable_invocation, $expected_args, $form) {
413+
self::assertEquals($expected_args[$variable_invocation][0], $arg1);
414+
self::assertEquals($expected_args[$variable_invocation][1], $arg2);
415+
return $form[$variable_invocation++];
416+
});
417+
418+
$post_expectations = [
419+
['submit', $submit],
420+
['preview', $preview]
421+
];
407422
$this->request
408423
->expects(self::exactly(2))
409424
->method('is_set_post')
410-
->withConsecutive(['submit'], ['preview'])
411-
->willReturnOnConsecutiveCalls($submit, $preview);
425+
->willReturnCallback(function($arg) use (&$post_expectations) {
426+
$expectation = array_shift($post_expectations);
427+
self::assertEquals($expectation[0], $arg);
428+
return $expectation[1];
429+
});
412430

413431
$this->manager->expects($submit && !$errors ? self::never() : self::once())
414432
->method('decode_json')
@@ -462,11 +480,18 @@ public function test_action_delete($id, $confirm_action, $success)
462480

463481
$controller = $this->get_controller();
464482

483+
$variable_invocation = 0;
484+
$expected_calls = $confirm_action ? 2 : 4;
485+
$expected_args = [['action', ''], ['id', 0], ['i', ''], ['mode', '']];
486+
$expected_returns = ['delete', $id, '', ''];
465487
$this->request
466-
->expects(self::exactly($confirm_action ? 2 : 4))
488+
->expects(self::exactly($expected_calls))
467489
->method('variable')
468-
->withConsecutive(['action', ''], ['id', 0], ['i', ''], ['mode', ''])
469-
->willReturnOnConsecutiveCalls('delete', $id, '', '');
490+
->willReturnCallback(function($arg1, $arg2) use (&$variable_invocation, $expected_args, $expected_returns) {
491+
self::assertEquals($expected_args[$variable_invocation][0], $arg1);
492+
self::assertEquals($expected_args[$variable_invocation][1], $arg2);
493+
return $expected_returns[$variable_invocation++];
494+
});
470495

471496
if (!$confirm_action)
472497
{
@@ -553,10 +578,16 @@ public function test_action_move($id, $dir, $valid, $error, $is_ajax)
553578
$this->setExpectedTriggerError(E_WARNING);
554579
}
555580

581+
$variable_invocation = 0;
582+
$expected_args = [['action', ''], ['id', 0], ['dir', ''], ['hash', '']];
583+
$expected_returns = ['move', $id, $dir, ($valid ? generate_link_hash($dir . $id) : '')];
556584
$this->request->expects(self::exactly(4))
557585
->method('variable')
558-
->withConsecutive(['action', ''], ['id', 0], ['dir', ''], ['hash', ''])
559-
->willReturnOnConsecutiveCalls('move', $id, $dir, ($valid ? generate_link_hash($dir . $id) : ''));
586+
->willReturnCallback(function($arg1, $arg2) use (&$variable_invocation, $expected_args, $expected_returns) {
587+
self::assertEquals($expected_args[$variable_invocation][0], $arg1);
588+
self::assertEquals($expected_args[$variable_invocation][1], $arg2);
589+
return $expected_returns[$variable_invocation++];
590+
});
560591

561592
$this->request->expects($valid && !$error ? self::once() : self::never())
562593
->method('is_ajax')
@@ -603,16 +634,25 @@ public function test_action_settings($enable, $valid_form)
603634
$this->setExpectedTriggerError(E_USER_WARNING, 'The submitted form was invalid. Try submitting again.');
604635
$this->request->expects(self::once())
605636
->method('variable')
606-
->withConsecutive(['action', ''])
607-
->willReturnOnConsecutiveCalls('settings');
637+
->with('action', '')
638+
->willReturn('settings');
608639
}
609640
else
610641
{
611642
$this->setExpectedTriggerError(E_USER_NOTICE, 'CONFIG_UPDATED');
612-
$this->request->expects(self::exactly(2))
643+
$variable_expectations = [
644+
['action', '', 'settings'],
645+
['board_announcements_enable_all', 0, $enable]
646+
];
647+
$this->request
648+
->expects(self::exactly(2))
613649
->method('variable')
614-
->withConsecutive(['action', ''], ['board_announcements_enable_all', 0])
615-
->willReturnOnConsecutiveCalls('settings', $enable);
650+
->willReturnCallback(function($arg1, $arg2) use (&$variable_expectations) {
651+
$expectation = array_shift($variable_expectations);
652+
self::assertEquals($expectation[0], $arg1);
653+
self::assertEquals($expectation[1], $arg2);
654+
return $expectation[2];
655+
});
616656
$this->config->expects(self::once())
617657
->method('set')
618658
->with('board_announcements_enable', $enable);

tests/cron/cron_test.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ public function test_run($expired_set)
6060
->with('announcement_id')
6161
->willReturn($expired_set);
6262

63-
array_walk($expired_set, static function (&$value) {
64-
$value = [$value];
65-
});
66-
6763
// Check disable_announcement get called as expected
64+
$invocation = 0;
6865
$this->manager->expects(self::exactly(count($expired_set)))
6966
->method('disable_announcement')
70-
->withConsecutive(...$expired_set);
67+
->willReturnCallback(function($arg) use (&$invocation, $expired_set) {
68+
self::assertEquals($expired_set[$invocation++], $arg);
69+
});
7170

7271
// Run the cron task
7372
$this->cron_task->run();

tests/event/listener_test.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,16 @@ public function test_display_board_announcements($user_id, $page, $enabled, $exp
250250

251251
$this->set_listener();
252252

253+
$invocation = 0;
253254
$this->template->expects($enabled ? self::atLeastOnce() : self::never())
254255
->method('assign_block_vars')
255-
->withConsecutive(...$expected);
256+
->willReturnCallback(function($block, $vars) use (&$invocation, $expected) {
257+
if (isset($expected[$invocation])) {
258+
self::assertEquals($expected[$invocation][0], $block);
259+
self::assertEquals($expected[$invocation][1], $vars);
260+
$invocation++;
261+
}
262+
});
256263

257264
$this->request->expects(self::atMost(10))
258265
->method('variable')

0 commit comments

Comments
 (0)