FunctionExpressionTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The Open Group Test Suite License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Database\Expression;
  15. use Cake\Database\Expression\FunctionExpression;
  16. use Cake\Database\Expression\QueryExpression;
  17. use Cake\Database\ValueBinder;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Tests FunctionExpression class
  21. */
  22. class FunctionExpressionTest extends TestCase
  23. {
  24. /**
  25. * Tests generating a function with no arguments
  26. *
  27. * @return void
  28. */
  29. public function testArityZero()
  30. {
  31. $f = new FunctionExpression('MyFunction');
  32. $this->assertEquals('MyFunction()', $f->sql(new ValueBinder));
  33. }
  34. /**
  35. * Tests generating a function one or multiple arguments and make sure
  36. * they are correctly replaced by placeholders
  37. *
  38. * @return void
  39. */
  40. public function testArityMultiplePlainValues()
  41. {
  42. $f = new FunctionExpression('MyFunction', ['foo', 'bar']);
  43. $binder = new ValueBinder;
  44. $this->assertEquals('MyFunction(:param0, :param1)', $f->sql($binder));
  45. $this->assertEquals('foo', $binder->bindings()[':param0']['value']);
  46. $this->assertEquals('bar', $binder->bindings()[':param1']['value']);
  47. $binder = new ValueBinder;
  48. $f = new FunctionExpression('MyFunction', ['bar']);
  49. $this->assertEquals('MyFunction(:param0)', $f->sql($binder));
  50. $this->assertEquals('bar', $binder->bindings()[':param0']['value']);
  51. }
  52. /**
  53. * Tests that it is possible to use literal strings as arguments
  54. *
  55. * @return void
  56. */
  57. public function testLiteralParams()
  58. {
  59. $binder = new ValueBinder;
  60. $f = new FunctionExpression('MyFunction', ['foo' => 'literal', 'bar']);
  61. $this->assertEquals('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. * @return void
  68. */
  69. public function testFunctionNesting()
  70. {
  71. $binder = new ValueBinder;
  72. $f = new FunctionExpression('MyFunction', ['foo', 'bar']);
  73. $g = new FunctionExpression('Wrapper', ['bar' => 'literal', $f]);
  74. $this->assertEquals('Wrapper(bar, MyFunction(:param0, :param1))', $g->sql($binder));
  75. }
  76. /**
  77. * Tests to avoid regression, prevents double parenthesis
  78. * In particular nesting with QueryExpression
  79. *
  80. * @return void
  81. */
  82. public function testFunctionNestingQueryExpression()
  83. {
  84. $binder = new ValueBinder;
  85. $q = new QueryExpression('a');
  86. $f = new FunctionExpression('MyFunction', [$q]);
  87. $this->assertEquals('MyFunction(a)', $f->sql($binder));
  88. }
  89. /**
  90. * Tests that it is possible to use a number as a literal in a function.
  91. *
  92. * @return void
  93. */
  94. public function testNumericLiteral()
  95. {
  96. $binder = new ValueBinder;
  97. $f = new FunctionExpression('MyFunction', ['a_field' => 'literal', '32' => 'literal']);
  98. $this->assertEquals('MyFunction(a_field, 32)', $f->sql($binder));
  99. $f = new FunctionExpression('MyFunction', ['a_field' => 'literal', 32 => 'literal']);
  100. $this->assertEquals('MyFunction(a_field, 32)', $f->sql($binder));
  101. }
  102. /**
  103. * Tests setReturnType() and getReturnType()
  104. *
  105. * @return void
  106. */
  107. public function testGetSetReturnType()
  108. {
  109. $f = new FunctionExpression('MyFunction');
  110. $f = $f->setReturnType('foo');
  111. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $f);
  112. $this->assertSame('foo', $f->getReturnType());
  113. }
  114. }