QueryExpressionTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.6
  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\CaseExpression;
  17. use Cake\Database\Expression\QueryExpression;
  18. use Cake\Database\ValueBinder;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests QueryExpression class
  22. */
  23. class QueryExpressionTest extends TestCase
  24. {
  25. /**
  26. * Test and() and or() calls work transparently
  27. *
  28. * @return void
  29. */
  30. public function testAndOrCalls()
  31. {
  32. $expr = new QueryExpression();
  33. $expected = '\Cake\Database\Expression\QueryExpression';
  34. $this->assertInstanceOf($expected, $expr->and([]));
  35. $this->assertInstanceOf($expected, $expr->or([]));
  36. }
  37. /**
  38. * Test SQL generation with one element
  39. *
  40. * @return void
  41. */
  42. public function testSqlGenerationOneClause()
  43. {
  44. $expr = new QueryExpression();
  45. $binder = new ValueBinder();
  46. $expr->add(['Users.username' => 'sally'], ['Users.username' => 'string']);
  47. $result = $expr->sql($binder);
  48. $this->assertEquals('Users.username = :c0', $result);
  49. }
  50. /**
  51. * Test SQL generation with many elements
  52. *
  53. * @return void
  54. */
  55. public function testSqlGenerationMultipleClauses()
  56. {
  57. $expr = new QueryExpression();
  58. $binder = new ValueBinder();
  59. $expr->add(
  60. [
  61. 'Users.username' => 'sally',
  62. 'Users.active' => 1,
  63. ],
  64. [
  65. 'Users.username' => 'string',
  66. 'Users.active' => 'boolean'
  67. ]
  68. );
  69. $result = $expr->sql($binder);
  70. $this->assertEquals('(Users.username = :c0 AND Users.active = :c1)', $result);
  71. }
  72. /**
  73. * Test that empty expressions don't emit invalid SQL.
  74. *
  75. * @return void
  76. */
  77. public function testSqlWhenEmpty()
  78. {
  79. $expr = new QueryExpression();
  80. $binder = new ValueBinder();
  81. $result = $expr->sql($binder);
  82. $this->assertEquals('', $result);
  83. }
  84. /**
  85. * Test deep cloning of expression trees.
  86. *
  87. * @return void
  88. */
  89. public function testDeepCloning()
  90. {
  91. $expr = new QueryExpression();
  92. $expr = $expr->add(new QueryExpression('1 + 1'))
  93. ->isNull('deleted')
  94. ->like('title', 'things%');
  95. $dupe = clone $expr;
  96. $this->assertEquals($dupe, $expr);
  97. $this->assertNotSame($dupe, $expr);
  98. $originalParts = [];
  99. $expr->iterateParts(function ($part) use (&$originalParts) {
  100. $originalParts[] = $part;
  101. });
  102. $dupe->iterateParts(function ($part, $i) use ($originalParts) {
  103. $this->assertNotSame($originalParts[$i], $part);
  104. });
  105. }
  106. /**
  107. * Tests the hasNestedExpression() function
  108. *
  109. * @return void
  110. */
  111. public function testHasNestedExpression()
  112. {
  113. $expr = new QueryExpression();
  114. $this->assertFalse($expr->hasNestedExpression());
  115. $expr->add(['a' => 'b']);
  116. $this->assertTrue($expr->hasNestedExpression());
  117. $expr = new QueryExpression();
  118. $expr->add('a = b');
  119. $this->assertFalse($expr->hasNestedExpression());
  120. $expr->add(new QueryExpression('1 = 1'));
  121. $this->assertTrue($expr->hasNestedExpression());
  122. }
  123. /**
  124. * Returns the list of specific comparison methods
  125. *
  126. * @return void
  127. */
  128. public function methodsProvider()
  129. {
  130. return [
  131. ['eq'], ['notEq'], ['gt'], ['lt'], ['gte'], ['lte'], ['like'],
  132. ['notLike'], ['in'], ['notIn']
  133. ];
  134. }
  135. /**
  136. * Tests that the query expression uses the type map when the
  137. * specific comparison functions are used.
  138. *
  139. * @dataProvider methodsProvider
  140. * @return void
  141. */
  142. public function testTypeMapUsage($method)
  143. {
  144. $expr = new QueryExpression([], ['created' => 'date']);
  145. $expr->{$method}('created', 'foo');
  146. $binder = new ValueBinder();
  147. $expr->sql($binder);
  148. $bindings = $binder->bindings();
  149. $type = current($bindings)['type'];
  150. $this->assertEquals('date', $type);
  151. }
  152. }