FunctionExpressionTest.php 5.3 KB

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