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:
Complex classes like QueryBuilderPsr1Test53 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueryBuilderPsr1Test53, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class QueryBuilderPsr1Test53 extends PHPUnit_Framework_TestCase { |
||
| 4 | |||
| 5 | public function setUp() { |
||
| 6 | // Enable logging |
||
| 7 | ORM::configure('logging', true); |
||
| 8 | |||
| 9 | // Set up the dummy database connection |
||
| 10 | $db = new MockPDO('sqlite::memory:'); |
||
| 11 | ORM::setDb($db); |
||
| 12 | } |
||
| 13 | |||
| 14 | public function tearDown() { |
||
| 15 | ORM::configure('logging', false); |
||
| 16 | ORM::setDb(null); |
||
| 17 | } |
||
| 18 | |||
| 19 | public function testFindMany() { |
||
| 20 | ORM::forTable('widget')->findMany(); |
||
| 21 | $expected = "SELECT * FROM `widget`"; |
||
| 22 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function testFindOne() { |
||
| 26 | ORM::forTable('widget')->findOne(); |
||
| 27 | $expected = "SELECT * FROM `widget` LIMIT 1"; |
||
| 28 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function testFindOneWithPrimaryKeyFilter() { |
||
| 32 | ORM::forTable('widget')->findOne(5); |
||
| 33 | $expected = "SELECT * FROM `widget` WHERE `id` = '5' LIMIT 1"; |
||
| 34 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function testWhereIdIs() { |
||
| 38 | ORM::forTable('widget')->whereIdIs(5)->findOne(); |
||
| 39 | $expected = "SELECT * FROM `widget` WHERE `id` = '5' LIMIT 1"; |
||
| 40 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testSingleWhereClause() { |
||
| 44 | ORM::forTable('widget')->where('name', 'Fred')->findOne(); |
||
| 45 | $expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' LIMIT 1"; |
||
| 46 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function testMultipleWhereClauses() { |
||
| 50 | ORM::forTable('widget')->where('name', 'Fred')->where('age', 10)->findOne(); |
||
| 51 | $expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' AND `age` = '10' LIMIT 1"; |
||
| 52 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function testWhereNotEqual() { |
||
| 56 | ORM::forTable('widget')->whereNotEqual('name', 'Fred')->findMany(); |
||
| 57 | $expected = "SELECT * FROM `widget` WHERE `name` != 'Fred'"; |
||
| 58 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function testWhereLike() { |
||
| 62 | ORM::forTable('widget')->whereLike('name', '%Fred%')->findOne(); |
||
| 63 | $expected = "SELECT * FROM `widget` WHERE `name` LIKE '%Fred%' LIMIT 1"; |
||
| 64 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function testWhereNotLike() { |
||
| 68 | ORM::forTable('widget')->whereNotLike('name', '%Fred%')->findOne(); |
||
| 69 | $expected = "SELECT * FROM `widget` WHERE `name` NOT LIKE '%Fred%' LIMIT 1"; |
||
| 70 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function testWhereIn() { |
||
| 74 | ORM::forTable('widget')->whereIn('name', array('Fred', 'Joe'))->findMany(); |
||
| 75 | $expected = "SELECT * FROM `widget` WHERE `name` IN ('Fred', 'Joe')"; |
||
| 76 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function testWhereNotIn() { |
||
| 80 | ORM::forTable('widget')->whereNotIn('name', array('Fred', 'Joe'))->findMany(); |
||
| 81 | $expected = "SELECT * FROM `widget` WHERE `name` NOT IN ('Fred', 'Joe')"; |
||
| 82 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function testLimit() { |
||
| 86 | ORM::forTable('widget')->limit(5)->findMany(); |
||
| 87 | $expected = "SELECT * FROM `widget` LIMIT 5"; |
||
| 88 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function testLimitAndOffset() { |
||
| 92 | ORM::forTable('widget')->limit(5)->offset(5)->findMany(); |
||
| 93 | $expected = "SELECT * FROM `widget` LIMIT 5 OFFSET 5"; |
||
| 94 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function testOrderByDesc() { |
||
| 98 | ORM::forTable('widget')->orderByDesc('name')->findOne(); |
||
| 99 | $expected = "SELECT * FROM `widget` ORDER BY `name` DESC LIMIT 1"; |
||
| 100 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function testOrderByAsc() { |
||
| 104 | ORM::forTable('widget')->orderByAsc('name')->findOne(); |
||
| 105 | $expected = "SELECT * FROM `widget` ORDER BY `name` ASC LIMIT 1"; |
||
| 106 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testOrderByExpression() { |
||
| 110 | ORM::forTable('widget')->orderByExpr('SOUNDEX(`name`)')->findOne(); |
||
| 111 | $expected = "SELECT * FROM `widget` ORDER BY SOUNDEX(`name`) LIMIT 1"; |
||
| 112 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function testMultipleOrderBy() { |
||
| 116 | ORM::forTable('widget')->orderByAsc('name')->orderByDesc('age')->findOne(); |
||
| 117 | $expected = "SELECT * FROM `widget` ORDER BY `name` ASC, `age` DESC LIMIT 1"; |
||
| 118 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function testGroupBy() { |
||
| 122 | ORM::forTable('widget')->groupBy('name')->findMany(); |
||
| 123 | $expected = "SELECT * FROM `widget` GROUP BY `name`"; |
||
| 124 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 125 | } |
||
| 126 | |||
| 127 | public function testMultipleGroupBy() { |
||
| 128 | ORM::forTable('widget')->groupBy('name')->groupBy('age')->findMany(); |
||
| 129 | $expected = "SELECT * FROM `widget` GROUP BY `name`, `age`"; |
||
| 130 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function testGroupByExpression() { |
||
| 134 | ORM::forTable('widget')->groupByExpr("FROM_UNIXTIME(`time`, '%Y-%m')")->findMany(); |
||
| 135 | $expected = "SELECT * FROM `widget` GROUP BY FROM_UNIXTIME(`time`, '%Y-%m')"; |
||
| 136 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function testHaving() { |
||
| 140 | ORM::forTable('widget')->groupBy('name')->having('name', 'Fred')->findOne(); |
||
| 141 | $expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` = 'Fred' LIMIT 1"; |
||
| 142 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function testMultipleHaving() { |
||
| 146 | ORM::forTable('widget')->groupBy('name')->having('name', 'Fred')->having('age', 10)->findOne(); |
||
| 147 | $expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` = 'Fred' AND `age` = '10' LIMIT 1"; |
||
| 148 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function testHavingNotEqual() { |
||
| 152 | ORM::forTable('widget')->groupBy('name')->havingNotEqual('name', 'Fred')->findMany(); |
||
| 153 | $expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` != 'Fred'"; |
||
| 154 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 155 | } |
||
| 156 | |||
| 157 | public function testHavingLike() { |
||
| 158 | ORM::forTable('widget')->groupBy('name')->havingLike('name', '%Fred%')->findOne(); |
||
| 159 | $expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` LIKE '%Fred%' LIMIT 1"; |
||
| 160 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function testHavingNotLike() { |
||
| 164 | ORM::forTable('widget')->groupBy('name')->havingNotLike('name', '%Fred%')->findOne(); |
||
| 165 | $expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` NOT LIKE '%Fred%' LIMIT 1"; |
||
| 166 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 167 | } |
||
| 168 | |||
| 169 | public function testHavingIn() { |
||
| 170 | ORM::forTable('widget')->groupBy('name')->havingIn('name', array('Fred', 'Joe'))->findMany(); |
||
| 171 | $expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` IN ('Fred', 'Joe')"; |
||
| 172 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 173 | } |
||
| 174 | |||
| 175 | public function testHavingNotIn() { |
||
| 176 | ORM::forTable('widget')->groupBy('name')->havingNotIn('name', array('Fred', 'Joe'))->findMany(); |
||
| 177 | $expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` NOT IN ('Fred', 'Joe')"; |
||
| 178 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 179 | } |
||
| 180 | |||
| 181 | public function testHavingLessThan() { |
||
| 182 | ORM::forTable('widget')->groupBy('name')->havingLt('age', 10)->havingGt('age', 5)->findMany(); |
||
| 183 | $expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `age` < '10' AND `age` > '5'"; |
||
| 184 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function testHavingLessThanOrEqualAndGreaterThanOrEqual() { |
||
| 188 | ORM::forTable('widget')->groupBy('name')->havingLte('age', 10)->havingGte('age', 5)->findMany(); |
||
| 189 | $expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `age` <= '10' AND `age` >= '5'"; |
||
| 190 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 191 | } |
||
| 192 | |||
| 193 | public function testHavingNull() { |
||
| 194 | ORM::forTable('widget')->groupBy('name')->havingNull('name')->findMany(); |
||
| 195 | $expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` IS NULL"; |
||
| 196 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 197 | } |
||
| 198 | |||
| 199 | public function testHavingNotNull() { |
||
| 200 | ORM::forTable('widget')->groupBy('name')->havingNotNull('name')->findMany(); |
||
| 201 | $expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` IS NOT NULL"; |
||
| 202 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 203 | } |
||
| 204 | |||
| 205 | public function testRawHaving() { |
||
| 206 | ORM::forTable('widget')->groupBy('name')->havingRaw('`name` = ? AND (`age` = ? OR `age` = ?)', array('Fred', 5, 10))->findMany(); |
||
| 207 | $expected = "SELECT * FROM `widget` GROUP BY `name` HAVING `name` = 'Fred' AND (`age` = '5' OR `age` = '10')"; |
||
| 208 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 209 | } |
||
| 210 | |||
| 211 | public function testComplexQuery() { |
||
| 212 | ORM::forTable('widget')->where('name', 'Fred')->limit(5)->offset(5)->orderByAsc('name')->findMany(); |
||
| 213 | $expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' ORDER BY `name` ASC LIMIT 5 OFFSET 5"; |
||
| 214 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 215 | } |
||
| 216 | |||
| 217 | public function testWhereLessThanAndGreaterThan() { |
||
| 218 | ORM::forTable('widget')->whereLt('age', 10)->whereGt('age', 5)->findMany(); |
||
| 219 | $expected = "SELECT * FROM `widget` WHERE `age` < '10' AND `age` > '5'"; |
||
| 220 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 221 | } |
||
| 222 | |||
| 223 | public function testWhereLessThanAndEqualAndGreaterThanAndEqual() { |
||
| 224 | ORM::forTable('widget')->whereLte('age', 10)->whereGte('age', 5)->findMany(); |
||
| 225 | $expected = "SELECT * FROM `widget` WHERE `age` <= '10' AND `age` >= '5'"; |
||
| 226 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 227 | } |
||
| 228 | |||
| 229 | public function testWhereNull() { |
||
| 230 | ORM::forTable('widget')->whereNull('name')->findMany(); |
||
| 231 | $expected = "SELECT * FROM `widget` WHERE `name` IS NULL"; |
||
| 232 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function testWhereNotNull() { |
||
| 236 | ORM::forTable('widget')->whereNotNull('name')->findMany(); |
||
| 237 | $expected = "SELECT * FROM `widget` WHERE `name` IS NOT NULL"; |
||
| 238 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 239 | } |
||
| 240 | |||
| 241 | public function testRawWhereClause() { |
||
| 242 | ORM::forTable('widget')->whereRaw('`name` = ? AND (`age` = ? OR `age` = ?)', array('Fred', 5, 10))->findMany(); |
||
| 243 | $expected = "SELECT * FROM `widget` WHERE `name` = 'Fred' AND (`age` = '5' OR `age` = '10')"; |
||
| 244 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 245 | } |
||
| 246 | |||
| 247 | public function testRawWhereClauseWithPercentSign() { |
||
| 248 | ORM::forTable('widget')->whereRaw('STRFTIME("%Y", "now") = ?', array(2012))->findMany(); |
||
| 249 | $expected = "SELECT * FROM `widget` WHERE STRFTIME(\"%Y\", \"now\") = '2012'"; |
||
| 250 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function testRawWhereClauseWithNoParameters() { |
||
| 254 | ORM::forTable('widget')->whereRaw('`name` = "Fred"')->findMany(); |
||
| 255 | $expected = "SELECT * FROM `widget` WHERE `name` = \"Fred\""; |
||
| 256 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 257 | } |
||
| 258 | |||
| 259 | public function testRawWhereClauseInMethodChain() { |
||
| 260 | ORM::forTable('widget')->where('age', 18)->whereRaw('(`name` = ? OR `name` = ?)', array('Fred', 'Bob'))->where('size', 'large')->findMany(); |
||
| 261 | $expected = "SELECT * FROM `widget` WHERE `age` = '18' AND (`name` = 'Fred' OR `name` = 'Bob') AND `size` = 'large'"; |
||
| 262 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 263 | } |
||
| 264 | |||
| 265 | public function testRawQuery() { |
||
| 266 | ORM::forTable('widget')->rawQuery('SELECT `w`.* FROM `widget` w')->findMany(); |
||
| 267 | $expected = "SELECT `w`.* FROM `widget` w"; |
||
| 268 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 269 | } |
||
| 270 | |||
| 271 | public function testRawQueryWithParameters() { |
||
| 272 | ORM::forTable('widget')->rawQuery('SELECT `w`.* FROM `widget` w WHERE `name` = ? AND `age` = ?', array('Fred', 5))->findMany(); |
||
| 273 | $expected = "SELECT `w`.* FROM `widget` w WHERE `name` = 'Fred' AND `age` = '5'"; |
||
| 274 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 275 | } |
||
| 276 | |||
| 277 | public function testSimpleResultColumn() { |
||
| 278 | ORM::forTable('widget')->select('name')->findMany(); |
||
| 279 | $expected = "SELECT `name` FROM `widget`"; |
||
| 280 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 281 | } |
||
| 282 | |||
| 283 | public function testMultipleSimpleResultColumns() { |
||
| 284 | ORM::forTable('widget')->select('name')->select('age')->findMany(); |
||
| 285 | $expected = "SELECT `name`, `age` FROM `widget`"; |
||
| 286 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 287 | } |
||
| 288 | |||
| 289 | public function testSpecifyTableNameAndColumnInResultColumns() { |
||
| 290 | ORM::forTable('widget')->select('widget.name')->findMany(); |
||
| 291 | $expected = "SELECT `widget`.`name` FROM `widget`"; |
||
| 292 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 293 | } |
||
| 294 | |||
| 295 | public function testMainTableAlias() { |
||
| 296 | ORM::forTable('widget')->tableAlias('w')->findMany(); |
||
| 297 | $expected = "SELECT * FROM `widget` `w`"; |
||
| 298 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 299 | } |
||
| 300 | |||
| 301 | public function testAliasesInResultColumns() { |
||
| 302 | ORM::forTable('widget')->select('widget.name', 'widget_name')->findMany(); |
||
| 303 | $expected = "SELECT `widget`.`name` AS `widget_name` FROM `widget`"; |
||
| 304 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 305 | } |
||
| 306 | |||
| 307 | public function testAliasesInSelectManyResults() { |
||
| 308 | ORM::forTable('widget')->selectMany(array('widget_name' => 'widget.name'), 'widget_handle')->findMany(); |
||
| 309 | $expected = "SELECT `widget`.`name` AS `widget_name`, `widget_handle` FROM `widget`"; |
||
| 310 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 311 | } |
||
| 312 | |||
| 313 | public function testLiteralExpressionInResultColumn() { |
||
| 314 | ORM::forTable('widget')->selectExpr('COUNT(*)', 'count')->findMany(); |
||
| 315 | $expected = "SELECT COUNT(*) AS `count` FROM `widget`"; |
||
| 316 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 317 | } |
||
| 318 | |||
| 319 | public function testLiteralExpressionInSelectManyResultColumns() { |
||
| 320 | ORM::forTable('widget')->selectManyExpr(array('count' => 'COUNT(*)'), 'SUM(widget_order)')->findMany(); |
||
| 321 | $expected = "SELECT COUNT(*) AS `count`, SUM(widget_order) FROM `widget`"; |
||
| 322 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 323 | } |
||
| 324 | |||
| 325 | public function testSimpleJoin() { |
||
| 326 | ORM::forTable('widget')->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->findMany(); |
||
| 327 | $expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; |
||
| 328 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 329 | } |
||
| 330 | |||
| 331 | public function testSimpleJoinWithWhereIdIsMethod() { |
||
| 332 | ORM::forTable('widget')->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->findOne(5); |
||
| 333 | $expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` WHERE `widget`.`id` = '5' LIMIT 1"; |
||
| 334 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 335 | } |
||
| 336 | |||
| 337 | public function testInnerJoin() { |
||
| 338 | ORM::forTable('widget')->innerJoin('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->findMany(); |
||
| 339 | $expected = "SELECT * FROM `widget` INNER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; |
||
| 340 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 341 | } |
||
| 342 | |||
| 343 | public function testLeftOuterJoin() { |
||
| 344 | ORM::forTable('widget')->leftOuterJoin('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->findMany(); |
||
| 345 | $expected = "SELECT * FROM `widget` LEFT OUTER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; |
||
| 346 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 347 | } |
||
| 348 | |||
| 349 | public function testRightOuterJoin() { |
||
| 350 | ORM::forTable('widget')->rightOuterJoin('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->findMany(); |
||
| 351 | $expected = "SELECT * FROM `widget` RIGHT OUTER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; |
||
| 352 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 353 | } |
||
| 354 | |||
| 355 | public function testFullOuterJoin() { |
||
| 356 | ORM::forTable('widget')->fullOuterJoin('widget_handle', array('widget_handle.widget_id', '=', 'widget.id'))->findMany(); |
||
| 357 | $expected = "SELECT * FROM `widget` FULL OUTER JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; |
||
| 358 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 359 | } |
||
| 360 | |||
| 361 | public function testMultipleJoinSources() { |
||
| 362 | ORM::forTable('widget') |
||
| 363 | ->join('widget_handle', array('widget_handle.widget_id', '=', 'widget.id')) |
||
| 364 | ->join('widget_nozzle', array('widget_nozzle.widget_id', '=', 'widget.id')) |
||
| 365 | ->findMany(); |
||
| 366 | $expected = "SELECT * FROM `widget` JOIN `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` JOIN `widget_nozzle` ON `widget_nozzle`.`widget_id` = `widget`.`id`"; |
||
| 367 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 368 | } |
||
| 369 | |||
| 370 | public function testJoinWithAliases() { |
||
| 371 | ORM::forTable('widget')->join('widget_handle', array('wh.widget_id', '=', 'widget.id'), 'wh')->findMany(); |
||
| 372 | $expected = "SELECT * FROM `widget` JOIN `widget_handle` `wh` ON `wh`.`widget_id` = `widget`.`id`"; |
||
| 373 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 374 | } |
||
| 375 | |||
| 376 | public function testJoinWithAliasesAndWhere() { |
||
| 377 | ORM::forTable('widget')->tableAlias('w')->join('widget_handle', array('wh.widget_id', '=', 'w.id'), 'wh')->whereEqual('id', 1)->findMany(); |
||
| 378 | $expected = "SELECT * FROM `widget` `w` JOIN `widget_handle` `wh` ON `wh`.`widget_id` = `w`.`id` WHERE `w`.`id` = '1'"; |
||
| 379 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 380 | } |
||
| 381 | |||
| 382 | public function testJoinWithStringConstraint() { |
||
| 383 | ORM::forTable('widget')->join('widget_handle', "widget_handle.widget_id = widget.id")->findMany(); |
||
| 384 | $expected = "SELECT * FROM `widget` JOIN `widget_handle` ON widget_handle.widget_id = widget.id"; |
||
| 385 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 386 | } |
||
| 387 | |||
| 388 | public function testRawJoin() { |
||
| 389 | ORM::forTable('widget')->rawJoin('INNER JOIN ( SELECT * FROM `widget_handle` )', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle')->findMany(); |
||
| 390 | $expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` ) `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; |
||
| 391 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 392 | } |
||
| 393 | |||
| 394 | public function testRawJoinWithParameters() { |
||
| 395 | ORM::forTable('widget')->rawJoin('INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE ? AND `widget_handle`.category = ?)', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle', array('%button%', 2))->findMany(); |
||
| 396 | $expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE '%button%' AND `widget_handle`.category = '2') `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`"; |
||
| 397 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 398 | } |
||
| 399 | |||
| 400 | public function testRawJoinAndRawWhereWithParameters() { |
||
| 401 | ORM::forTable('widget') |
||
| 402 | ->rawJoin('INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE ? AND `widget_handle`.category = ?)', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle', array('%button%', 2)) |
||
| 403 | ->rawJoin('INNER JOIN ( SELECT * FROM `person` WHERE `person`.name LIKE ?)', array('person.id', '=', 'widget.person_id'), 'person', array('%Fred%')) |
||
| 404 | ->whereRaw('`id` > ? AND `id` < ?', array(5, 10)) |
||
| 405 | ->findMany(); |
||
| 406 | $expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE '%button%' AND `widget_handle`.category = '2') `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id` INNER JOIN ( SELECT * FROM `person` WHERE `person`.name LIKE '%Fred%') `person` ON `person`.`id` = `widget`.`person_id` WHERE `id` > '5' AND `id` < '10'"; |
||
| 407 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 408 | } |
||
| 409 | |||
| 410 | public function testSelectWithDistinct() { |
||
| 411 | ORM::forTable('widget')->distinct()->select('name')->findMany(); |
||
| 412 | $expected = "SELECT DISTINCT `name` FROM `widget`"; |
||
| 413 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 414 | } |
||
| 415 | |||
| 416 | public function testInsertData() { |
||
| 417 | $widget = ORM::forTable('widget')->create(); |
||
| 418 | $widget->name = "Fred"; |
||
| 419 | $widget->age = 10; |
||
| 420 | $widget->save(); |
||
| 421 | $expected = "INSERT INTO `widget` (`name`, `age`) VALUES ('Fred', '10')"; |
||
| 422 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 423 | } |
||
| 424 | |||
| 425 | public function testInsertDataContainingAnExpression() { |
||
| 426 | $widget = ORM::forTable('widget')->create(); |
||
| 427 | $widget->name = "Fred"; |
||
| 428 | $widget->age = 10; |
||
| 429 | $widget->setExpr('added', 'NOW()'); |
||
| 430 | $widget->save(); |
||
| 431 | $expected = "INSERT INTO `widget` (`name`, `age`, `added`) VALUES ('Fred', '10', NOW())"; |
||
| 432 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 433 | } |
||
| 434 | |||
| 435 | public function testInsertDataUsingArrayAccess() { |
||
| 436 | $widget = ORM::forTable('widget')->create(); |
||
| 437 | $widget['name'] = "Fred"; |
||
| 438 | $widget['age'] = 10; |
||
| 439 | $widget->save(); |
||
| 440 | $expected = "INSERT INTO `widget` (`name`, `age`) VALUES ('Fred', '10')"; |
||
| 441 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 442 | } |
||
| 443 | |||
| 444 | public function testUpdateData() { |
||
| 445 | $widget = ORM::forTable('widget')->findOne(1); |
||
| 446 | $widget->name = "Fred"; |
||
| 447 | $widget->age = 10; |
||
| 448 | $widget->save(); |
||
| 449 | $expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10' WHERE `id` = '1'"; |
||
| 450 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 451 | } |
||
| 452 | |||
| 453 | public function testUpdateDataContainingAnExpression() { |
||
| 454 | $widget = ORM::forTable('widget')->findOne(1); |
||
| 455 | $widget->name = "Fred"; |
||
| 456 | $widget->age = 10; |
||
| 457 | $widget->setExpr('added', 'NOW()'); |
||
| 458 | $widget->save(); |
||
| 459 | $expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10', `added` = NOW() WHERE `id` = '1'"; |
||
| 460 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 461 | } |
||
| 462 | |||
| 463 | public function testUpdateMultipleFields() { |
||
| 464 | $widget = ORM::forTable('widget')->findOne(1); |
||
| 465 | $widget->set(array("name" => "Fred", "age" => 10)); |
||
| 466 | $widget->save(); |
||
| 467 | $expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10' WHERE `id` = '1'"; |
||
| 468 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 469 | } |
||
| 470 | |||
| 471 | public function testUpdateMultipleFieldsContainingAnExpression() { |
||
| 472 | $widget = ORM::forTable('widget')->findOne(1); |
||
| 473 | $widget->set(array("name" => "Fred", "age" => 10)); |
||
| 474 | $widget->setExpr(array("added" => "NOW()", "lat_long" => "GeomFromText('POINT(1.2347 2.3436)')")); |
||
| 475 | $widget->save(); |
||
| 476 | $expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10', `added` = NOW(), `lat_long` = GeomFromText('POINT(1.2347 2.3436)') WHERE `id` = '1'"; |
||
| 477 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 478 | } |
||
| 479 | |||
| 480 | public function testUpdateMultipleFieldsContainingAnExpressionAndOverridePreviouslySetExpression() { |
||
| 481 | $widget = ORM::forTable('widget')->findOne(1); |
||
| 482 | $widget->set(array("name" => "Fred", "age" => 10)); |
||
| 483 | $widget->setExpr(array("added" => "NOW()", "lat_long" => "GeomFromText('POINT(1.2347 2.3436)')")); |
||
| 484 | $widget->lat_long = 'unknown'; |
||
| 485 | $widget->save(); |
||
| 486 | $expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10', `added` = NOW(), `lat_long` = 'unknown' WHERE `id` = '1'"; |
||
| 487 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 488 | } |
||
| 489 | |||
| 490 | public function testDeleteData() { |
||
| 491 | $widget = ORM::forTable('widget')->findOne(1); |
||
| 492 | $widget->delete(); |
||
| 493 | $expected = "DELETE FROM `widget` WHERE `id` = '1'"; |
||
| 494 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 495 | } |
||
| 496 | |||
| 497 | public function testDeleteMany() { |
||
| 498 | ORM::forTable('widget')->whereEqual('age', 10)->delete_many(); |
||
| 499 | $expected = "DELETE FROM `widget` WHERE `age` = '10'"; |
||
| 500 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 501 | } |
||
| 502 | |||
| 503 | public function testCount() { |
||
| 504 | ORM::forTable('widget')->count(); |
||
| 505 | $expected = "SELECT COUNT(*) AS `count` FROM `widget` LIMIT 1"; |
||
| 506 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 507 | } |
||
| 508 | |||
| 509 | public function testIgnoreSelectAndCount() { |
||
| 510 | ORM::forTable('widget')->select('test')->count(); |
||
| 511 | $expected = "SELECT COUNT(*) AS `count` FROM `widget` LIMIT 1"; |
||
| 512 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 513 | } |
||
| 514 | |||
| 515 | public function testMax() { |
||
| 516 | ORM::forTable('person')->max('height'); |
||
| 517 | $expected = "SELECT MAX(`height`) AS `max` FROM `person` LIMIT 1"; |
||
| 518 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 519 | } |
||
| 520 | |||
| 521 | public function testMin() { |
||
| 522 | ORM::forTable('person')->min('height'); |
||
| 523 | $expected = "SELECT MIN(`height`) AS `min` FROM `person` LIMIT 1"; |
||
| 524 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 525 | } |
||
| 526 | |||
| 527 | public function testAvg() { |
||
| 528 | ORM::forTable('person')->avg('height'); |
||
| 529 | $expected = "SELECT AVG(`height`) AS `avg` FROM `person` LIMIT 1"; |
||
| 530 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 531 | } |
||
| 532 | |||
| 533 | public function testSum() { |
||
| 534 | ORM::forTable('person')->sum('height'); |
||
| 535 | $expected = "SELECT SUM(`height`) AS `sum` FROM `person` LIMIT 1"; |
||
| 536 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Regression tests |
||
| 541 | */ |
||
| 542 | public function testIssue12IncorrectQuotingOfColumnWildcard() { |
||
| 543 | ORM::forTable('widget')->select('widget.*')->findOne(); |
||
| 544 | $expected = "SELECT `widget`.* FROM `widget` LIMIT 1"; |
||
| 545 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 546 | } |
||
| 547 | |||
| 548 | public function testIssue57LogQueryRaisesWarningWhenPercentSymbolSupplied() { |
||
| 549 | ORM::forTable('widget')->whereRaw('username LIKE "ben%"')->findMany(); |
||
| 550 | $expected = 'SELECT * FROM `widget` WHERE username LIKE "ben%"'; |
||
| 551 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 552 | } |
||
| 553 | |||
| 554 | public function testIssue57LogQueryRaisesWarningWhenQuestionMarkSupplied() { |
||
| 555 | ORM::forTable('widget')->whereRaw('comments LIKE "has been released?%"')->findMany(); |
||
| 556 | $expected = 'SELECT * FROM `widget` WHERE comments LIKE "has been released?%"'; |
||
| 557 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 558 | } |
||
| 559 | |||
| 560 | public function testIssue74EscapingQuoteMarksIn_quote_identifier_part() { |
||
| 561 | $widget = ORM::forTable('widget')->findOne(1); |
||
| 562 | $widget->set('ad`ded', '2013-01-04'); |
||
| 563 | $widget->save(); |
||
| 564 | $expected = "UPDATE `widget` SET `ad``ded` = '2013-01-04' WHERE `id` = '1'"; |
||
| 565 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 566 | } |
||
| 567 | |||
| 568 | public function testIssue90UsingSetExprAloneDoesTriggerQueryGeneration() { |
||
| 569 | $widget = ORM::forTable('widget')->findOne(1); |
||
| 570 | $widget->setExpr('added', 'NOW()'); |
||
| 571 | $widget->save(); |
||
| 572 | $expected = "UPDATE `widget` SET `added` = NOW() WHERE `id` = '1'"; |
||
| 573 | $this->assertEquals($expected, ORM::getLastQuery()); |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 577 |