FunctionExpressionTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.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\ValueBinder;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Tests FunctionExpression class
  20. *
  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(:c0, :c1)", $f->sql($binder));
  45. $this->assertEquals('foo', $binder->bindings()[':c0']['value']);
  46. $this->assertEquals('bar', $binder->bindings()[':c1']['value']);
  47. $binder = new ValueBinder;
  48. $f = new FunctionExpression('MyFunction', ['bar']);
  49. $this->assertEquals("MyFunction(:c0)", $f->sql($binder));
  50. $this->assertEquals('bar', $binder->bindings()[':c0']['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, :c0)", $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(:c0, :c1)))", $g->sql($binder));
  75. }
  76. /**
  77. * Tests that it is possible to use a number as a literal in a function.
  78. *
  79. * @return void
  80. */
  81. public function testNumericLiteral()
  82. {
  83. $binder = new ValueBinder;
  84. $f = new FunctionExpression('MyFunction', ['a_field' => 'literal', '32' => 'literal']);
  85. $this->assertEquals('MyFunction(a_field, 32)', $f->sql($binder));
  86. $f = new FunctionExpression('MyFunction', ['a_field' => 'literal', 32 => 'literal']);
  87. $this->assertEquals('MyFunction(a_field, 32)', $f->sql($binder));
  88. }
  89. }