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