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 |
||
22 | class CIPHPUnitTestDbTestCase extends CIPHPUnitTestCase |
||
|
|||
23 | { |
||
24 | protected $db; |
||
25 | |||
26 | /** |
||
27 | * Stores information needed to remove any |
||
28 | * rows inserted via $this->hasInDatabase(); |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $insertCache = []; |
||
33 | |||
34 | protected function loadDependencies() |
||
43 | |||
44 | protected function setUp() |
||
48 | |||
49 | //-------------------------------------------------------------------- |
||
50 | |||
51 | /** |
||
52 | * Takes care of any required cleanup after the test, like |
||
53 | * removing any rows inserted via $this->hasInDatabase() |
||
54 | */ |
||
55 | protected function tearDown() |
||
65 | |||
66 | /** |
||
67 | * Reconnect to the database |
||
68 | */ |
||
69 | public function reconnectDb() |
||
78 | |||
79 | //-------------------------------------------------------------------- |
||
80 | // Database Test Helpers |
||
81 | //-------------------------------------------------------------------- |
||
82 | |||
83 | /** |
||
84 | * Asserts that records that match the conditions in $where do |
||
85 | * not exist in the database. |
||
86 | * |
||
87 | * @param string $table |
||
88 | * @param array $where |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | View Code Duplication | public function dontSeeInDatabase($table, array $where) |
|
100 | |||
101 | //-------------------------------------------------------------------- |
||
102 | |||
103 | /** |
||
104 | * Asserts that records that match the conditions in $where DO |
||
105 | * exist in the database. |
||
106 | * |
||
107 | * @param string $table |
||
108 | * @param array $where |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | View Code Duplication | public function seeInDatabase($table, array $where) |
|
120 | |||
121 | //-------------------------------------------------------------------- |
||
122 | |||
123 | /** |
||
124 | * Fetches a single column from a database row with criteria |
||
125 | * matching $where. |
||
126 | * |
||
127 | * @param string $table |
||
128 | * @param string $column |
||
129 | * @param array $where |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function grabFromDatabase($table, $column, array $where) |
||
142 | |||
143 | //-------------------------------------------------------------------- |
||
144 | |||
145 | /** |
||
146 | * Inserts a row into to the database. This row will be removed |
||
147 | * after the test has run. |
||
148 | * |
||
149 | * @param string $table |
||
150 | * @param array $data |
||
151 | * |
||
152 | */ |
||
153 | public function hasInDatabase($table, array $data) |
||
161 | |||
162 | //-------------------------------------------------------------------- |
||
163 | |||
164 | /** |
||
165 | * Asserts that the number of rows in the database that match $where |
||
166 | * is equal to $expected. |
||
167 | * |
||
168 | * @param int $expected |
||
169 | * @param string $table |
||
170 | * @param array $where |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function seeNumRecords($expected, $table, array $where = []) |
||
182 | |||
183 | //-------------------------------------------------------------------- |
||
184 | |||
185 | } |
||
186 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.