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 |
||
18 | * |
||
19 | * @property Select $object |
||
20 | */ |
||
21 | class SelectTest extends AbstractTest |
||
22 | { |
||
23 | /** |
||
24 | * @var \UnitTester |
||
25 | */ |
||
26 | protected $tester; |
||
27 | |||
28 | /** |
||
29 | * @var Connection |
||
30 | */ |
||
31 | protected $connection; |
||
32 | |||
33 | /** |
||
34 | * @var Select |
||
35 | */ |
||
36 | protected $selectQuery; |
||
37 | |||
38 | public function testSelectSimple() |
||
39 | { |
||
40 | $array = ['id, name as new_name', 'table2.test', 'MAX(pos) as pos']; |
||
41 | call_user_func_array([$this->selectQuery, 'cols'], $array); |
||
42 | $this->selectQuery->from('table x')->where('id = 5'); |
||
43 | |||
44 | static::assertEquals( |
||
45 | 'SELECT id, name as new_name, table2.test, MAX(pos) as pos FROM table x WHERE id = 5', |
||
46 | $this->selectQuery->assemble() |
||
47 | ); |
||
48 | } |
||
49 | |||
50 | public function testSimpleSelectDistinct() |
||
51 | { |
||
52 | $this->selectQuery->cols('id, name')->options('distinct')->from('table x')->where('id = 5'); |
||
53 | static::assertEquals( |
||
54 | "SELECT DISTINCT id, name FROM table x WHERE id = 5", |
||
55 | $this->selectQuery->assemble() |
||
56 | ); |
||
57 | } |
||
58 | |||
59 | public function testWhereAndWhere() |
||
60 | { |
||
61 | $this->selectQuery->cols('id, name')->from('table x'); |
||
62 | $this->selectQuery->where('id = 5')->where("active = 'yes'"); |
||
63 | static::assertEquals( |
||
64 | "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes'", |
||
65 | $this->selectQuery->assemble() |
||
66 | ); |
||
67 | $this->object->cols('id, name')->from('table x'); |
||
68 | $this->object->where('id = 5')->where("active = 'yes'"); |
||
69 | static::assertEquals( |
||
70 | "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes'", |
||
71 | $this->object->assemble()); |
||
72 | } |
||
73 | |||
74 | public function testWhereOrWhere() |
||
75 | { |
||
76 | $this->selectQuery->cols('id, name')->from('table x'); |
||
77 | $this->selectQuery->where('id = 5')->orWhere('id = 7'); |
||
78 | static::assertEquals( |
||
79 | "SELECT id, name FROM table x WHERE id = 5 OR id = 7", |
||
80 | $this->selectQuery->assemble() |
||
81 | $this->object->cols('id, name')->from('table x'); |
||
|
|||
82 | $this->object->where('id = 5')->orWhere('id = 7'); |
||
83 | static::assertEquals( |
||
84 | "SELECT id, name FROM table x WHERE id = 5 OR id = 7", |
||
85 | $this->object->assemble() |
||
86 | ); |
||
87 | } |
||
88 | |||
89 | public function testInitializeCondition() |
||
90 | { |
||
91 | $condition = $this->object->getCondition("lorem ipsum"); |
||
92 | static::assertThat($condition, $this->isInstanceOf("Nip\Database\Query\Condition\Condition")); |
||
93 | } |
||
94 | |||
95 | public function testNested() |
||
96 | { |
||
97 | $this->selectQuery->from("table1"); |
||
98 | $this->object->from("table1"); |
||
99 | |||
100 | $query = $this->connection->newQuery(); |
||
101 | $query->from("table2"); |
||
102 | $query->where("id != 5"); |
||
103 | |||
104 | $this->object->where("id NOT IN ?", $query); |
||
105 | |||
106 | static::assertEquals( |
||
107 | "SELECT * FROM `table1` WHERE id NOT IN (SELECT * FROM `table2` WHERE id != 5)", |
||
108 | $this->object->assemble() |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | public function testUnion() |
||
113 | { |
||
114 | $this->object->from("table1"); |
||
115 | |||
116 | $query = $this->connection->newQuery(); |
||
117 | $query->from("table2"); |
||
118 | |||
119 | $union = $this->object->union($query); |
||
120 | |||
121 | static::assertEquals("SELECT * FROM `table1` UNION SELECT * FROM `table2`", $union->assemble()); |
||
122 | } |
||
123 | |||
124 | public function testJoinTableName() |
||
125 | { |
||
126 | $this->object->from("table1"); |
||
127 | $this->object->join("table2", ['id', 'id_table1']); |
||
128 | |||
129 | static::assertEquals( |
||
130 | "SELECT * FROM `table1` JOIN `table2` ON `table1`.`id` = `table2`.`id_table1`", |
||
131 | $this->object->assemble() |
||
132 | ); |
||
133 | } |
||
134 | |||
135 | public function testJoinTableNameWithAlias() |
||
136 | { |
||
137 | $this->object->from("table1"); |
||
138 | $this->object->join(["table2", "alias"], ['id', 'id_table1']); |
||
139 | |||
140 | static::assertEquals( |
||
141 | "SELECT * FROM `table1` JOIN `table2` AS `alias` ON `table1`.`id` = `table2`.`id_table1`", |
||
142 | $this->object->assemble() |
||
143 | ); |
||
144 | } |
||
145 | |||
146 | public function testJoinSubQuery() |
||
147 | { |
||
148 | $this->object->from("table1"); |
||
149 | |||
150 | $query = $this->connection->newQuery(); |
||
151 | $query->from("table2"); |
||
152 | |||
153 | $this->object->join([$query, "alias"], ['id', 'id_table1']); |
||
154 | |||
155 | static::assertEquals( |
||
156 | 'SELECT * FROM `table1` JOIN (SELECT * FROM `table2`) AS `alias` ON `table1`.`id` = `alias`.`id_table1`', |
||
157 | $this->object->assemble() |
||
158 | ); |
||
159 | } |
||
160 | |||
161 | public function testHasPart() |
||
162 | { |
||
163 | $this->object->cols('id, name'); |
||
164 | self::assertTrue($this->object->hasPart('cols')); |
||
165 | |||
166 | $this->object->setCols('id, name'); |
||
167 | self::assertTrue($this->object->hasPart('cols')); |
||
168 | |||
169 | $this->object->limit(''); |
||
170 | self::assertFalse($this->object->hasPart('limit')); |
||
171 | |||
172 | $this->object->limit('6'); |
||
173 | self::assertTrue($this->object->hasPart('limit')); |
||
174 | |||
175 | self::assertFalse($this->object->hasPart('where')); |
||
176 | } |
||
177 | |||
178 | public function testLimit() |
||
179 | { |
||
180 | $this->object->cols('id, name')->from('table x'); |
||
181 | $this->object->where('id = 5')->where("active = 'yes'"); |
||
182 | $this->object->limit(5); |
||
183 | |||
184 | static::assertEquals( |
||
185 | "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes' LIMIT 5", |
||
186 | $this->object->assemble() |
||
187 | ); |
||
188 | |||
189 | $this->object->limit(5, 10); |
||
190 | static::assertEquals( |
||
191 | "SELECT id, name FROM table x WHERE id = 5 AND active = 'yes' LIMIT 5,10", |
||
192 | $this->object->assemble() |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | protected function setUp() |
||
210 |