Skip to content

Commit 3dca34c

Browse files
committed
Refactor withConsecutive()
1 parent 7e4e820 commit 3dca34c

File tree

2 files changed

+108
-69
lines changed

2 files changed

+108
-69
lines changed

tests/controller/display_autogroups_test.php

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,60 +22,81 @@ public function test_display_autogroups()
2222

2323
// Mocked manager should output the expected values
2424
// from get_condition_lang() at the expected times.
25+
$manager_expectations = [
26+
['phpbb.autogroups.type.sample1', 'phpbb.autogroups.type.sample1'],
27+
['phpbb.autogroups.type.sample2', 'phpbb.autogroups.type.sample2']
28+
];
2529
$this->manager->expects(self::exactly(2))
2630
->method('get_condition_lang')
27-
->withConsecutive(['phpbb.autogroups.type.sample1'], ['phpbb.autogroups.type.sample2'])
28-
->willReturnOnConsecutiveCalls('phpbb.autogroups.type.sample1', 'phpbb.autogroups.type.sample2');
31+
->willReturnCallback(function($arg) use (&$manager_expectations) {
32+
$expectation = array_shift($manager_expectations);
33+
self::assertEquals($expectation[0], $arg);
34+
return $expectation[1];
35+
});
2936

3037
// Set expectations for the assign_block_vars template values
38+
$template_invocation = 0;
3139
$this->template->expects(self::exactly(4))
3240
->method('assign_block_vars')
33-
->withConsecutive(
34-
array('autogroups', array(
35-
'GROUP_NAME' => 'GROUP1',
36-
'CONDITION_NAME' => 'phpbb.autogroups.type.sample1',
37-
'MIN_VALUE' => 0,
38-
'MAX_VALUE' => 20,
39-
'S_DEFAULT' => 1,
40-
'S_NOTIFY' => 0,
41-
'EXCLUDED_GROUPS' => '',
42-
'U_EDIT' => 'index.php&action=edit&autogroups_id=1',
43-
'U_DELETE' => 'index.php&action=delete&autogroups_id=1',
44-
'U_SYNC' => 'index.php&action=sync&autogroups_id=1&hash=' . generate_link_hash('sync' . 1),
45-
)),
46-
array('autogroups', array(
47-
'GROUP_NAME' => 'GROUP2',
48-
'CONDITION_NAME' => 'phpbb.autogroups.type.sample2',
49-
'MIN_VALUE' => 30,
50-
'MAX_VALUE' => 50,
51-
'S_DEFAULT' => 0,
52-
'S_NOTIFY' => 0,
53-
'EXCLUDED_GROUPS' => '',
54-
'U_EDIT' => 'index.php&action=edit&autogroups_id=2',
55-
'U_DELETE' => 'index.php&action=delete&autogroups_id=2',
56-
'U_SYNC' => 'index.php&action=sync&autogroups_id=2&hash=' . generate_link_hash('sync' . 2),
57-
)),
58-
array('groups', array(
59-
'GROUP_ID' => 1,
60-
'GROUP_NAME' => 'GROUP1',
61-
'S_SELECTED' => false,
62-
)),
63-
array('groups', array(
64-
'GROUP_ID' => 2,
65-
'GROUP_NAME' => 'GROUP2',
66-
'S_SELECTED' => true,
67-
))
68-
)
69-
;
41+
->willReturnCallback(function($block, $vars) use (&$template_invocation) {
42+
switch ($template_invocation) {
43+
case 0:
44+
self::assertEquals('autogroups', $block);
45+
self::assertEquals(array(
46+
'GROUP_NAME' => 'GROUP1',
47+
'CONDITION_NAME' => 'phpbb.autogroups.type.sample1',
48+
'MIN_VALUE' => '0',
49+
'MAX_VALUE' => '20',
50+
'S_DEFAULT' => '1',
51+
'S_NOTIFY' => '0',
52+
'EXCLUDED_GROUPS' => '',
53+
'U_EDIT' => 'index.php&action=edit&autogroups_id=1',
54+
'U_DELETE' => 'index.php&action=delete&autogroups_id=1',
55+
'U_SYNC' => 'index.php&action=sync&autogroups_id=1&hash=' . generate_link_hash('sync' . 1),
56+
), $vars);
57+
break;
58+
case 1:
59+
self::assertEquals('autogroups', $block);
60+
self::assertEquals(array(
61+
'GROUP_NAME' => 'GROUP2',
62+
'CONDITION_NAME' => 'phpbb.autogroups.type.sample2',
63+
'MIN_VALUE' => '30',
64+
'MAX_VALUE' => '50',
65+
'S_DEFAULT' => '0',
66+
'S_NOTIFY' => '0',
67+
'EXCLUDED_GROUPS' => '',
68+
'U_EDIT' => 'index.php&action=edit&autogroups_id=2',
69+
'U_DELETE' => 'index.php&action=delete&autogroups_id=2',
70+
'U_SYNC' => 'index.php&action=sync&autogroups_id=2&hash=' . generate_link_hash('sync' . 2),
71+
), $vars);
72+
break;
73+
case 2:
74+
self::assertEquals('groups', $block);
75+
self::assertEquals(array(
76+
'GROUP_ID' => 1,
77+
'GROUP_NAME' => 'GROUP1',
78+
'S_SELECTED' => false,
79+
), $vars);
80+
break;
81+
case 3:
82+
self::assertEquals('groups', $block);
83+
self::assertEquals(array(
84+
'GROUP_ID' => 2,
85+
'GROUP_NAME' => 'GROUP2',
86+
'S_SELECTED' => true,
87+
), $vars);
88+
break;
89+
}
90+
$template_invocation++;
91+
});
7092

