|
2 | 2 |
|
3 | 3 | namespace LaminasTest\Db\Sql\Predicate;
|
4 | 4 |
|
| 5 | +use Laminas\Db\Adapter\Platform\Sql92; |
5 | 6 | use Laminas\Db\Sql\Expression;
|
6 | 7 | use Laminas\Db\Sql\Predicate\Predicate;
|
| 8 | +use Laminas\Db\Sql\Select; |
| 9 | +use Laminas\Stdlib\ErrorHandler; |
7 | 10 | use PHPUnit\Framework\TestCase;
|
8 | 11 |
|
| 12 | +use const E_USER_NOTICE; |
| 13 | + |
9 | 14 | class PredicateTest extends TestCase
|
10 | 15 | {
|
11 | 16 | public function testEqualToCreatesOperatorPredicate()
|
@@ -240,7 +245,7 @@ public function testExpressionNullParameters()
|
240 | 245 | $predicate->expression('foo = bar');
|
241 | 246 | $predicates = $predicate->getPredicates();
|
242 | 247 | $expression = $predicates[0][1];
|
243 |
| - self::assertEquals([null], $expression->getParameters()); |
| 248 | + self::assertEquals([], $expression->getParameters()); |
244 | 249 | }
|
245 | 250 |
|
246 | 251 | /**
|
@@ -276,4 +281,59 @@ public function testLiteral()
|
276 | 281 | $predicate->getExpressionData()
|
277 | 282 | );
|
278 | 283 | }
|
| 284 | + |
| 285 | + public function testCanCreateExpressionsWithoutAnyBoundSqlParameters(): void |
| 286 | + { |
| 287 | + $where1 = new Predicate(); |
| 288 | + |
| 289 | + $where1->expression('some_expression()'); |
| 290 | + |
| 291 | + self::assertSame( |
| 292 | + 'SELECT "a_table".* FROM "a_table" WHERE (some_expression())', |
| 293 | + $this->makeSqlString($where1) |
| 294 | + ); |
| 295 | + } |
| 296 | + |
| 297 | + public function testWillBindSqlParametersToExpressionsWithGivenParameter(): void |
| 298 | + { |
| 299 | + $where = new Predicate(); |
| 300 | + |
| 301 | + $where->expression('some_expression(?)', null); |
| 302 | + |
| 303 | + self::assertSame( |
| 304 | + 'SELECT "a_table".* FROM "a_table" WHERE (some_expression(\'\'))', |
| 305 | + $this->makeSqlString($where) |
| 306 | + ); |
| 307 | + } |
| 308 | + |
| 309 | + public function testWillBindSqlParametersToExpressionsWithGivenStringParameter(): void |
| 310 | + { |
| 311 | + $where = new Predicate(); |
| 312 | + |
| 313 | + $where->expression('some_expression(?)', 'a string'); |
| 314 | + |
| 315 | + self::assertSame( |
| 316 | + 'SELECT "a_table".* FROM "a_table" WHERE (some_expression(\'a string\'))', |
| 317 | + $this->makeSqlString($where) |
| 318 | + ); |
| 319 | + } |
| 320 | + |
| 321 | + private function makeSqlString(Predicate $where): string |
| 322 | + { |
| 323 | + $select = new Select('a_table'); |
| 324 | + |
| 325 | + $select->where($where); |
| 326 | + |
| 327 | + // this is still faster than connecting to a real DB for this kind of test. |
| 328 | + // we are using unsafe SQL quoting on purpose here: this raises warnings in production. |
| 329 | + ErrorHandler::start(E_USER_NOTICE); |
| 330 | + |
| 331 | + try { |
| 332 | + $string = $select->getSqlString(new Sql92()); |
| 333 | + } finally { |
| 334 | + ErrorHandler::stop(); |
| 335 | + } |
| 336 | + |
| 337 | + return $string; |
| 338 | + } |
279 | 339 | }
|
0 commit comments