FunctionExpressionTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. class FunctionExpressionTest extends TestCase
  22. {
  23. /**
  24. * Tests generating a function with no arguments
  25. *
  26. * @return void
  27. */
  28. public function testArityZero()
  29. {
  30. $f = new FunctionExpression('MyFunction');
  31. $this->assertEquals('MyFunction()', $f->sql(new ValueBinder));
  32. }
  33. /**
  34. * Tests generating a function one or multiple arguments and make sure
  35. * they are correctly replaced by placeholders
  36. *
  37. * @return void
  38. */
  39. public function testArityMultiplePlainValues()
  40. {
  41. $f = new FunctionExpression('MyFunction', ['foo', 'bar']);
  42. $binder = new ValueBinder;
  43. $this->assertEquals("MyFunction(:c0, :c1)", $f->sql($binder));
  44. $this->assertEquals('foo', $binder->bindings()[':c0']['value']);
  45. $this->assertEquals('bar', $binder->bindings()[':c1']['value']);
  46. $binder = new ValueBinder;
  47. $f = new FunctionExpression('MyFunction', ['bar']);
  48. $this->assertEquals("MyFunction(:c0)", $f->sql($binder));
  49. $this->assertEquals('bar', $binder->bindings()[':c0']['value']);
  50. }
  51. /**
  52. * Tests that it is possible to use literal strings as arguments
  53. *
  54. * @return void
  55. */
  56. public function testLiteralParams()
  57. {
  58. $binder = new ValueBinder;
  59. $f = new FunctionExpression('MyFunction', ['foo' => 'literal', 'bar']);
  60. $this->assertEquals("MyFunction(foo, :c0)", $f->sql($binder));
  61. }
  62. /**
  63. * Tests that it is possible to nest expression objects and pass them as arguments
  64. * In particular nesting multiple FunctionExpression
  65. *
  66. * @return void
  67. */
  68. public function testFunctionNesting()
  69. {
  70. $binder = new ValueBinder;
  71. $f = new FunctionExpression('MyFunction', ['foo', 'bar']);
  72. $g = new FunctionExpression('Wrapper', ['bar' => 'literal', $f]);
  73. $this->assertEquals("Wrapper(bar, (MyFunction(:c0, :c1)))", $g->sql($binder));
  74. }
  75. /**
  76. * Tests that it is possible to use a number as a literal in a function.
  77. *
  78. * @return void
  79. */
  80. public function testNumericLiteral()
  81. {
  82. $binder = new ValueBinder;
  83. $f = new FunctionExpression('MyFunction', ['a_field' => 'literal', '32' => 'literal']);
  84. $this->assertEquals('MyFunction(a_field, 32)', $f->sql($binder));
  85. $f = new FunctionExpression('MyFunction', ['a_field' => 'literal', 32 => 'literal']);
  86. $this->assertEquals('MyFunction(a_field, 32)', $f->sql($binder));
  87. }
  88. }