FunctionExpressionTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The Open Group Test Suite License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Database\Expression;
  15. use Cake\Database\Expression\FunctionExpression;
  16. use Cake\Database\Expression\QueryExpression;
  17. use Cake\Database\ValueBinder;
  18. use Cake\Datasource\ConnectionManager;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests FunctionExpression class
  22. */
  23. class FunctionExpressionTest extends TestCase
  24. {
  25. /**
  26. * Tests generating a function with no arguments
  27. *
  28. * @return void
  29. */
  30. public function testArityZero()
  31. {
  32. $f = new FunctionExpression('MyFunction');
  33. $this->assertEquals('MyFunction()', $f->sql(new ValueBinder()));
  34. }
  35. /**
  36. * Tests generating a function one or multiple arguments and make sure
  37. * they are correctly replaced by placeholders
  38. *
  39. * @return void
  40. */
  41. public function testArityMultiplePlainValues()
  42. {
  43. $f = new FunctionExpression('MyFunction', ['foo', 'bar']);
  44. $binder = new ValueBinder();
  45. $this->assertEquals('MyFunction(:param0, :param1)', $f->sql($binder));
  46. $this->assertEquals('foo', $binder->bindings()[':param0']['value']);
  47. $this->assertEquals('bar', $binder->bindings()[':param1']['value']);
  48. $binder = new ValueBinder();
  49. $f = new FunctionExpression('MyFunction', ['bar']);
  50. $this->assertEquals('MyFunction(:param0)', $f->sql($binder));
  51. $this->assertEquals('bar', $binder->bindings()[':param0']['value']);
  52. }
  53. /**
  54. * Tests that it is possible to use literal strings as arguments
  55. *
  56. * @return void
  57. */
  58. public function testLiteralParams()
  59. {
  60. $binder = new ValueBinder();
  61. $f = new FunctionExpression('MyFunction', ['foo' => 'literal', 'bar']);
  62. $this->assertEquals('MyFunction(foo, :param0)', $f->sql($binder));
  63. }
  64. /**
  65. * Tests that it is possible to nest expression objects and pass them as arguments
  66. * In particular nesting multiple FunctionExpression
  67. *
  68. * @return void
  69. */
  70. public function testFunctionNesting()
  71. {
  72. $binder = new ValueBinder();
  73. $f = new FunctionExpression('MyFunction', ['foo', 'bar']);
  74. $g = new FunctionExpression('Wrapper', ['bar' => 'literal', $f]);
  75. $this->assertEquals('Wrapper(bar, MyFunction(:param0, :param1))', $g->sql($binder));
  76. }
  77. /**
  78. * Tests to avoid regression, prevents double parenthesis
  79. * In particular nesting with QueryExpression
  80. *
  81. * @return void
  82. */
  83. public function testFunctionNestingQueryExpression()
  84. {
  85. $binder = new ValueBinder();
  86. $q = new QueryExpression('a');
  87. $f = new FunctionExpression('MyFunction', [$q]);
  88. $this->assertEquals('MyFunction(a)', $f->sql($binder));
  89. }
  90. /**
  91. * Tests that passing a database query as an argument wraps the query SQL into parentheses.
  92. *
  93. * @return void
  94. */
  95. public function testFunctionWithDatabaseQuery()
  96. {
  97. $query = ConnectionManager::get('test')
  98. ->newQuery()
  99. ->select(['column']);
  100. $binder = new ValueBinder();
  101. $function = new FunctionExpression('MyFunction', [$query]);
  102. $this->assertEquals(
  103. 'MyFunction((SELECT column))',
  104. preg_replace('/[`"\[\]]/', '', $function->sql($binder))
  105. );
  106. }
  107. /**
  108. * Tests that passing a ORM query as an argument wraps the query SQL into parentheses.
  109. *
  110. * @return void
  111. */
  112. public function testFunctionWithOrmQuery()
  113. {
  114. $query = $this->getTableLocator()->get('Articles')
  115. ->setSchema(['column' => 'integer'])
  116. ->find()
  117. ->select(['column']);
  118. $binder = new ValueBinder();
  119. $function = new FunctionExpression('MyFunction', [$query]);
  120. $this->assertEquals(
  121. 'MyFunction((SELECT Articles.column AS Articles__column FROM articles Articles))',
  122. preg_replace('/[`"\[\]]/', '', $function->sql($binder))
  123. );
  124. }
  125. /**
  126. * Tests that it is possible to use a number as a literal in a function.
  127. *
  128. * @return void
  129. */
  130. public function testNumericLiteral()
  131. {
  132. $binder = new ValueBinder();
  133. $f = new FunctionExpression('MyFunction', ['a_field' => 'literal', '32' => 'literal']);
  134. $this->assertEquals('MyFunction(a_field, 32)', $f->sql($binder));
  135. $f = new FunctionExpression('MyFunction', ['a_field' => 'literal', 32 => 'literal']);
  136. $this->assertEquals('MyFunction(a_field, 32)', $f->sql($binder));
  137. }
  138. /**
  139. * Tests setReturnType() and getReturnType()
  140. *
  141. * @return void
  142. */
  143. public function testGetSetReturnType()
  144. {
  145. $f = new FunctionExpression('MyFunction');
  146. $f = $f->setReturnType('foo');
  147. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $f);
  148. $this->assertSame('foo', $f->getReturnType());
  149. }
  150. }