TupleComparisonTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /**
  27. * Tests generating a function with no arguments
  28. *
  29. * @return void
  30. */
  31. public function testsSimpleTuple()
  32. {
  33. $f = new TupleComparison(['field1', 'field2'], [1, 2], ['integer', 'integer'], '=');
  34. $binder = new ValueBinder;
  35. $this->assertEquals('(field1, field2) = (:c0, :c1)', $f->sql($binder));
  36. $this->assertSame(1, $binder->bindings()[':c0']['value']);
  37. $this->assertSame(2, $binder->bindings()[':c1']['value']);
  38. $this->assertSame('integer', $binder->bindings()[':c0']['type']);
  39. $this->assertSame('integer', $binder->bindings()[':c1']['type']);
  40. }
  41. /**
  42. * Tests generating tuples in the fields side containing expressions
  43. *
  44. * @return void
  45. */
  46. public function testTupleWithExpressionFields()
  47. {
  48. $field1 = new QueryExpression(['a' => 1]);
  49. $f = new TupleComparison([$field1, 'field2'], [4, 5], ['integer', 'integer'], '>');
  50. $binder = new ValueBinder;
  51. $this->assertEquals('(a = :c0, field2) > (:c1, :c2)', $f->sql($binder));
  52. $this->assertSame(1, $binder->bindings()[':c0']['value']);
  53. $this->assertSame(4, $binder->bindings()[':c1']['value']);
  54. $this->assertSame(5, $binder->bindings()[':c2']['value']);
  55. }
  56. /**
  57. * Tests generating tuples in the values side containing expressions
  58. *
  59. * @return void
  60. */
  61. public function testTupleWithExpressionValues()
  62. {
  63. $value1 = new QueryExpression(['a' => 1]);
  64. $f = new TupleComparison(['field1', 'field2'], [$value1, 2], ['integer', 'integer'], '=');
  65. $binder = new ValueBinder;
  66. $this->assertEquals('(field1, field2) = (a = :c0, :c1)', $f->sql($binder));
  67. $this->assertSame(1, $binder->bindings()[':c0']['value']);
  68. $this->assertSame(2, $binder->bindings()[':c1']['value']);
  69. }
  70. /**
  71. * Tests generating tuples using the IN conjunction
  72. *
  73. * @return void
  74. */
  75. public function testTupleWithInComparison()
  76. {
  77. $f = new TupleComparison(
  78. ['field1', 'field2'],
  79. [[1, 2], [3, 4]],
  80. ['integer', 'integer'],
  81. 'IN'
  82. );
  83. $binder = new ValueBinder;
  84. $this->assertEquals('(field1, field2) IN ((:c0,:c1), (:c2,:c3))', $f->sql($binder));
  85. $this->assertSame(1, $binder->bindings()[':c0']['value']);
  86. $this->assertSame(2, $binder->bindings()[':c1']['value']);
  87. $this->assertSame(3, $binder->bindings()[':c2']['value']);
  88. $this->assertSame(4, $binder->bindings()[':c3']['value']);
  89. }
  90. /**
  91. * Tests traversing
  92. *
  93. * @return void
  94. */
  95. public function testTraverse()
  96. {
  97. $value1 = new QueryExpression(['a' => 1]);
  98. $field2 = new QueryExpression(['b' => 2]);
  99. $f = new TupleComparison(['field1', $field2], [$value1, 2], ['integer', 'integer'], '=');
  100. $binder = new ValueBinder;
  101. $expressions = [];
  102. $collector = function ($e) use (&$expressions) {
  103. $expressions[] = $e;
  104. };
  105. $f->traverse($collector);
  106. $this->assertCount(4, $expressions);
  107. $this->assertSame($field2, $expressions[0]);
  108. $this->assertSame($value1, $expressions[2]);
  109. $f = new TupleComparison(
  110. ['field1', $field2],
  111. [[1, 2], [3, $value1]],
  112. ['integer', 'integer'],
  113. 'IN'
  114. );
  115. $expressions = [];
  116. $f->traverse($collector);
  117. $this->assertCount(4, $expressions);
  118. $this->assertSame($field2, $expressions[0]);
  119. $this->assertSame($value1, $expressions[2]);
  120. }
  121. /**
  122. * Tests that a single ExpressionInteface can be used as the value for
  123. * comparison
  124. *
  125. * @return void
  126. */
  127. public function testValueAsSingleExpression()
  128. {
  129. $value = new QueryExpression('SELECT 1, 1');
  130. $f = new TupleComparison(['field1', 'field2'], $value);
  131. $binder = new ValueBinder;
  132. $this->assertEquals('(field1, field2) = (SELECT 1, 1)', $f->sql($binder));
  133. }
  134. /**
  135. * Tests that a single ExpressionInteface can be used as the field for
  136. * comparison
  137. *
  138. * @return void
  139. */
  140. public function testFieldAsSingleExpression()
  141. {
  142. $value = [1, 1];
  143. $f = new TupleComparison(new QueryExpression('a, b'), $value);
  144. $binder = new ValueBinder;
  145. $this->assertEquals('(a, b) = (:c0, :c1)', $f->sql($binder));
  146. }
  147. }