FunctionExpressionTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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')
  91. ->selectQuery(['column']);
  92. $binder = new ValueBinder();
  93. $function = new $this->expressionClass('MyFunction', [$query]);
  94. $this->assertSame(
  95. 'MyFunction((SELECT column))',
  96. preg_replace('/[`"\[\]]/', '', $function->sql($binder))
  97. );
  98. }
  99. /**
  100. * Tests that it is possible to use a number as a literal in a function.
  101. */
  102. public function testNumericLiteral(): void
  103. {
  104. $binder = new ValueBinder();
  105. $f = new $this->expressionClass('MyFunction', ['a_field' => 'literal', '32' => 'literal']);
  106. $this->assertSame('MyFunction(a_field, 32)', $f->sql($binder));
  107. $f = new $this->expressionClass('MyFunction', ['a_field' => 'literal', 32 => 'literal']);
  108. $this->assertSame('MyFunction(a_field, 32)', $f->sql($binder));
  109. }
  110. /**
  111. * Tests setReturnType() and getReturnType()
  112. */
  113. public function testGetSetReturnType(): void
  114. {
  115. $f = new $this->expressionClass('MyFunction');
  116. $f = $f->setReturnType('foo');
  117. $this->assertInstanceOf($this->expressionClass, $f);
  118. $this->assertSame('foo', $f->getReturnType());
  119. }
  120. }