FunctionExpressionTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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 2005-2013, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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. * Tests generating a function with no arguments
  25. *
  26. * @return void
  27. */
  28. public function testArityZero() {
  29. $f = new FunctionExpression('MyFunction');
  30. $this->assertEquals('MyFunction()', $f->sql(new ValueBinder));
  31. }
  32. /**
  33. * Tests generating a function one or multiple arguments and make sure
  34. * they are correctly replaced by placeholders
  35. *
  36. * @return void
  37. */
  38. public function testArityMultiplePlainValues() {
  39. $f = new FunctionExpression('MyFunction', ['foo', 'bar']);
  40. $binder = new ValueBinder;
  41. $this->assertEquals("MyFunction(:c0, :c1)", $f->sql($binder));
  42. $this->assertEquals('foo', $binder->bindings()[':c0']['value']);
  43. $this->assertEquals('bar', $binder->bindings()[':c1']['value']);
  44. $binder = new ValueBinder;
  45. $f = new FunctionExpression('MyFunction', ['bar']);
  46. $this->assertEquals("MyFunction(:c0)", $f->sql($binder));
  47. $this->assertEquals('bar', $binder->bindings()[':c0']['value']);
  48. }
  49. /**
  50. * Tests that it is possible to use literal strings as arguments
  51. *
  52. * @return void
  53. */
  54. public function testLiteralParams() {
  55. $binder = new ValueBinder;
  56. $f = new FunctionExpression('MyFunction', ['foo' => 'literal', 'bar']);
  57. $this->assertEquals("MyFunction(foo, :c0)", $f->sql($binder));
  58. }
  59. /**
  60. * Tests that it is possible to nest expression objects and pass them as arguments
  61. * In particular nesting multiple FunctionExpression
  62. *
  63. * @return void
  64. */
  65. public function testFunctionNesting() {
  66. $binder = new ValueBinder;
  67. $f = new FunctionExpression('MyFunction', ['foo', 'bar']);
  68. $g = new FunctionExpression('Wrapper', ['bar' => 'literal', $f]);
  69. $this->assertEquals("Wrapper(bar, MyFunction(:c0, :c1))", $g->sql($binder));
  70. }
  71. }