TupleComparisonTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 in the values side containing expressions
  68. *
  69. * @return void
  70. */
  71. public function testTupleWithClosureExpression() {
  72. $field1 = new QueryExpression([function($e){return $e->eq('a', 1);}]);
  73. $f = new TupleComparison([$field1, 'field2'], [4, 5], ['integer', 'integer'], '>');
  74. $binder = new ValueBinder;
  75. $this->assertEquals('(a = :c0, field2) > (:c1, :c2)', $f->sql($binder));
  76. $this->assertSame(1, $binder->bindings()[':c0']['value']);
  77. $this->assertSame(4, $binder->bindings()[':c1']['value']);
  78. $this->assertSame(5, $binder->bindings()[':c2']['value']);
  79. }
  80. /**
  81. * Tests generating tuples using the IN conjunction
  82. *
  83. * @return void
  84. */
  85. public function testTupleWithInComparison() {
  86. $f = new TupleComparison(
  87. ['field1', 'field2'],
  88. [[1, 2], [3, 4]],
  89. ['integer', 'integer'],
  90. 'IN'
  91. );
  92. $binder = new ValueBinder;
  93. $this->assertEquals('(field1, field2) IN ((:c0,:c1), (:c2,:c3))', $f->sql($binder));
  94. $this->assertSame(1, $binder->bindings()[':c0']['value']);
  95. $this->assertSame(2, $binder->bindings()[':c1']['value']);
  96. $this->assertSame(3, $binder->bindings()[':c2']['value']);
  97. $this->assertSame(4, $binder->bindings()[':c3']['value']);
  98. }
  99. /**
  100. * Tests traversing
  101. *
  102. * @return void
  103. */
  104. public function testTraverse() {
  105. $value1 = new QueryExpression(['a' => 1]);
  106. $field2 = new QueryExpression(['b' => 2]);
  107. $f = new TupleComparison(['field1', $field2], [$value1, 2], ['integer', 'integer'], '=');
  108. $binder = new ValueBinder;
  109. $expressions = [];
  110. $collector = function($e) use (&$expressions) {
  111. $expressions[] = $e;
  112. };
  113. $f->traverse($collector);
  114. $this->assertCount(4, $expressions);
  115. $this->assertSame($field2, $expressions[0]);
  116. $this->assertSame($value1, $expressions[2]);
  117. $f = new TupleComparison(
  118. ['field1', $field2],
  119. [[1, 2], [3, $value1]],
  120. ['integer', 'integer'],
  121. 'IN'
  122. );
  123. $expressions = [];
  124. $f->traverse($collector);
  125. $this->assertCount(4, $expressions);
  126. $this->assertSame($field2, $expressions[0]);
  127. $this->assertSame($value1, $expressions[2]);
  128. }
  129. /**
  130. * Tests that a single ExpressionInteface can be used as the value for
  131. * comparison
  132. *
  133. * @return void
  134. */
  135. public function testValueAsSingleExpression() {
  136. $value = new QueryExpression('SELECT 1, 1');
  137. $f = new TupleComparison(['field1', 'field2'], $value);
  138. $binder = new ValueBinder;
  139. $this->assertEquals('(field1, field2) = (SELECT 1, 1)', $f->sql($binder));
  140. }
  141. /**
  142. * Tests that a single ExpressionInteface can be used as the field for
  143. * comparison
  144. *
  145. * @return void
  146. */
  147. public function testFieldAsSingleExpression() {
  148. $value = [1, 1];
  149. $f = new TupleComparison(new QueryExpression('a, b'), $value);
  150. $binder = new ValueBinder;
  151. $this->assertEquals('(a, b) = (:c0, :c1)', $f->sql($binder));
  152. }
  153. }