ExpressionTypeCastingTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.3.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Database;
  15. use Cake\TestSuite\TestCase;
  16. use Cake\Database\Type\StringType;
  17. use Cake\Database\Type\ExpressionTypeInterface;
  18. use Cake\Database\Expression\FunctionExpression;
  19. use Cake\Database\ValueBinder;
  20. use Cake\Database\Type;
  21. use Cake\Database\Expression\Comparison;
  22. class TestType extends StringType implements ExpressionTypeInterface
  23. {
  24. public function toExpression($value)
  25. {
  26. return new FunctionExpression('CONCAT', [$value, ' - foo']);
  27. }
  28. }
  29. /**
  30. * Tests for Expression objects casting values to other expressions
  31. * using the type classes
  32. *
  33. */
  34. class FunctionsBuilderTest extends TestCase
  35. {
  36. /**
  37. * Setups a mock for FunctionsBuilder
  38. *
  39. * @return void
  40. */
  41. public function setUp()
  42. {
  43. parent::setUp();
  44. Type::map('test', new TestType);
  45. }
  46. public function testComparisonSimple()
  47. {
  48. $comparison = new Comparison('field', 'the thing', 'test', '=');
  49. $binder = new ValueBinder;
  50. $sql = $comparison->sql($binder);
  51. $this->assertEquals('field = (CONCAT(:c0, :c1))', $sql);
  52. $this->assertEquals('the thing', $binder->bindings()[':c0']['value']);
  53. }
  54. public function testComparisonMultiple()
  55. {
  56. $comparison = new Comparison('field', ['2', '3'], 'test[]', 'IN');
  57. $binder = new ValueBinder;
  58. $sql = $comparison->sql($binder);
  59. $this->assertEquals('field IN (CONCAT(:c0, :c1),CONCAT(:c2, :c3))', $sql);
  60. $this->assertEquals('2', $binder->bindings()[':c0']['value']);
  61. $this->assertEquals('3', $binder->bindings()[':c2']['value']);
  62. }
  63. }