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 |
||
| 17 | class phpbbdirectory_cats_test extends controller_base |
||
| 18 | { |
||
| 19 | public function getDataSet() |
||
| 20 | { |
||
| 21 | return $this->createXMLDataSet(__DIR__ . '/fixtures/fixture_categories.xml'); |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Setup test environment |
||
| 26 | */ |
||
| 27 | public function setUp() |
||
| 28 | { |
||
| 29 | parent::setUp(); |
||
| 30 | |||
| 31 | $this->config['dir_default_order'] = 't d'; |
||
| 32 | $this->config['dir_banner_width'] = 1; |
||
| 33 | $this->config['dir_banner_height'] = 1; |
||
| 34 | $this->config['dir_show'] = 5; |
||
| 35 | $this->config['dir_activ_flag'] = 1; |
||
| 36 | $this->config['dir_activ_rss'] = 1; |
||
| 37 | $this->config['dir_activ_thumb'] = 1; |
||
| 38 | $this->config['dir_activ_thumb_remote'] = 1; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function get_controller($user_id = 1) |
||
| 42 | { |
||
| 43 | global $table_categories, $tables_comments, $tables_links, $tables_votes, $tables_watch; |
||
| 44 | global $phpEx, $phpbb_root_path; |
||
| 45 | |||
| 46 | $this->user->data['user_id'] = $user_id; |
||
| 47 | $this->user->data['is_registered'] = ($this->user->data['user_id'] != ANONYMOUS && ($this->user->data['user_type'] == USER_NORMAL || $this->user->data['user_type'] == USER_FOUNDER)) ? true : false; |
||
| 48 | |||
| 49 | $controller = new \ernadoo\phpbbdirectory\controller\categories( |
||
| 50 | $this->db, |
||
| 51 | $this->config, |
||
| 52 | $this->lang, |
||
| 53 | $this->template, |
||
| 54 | $this->user, |
||
| 55 | $this->helper, |
||
| 56 | $this->request, |
||
| 57 | $this->auth, |
||
| 58 | $this->pagination, |
||
| 59 | $this->core_categorie, |
||
| 60 | $this->core_link |
||
| 61 | ); |
||
| 62 | $controller->set_tables($table_categories, $tables_comments, $tables_links, $tables_votes, $tables_watch); |
||
| 63 | $controller->set_path_helper($this->phpbb_path_helper); |
||
| 64 | $controller->set_extension_manager($this->phpbb_extension_manager); |
||
| 65 | |||
| 66 | return $controller; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Test data for the test_display_cat_by_id() function |
||
| 71 | * |
||
| 72 | * @return array Array of test data |
||
| 73 | */ |
||
| 74 | public function display_cat_by_id_data() |
||
| 75 | { |
||
| 76 | return array( |
||
| 77 | array(1, 1, 301), // old viewable cat |
||
| 78 | ); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Test controller display |
||
| 83 | * |
||
| 84 | * @dataProvider display_cat_by_id_data |
||
| 85 | */ |
||
| 86 | public function test_display_cat_by_id($cat_id, $page, $status_code) |
||
| 87 | { |
||
| 88 | $controller = $this->get_controller(); |
||
| 89 | $response = $controller->view($cat_id, $page); |
||
| 90 | |||
| 91 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
||
| 92 | $this->assertEquals($status_code, $response->getStatusCode()); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Test data for the test_display_cat_by_route() function |
||
| 97 | * |
||
| 98 | * @return array Array of test data |
||
| 99 | */ |
||
| 100 | public function display_cat_by_route_data() |
||
| 101 | { |
||
| 102 | return array( |
||
| 103 | array(1, 1, 200, 'view_cat.html'), // viewable cat |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Test controller display |
||
| 109 | * |
||
| 110 | * @dataProvider display_cat_by_route_data |
||
| 111 | */ |
||
| 112 | public function test_display_cat_by_route($cat_id, $page, $status_code, $page_content) |
||
| 113 | { |
||
| 114 | $controller = $this->get_controller(); |
||
| 115 | $response = $controller->view_route($cat_id, $page); |
||
| 116 | |||
| 117 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
||
| 118 | $this->assertEquals($status_code, $response->getStatusCode()); |
||
| 119 | $this->assertEquals($page_content, $response->getContent()); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Test data for the test_display_cat_fails() function |
||
| 124 | * |
||
| 125 | * @return array Array of test data |
||
| 126 | */ |
||
| 127 | public function display_cat_fails_error() |
||
| 128 | { |
||
| 129 | return array( |
||
| 130 | array(5, 1, 404, 'DIR_ERROR_NO_CATS'), |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Test controller display throws 404 exceptions |
||
| 136 | * |
||
| 137 | * @dataProvider display_cat_fails_error |
||
| 138 | */ |
||
| 139 | public function test_display_cat_fails($cat_id, $page, $status_code, $page_content) |
||
| 140 | { |
||
| 141 | $controller = $this->get_controller(); |
||
| 142 | |||
| 143 | try |
||
| 144 | { |
||
| 145 | $controller->view($cat_id, $page); |
||
| 146 | $this->fail('The expected \phpbb\exception\http_exception was not thrown'); |
||
| 147 | } |
||
| 148 | catch (\phpbb\exception\http_exception $exception) |
||
| 149 | { |
||
| 150 | $this->assertEquals($status_code, $exception->getStatusCode()); |
||
| 151 | $this->assertEquals($page_content, $exception->getMessage()); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Test data for the test_category_one_page() function |
||
| 157 | * |
||
| 158 | * @return array Array of test data |
||
| 159 | */ |
||
| 160 | public function category_one_page_data() |
||
| 161 | { |
||
| 162 | return array( |
||
| 163 | array(3, 1, '200', 'view_cat.html'), |
||
| 164 | array(3, 2, '200', 'view_cat.html'), |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Test base case scenario |
||
| 170 | * |
||
| 171 | * @dataProvider category_one_page_data |
||
| 172 | */ |
||
| 173 | function test_category_one_page($cat_id, $user_id, $status_code, $page_content) |
||
| 174 | { |
||
| 175 | $user_data = $this->auth->obtain_user_data($user_id); |
||
| 176 | $this->auth->acl($user_data); |
||
| 177 | |||
| 178 | $controller = $this->get_controller($user_id); |
||
| 179 | $response = $controller->view_route($cat_id); |
||
| 180 | |||
| 181 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
||
| 182 | $this->assertEquals($status_code, $response->getStatusCode()); |
||
| 183 | $this->assertEquals($page_content, $response->getContent()); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Test data for the test_category_no_links() function |
||
| 188 | * |
||
| 189 | * @return array Array of test data |
||
| 190 | */ |
||
| 191 | public function category_no_links_data() |
||
| 192 | { |
||
| 193 | return array( |
||
| 194 | array(1, 200, 'view_cat.html'), |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Test base case scenario |
||
| 200 | * |
||
| 201 | * @dataProvider category_no_links_data |
||
| 202 | */ |
||
| 203 | function test_category_no_links($cat_id, $status_code, $page_content) |
||
| 204 | { |
||
| 205 | $this->template->expects($this->at(3)) |
||
| 206 | ->method('assign_block_vars') |
||
| 207 | ->withConsecutive( |
||
| 208 | array('no_draw_link') |
||
| 209 | ); |
||
| 210 | |||
| 211 | $controller = $this->get_controller(); |
||
| 212 | $response = $controller->view_route($cat_id); |
||
| 213 | |||
| 214 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
||
| 215 | $this->assertEquals($status_code, $response->getStatusCode()); |
||
| 216 | $this->assertEquals($page_content, $response->getContent()); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Test data for the test_category_with_pages() function |
||
| 221 | * |
||
| 222 | * @return array Array of test data |
||
| 223 | */ |
||
| 224 | public function category_with_pages_data() |
||
| 225 | { |
||
| 226 | return array( |
||
| 227 | array(2, 1, 1, 200, 'view_cat.html'), |
||
| 228 | array(2, 1, 2, 200, 'view_cat.html'), |
||
| 229 | array(2, 2, 2, 200, 'view_cat.html'), |
||
| 230 | ); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Test base case scenario |
||
| 235 | * |
||
| 236 | * @dataProvider category_with_pages_data |
||
| 237 | */ |
||
| 238 | public function test_category_with_pages($cat_id, $page, $user_id, $status_code, $page_content) |
||
| 239 | { |
||
| 240 | $user_data = $this->auth->obtain_user_data($user_id); |
||
| 241 | $this->auth->acl($user_data); |
||
| 242 | |||
| 256 |