TupleComparisonTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Expression;
  16. use Cake\Database\Expression\QueryExpression;
  17. use Cake\Database\Expression\TupleComparison;
  18. use Cake\Database\ValueBinder;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests TupleComparison class
  22. *
  23. **/
  24. class TupleComparisonTest extends TestCase {
  25. /**
  26. * Tests generating a function with no arguments
  27. *
  28. * @return void
  29. */
  30. public function testsSimpleTuple() {
  31. $f = new TupleComparison(['field1', 'field2'], [1, 2], ['integer', 'integer'], '=');
  32. $binder = new ValueBinder;
  33. $this->assertEquals('(field1, field2) = (:c0, :c1)', $f->sql($binder));
  34. $this->assertSame(1, $binder->bindings()[':c0']['value']);
  35. $this->assertSame(2, $binder->bindings()[':c1']['value']);
  36. $this->assertSame('integer', $binder->bindings()[':c0']['type']);
  37. $this->assertSame('integer', $binder->bindings()[':c1']['type']);
  38. }
  39. /**
  40. * Tests generating tuples in the fields side containing expressions
  41. *
  42. * @return void
  43. */
  44. public function testTupleWithExpressionFields() {
  45. $field1 = new QueryExpression(['a' => 1]);
  46. $f = new TupleComparison([$field1, 'field2'], [4, 5], ['integer', 'integer'], '>');
  47. $binder = new ValueBinder;
  48. $this->assertEquals('(a = :c0, field2) > (:c1, :c2)', $f->sql($binder));
  49. $this->assertSame(1, $binder->bindings()[':c0']['value']);
  50. $this->assertSame(4, $binder->bindings()[':c1']['value']);
  51. $this->assertSame(5, $binder->bindings()[':c2']['value']);
  52. }
  53. /**
  54. * Tests generating tuples in the values side containing expressions
  55. *
  56. * @return void
  57. */
  58. public function testTupleWithExpressionValues() {
  59. $value1 = new QueryExpression(['a' => 1]);
  60. $f = new TupleComparison(['field1', 'field2'], [$value1, 2], ['integer', 'integer'], '=');
  61. $binder = new ValueBinder;
  62. $this->assertEquals('(field1, field2) = (a = :c0, :c1)', $f->sql($binder));
  63. $this->assertSame(1, $binder->bindings()[':c0']['value']);
  64. $this->assertSame(2, $binder->bindings()[':c1']['value']);
  65. }
  66. /**
  67. * Tests generating tuples using the IN conjunction
  68. *
  69. * @return void
  70. */
  71. public function testTupleWithInComparison() {
  72. $f = new TupleComparison(
  73. ['field1', 'field2'],
  74. [[1, 2], [3, 4]],
  75. ['integer', 'integer'],
  76. 'IN'
  77. );
  78. $binder = new ValueBinder;
  79. $this->assertEquals('(field1, field2) IN ((:c0,:c1), (:c2,:c3))', $f->sql($binder));
  80. $this->assertSame(1, $binder->bindings()[':c0']['value']);
  81. $this->assertSame(2, $binder->bindings()[':c1']['value']);
  82. $this->assertSame(3, $binder->bindings()[':c2']['value']);
  83. $this->assertSame(4, $binder->bindings()[':c3']['value']);
  84. }
  85. /**
  86. * Tests traversing
  87. *
  88. * @return void
  89. */
  90. public function testTraverse() {
  91. $value1 = new QueryExpression(['a' => 1]);
  92. $field2 = new QueryExpression(['b' => 2]);
  93. $f = new TupleComparison(['field1', $field2], [$value1, 2], ['integer', 'integer'], '=');
  94. $binder = new ValueBinder;
  95. $expressions = [];
  96. $collector = function($e) use (&$expressions) {
  97. $expressions[] = $e;
  98. };
  99. $f->traverse($collector);
  100. $this->assertCount(4, $expressions);
  101. $this->assertSame($field2, $expressions[0]);
  102. $this->assertSame($value1, $expressions[2]);
  103. $f = new TupleComparison(
  104. ['field1', $field2],
  105. [[1, 2], [3, $value1]],
  106. ['integer', 'integer'],
  107. 'IN'
  108. );
  109. $expressions = [];
  110. $f->traverse($collector);
  111. $this->assertCount(4, $expressions);
  112. $this->assertSame($field2, $expressions[0]);
  113. $this->assertSame($value1, $expressions[2]);
  114. }
  115. /**
  116. * Tests that a single ExpressionInteface can be used as the value for
  117. * comparison
  118. *
  119. * @return void
  120. */
  121. public function testValueAsSingleExpression() {
  122. $value = new QueryExpression('SELECT 1, 1');
  123. $f = new TupleComparison(['field1', 'field2'], $value);
  124. $binder = new ValueBinder;
  125. $this->assertEquals('(field1, field2) = (SELECT 1, 1)', $f->sql($binder));
  126. }
  127. /**
  128. * Tests that a single ExpressionInteface can be used as the field for
  129. * comparison
  130. *
  131. * @return void
  132. */
  133. public function testFieldAsSingleExpression() {
  134. $value = [1, 1];
  135. $f = new TupleComparison(new QueryExpression('a, b'), $value);
  136. $binder = new ValueBinder;
  137. $this->assertEquals('(a, b) = (:c0, :c1)', $f->sql($binder));
  138. }
  139. }