| Total Complexity | 59 | 
| Total Lines | 418 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like ExprTest 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 ExprTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 17 | class ExprTest extends OrmTestCase | ||
| 18 | { | ||
| 19 | private $em; | ||
| 20 | |||
| 21 | /** @var Expr */ | ||
| 22 | private $expr; | ||
| 23 | |||
| 24 | protected function setUp() : void | ||
| 25 |     { | ||
| 26 | $this->em = $this->getTestEntityManager(); | ||
| 27 | $this->expr = new Expr(); | ||
| 28 | } | ||
| 29 | |||
| 30 | public function testAvgExpr() : void | ||
| 31 |     { | ||
| 32 |         self::assertEquals('AVG(u.id)', (string) $this->expr->avg('u.id')); | ||
| 33 | } | ||
| 34 | |||
| 35 | public function testMaxExpr() : void | ||
| 36 |     { | ||
| 37 |         self::assertEquals('MAX(u.id)', (string) $this->expr->max('u.id')); | ||
| 38 | } | ||
| 39 | |||
| 40 | public function testMinExpr() : void | ||
| 41 |     { | ||
| 42 |         self::assertEquals('MIN(u.id)', (string) $this->expr->min('u.id')); | ||
| 43 | } | ||
| 44 | |||
| 45 | public function testCountExpr() : void | ||
| 46 |     { | ||
| 47 |         self::assertEquals('MAX(u.id)', (string) $this->expr->max('u.id')); | ||
| 48 | } | ||
| 49 | |||
| 50 | public function testCountDistinctExpr() : void | ||
| 51 |     { | ||
| 52 |         self::assertEquals('COUNT(DISTINCT u.id)', (string) $this->expr->countDistinct('u.id')); | ||
| 53 | } | ||
| 54 | |||
| 55 | public function testCountDistinctExprMulti() : void | ||
| 56 |     { | ||
| 57 |         self::assertEquals('COUNT(DISTINCT u.id, u.name)', (string) $this->expr->countDistinct('u.id', 'u.name')); | ||
| 58 | } | ||
| 59 | |||
| 60 | public function testExistsExpr() : void | ||
| 61 |     { | ||
| 62 | $qb = $this->em->createQueryBuilder(); | ||
| 63 |         $qb->select('u')->from('User', 'u')->where('u.name = ?1'); | ||
| 64 | |||
| 65 |         self::assertEquals('EXISTS(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->expr->exists($qb)); | ||
| 66 | } | ||
| 67 | |||
| 68 | public function testAllExpr() : void | ||
| 69 |     { | ||
| 70 | $qb = $this->em->createQueryBuilder(); | ||
| 71 |         $qb->select('u')->from('User', 'u')->where('u.name = ?1'); | ||
| 72 | |||
| 73 |         self::assertEquals('ALL(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->expr->all($qb)); | ||
| 74 | } | ||
| 75 | |||
| 76 | public function testSomeExpr() : void | ||
| 77 |     { | ||
| 78 | $qb = $this->em->createQueryBuilder(); | ||
| 79 |         $qb->select('u')->from('User', 'u')->where('u.name = ?1'); | ||
| 80 | |||
| 81 |         self::assertEquals('SOME(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->expr->some($qb)); | ||
| 82 | } | ||
| 83 | |||
| 84 | public function testAnyExpr() : void | ||
| 85 |     { | ||
| 86 | $qb = $this->em->createQueryBuilder(); | ||
| 87 |         $qb->select('u')->from('User', 'u')->where('u.name = ?1'); | ||
| 88 | |||
| 89 |         self::assertEquals('ANY(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->expr->any($qb)); | ||
| 90 | } | ||
| 91 | |||
| 92 | public function testNotExpr() : void | ||
| 93 |     { | ||
| 94 | $qb = $this->em->createQueryBuilder(); | ||
| 95 |         $qb->select('u')->from('User', 'u')->where('u.name = ?1'); | ||
| 96 | |||
| 97 |         self::assertEquals('NOT(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->expr->not($qb)); | ||
| 98 | } | ||
| 99 | |||
| 100 | public function testAndExpr() : void | ||
| 101 |     { | ||
| 102 |         self::assertEquals('1 = 1 AND 2 = 2', (string) $this->expr->andX((string) $this->expr->eq(1, 1), (string) $this->expr->eq(2, 2))); | ||
| 103 | } | ||
| 104 | |||
| 105 | public function testIntelligentParenthesisPreventionAndExpr() : void | ||
| 106 |     { | ||
| 107 | self::assertEquals( | ||
| 108 | '1 = 1 AND 2 = 2', | ||
| 109 | (string) $this->expr->andX($this->expr->orX($this->expr->andX($this->expr->eq(1, 1))), (string) $this->expr->eq(2, 2)) | ||
| 110 | ); | ||
| 111 | } | ||
| 112 | |||
| 113 | public function testOrExpr() : void | ||
| 114 |     { | ||
| 115 |         self::assertEquals('1 = 1 OR 2 = 2', (string) $this->expr->orX((string) $this->expr->eq(1, 1), (string) $this->expr->eq(2, 2))); | ||
| 116 | } | ||
| 117 | |||
| 118 | public function testAbsExpr() : void | ||
| 119 |     { | ||
| 120 |         self::assertEquals('ABS(1)', (string) $this->expr->abs(1)); | ||
| 121 | } | ||
| 122 | |||
| 123 | public function testProdExpr() : void | ||
| 124 |     { | ||
| 125 |         self::assertEquals('1 * 2', (string) $this->expr->prod(1, 2)); | ||
| 126 | } | ||
| 127 | |||
| 128 | public function testDiffExpr() : void | ||
| 129 |     { | ||
| 130 |         self::assertEquals('1 - 2', (string) $this->expr->diff(1, 2)); | ||
| 131 | } | ||
| 132 | |||
| 133 | public function testSumExpr() : void | ||
| 134 |     { | ||
| 135 |         self::assertEquals('1 + 2', (string) $this->expr->sum(1, 2)); | ||
| 136 | } | ||
| 137 | |||
| 138 | public function testQuotientExpr() : void | ||
| 139 |     { | ||
| 140 |         self::assertEquals('10 / 2', (string) $this->expr->quot(10, 2)); | ||
| 141 | } | ||
| 142 | |||
| 143 | public function testScopeInArithmeticExpr() : void | ||
| 144 |     { | ||
| 145 |         self::assertEquals('(100 - 20) / 2', (string) $this->expr->quot($this->expr->diff(100, 20), 2)); | ||
| 146 |         self::assertEquals('100 - (20 / 2)', (string) $this->expr->diff(100, $this->expr->quot(20, 2))); | ||
| 147 | } | ||
| 148 | |||
| 149 | public function testSquareRootExpr() : void | ||
| 150 |     { | ||
| 151 |         self::assertEquals('SQRT(1)', (string) $this->expr->sqrt(1)); | ||
| 152 | } | ||
| 153 | |||
| 154 | public function testEqualExpr() : void | ||
| 155 |     { | ||
| 156 |         self::assertEquals('1 = 1', (string) $this->expr->eq(1, 1)); | ||
| 157 | } | ||
| 158 | |||
| 159 | public function testLikeExpr() : void | ||
| 160 |     { | ||
| 161 |         self::assertEquals('a.description LIKE :description', (string) $this->expr->like('a.description', ':description')); | ||
| 162 | } | ||
| 163 | |||
| 164 | public function testNotLikeExpr() : void | ||
| 165 |     { | ||
| 166 |         self::assertEquals('a.description NOT LIKE :description', (string) $this->expr->notLike('a.description', ':description')); | ||
| 167 | } | ||
| 168 | |||
| 169 | public function testConcatExpr() : void | ||
| 170 |     { | ||
| 171 |         self::assertEquals('CONCAT(u.first_name, u.last_name)', (string) $this->expr->concat('u.first_name', 'u.last_name')); | ||
| 172 |         self::assertEquals('CONCAT(u.first_name, u.middle_name, u.last_name)', (string) $this->expr->concat('u.first_name', 'u.middle_name', 'u.last_name')); | ||
| 173 | } | ||
| 174 | |||
| 175 | public function testSubstringExpr() : void | ||
| 176 |     { | ||
| 177 |         self::assertEquals('SUBSTRING(a.title, 0, 25)', (string) $this->expr->substring('a.title', 0, 25)); | ||
| 178 | } | ||
| 179 | |||
| 180 | /** | ||
| 181 | * @group regression | ||
| 182 | * @group DDC-612 | ||
| 183 | */ | ||
| 184 | public function testSubstringExprAcceptsTwoArguments() : void | ||
| 185 |     { | ||
| 186 |         self::assertEquals('SUBSTRING(a.title, 5)', (string) $this->expr->substring('a.title', 5)); | ||
| 187 | } | ||
| 188 | |||
| 189 | public function testLowerExpr() : void | ||
| 190 |     { | ||
| 191 |         self::assertEquals('LOWER(u.first_name)', (string) $this->expr->lower('u.first_name')); | ||
| 192 | } | ||
| 193 | |||
| 194 | public function testUpperExpr() : void | ||
| 195 |     { | ||
| 196 |         self::assertEquals('UPPER(u.first_name)', (string) $this->expr->upper('u.first_name')); | ||
| 197 | } | ||
| 198 | |||
| 199 | public function testLengthExpr() : void | ||
| 200 |     { | ||
| 201 |         self::assertEquals('LENGTH(u.first_name)', (string) $this->expr->length('u.first_name')); | ||
| 202 | } | ||
| 203 | |||
| 204 | public function testGreaterThanExpr() : void | ||
| 205 |     { | ||
| 206 |         self::assertEquals('5 > 2', (string) $this->expr->gt(5, 2)); | ||
| 207 | } | ||
| 208 | |||
| 209 | public function testLessThanExpr() : void | ||
| 210 |     { | ||
| 211 |         self::assertEquals('2 < 5', (string) $this->expr->lt(2, 5)); | ||
| 212 | } | ||
| 213 | |||
| 214 | public function testStringLiteralExpr() : void | ||
| 215 |     { | ||
| 216 |         self::assertEquals("'word'", (string) $this->expr->literal('word')); | ||
| 217 | } | ||
| 218 | |||
| 219 | public function testNumericLiteralExpr() : void | ||
| 220 |     { | ||
| 221 | self::assertEquals(5, (string) $this->expr->literal(5)); | ||
| 222 | } | ||
| 223 | |||
| 224 | /** | ||
| 225 | * @group regression | ||
| 226 | * @group DDC-610 | ||
| 227 | */ | ||
| 228 | public function testLiteralExprProperlyQuotesStrings() : void | ||
| 229 |     { | ||
| 230 |         self::assertEquals("'00010001'", (string) $this->expr->literal('00010001')); | ||
| 231 | } | ||
| 232 | |||
| 233 | public function testGreaterThanOrEqualToExpr() : void | ||
| 234 |     { | ||
| 235 |         self::assertEquals('5 >= 2', (string) $this->expr->gte(5, 2)); | ||
| 236 | } | ||
| 237 | |||
| 238 | public function testLessThanOrEqualTo() : void | ||
| 239 |     { | ||
| 240 |         self::assertEquals('2 <= 5', (string) $this->expr->lte(2, 5)); | ||
| 241 | } | ||
| 242 | |||
| 243 | public function testBetweenExpr() : void | ||
| 244 |     { | ||
| 245 |         self::assertSame('u.id BETWEEN 3 AND 6', (string) $this->expr->between('u.id', 3, 6)); | ||
| 246 | } | ||
| 247 | |||
| 248 | public function testNotBetweenExpr() : void | ||
| 249 |     { | ||
| 250 |         self::assertSame('u.id NOT BETWEEN 3 AND 6', (string) $this->expr->notBetween('u.id', 3, 6)); | ||
|  | |||
| 251 | } | ||
| 252 | |||
| 253 | public function testTrimExpr() : void | ||
| 254 |     { | ||
| 255 |         self::assertEquals('TRIM(u.id)', (string) $this->expr->trim('u.id')); | ||
| 256 | } | ||
| 257 | |||
| 258 | public function testIsNullExpr() : void | ||
| 259 |     { | ||
| 260 |         self::assertEquals('u.id IS NULL', (string) $this->expr->isNull('u.id')); | ||
| 261 | } | ||
| 262 | |||
| 263 | public function testIsNotNullExpr() : void | ||
| 264 |     { | ||
| 265 |         self::assertEquals('u.id IS NOT NULL', (string) $this->expr->isNotNull('u.id')); | ||
| 266 | } | ||
| 267 | |||
| 268 | public function testIsInstanceOfExpr() : void | ||
| 269 |     { | ||
| 270 |         self::assertEquals('u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee', (string) $this->expr->isInstanceOf('u', CompanyEmployee::class)); | ||
| 271 | } | ||
| 272 | |||
| 273 | public function testIsMemberOfExpr() : void | ||
| 274 |     { | ||
| 275 |         self::assertEquals(':groupId MEMBER OF u.groups', (string) $this->expr->isMemberOf(':groupId', 'u.groups')); | ||
| 276 | } | ||
| 277 | |||
| 278 | public function testInExpr() : void | ||
| 279 |     { | ||
| 280 |         self::assertEquals('u.id IN(1, 2, 3)', (string) $this->expr->in('u.id', [1, 2, 3])); | ||
| 281 | } | ||
| 282 | |||
| 283 | public function testInLiteralExpr() : void | ||
| 284 |     { | ||
| 285 |         self::assertEquals("u.type IN('foo', 'bar')", (string) $this->expr->in('u.type', ['foo', 'bar'])); | ||
| 286 | } | ||
| 287 | |||
| 288 | public function testNotInExpr() : void | ||
| 289 |     { | ||
| 290 |         self::assertEquals('u.id NOT IN(1, 2, 3)', (string) $this->expr->notIn('u.id', [1, 2, 3])); | ||
| 291 | } | ||
| 292 | |||
| 293 | public function testNotInLiteralExpr() : void | ||
| 294 |     { | ||
| 295 |         self::assertEquals("u.type NOT IN('foo', 'bar')", (string) $this->expr->notIn('u.type', ['foo', 'bar'])); | ||
| 296 | } | ||
| 297 | |||
| 298 | public function testAndxOrxExpr() : void | ||
| 299 |     { | ||
| 300 | $andExpr = $this->expr->andX(); | ||
| 301 | $andExpr->add($this->expr->eq(1, 1)); | ||
| 302 | $andExpr->add($this->expr->lt(1, 5)); | ||
| 303 | |||
| 304 | $orExpr = $this->expr->orX(); | ||
| 305 | $orExpr->add($andExpr); | ||
| 306 | $orExpr->add($this->expr->eq(1, 1)); | ||
| 307 | |||
| 308 |         self::assertEquals('(1 = 1 AND 1 < 5) OR 1 = 1', (string) $orExpr); | ||
| 309 | } | ||
| 310 | |||
| 311 | public function testOrxExpr() : void | ||
| 312 |     { | ||
| 313 | $orExpr = $this->expr->orX(); | ||
| 314 | $orExpr->add($this->expr->eq(1, 1)); | ||
| 315 | $orExpr->add($this->expr->lt(1, 5)); | ||
| 316 | |||
| 317 |         self::assertEquals('1 = 1 OR 1 < 5', (string) $orExpr); | ||
| 318 | } | ||
| 319 | |||
| 320 | public function testOrderByCountExpr() : void | ||
| 321 |     { | ||
| 322 |         $orderExpr = $this->expr->desc('u.username'); | ||
| 323 | |||
| 324 | self::assertEquals($orderExpr->count(), 1); | ||
| 325 |         self::assertEquals('u.username DESC', (string) $orderExpr); | ||
| 326 | } | ||
| 327 | |||
| 328 | public function testOrderByOrder() : void | ||
| 329 |     { | ||
| 330 |         $orderExpr = $this->expr->desc('u.username'); | ||
| 331 |         self::assertEquals('u.username DESC', (string) $orderExpr); | ||
| 332 | } | ||
| 333 | |||
| 334 | public function testOrderByAsc() : void | ||
| 335 |     { | ||
| 336 |         $orderExpr = $this->expr->asc('u.username'); | ||
| 337 |         self::assertEquals('u.username ASC', (string) $orderExpr); | ||
| 338 | } | ||
| 339 | |||
| 340 | /** | ||
| 341 | * @expectedException \InvalidArgumentException | ||
| 342 | */ | ||
| 343 | public function testAddThrowsException() : void | ||
| 344 |     { | ||
| 345 | $orExpr = $this->expr->orX(); | ||
| 346 | $orExpr->add($this->expr->quot(5, 2)); | ||
| 347 | } | ||
| 348 | |||
| 349 | /** | ||
| 350 | * @group DDC-1683 | ||
| 351 | */ | ||
| 352 | public function testBooleanLiteral() : void | ||
| 353 |     { | ||
| 354 |         self::assertEquals('true', $this->expr->literal(true)); | ||
| 355 |         self::assertEquals('false', $this->expr->literal(false)); | ||
| 356 | } | ||
| 357 | |||
| 358 | |||
| 359 | /** | ||
| 360 | * @group DDC-1686 | ||
| 361 | */ | ||
| 362 | public function testExpressionGetter() : void | ||
| 363 |     { | ||
| 364 | // Andx | ||
| 365 | $andx = new Expr\Andx(['1 = 1', '2 = 2']); | ||
| 366 | self::assertEquals(['1 = 1', '2 = 2'], $andx->getParts()); | ||
| 367 | |||
| 368 | // Comparison | ||
| 369 |         $comparison = new Expr\Comparison('foo', Expr\Comparison::EQ, 'bar'); | ||
| 370 |         self::assertEquals('foo', $comparison->getLeftExpr()); | ||
| 371 |         self::assertEquals('bar', $comparison->getRightExpr()); | ||
| 372 | self::assertEquals(Expr\Comparison::EQ, $comparison->getOperator()); | ||
| 373 | |||
| 374 | // From | ||
| 375 |         $from = new Expr\From('Foo', 'f', 'f.id'); | ||
| 376 |         self::assertEquals('f', $from->getAlias()); | ||
| 377 |         self::assertEquals('Foo', $from->getFrom()); | ||
| 378 |         self::assertEquals('f.id', $from->getIndexBy()); | ||
| 379 | |||
| 380 | // Func | ||
| 381 |         $func = new Expr\Func('MAX', ['f.id']); | ||
| 382 |         self::assertEquals('MAX', $func->getName()); | ||
| 383 | self::assertEquals(['f.id'], $func->getArguments()); | ||
| 384 | |||
| 385 | // GroupBy | ||
| 386 | $group = new Expr\GroupBy(['foo DESC', 'bar ASC']); | ||
| 387 | self::assertEquals(['foo DESC', 'bar ASC'], $group->getParts()); | ||
| 388 | |||
| 389 | // Join | ||
| 390 | $join = new Expr\Join(Expr\Join::INNER_JOIN, 'f.bar', 'b', Expr\Join::ON, 'b.bar_id = 1', 'b.bar_id'); | ||
| 391 | self::assertEquals(Expr\Join::INNER_JOIN, $join->getJoinType()); | ||
| 392 | self::assertEquals(Expr\Join::ON, $join->getConditionType()); | ||
| 393 |         self::assertEquals('b.bar_id = 1', $join->getCondition()); | ||
| 394 |         self::assertEquals('b.bar_id', $join->getIndexBy()); | ||
| 395 |         self::assertEquals('f.bar', $join->getJoin()); | ||
| 396 |         self::assertEquals('b', $join->getAlias()); | ||
| 397 | |||
| 398 | // Literal | ||
| 399 | $literal = new Expr\Literal(['foo']); | ||
| 400 | self::assertEquals(['foo'], $literal->getParts()); | ||
| 401 | |||
| 402 | // Math | ||
| 403 | $math = new Expr\Math(10, '+', 20); | ||
| 404 | self::assertEquals(10, $math->getLeftExpr()); | ||
| 405 | self::assertEquals(20, $math->getRightExpr()); | ||
| 406 |         self::assertEquals('+', $math->getOperator()); | ||
| 407 | |||
| 408 | // OrderBy | ||
| 409 |         $order = new Expr\OrderBy('foo', 'DESC'); | ||
| 410 | self::assertEquals(['foo DESC'], $order->getParts()); | ||
| 411 | |||
| 412 | // Andx | ||
| 413 | $orx = new Expr\Orx(['foo = 1', 'bar = 2']); | ||
| 414 | self::assertEquals(['foo = 1', 'bar = 2'], $orx->getParts()); | ||
| 415 | |||
| 416 | // Select | ||
| 417 | $select = new Expr\Select(['foo', 'bar']); | ||
| 418 | self::assertEquals(['foo', 'bar'], $select->getParts()); | ||
| 419 | } | ||
| 420 | |||
| 421 | public function testAddEmpty() : void | ||
| 422 |     { | ||
| 423 | $andExpr = $this->expr->andX(); | ||
| 424 | $andExpr->add($this->expr->andX()); | ||
| 425 | |||
| 426 | self::assertEquals(0, $andExpr->count()); | ||
| 427 | } | ||
| 428 | |||
| 429 | public function testAddNull() : void | ||
| 435 | } | ||
| 436 | } | ||
| 437 | 
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.