| Total Complexity | 50 |
| Total Lines | 813 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SQLSelectTest 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.
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 SQLSelectTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class SQLSelectTest extends SapphireTest |
||
| 15 | { |
||
| 16 | |||
| 17 | protected static $fixture_file = 'SQLSelectTest.yml'; |
||
| 18 | |||
| 19 | protected static $extra_dataobjects = [ |
||
| 20 | SQLSelectTest\TestObject::class, |
||
| 21 | SQLSelectTest\TestBase::class, |
||
| 22 | SQLSelectTest\TestChild::class |
||
| 23 | ]; |
||
| 24 | |||
| 25 | protected $oldDeprecation = null; |
||
| 26 | |||
| 27 | protected function setUp(): void |
||
| 28 | { |
||
| 29 | parent::setUp(); |
||
| 30 | $this->oldDeprecation = Deprecation::dump_settings(); |
||
| 31 | } |
||
| 32 | |||
| 33 | protected function tearDown(): void |
||
| 34 | { |
||
| 35 | Deprecation::restore_settings($this->oldDeprecation); |
||
| 36 | parent::tearDown(); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function testCount() |
||
| 40 | { |
||
| 41 | |||
| 42 | //basic counting |
||
| 43 | $qry = SQLSelectTest\TestObject::get()->dataQuery()->getFinalisedQuery(); |
||
| 44 | $ids = $this->allFixtureIDs(SQLSelectTest\TestObject::class); |
||
| 45 | $count = $qry->count('"SQLSelectTest_DO"."ID"'); |
||
| 46 | $this->assertEquals(count($ids), $count); |
||
| 47 | $this->assertIsInt($count); |
||
| 48 | //test with `having` |
||
| 49 | if (DB::get_conn() instanceof MySQLDatabase) { |
||
| 50 | $qry->setSelect([ |
||
| 51 | 'Date' => 'MAX("Date")', |
||
| 52 | 'Common' => '"Common"', |
||
| 53 | ]); |
||
| 54 | $qry->setGroupBy('"Common"'); |
||
| 55 | $qry->setHaving('"Date" > 2012-02-01'); |
||
| 56 | $count = $qry->count('"SQLSelectTest_DO"."ID"'); |
||
| 57 | $this->assertEquals(1, $count); |
||
| 58 | $this->assertIsInt($count); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | public function testUnlimitedRowCount() |
||
| 62 | { |
||
| 63 | //basic counting |
||
| 64 | $qry = SQLSelectTest\TestObject::get()->dataQuery()->getFinalisedQuery(); |
||
| 65 | $ids = $this->allFixtureIDs(SQLSelectTest\TestObject::class); |
||
| 66 | $qry->setLimit(1); |
||
| 67 | $count = $qry->unlimitedRowCount('"SQLSelectTest_DO"."ID"'); |
||
| 68 | $this->assertEquals(count($ids), $count); |
||
| 69 | $this->assertIsInt($count); |
||
| 70 | // Test without column - SQLSelect has different logic for this |
||
| 71 | $count = $qry->unlimitedRowCount(); |
||
| 72 | $this->assertEquals(2, $count); |
||
| 73 | $this->assertIsInt($count); |
||
| 74 | //test with `having` |
||
| 75 | if (DB::get_conn() instanceof MySQLDatabase) { |
||
| 76 | $qry->setHaving('"Date" > 2012-02-01'); |
||
| 77 | $count = $qry->unlimitedRowCount('"SQLSelectTest_DO"."ID"'); |
||
| 78 | $this->assertEquals(1, $count); |
||
| 79 | $this->assertIsInt($count); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | public function testEmptyQueryReturnsNothing() |
||
| 84 | { |
||
| 85 | $query = new SQLSelect(); |
||
| 86 | $this->assertSQLEquals('', $query->sql($parameters)); |
||
| 87 | } |
||
| 88 | |||
| 89 | public function testSelectFromBasicTable() |
||
| 90 | { |
||
| 91 | $query = new SQLSelect(); |
||
| 92 | $query->setFrom('MyTable'); |
||
| 93 | $this->assertSQLEquals("SELECT * FROM MyTable", $query->sql($parameters)); |
||
| 94 | $query->addFrom('MyJoin'); |
||
| 95 | $this->assertSQLEquals("SELECT * FROM MyTable MyJoin", $query->sql($parameters)); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function testSelectFromUserSpecifiedFields() |
||
| 104 | } |
||
| 105 | |||
| 106 | public function testSelectWithWhereClauseFilter() |
||
| 107 | { |
||
| 108 | $query = new SQLSelect(); |
||
| 109 | $query->setSelect(["Name","Meta"]); |
||
| 110 | $query->setFrom("MyTable"); |
||
| 111 | $query->setWhere("Name = 'Name'"); |
||
| 112 | $query->addWhere("Meta = 'Test'"); |
||
| 113 | $this->assertSQLEquals( |
||
| 114 | "SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test')", |
||
| 115 | $query->sql($parameters) |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function testSelectWithConstructorParameters() |
||
| 120 | { |
||
| 121 | $query = new SQLSelect(["Foo", "Bar"], "FooBarTable"); |
||
| 122 | $this->assertSQLEquals("SELECT Foo, Bar FROM FooBarTable", $query->sql($parameters)); |
||
| 123 | $query = new SQLSelect(["Foo", "Bar"], "FooBarTable", ["Foo = 'Boo'"]); |
||
| 124 | $this->assertSQLEquals("SELECT Foo, Bar FROM FooBarTable WHERE (Foo = 'Boo')", $query->sql($parameters)); |
||
| 125 | } |
||
| 126 | |||
| 127 | public function testSelectWithChainedMethods() |
||
| 128 | { |
||
| 129 | $query = new SQLSelect(); |
||
| 130 | $query->setSelect("Name", "Meta")->setFrom("MyTable")->setWhere("Name = 'Name'")->addWhere("Meta = 'Test'"); |
||
| 131 | $this->assertSQLEquals( |
||
| 132 | "SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test')", |
||
| 133 | $query->sql($parameters) |
||
| 134 | ); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function testCanSortBy() |
||
| 138 | { |
||
| 139 | $query = new SQLSelect(); |
||
| 140 | $query->setSelect("Name", "Meta")->setFrom("MyTable")->setWhere("Name = 'Name'")->addWhere("Meta = 'Test'"); |
||
| 141 | $this->assertTrue($query->canSortBy('Name ASC')); |
||
| 142 | $this->assertTrue($query->canSortBy('Name')); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Test multiple order by SQL clauses. |
||
| 147 | */ |
||
| 148 | public function testAddOrderBy() |
||
| 149 | { |
||
| 150 | $query = new SQLSelect(); |
||
| 151 | $query->setSelect('ID', "Title")->setFrom('Page')->addOrderBy('(ID % 2) = 0', 'ASC')->addOrderBy('ID > 50', 'ASC'); |
||
| 152 | $this->assertSQLEquals( |
||
| 153 | 'SELECT ID, Title, (ID % 2) = 0 AS "_SortColumn0", ID > 50 AS "_SortColumn1" FROM Page ORDER BY "_SortColumn0" ASC, "_SortColumn1" ASC', |
||
| 154 | $query->sql($parameters) |
||
| 155 | ); |
||
| 156 | } |
||
| 157 | |||
| 158 | public function testSelectWithChainedFilterParameters() |
||
| 159 | { |
||
| 160 | $query = new SQLSelect(); |
||
| 161 | $query->setSelect(["Name","Meta"])->setFrom("MyTable"); |
||
| 162 | $query->setWhere("Name = 'Name'")->addWhere("Meta = 'Test'")->addWhere("Beta != 'Gamma'"); |
||
| 163 | $this->assertSQLEquals( |
||
| 164 | "SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test') AND (Beta != 'Gamma')", |
||
| 165 | $query->sql($parameters) |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | |||
| 169 | public function testSelectWithLimitClause() |
||
| 170 | { |
||
| 171 | if (!(DB::get_conn() instanceof MySQLDatabase || DB::get_conn() instanceof SQLite3Database |
||
| 172 | || DB::get_conn() instanceof PostgreSQLDatabase) |
||
| 173 | ) { |
||
| 174 | $this->markTestIncomplete(); |
||
| 175 | } |
||
| 176 | |||
| 177 | $query = new SQLSelect(); |
||
| 178 | $query->setFrom("MyTable"); |
||
| 179 | $query->setLimit(99); |
||
| 180 | $this->assertSQLEquals("SELECT * FROM MyTable LIMIT 99", $query->sql($parameters)); |
||
| 181 | |||
| 182 | // array limit with start (MySQL specific) |
||
| 183 | $query = new SQLSelect(); |
||
| 184 | $query->setFrom("MyTable"); |
||
| 185 | $query->setLimit(99, 97); |
||
| 186 | $this->assertSQLEquals("SELECT * FROM MyTable LIMIT 99 OFFSET 97", $query->sql($parameters)); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function testSelectWithOrderbyClause() |
||
| 190 | { |
||
| 191 | $query = new SQLSelect(); |
||
| 192 | $query->setFrom("MyTable"); |
||
| 193 | $query->setOrderBy('MyName'); |
||
| 194 | $this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName ASC', $query->sql($parameters)); |
||
| 195 | |||
| 196 | $query = new SQLSelect(); |
||
| 197 | $query->setFrom("MyTable"); |
||
| 198 | $query->setOrderBy('MyName desc'); |
||
| 199 | $this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName DESC', $query->sql($parameters)); |
||
| 200 | |||
| 201 | $query = new SQLSelect(); |
||
| 202 | $query->setFrom("MyTable"); |
||
| 203 | $query->setOrderBy('MyName ASC, Color DESC'); |
||
| 204 | $this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName ASC, Color DESC', $query->sql($parameters)); |
||
| 205 | |||
| 206 | $query = new SQLSelect(); |
||
| 207 | $query->setFrom("MyTable"); |
||
| 208 | $query->setOrderBy('MyName ASC, Color'); |
||
| 209 | $this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName ASC, Color ASC', $query->sql($parameters)); |
||
| 210 | |||
| 211 | $query = new SQLSelect(); |
||
| 212 | $query->setFrom("MyTable"); |
||
| 213 | $query->setOrderBy(['MyName' => 'desc']); |
||
| 214 | $this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName DESC', $query->sql($parameters)); |
||
| 215 | |||
| 216 | $query = new SQLSelect(); |
||
| 217 | $query->setFrom("MyTable"); |
||
| 218 | $query->setOrderBy(['MyName' => 'desc', 'Color']); |
||
| 219 | $this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName DESC, Color ASC', $query->sql($parameters)); |
||
| 220 | |||
| 221 | $query = new SQLSelect(); |
||
| 222 | $query->setFrom("MyTable"); |
||
| 223 | $query->setOrderBy('implode("MyName","Color")'); |
||
| 224 | $this->assertSQLEquals( |
||
| 225 | 'SELECT *, implode("MyName","Color") AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" ASC', |
||
| 226 | $query->sql($parameters) |
||
| 227 | ); |
||
| 228 | |||
| 229 | $query = new SQLSelect(); |
||
| 230 | $query->setFrom("MyTable"); |
||
| 231 | $query->setOrderBy('implode("MyName","Color") DESC'); |
||
| 232 | $this->assertSQLEquals( |
||
| 233 | 'SELECT *, implode("MyName","Color") AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" DESC', |
||
| 234 | $query->sql($parameters) |
||
| 235 | ); |
||
| 236 | |||
| 237 | $query = new SQLSelect(); |
||
| 238 | $query->setFrom("MyTable"); |
||
| 239 | $query->setOrderBy('RAND()'); |
||
| 240 | $this->assertSQLEquals( |
||
| 241 | 'SELECT *, RAND() AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" ASC', |
||
| 242 | $query->sql($parameters) |
||
| 243 | ); |
||
| 244 | |||
| 245 | $query = new SQLSelect(); |
||
| 246 | $query->setFrom("MyTable"); |
||
| 247 | $query->addFrom('INNER JOIN SecondTable USING (ID)'); |
||
| 248 | $query->addFrom('INNER JOIN ThirdTable USING (ID)'); |
||
| 249 | $query->setOrderBy('MyName'); |
||
| 250 | $this->assertSQLEquals( |
||
| 251 | 'SELECT * FROM MyTable ' |
||
| 252 | . 'INNER JOIN SecondTable USING (ID) ' |
||
| 253 | . 'INNER JOIN ThirdTable USING (ID) ' |
||
| 254 | . 'ORDER BY MyName ASC', |
||
| 255 | $query->sql($parameters) |
||
| 256 | ); |
||
| 257 | } |
||
| 258 | |||
| 259 | public function testNullLimit() |
||
| 268 | ); |
||
| 269 | } |
||
| 270 | |||
| 271 | public function testNegativeLimit() |
||
| 272 | { |
||
| 273 | $this->expectException(\InvalidArgumentException::class); |
||
| 274 | $query = new SQLSelect(); |
||
| 275 | $query->setLimit(-10); |
||
| 276 | } |
||
| 277 | |||
| 278 | public function testNegativeOffset() |
||
| 279 | { |
||
| 280 | $this->expectException(\InvalidArgumentException::class); |
||
| 281 | $query = new SQLSelect(); |
||
| 282 | $query->setLimit(1, -10); |
||
| 283 | } |
||
| 284 | |||
| 285 | public function testNegativeOffsetAndLimit() |
||
| 286 | { |
||
| 287 | $this->expectException(\InvalidArgumentException::class); |
||
| 288 | $query = new SQLSelect(); |
||
| 289 | $query->setLimit(-10, -10); |
||
| 290 | } |
||
| 291 | |||
| 292 | public function testReverseOrderBy() |
||
| 293 | { |
||
| 294 | $query = new SQLSelect(); |
||
| 295 | $query->setFrom('MyTable'); |
||
| 296 | |||
| 297 | // default is ASC |
||
| 298 | $query->setOrderBy("Name"); |
||
| 299 | $query->reverseOrderBy(); |
||
| 300 | |||
| 301 | $this->assertSQLEquals('SELECT * FROM MyTable ORDER BY Name DESC', $query->sql($parameters)); |
||
| 302 | |||
| 303 | $query->setOrderBy("Name DESC"); |
||
| 304 | $query->reverseOrderBy(); |
||
| 305 | |||
| 306 | $this->assertSQLEquals('SELECT * FROM MyTable ORDER BY Name ASC', $query->sql($parameters)); |
||
| 307 | |||
| 308 | $query->setOrderBy(["Name" => "ASC"]); |
||
| 309 | $query->reverseOrderBy(); |
||
| 310 | |||
| 311 | $this->assertSQLEquals('SELECT * FROM MyTable ORDER BY Name DESC', $query->sql($parameters)); |
||
| 312 | |||
| 313 | $query->setOrderBy(["Name" => 'DESC', 'Color' => 'asc']); |
||
| 314 | $query->reverseOrderBy(); |
||
| 315 | |||
| 316 | $this->assertSQLEquals('SELECT * FROM MyTable ORDER BY Name ASC, Color DESC', $query->sql($parameters)); |
||
| 317 | |||
| 318 | $query->setOrderBy('implode("MyName","Color") DESC'); |
||
| 319 | $query->reverseOrderBy(); |
||
| 320 | |||
| 321 | $this->assertSQLEquals( |
||
| 322 | 'SELECT *, implode("MyName","Color") AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" ASC', |
||
| 323 | $query->sql($parameters) |
||
| 324 | ); |
||
| 325 | } |
||
| 326 | |||
| 327 | public function testFiltersOnID() |
||
| 328 | { |
||
| 329 | $query = new SQLSelect(); |
||
| 330 | $query->setWhere("ID = 5"); |
||
| 331 | $this->assertTrue( |
||
| 332 | $query->filtersOnID(), |
||
| 333 | "filtersOnID() is true with simple unquoted column name" |
||
| 334 | ); |
||
| 335 | |||
| 336 | $query = new SQLSelect(); |
||
| 337 | $query->setWhere('"ID" = 5'); |
||
| 338 | $this->assertTrue( |
||
| 339 | $query->filtersOnID(), |
||
| 340 | "filtersOnID() is true with simple quoted column name" |
||
| 341 | ); |
||
| 342 | |||
| 343 | $query = new SQLSelect(); |
||
| 344 | $query->setWhere(['"ID"' => 4]); |
||
| 345 | $this->assertTrue( |
||
| 346 | $query->filtersOnID(), |
||
| 347 | "filtersOnID() is true with parameterised quoted column name" |
||
| 348 | ); |
||
| 349 | |||
| 350 | $query = new SQLSelect(); |
||
| 351 | $query->setWhere(['"ID" = ?' => 4]); |
||
| 352 | $this->assertTrue( |
||
| 353 | $query->filtersOnID(), |
||
| 354 | "filtersOnID() is true with parameterised quoted column name" |
||
| 355 | ); |
||
| 356 | |||
| 357 | $query = new SQLSelect(); |
||
| 358 | $query->setWhere('"ID" IN (5,4)'); |
||
| 359 | $this->assertTrue( |
||
| 360 | $query->filtersOnID(), |
||
| 361 | "filtersOnID() is true with WHERE ID IN" |
||
| 362 | ); |
||
| 363 | |||
| 364 | $query = new SQLSelect(); |
||
| 365 | $query->setWhere(['"ID" IN ?' => [1,2]]); |
||
| 366 | $this->assertTrue( |
||
| 367 | $query->filtersOnID(), |
||
| 368 | "filtersOnID() is true with parameterised WHERE ID IN" |
||
| 369 | ); |
||
| 370 | |||
| 371 | $query = new SQLSelect(); |
||
| 372 | $query->setWhere("ID=5"); |
||
| 373 | $this->assertTrue( |
||
| 374 | $query->filtersOnID(), |
||
| 375 | "filtersOnID() is true with simple unquoted column name and no spaces in equals sign" |
||
| 376 | ); |
||
| 377 | |||
| 378 | $query = new SQLSelect(); |
||
| 379 | $query->setWhere("Identifier = 5"); |
||
| 380 | $this->assertFalse( |
||
| 381 | $query->filtersOnID(), |
||
| 382 | "filtersOnID() is false with custom column name (starting with 'id')" |
||
| 383 | ); |
||
| 384 | |||
| 385 | $query = new SQLSelect(); |
||
| 386 | $query->setWhere("ParentID = 5"); |
||
| 387 | $this->assertFalse( |
||
| 388 | $query->filtersOnID(), |
||
| 389 | "filtersOnID() is false with column name ending in 'ID'" |
||
| 390 | ); |
||
| 391 | |||
| 392 | $query = new SQLSelect(); |
||
| 393 | $query->setWhere("MyTable.ID = 5"); |
||
| 394 | $this->assertTrue( |
||
| 395 | $query->filtersOnID(), |
||
| 396 | "filtersOnID() is true with table and column name" |
||
| 397 | ); |
||
| 398 | |||
| 399 | $query = new SQLSelect(); |
||
| 400 | $query->setWhere("MyTable.ID = 5"); |
||
| 401 | $this->assertTrue( |
||
| 402 | $query->filtersOnID(), |
||
| 403 | "filtersOnID() is true with table and quoted column name " |
||
| 404 | ); |
||
| 405 | } |
||
| 406 | |||
| 407 | public function testFiltersOnFK() |
||
| 408 | { |
||
| 409 | $query = new SQLSelect(); |
||
| 410 | $query->setWhere("ID = 5"); |
||
| 411 | $this->assertFalse( |
||
| 412 | $query->filtersOnFK(), |
||
| 413 | "filtersOnFK() is true with simple unquoted column name" |
||
| 414 | ); |
||
| 415 | |||
| 416 | $query = new SQLSelect(); |
||
| 417 | $query->setWhere("Identifier = 5"); |
||
| 418 | $this->assertFalse( |
||
| 419 | $query->filtersOnFK(), |
||
| 420 | "filtersOnFK() is false with custom column name (starting with 'id')" |
||
| 421 | ); |
||
| 422 | |||
| 423 | $query = new SQLSelect(); |
||
| 424 | $query->setWhere("MyTable.ParentID = 5"); |
||
| 425 | $this->assertTrue( |
||
| 426 | $query->filtersOnFK(), |
||
| 427 | "filtersOnFK() is true with table and column name" |
||
| 428 | ); |
||
| 429 | |||
| 430 | $query = new SQLSelect(); |
||
| 431 | $query->setWhere("MyTable.`ParentID`= 5"); |
||
| 432 | $this->assertTrue( |
||
| 433 | $query->filtersOnFK(), |
||
| 434 | "filtersOnFK() is true with table and quoted column name " |
||
| 435 | ); |
||
| 436 | } |
||
| 437 | |||
| 438 | public function testInnerJoin() |
||
| 462 | ); |
||
| 463 | } |
||
| 464 | |||
| 465 | public function testJoinSubSelect() |
||
| 466 | { |
||
| 467 | |||
| 468 | $query = new SQLSelect(); |
||
| 469 | $query->setFrom('MyTable'); |
||
| 470 | $query->addInnerJoin( |
||
| 471 | '(SELECT * FROM MyOtherTable)', |
||
| 472 | 'Mot.MyTableID = MyTable.ID', |
||
| 473 | 'Mot' |
||
| 474 | ); |
||
| 475 | $query->addLeftJoin( |
||
| 476 | '(SELECT MyLastTable.MyOtherTableID, COUNT(1) as MyLastTableCount FROM MyLastTable ' |
||
| 477 | . 'GROUP BY MyOtherTableID)', |
||
| 478 | 'Mlt.MyOtherTableID = Mot.ID', |
||
| 479 | 'Mlt' |
||
| 480 | ); |
||
| 481 | $query->setOrderBy('COALESCE(Mlt.MyLastTableCount, 0) DESC'); |
||
| 482 | |||
| 483 | $this->assertSQLEquals( |
||
| 484 | 'SELECT *, COALESCE(Mlt.MyLastTableCount, 0) AS "_SortColumn0" FROM MyTable ' . |
||
| 485 | 'INNER JOIN (SELECT * FROM MyOtherTable) AS "Mot" ON Mot.MyTableID = MyTable.ID ' . |
||
| 486 | 'LEFT JOIN (SELECT MyLastTable.MyOtherTableID, COUNT(1) as MyLastTableCount FROM MyLastTable ' |
||
| 487 | . 'GROUP BY MyOtherTableID) AS "Mlt" ON Mlt.MyOtherTableID = Mot.ID ' . |
||
| 488 | 'ORDER BY "_SortColumn0" DESC', |
||
| 489 | $query->sql($parameters) |
||
| 490 | ); |
||
| 491 | } |
||
| 492 | |||
| 493 | public function testSetWhereAny() |
||
| 494 | { |
||
| 495 | $query = new SQLSelect(); |
||
| 496 | $query->setFrom('MyTable'); |
||
| 497 | |||
| 498 | $query->setWhereAny( |
||
| 499 | [ |
||
| 500 | 'Monkey' => 'Chimp', |
||
| 501 | 'Color' => 'Brown' |
||
| 502 | ] |
||
| 503 | ); |
||
| 504 | $sql = $query->sql($parameters); |
||
| 505 | $this->assertSQLEquals("SELECT * FROM MyTable WHERE ((Monkey = ?) OR (Color = ?))", $sql); |
||
| 506 | $this->assertEquals(['Chimp', 'Brown'], $parameters); |
||
| 507 | } |
||
| 508 | |||
| 509 | public function testSelectFirst() |
||
| 510 | { |
||
| 511 | // Test first from sequence |
||
| 512 | $query = new SQLSelect(); |
||
| 513 | $query->setFrom('"SQLSelectTest_DO"'); |
||
| 514 | $query->setOrderBy('"Name"'); |
||
| 515 | $result = $query->firstRow()->execute(); |
||
| 516 | |||
| 517 | $records = []; |
||
| 518 | foreach ($result as $row) { |
||
| 519 | $records[] = $row; |
||
| 520 | } |
||
| 521 | |||
| 522 | $this->assertCount(1, $records); |
||
| 523 | $this->assertEquals('Object 1', $records[0]['Name']); |
||
| 524 | |||
| 525 | // Test first from empty sequence |
||
| 526 | $query = new SQLSelect(); |
||
| 527 | $query->setFrom('"SQLSelectTest_DO"'); |
||
| 528 | $query->setOrderBy('"Name"'); |
||
| 529 | $query->setWhere(['"Name"' => 'Nonexistent Object']); |
||
| 530 | $result = $query->firstRow()->execute(); |
||
| 531 | |||
| 532 | $records = []; |
||
| 533 | foreach ($result as $row) { |
||
| 534 | $records[] = $row; |
||
| 535 | } |
||
| 536 | |||
| 537 | $this->assertCount(0, $records); |
||
| 538 | |||
| 539 | // Test that given the last item, the 'first' in this list matches the last |
||
| 540 | $query = new SQLSelect(); |
||
| 541 | $query->setFrom('"SQLSelectTest_DO"'); |
||
| 542 | $query->setOrderBy('"Name"'); |
||
| 543 | $query->setLimit(1, 1); |
||
| 544 | $result = $query->firstRow()->execute(); |
||
| 545 | |||
| 546 | $records = []; |
||
| 547 | foreach ($result as $row) { |
||
| 548 | $records[] = $row; |
||
| 549 | } |
||
| 550 | |||
| 551 | $this->assertCount(1, $records); |
||
| 552 | $this->assertEquals('Object 2', $records[0]['Name']); |
||
| 553 | } |
||
| 554 | |||
| 555 | public function testSelectLast() |
||
| 556 | { |
||
| 557 | // Test last in sequence |
||
| 558 | $query = new SQLSelect(); |
||
| 559 | $query->setFrom('"SQLSelectTest_DO"'); |
||
| 560 | $query->setOrderBy('"Name"'); |
||
| 561 | $result = $query->lastRow()->execute(); |
||
| 562 | |||
| 563 | $records = []; |
||
| 564 | foreach ($result as $row) { |
||
| 565 | $records[] = $row; |
||
| 566 | } |
||
| 567 | |||
| 568 | $this->assertCount(1, $records); |
||
| 569 | $this->assertEquals('Object 2', $records[0]['Name']); |
||
| 570 | |||
| 571 | // Test last from empty sequence |
||
| 572 | $query = new SQLSelect(); |
||
| 573 | $query->setFrom('"SQLSelectTest_DO"'); |
||
| 574 | $query->setOrderBy('"Name"'); |
||
| 575 | $query->setWhere(["\"Name\" = 'Nonexistent Object'"]); |
||
| 576 | $result = $query->lastRow()->execute(); |
||
| 577 | |||
| 578 | $records = []; |
||
| 579 | foreach ($result as $row) { |
||
| 580 | $records[] = $row; |
||
| 581 | } |
||
| 582 | |||
| 583 | $this->assertCount(0, $records); |
||
| 584 | |||
| 585 | // Test that given the first item, the 'last' in this list matches the first |
||
| 586 | $query = new SQLSelect(); |
||
| 587 | $query->setFrom('"SQLSelectTest_DO"'); |
||
| 588 | $query->setOrderBy('"Name"'); |
||
| 589 | $query->setLimit(1); |
||
| 590 | $result = $query->lastRow()->execute(); |
||
| 591 | |||
| 592 | $records = []; |
||
| 593 | foreach ($result as $row) { |
||
| 594 | $records[] = $row; |
||
| 595 | } |
||
| 596 | |||
| 597 | $this->assertCount(1, $records); |
||
| 598 | $this->assertEquals('Object 1', $records[0]['Name']); |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Tests aggregate() function |
||
| 603 | */ |
||
| 604 | public function testAggregate() |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Tests that an ORDER BY is only added if a LIMIT is set. |
||
| 617 | */ |
||
| 618 | public function testAggregateNoOrderByIfNoLimit() |
||
| 619 | { |
||
| 620 | $query = new SQLSelect(); |
||
| 621 | $query->setFrom('"SQLSelectTest_DO"'); |
||
| 622 | $query->setOrderBy('Common'); |
||
| 623 | $query->setLimit([]); |
||
| 624 | |||
| 625 | $aggregate = $query->aggregate('MAX("ID")'); |
||
| 626 | $limit = $aggregate->getLimit(); |
||
| 627 | $this->assertEquals([], $aggregate->getOrderBy()); |
||
| 628 | $this->assertEquals([], $limit); |
||
| 629 | |||
| 630 | $query = new SQLSelect(); |
||
| 631 | $query->setFrom('"SQLSelectTest_DO"'); |
||
| 632 | $query->setOrderBy('Common'); |
||
| 633 | $query->setLimit(2); |
||
| 634 | |||
| 635 | $aggregate = $query->aggregate('MAX("ID")'); |
||
| 636 | $limit = $aggregate->getLimit(); |
||
| 637 | $this->assertEquals(['Common' => 'ASC'], $aggregate->getOrderBy()); |
||
| 638 | $this->assertEquals(['start' => 0, 'limit' => 2], $limit); |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Test that "_SortColumn0" is added for an aggregate in the ORDER BY |
||
| 643 | * clause, in combination with a LIMIT and GROUP BY clause. |
||
| 644 | * For some databases, like MSSQL, this is a complicated scenario |
||
| 645 | * because a subselect needs to be done to query paginated data. |
||
| 646 | */ |
||
| 647 | public function testOrderByContainingAggregateAndLimitOffset() |
||
| 648 | { |
||
| 649 | $query = new SQLSelect(); |
||
| 650 | $query->setSelect(['"Name"', '"Meta"']); |
||
| 651 | $query->setFrom('"SQLSelectTest_DO"'); |
||
| 652 | $query->setOrderBy(['MAX("Date")']); |
||
| 653 | $query->setGroupBy(['"Name"', '"Meta"']); |
||
| 654 | $query->setLimit('1', '1'); |
||
| 655 | |||
| 656 | $records = []; |
||
| 657 | foreach ($query->execute() as $record) { |
||
| 658 | $records[] = $record; |
||
| 659 | } |
||
| 660 | |||
| 661 | $this->assertCount(1, $records); |
||
| 662 | |||
| 663 | $this->assertEquals('Object 2', $records[0]['Name']); |
||
| 664 | $this->assertEquals('2012-05-01 09:00:00', $records['0']['_SortColumn0']); |
||
| 665 | } |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Test that multiple order elements are maintained in the given order |
||
| 669 | */ |
||
| 670 | public function testOrderByMultiple() |
||
| 671 | { |
||
| 672 | if (DB::get_conn() instanceof MySQLDatabase) { |
||
| 673 | $query = new SQLSelect(); |
||
| 674 | $query->setSelect(['"Name"', '"Meta"']); |
||
| 675 | $query->setFrom('"SQLSelectTest_DO"'); |
||
| 676 | $query->setOrderBy(['MID("Name", 8, 1) DESC', '"Name" ASC']); |
||
| 677 | |||
| 678 | $records = []; |
||
| 679 | foreach ($query->execute() as $record) { |
||
| 680 | $records[] = $record; |
||
| 681 | } |
||
| 682 | |||
| 683 | $this->assertCount(2, $records); |
||
| 684 | |||
| 685 | $this->assertEquals('Object 2', $records[0]['Name']); |
||
| 686 | $this->assertEquals('2', $records[0]['_SortColumn0']); |
||
| 687 | |||
| 688 | $this->assertEquals('Object 1', $records[1]['Name']); |
||
| 689 | $this->assertEquals('1', $records[1]['_SortColumn0']); |
||
| 690 | } |
||
| 691 | } |
||
| 692 | |||
| 693 | public function testSelect() |
||
| 694 | { |
||
| 695 | $query = new SQLSelect('"Title"', '"MyTable"'); |
||
| 696 | $query->addSelect('"TestField"'); |
||
| 697 | $this->assertSQLEquals( |
||
| 698 | 'SELECT "Title", "TestField" FROM "MyTable"', |
||
| 699 | $query->sql() |
||
| 700 | ); |
||
| 701 | |||
| 702 | // Test replacement of select |
||
| 703 | $query->setSelect( |
||
| 704 | [ |
||
| 705 | 'Field' => '"Field"', |
||
| 706 | 'AnotherAlias' => '"AnotherField"' |
||
| 707 | ] |
||
| 708 | ); |
||
| 709 | $this->assertSQLEquals( |
||
| 710 | 'SELECT "Field", "AnotherField" AS "AnotherAlias" FROM "MyTable"', |
||
| 711 | $query->sql() |
||
| 712 | ); |
||
| 713 | |||
| 714 | // Check that ' as ' selects don't get mistaken as aliases |
||
| 715 | $query->addSelect( |
||
| 716 | [ |
||
| 717 | 'Relevance' => "MATCH (Title, MenuTitle) AGAINST ('Two as One')" |
||
| 718 | ] |
||
| 719 | ); |
||
| 720 | $this->assertSQLEquals( |
||
| 721 | 'SELECT "Field", "AnotherField" AS "AnotherAlias", MATCH (Title, MenuTitle) AGAINST (' . |
||
| 722 | '\'Two as One\') AS "Relevance" FROM "MyTable"', |
||
| 723 | $query->sql() |
||
| 724 | ); |
||
| 725 | } |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Test passing in a LIMIT with OFFSET clause string. |
||
| 729 | */ |
||
| 730 | public function testLimitSetFromClauseString() |
||
| 740 | } |
||
| 741 | |||
| 742 | public function testParameterisedInnerJoins() |
||
| 743 | { |
||
| 744 | $query = new SQLSelect(); |
||
| 745 | $query->setSelect(['"SQLSelectTest_DO"."Name"', '"SubSelect"."Count"']); |
||
| 746 | $query->setFrom('"SQLSelectTest_DO"'); |
||
| 747 | $query->addInnerJoin( |
||
| 748 | '(SELECT "Title", COUNT(*) AS "Count" FROM "SQLSelectTestBase" GROUP BY "Title" HAVING "Title" NOT LIKE ?)', |
||
| 749 | '"SQLSelectTest_DO"."Name" = "SubSelect"."Title"', |
||
| 750 | 'SubSelect', |
||
| 751 | 20, |
||
| 752 | ['%MyName%'] |
||
| 753 | ); |
||
| 754 | $query->addWhere(['"SQLSelectTest_DO"."Date" > ?' => '2012-08-08 12:00']); |
||
| 755 | |||
| 756 | $this->assertSQLEquals( |
||
| 757 | 'SELECT "SQLSelectTest_DO"."Name", "SubSelect"."Count" |
||
| 758 | FROM "SQLSelectTest_DO" INNER JOIN (SELECT "Title", COUNT(*) AS "Count" FROM "SQLSelectTestBase" |
||
| 759 | GROUP BY "Title" HAVING "Title" NOT LIKE ?) AS "SubSelect" ON "SQLSelectTest_DO"."Name" = |
||
| 760 | "SubSelect"."Title" |
||
| 761 | WHERE ("SQLSelectTest_DO"."Date" > ?)', |
||
| 762 | $query->sql($parameters) |
||
| 763 | ); |
||
| 764 | $this->assertEquals(['%MyName%', '2012-08-08 12:00'], $parameters); |
||
| 765 | $query->execute(); |
||
| 766 | } |
||
| 767 | |||
| 768 | public function testParameterisedLeftJoins() |
||
| 769 | { |
||
| 770 | $query = new SQLSelect(); |
||
| 771 | $query->setSelect(['"SQLSelectTest_DO"."Name"', '"SubSelect"."Count"']); |
||
| 772 | $query->setFrom('"SQLSelectTest_DO"'); |
||
| 773 | $query->addLeftJoin( |
||
| 774 | '(SELECT "Title", COUNT(*) AS "Count" FROM "SQLSelectTestBase" GROUP BY "Title" HAVING "Title" NOT LIKE ?)', |
||
| 775 | '"SQLSelectTest_DO"."Name" = "SubSelect"."Title"', |
||
| 776 | 'SubSelect', |
||
| 777 | 20, |
||
| 778 | ['%MyName%'] |
||
| 779 | ); |
||
| 780 | $query->addWhere(['"SQLSelectTest_DO"."Date" > ?' => '2012-08-08 12:00']); |
||
| 781 | |||
| 782 | $this->assertSQLEquals( |
||
| 783 | 'SELECT "SQLSelectTest_DO"."Name", "SubSelect"."Count" |
||
| 784 | FROM "SQLSelectTest_DO" LEFT JOIN (SELECT "Title", COUNT(*) AS "Count" FROM "SQLSelectTestBase" |
||
| 785 | GROUP BY "Title" HAVING "Title" NOT LIKE ?) AS "SubSelect" ON "SQLSelectTest_DO"."Name" = |
||
| 786 | "SubSelect"."Title" |
||
| 787 | WHERE ("SQLSelectTest_DO"."Date" > ?)', |
||
| 788 | $query->sql($parameters) |
||
| 789 | ); |
||
| 790 | $this->assertEquals(['%MyName%', '2012-08-08 12:00'], $parameters); |
||
| 791 | $query->execute(); |
||
| 792 | } |
||
| 793 | |||
| 794 | public function testBaseTableAliases() |
||
| 827 | ); |
||
| 828 | } |
||
| 829 | } |
||
| 830 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths