ComparisonExpressionTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.7.5
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database\Expression;
  17. use Cake\Database\Expression\ComparisonExpression;
  18. use Cake\Database\Expression\IdentifierExpression;
  19. use Cake\Database\Expression\QueryExpression;
  20. use Cake\Database\ValueBinder;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Tests Comparison class
  24. */
  25. class ComparisonExpressionTest extends TestCase
  26. {
  27. /**
  28. * Test sql generation using IdentifierExpression
  29. */
  30. public function testIdentifiers(): void
  31. {
  32. $expr = new ComparisonExpression('field', new IdentifierExpression('other_field'));
  33. $this->assertEqualsSql('field = other_field', $expr->sql(new ValueBinder()));
  34. $expr = new ComparisonExpression(new IdentifierExpression('field'), new IdentifierExpression('other_field'));
  35. $this->assertEqualsSql('field = other_field', $expr->sql(new ValueBinder()));
  36. $expr = new ComparisonExpression(new IdentifierExpression('field'), new QueryExpression(['other_field']));
  37. $this->assertEqualsSql('field = (other_field)', $expr->sql(new ValueBinder()));
  38. $expr = new ComparisonExpression(new IdentifierExpression('field'), 'value');
  39. $this->assertEqualsSql('field = :c0', $expr->sql(new ValueBinder()));
  40. $expr = new ComparisonExpression(new QueryExpression(['field']), new IdentifierExpression('other_field'));
  41. $this->assertEqualsSql('field = other_field', $expr->sql(new ValueBinder()));
  42. }
  43. /**
  44. * Tests that cloning Comparion instance clones it's value and field expressions.
  45. */
  46. public function testClone(): void
  47. {
  48. $exp = new ComparisonExpression(new QueryExpression('field1'), 1, 'integer', '<');
  49. $exp2 = clone $exp;
  50. $this->assertNotSame($exp->getField(), $exp2->getField());
  51. }
  52. }