FunctionExpressionTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. *
  4. * PHP Version 5.4
  5. *
  6. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  7. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  8. *
  9. * Licensed under The Open Group Test Suite License
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright 2005-2013, Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 3.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Test\TestCase\Database\Expression;
  18. use Cake\Database\Expression\FunctionExpression;
  19. use Cake\Database\ValueBinder;
  20. /**
  21. * Tests FunctionExpression class
  22. *
  23. **/
  24. class FunctionExpressionTest extends \Cake\TestSuite\TestCase {
  25. /**
  26. * Tests generating a function with no arguments
  27. *
  28. * @return void
  29. */
  30. public function testArityZero() {
  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. $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. $binder = new ValueBinder;
  58. $f = new FunctionExpression('MyFunction', ['foo' => 'literal', 'bar']);
  59. $this->assertEquals("MyFunction(foo, :c0)", $f->sql($binder));
  60. }
  61. /**
  62. * Tests that it is possible to nest expression objects and pass them as arguments
  63. * In particular nesting multiple FunctionExpression
  64. *
  65. * @return void
  66. */
  67. public function testFunctionNesting() {
  68. $binder = new ValueBinder;
  69. $f = new FunctionExpression('MyFunction', ['foo', 'bar']);
  70. $g = new FunctionExpression('Wrapper', ['bar' => 'literal', $f]);
  71. $this->assertEquals("Wrapper(bar, MyFunction(:c0, :c1))", $g->sql($binder));
  72. }
  73. }