7193
// Set expectations for the assign_vars template values
7294
$this->template->expects(self::once())
7395
->method('assign_vars')
7496
->with(array(
7597
'U_ACTION' => 'index.php',
7698
'U_ADD_AUTOGROUP_RULE' => 'index.php&action=add',
77-
))
78-
;
99+
));
79100

80101
// Set a u_index test value
81102
$this->admin_controller->set_page_url('index.php');

tests/controller/save_autogroup_rule_test.php

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,54 @@ public function test_save_autogroup_rule($id, $min, $max, $default, $notify, $gr
5656
->willReturn('');
5757

5858
// Set expectations for the assign_block_vars template values
59+
$invocation = 0;
5960
$this->template->expects(self::exactly(5))
6061
->method('assign_block_vars')
61-
->withConsecutive(
62-
array('excluded_groups', array(
63-
'GROUP_ID' => 1,
64-
'GROUP_NAME' => 'GROUP1',
65-
'S_SELECTED' => false,
66-
)),
67-
array('excluded_groups', array(
68-
'GROUP_ID' => 2,
69-
'GROUP_NAME' => 'GROUP2',
70-
'S_SELECTED' => false,
71-
)),
72-
array('groups', array(
73-
'GROUP_ID' => 2,
74-
'GROUP_NAME' => 'GROUP2',
75-
'S_SELECTED' => $group_selected,
76-
)),
77-
array('conditions', array(
78-
'CONDITION_ID' => 1,
79-
'CONDITION_NAME' => '',
80-
'S_SELECTED' => $cond1_selected,
81-
)),
82-
array('conditions', array(
83-
'CONDITION_ID' => 2,
84-
'CONDITION_NAME' => '',
85-
'S_SELECTED' => $cond2_selected,
86-
))
87-
)
88-
;
62+
->willReturnCallback(function($block, $vars) use (&$invocation, $group_selected, $cond1_selected, $cond2_selected) {
63+
switch ($invocation) {
64+
case 0:
65+
self::assertEquals('excluded_groups', $block);
66+
self::assertEquals(array(
67+
'GROUP_ID' => 1,
68+
'GROUP_NAME' => 'GROUP1',
69+
'S_SELECTED' => false,
70+
), $vars);
71+
break;
72+
case 1:
73+
self::assertEquals('excluded_groups', $block);
74+
self::assertEquals(array(
75+
'GROUP_ID' => 2,
76+
'GROUP_NAME' => 'GROUP2',
77+
'S_SELECTED' => false,
78+
), $vars);
79+
break;
80+
case 2:
81+
self::assertEquals('groups', $block);
82+
self::assertEquals(array(
83+
'GROUP_ID' => 2,
84+
'GROUP_NAME' => 'GROUP2',
85+
'S_SELECTED' => $group_selected,
86+
), $vars);
87+
break;
88+
case 3:
89+
self::assertEquals('conditions', $block);
90+
self::assertEquals(array(
91+
'CONDITION_ID' => 1,
92+
'CONDITION_NAME' => '',
93+
'S_SELECTED' => $cond1_selected,
94+
), $vars);
95+
break;
96+
case 4:
97+
self::assertEquals('conditions', $block);
98+
self::assertEquals(array(
99+
'CONDITION_ID' => 2,
100+
'CONDITION_NAME' => '',
101+
'S_SELECTED' => $cond2_selected,
102+
), $vars);
103+
break;
104+
}
105+
$invocation++;
106+
});
89107

90108
// Set expectations for the assign_vars template values
91109
$this->template->expects(self::once())

0 commit comments

Comments
 (0)