QueryExpressionTest.php 4.6 KB

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