FunctionExpressionTest.php 2.4 KB

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