FunctionExpressionTest.php 5.2 KB

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