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 |
||
3 | class RowCountTest extends BridgeTestCase |
||
4 | { |
||
5 | public function testUpdateAffectedRows() |
||
6 | { |
||
7 | $this->bridge->query("UPDATE test_table SET testfield = 'updated' WHERE id > 100"); |
||
8 | $this->assertEquals(0, $this->bridge->affectedRows()); |
||
9 | |||
10 | $this->bridge->query("UPDATE test_table SET testfield = 'updated' WHERE id > 5"); |
||
11 | $this->assertEquals(5, $this->bridge->affectedRows()); |
||
12 | } |
||
13 | |||
14 | public function testInsertAffectedRows() |
||
15 | { |
||
16 | $this->assertEquals(0, $this->bridge->affectedRows()); |
||
17 | |||
18 | $this->bridge->query("INSERT INTO test_table VALUES (100, 'test insert 100')"); |
||
19 | $this->assertEquals(1, $this->bridge->affectedRows()); |
||
20 | |||
21 | $this->bridge->query("INSERT INTO test_table VALUES (101, 'test insert 101'), (102, 'test insert 102')"); |
||
22 | $this->assertEquals(2, $this->bridge->affectedRows()); |
||
23 | } |
||
24 | |||
25 | public function testDeleteAffectedRows() |
||
26 | { |
||
27 | $this->assertEquals(0, $this->bridge->affectedRows()); |
||
28 | |||
29 | $this->bridge->query('DELETE FROM test_table WHERE id = 1'); |
||
30 | $this->assertEquals(1, $this->bridge->affectedRows()); |
||
31 | |||
32 | $this->bridge->query('DELETE FROM test_table WHERE id > 5'); |
||
33 | $this->assertEquals(5, $this->bridge->affectedRows()); |
||
34 | } |
||
35 | |||
36 | public function testNumRows() |
||
37 | { |
||
38 | $result = $this->bridge->query('SELECT * FROM test_table WHERE id > 100'); |
||
39 | $this->assertEquals(0, $this->bridge->numRows($result)); |
||
40 | |||
41 | $result = $this->bridge->query('SELECT id FROM test_table where id < 5'); |
||
42 | $this->assertEquals(4, $this->bridge->numRows($result)); |
||
43 | } |
||
44 | } |