FunctionExpressionTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * @var string The expression class to test with
  28. */
  29. protected $expressionClass = FunctionExpression::class;
  30. /**
  31. * Tests generating a function with no arguments
  32. *
  33. * @return void
  34. */
  35. public function testArityZero()
  36. {
  37. $f = new $this->expressionClass('MyFunction');
  38. $this->assertSame('MyFunction()', $f->sql(new ValueBinder()));
  39. }
  40. /**
  41. * Tests generating a function one or multiple arguments and make sure
  42. * they are correctly replaced by placeholders
  43. *
  44. * @return void
  45. */
  46. public function testArityMultiplePlainValues()
  47. {
  48. $f = new $this->expressionClass('MyFunction', ['foo', 'bar']);
  49. $binder = new ValueBinder();
  50. $this->assertSame('MyFunction(:param0, :param1)', $f->sql($binder));
  51. $this->assertSame('foo', $binder->bindings()[':param0']['value']);
  52. $this->assertSame('bar', $binder->bindings()[':param1']['value']);
  53. $binder = new ValueBinder();
  54. $f = new $this->expressionClass('MyFunction', ['bar']);
  55. $this->assertSame('MyFunction(:param0)', $f->sql($binder));
  56. $this->assertSame('bar', $binder->bindings()[':param0']['value']);
  57. }
  58. /**
  59. * Tests that it is possible to use literal strings as arguments
  60. *
  61. * @return void
  62. */
  63. public function testLiteralParams()
  64. {
  65. $binder = new ValueBinder();
  66. $f = new $this->expressionClass('MyFunction', ['foo' => 'literal', 'bar']);
  67. $this->assertSame('MyFunction(foo, :param0)', $f->sql($binder));
  68. }
  69. /**
  70. * Tests that it is possible to nest expression objects and pass them as arguments
  71. * In particular nesting multiple FunctionExpression
  72. *
  73. * @return void
  74. */
  75. public function testFunctionNesting()
  76. {
  77. $binder = new ValueBinder();
  78. $f = new $this->expressionClass('MyFunction', ['foo', 'bar']);
  79. $g = new $this->expressionClass('Wrapper', ['bar' => 'literal', $f]);
  80. $this->assertSame('Wrapper(bar, MyFunction(:param0, :param1))', $g->sql($binder));
  81. }
  82. /**
  83. * Tests to avoid regression, prevents double parenthesis
  84. * In particular nesting with QueryExpression
  85. *
  86. * @return void
  87. */
  88. public function testFunctionNestingQueryExpression()
  89. {
  90. $binder = new ValueBinder();
  91. $q = new QueryExpression('a');
  92. $f = new $this->expressionClass('MyFunction', [$q]);
  93. $this->assertSame('MyFunction(a)', $f->sql($binder));
  94. }
  95. /**
  96. * Tests that passing a database query as an argument wraps the query SQL into parentheses.
  97. *
  98. * @return void
  99. */
  100. public function testFunctionWithDatabaseQuery()
  101. {
  102. $query = ConnectionManager::get('test')
  103. ->newQuery()
  104. ->select(['column']);
  105. $binder = new ValueBinder();
  106. $function = new $this->expressionClass('MyFunction', [$query]);
  107. $this->assertEquals(
  108. 'MyFunction((SELECT column))',
  109. preg_replace('/[`"\[\]]/', '', $function->sql($binder))
  110. );
  111. }
  112. /**
  113. * Tests that passing a ORM query as an argument wraps the query SQL into parentheses.
  114. *
  115. * @return void
  116. */
  117. public function testFunctionWithOrmQuery()
  118. {
  119. $query = $this->getTableLocator()->get('Articles')
  120. ->setSchema(['column' => 'integer'])
  121. ->find()
  122. ->select(['column']);
  123. $binder = new ValueBinder();
  124. $function = new $this->expressionClass('MyFunction', [$query]);
  125. $this->assertEquals(
  126. 'MyFunction((SELECT Articles.column AS Articles__column FROM articles Articles))',
  127. preg_replace('/[`"\[\]]/', '', $function->sql($binder))
  128. );
  129. }
  130. /**
  131. * Tests that it is possible to use a number as a literal in a function.
  132. *
  133. * @return void
  134. */
  135. public function testNumericLiteral()
  136. {
  137. $binder = new ValueBinder();
  138. $f = new $this->expressionClass('MyFunction', ['a_field' => 'literal', '32' => 'literal']);
  139. $this->assertSame('MyFunction(a_field, 32)', $f->sql($binder));
  140. $f = new $this->expressionClass('MyFunction', ['a_field' => 'literal', 32 => 'literal']);
  141. $this->assertSame('MyFunction(a_field, 32)', $f->sql($binder));
  142. }
  143. /**
  144. * Tests setReturnType() and getReturnType()
  145. *
  146. * @return void
  147. */
  148. public function testGetSetReturnType()
  149. {
  150. $f = new $this->expressionClass('MyFunction');
  151. $f = $f->setReturnType('foo');
  152. $this->assertInstanceOf($this->expressionClass, $f);
  153. $this->assertSame('foo', $f->getReturnType());
  154. }
  155. }