FunctionExpressionTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\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 it is possible to use a number as a literal in a function.
  92. *
  93. * @return void
  94. */
  95. public function testNumericLiteral()
  96. {
  97. $binder = new ValueBinder();
  98. $f = new FunctionExpression('MyFunction', ['a_field' => 'literal', '32' => 'literal']);
  99. $this->assertEquals('MyFunction(a_field, 32)', $f->sql($binder));
  100. $f = new FunctionExpression('MyFunction', ['a_field' => 'literal', 32 => 'literal']);
  101. $this->assertEquals('MyFunction(a_field, 32)', $f->sql($binder));
  102. }
  103. /**
  104. * Tests setReturnType() and getReturnType()
  105. *
  106. * @return void
  107. */
  108. public function testGetSetReturnType()
  109. {
  110. $f = new FunctionExpression('MyFunction');
  111. $f = $f->setReturnType('foo');
  112. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $f);
  113. $this->assertSame('foo', $f->getReturnType());
  114. }
  115. }