QueryExpressionTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.6
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Expression;
  16. use Cake\Database\Expression\CaseExpression;
  17. use Cake\Database\Expression\QueryExpression;
  18. use Cake\Database\ValueBinder;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests QueryExpression class
  22. */
  23. class QueryExpressionTest extends TestCase
  24. {
  25. /**
  26. * Test and() and or() calls work transparently
  27. *
  28. * @return void
  29. */
  30. public function testAndOrCalls()
  31. {
  32. $expr = new QueryExpression();
  33. $expected = '\Cake\Database\Expression\QueryExpression';
  34. $this->assertInstanceOf($expected, $expr->and([]));
  35. $this->assertInstanceOf($expected, $expr->or([]));
  36. }
  37. /**
  38. * Test SQL generation with one element
  39. *
  40. * @return void
  41. */
  42. public function testSqlGenerationOneClause()
  43. {
  44. $expr = new QueryExpression();
  45. $binder = new ValueBinder();
  46. $expr->add(['Users.username' => 'sally'], ['Users.username' => 'string']);
  47. $result = $expr->sql($binder);
  48. $this->assertEquals('Users.username = :c0', $result);
  49. }
  50. /**
  51. * Test SQL generation with many elements
  52. *
  53. * @return void
  54. */
  55. public function testSqlGenerationMultipleClauses()
  56. {
  57. $expr = new QueryExpression();
  58. $binder = new ValueBinder();
  59. $expr->add(
  60. [
  61. 'Users.username' => 'sally',
  62. 'Users.active' => 1,
  63. ],
  64. [
  65. 'Users.username' => 'string',
  66. 'Users.active' => 'boolean'
  67. ]
  68. );
  69. $result = $expr->sql($binder);
  70. $this->assertEquals('(Users.username = :c0 AND Users.active = :c1)', $result);
  71. }
  72. /**
  73. * Test that empty expressions don't emit invalid SQL.
  74. *
  75. * @return void
  76. */
  77. public function testSqlWhenEmpty()
  78. {
  79. $expr = new QueryExpression();
  80. $binder = new ValueBinder();
  81. $result = $expr->sql($binder);
  82. $this->assertEquals('', $result);
  83. }
  84. }