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 |
||
5 | class FetchTest extends BridgeTestCase |
||
6 | { |
||
7 | public function testFetchArray() |
||
8 | { |
||
9 | $query = 'SELECT * FROM test_table WHERE id <= 3'; |
||
10 | $result = $this->bridge->query($query); |
||
11 | |||
12 | $this->assertEquals( |
||
13 | [ |
||
14 | 0 => "1", |
||
15 | 1 => "test 1", |
||
16 | "id" => "1", |
||
17 | "testfield" => "test 1", |
||
18 | ], |
||
19 | $this->bridge->fetchArray($result) |
||
20 | ); |
||
21 | |||
22 | $this->assertEquals( |
||
23 | [ |
||
24 | "id" => "2", |
||
25 | "testfield" => "test 2", |
||
26 | ], |
||
27 | $this->bridge->fetchArray($result, Result::FETCH_ASSOC) |
||
28 | ); |
||
29 | |||
30 | $this->assertEquals( |
||
31 | [ |
||
32 | 0 => "3", |
||
33 | 1 => "test 3", |
||
34 | ], |
||
35 | $this->bridge->fetchArray($result, Result::FETCH_NUM) |
||
36 | ); |
||
37 | |||
38 | $this->assertFalse($this->bridge->fetchArray($result)); |
||
39 | } |
||
40 | |||
41 | public function testFetchAssoc() |
||
42 | { |
||
43 | $query = 'SELECT * FROM test_table WHERE id = 4'; |
||
44 | $result = $this->bridge->query($query); |
||
45 | |||
46 | $this->assertEquals( |
||
47 | [ |
||
48 | 'id' => '4', |
||
49 | 'testfield' => 'test 4' |
||
50 | ], |
||
51 | $this->bridge->fetchAssoc($result) |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | public function testFetchField() |
||
56 | { |
||
57 | $query = 'SELECT * FROM test_table'; |
||
58 | $result = $this->bridge->query($query); |
||
59 | |||
60 | $this->assertAttributeEquals(1, 'primary_key', $this->bridge->fetchField($result)); |
||
61 | $this->assertAttributeEquals('id', 'name', $this->bridge->fetchField($result)); |
||
62 | $this->assertAttributeEquals('testfield', 'name', $this->bridge->fetchField($result, 1)); |
||
63 | } |
||
64 | |||
65 | public function testFetchLengths() |
||
66 | { |
||
67 | $query = 'SELECT * FROM test_table WHERE id = 10'; |
||
68 | $result = $this->bridge->query($query); |
||
69 | $this->bridge->fetchAssoc($result); |
||
70 | |||
71 | $this->assertEquals([0 => 2, 1 => 7], $this->bridge->fetchLengths($result)); |
||
72 | } |
||
73 | |||
74 | public function testFetchObject() |
||
75 | { |
||
76 | $query = 'SELECT * FROM test_table WHERE id = 5'; |
||
77 | $result = $this->bridge->query($query); |
||
78 | |||
79 | $object = $this->bridge->fetchObject($result); |
||
80 | |||
81 | $this->assertInstanceOf(stdClass::class, $object); |
||
82 | $this->assertEquals('5', $object->id); |
||
83 | $this->assertEquals('test 5', $object->testfield); |
||
84 | |||
85 | $query = 'SELECT * FROM test_table WHERE id = 5'; |
||
86 | $result = $this->bridge->query($query); |
||
87 | |||
88 | $object = $this->bridge->fetchObject($result, DummyObject::class, [1, 2]); |
||
89 | |||
90 | $this->assertInstanceOf(DummyObject::class, $object); |
||
91 | $this->assertEquals('5', $object->id); |
||
92 | $this->assertEquals('test 5', $object->testfield); |
||
93 | $this->assertEquals(1, $object->one); |
||
94 | $this->assertEquals(2, $object->two); |
||
95 | } |
||
96 | |||
97 | public function testFetchRow() |
||
98 | { |
||
99 | $query = 'SELECT * FROM test_table WHERE id = 6'; |
||
100 | $result = $this->bridge->query($query); |
||
101 | |||
102 | $this->assertEquals( |
||
103 | [ |
||
104 | 0 => '6', |
||
105 | 1 => 'test 6' |
||
106 | ], |
||
107 | $this->bridge->fetchRow($result) |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | /** @expectedException \Mattbit\MysqlCompat\Exception\NotSupportedException */ |
||
112 | public function testDataSeek() |
||
113 | { |
||
114 | $result = $this->bridge->query('SELECT * FROM test_table'); |
||
115 | |||
116 | $this->bridge->dataSeek($result, 5); |
||
117 | } |
||
118 | } |
||
119 | |||
128 | } |