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_base_test extends controller_base |
||
| 18 | { |
||
| 19 | public function getDataSet() |
||
| 20 | { |
||
| 21 | return $this->createMySQLXMLDataSet(__DIR__ . '/fixtures/fixture_base.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->style['style_path'] = 'prosilver'; |
||
| 36 | } |
||
| 37 | |||
| 38 | public function get_controller() |
||
| 39 | { |
||
| 40 | global $table_categories, $tables_comments, $tables_links, $tables_votes, $tables_watch; |
||
| 41 | global $phpbb_path_helper, $phpbb_extension_manager; |
||
| 42 | |||
| 43 | $controller = new \ernadoo\phpbbdirectory\controller\categories( |
||
| 44 | $this->db, |
||
| 45 | $this->config, |
||
| 46 | $this->lang, |
||
| 47 | $this->template, |
||
| 48 | $this->user, |
||
| 49 | $this->helper, |
||
| 50 | $this->request, |
||
| 51 | $this->auth, |
||
| 52 | $this->pagination, |
||
| 53 | $this->core_categorie, |
||
| 54 | $this->core_link |
||
| 55 | ); |
||
| 56 | $controller->set_tables($table_categories, $tables_comments, $tables_links, $tables_votes, $tables_watch); |
||
| 57 | $controller->set_path_helper($phpbb_path_helper); |
||
| 58 | $controller->set_extension_manager($phpbb_extension_manager); |
||
| 59 | |||
| 60 | return $controller; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Test data for the test_display_base() function |
||
| 65 | * |
||
| 66 | * @return array Array of test data |
||
| 67 | */ |
||
| 68 | public function display_base_data() |
||
| 69 | { |
||
| 70 | return array( |
||
| 71 | array(200, 'body.html'), |
||
| 72 | ); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Test controller display |
||
| 77 | * |
||
| 78 | * @dataProvider display_base_data |
||
| 79 | */ |
||
| 80 | public function test_display_base($status_code, $page_content) |
||
| 81 | { |
||
| 82 | $controller = $this->get_controller(); |
||
| 83 | $response = $controller->base(); |
||
| 84 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
||
| 85 | $this->assertEquals($status_code, $response->getStatusCode()); |
||
| 86 | $this->assertEquals($page_content, $response->getContent()); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Test base case scenario |
||
| 91 | * |
||
| 92 | */ |
||
| 93 | public function test_for_root_categories() |
||
| 94 | { |
||
| 95 | $this->template->expects($this->exactly(2)) |
||
| 96 | ->method('assign_vars') |
||
| 97 | ->withConsecutive( |
||
| 98 | array( |
||
| 99 | array( // navlinks |
||
| 100 | 'S_PHPBB_DIRECTORY' => true, |
||
| 101 | ), |
||
| 102 | ), |
||
| 103 | array( |
||
| 104 | array( |
||
| 105 | 'S_AUTH_ADD' => null, |
||
| 106 | 'S_AUTH_SEARCH' => null, |
||
| 107 | 'S_HAS_SUBCAT' => true, |
||
| 108 | 'S_ROOT' => true, |
||
| 109 | |||
| 110 | 'U_MAKE_SEARCH' => 'ernadoo_phpbbdirectory_search_controller', |
||
| 111 | ) |
||
| 112 | ) |
||
| 113 | ); |
||
| 114 | |||
| 115 | $controller = $this->get_controller(); |
||
| 116 | |||
| 117 | $this->template->expects($this->exactly(2)) |
||
| 118 | ->method('assign_block_vars') |
||
| 119 | ->withConsecutive( |
||
| 120 | array('cat', |
||
| 121 | array( //expected |
||
| 122 | 'CAT_NAME' => 'Catégorie 1', |
||
| 123 | 'CAT_DESC' => 'Description_1', |
||
| 124 | 'CAT_LINKS' => 7, |
||
| 125 | 'CAT_IMG' => $controller->get_img_path('icons', 'icon_maison.gif'), |
||
| 126 | |||
| 127 | 'U_CAT' => 'ernadoo_phpbbdirectory_dynamic_route_1', |
||
| 128 | ) |
||
| 129 | ), |
||
| 130 | array('cat.subcat', |
||
| 131 | array( //expected |
||
| 132 | 'U_CAT' => 'ernadoo_phpbbdirectory_dynamic_route_2', |
||
| 133 | 'CAT_NAME' => 'Catégorie 2', |
||
| 134 | 'CAT_LINKS' => 6 |
||
| 135 | ) |
||
| 136 | ) |
||
| 137 | ); |
||
| 138 | |||
| 139 | $response = $controller->base(); |
||
| 140 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
||
| 141 | $this->assertEquals('200', $response->getStatusCode()); |
||
| 142 | } |
||
| 143 | |||
| 144 | protected function tearDown() |
||
| 145 | { |
||
| 146 | parent::tearDown(); |
||
| 147 | } |
||
| 148 | } |
||
| 149 |