FunctionsBuilderTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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;
  15. use Cake\Database\FunctionsBuilder;
  16. use Cake\Database\ValueBinder;
  17. /**
  18. * Tests FunctionsBuilder class
  19. *
  20. */
  21. class FunctionsBuilderTest extends \Cake\TestSuite\TestCase {
  22. /**
  23. * Setups a mock for FunctionsBuilder
  24. *
  25. * @return void
  26. */
  27. public function setUp() {
  28. parent::setUp();
  29. $this->functions = new FunctionsBuilder;
  30. }
  31. /**
  32. * Tests generating a generic function call
  33. *
  34. * @return void
  35. */
  36. public function testArbitrary() {
  37. $function = $this->functions->MyFunc(['b' => 'literal']);
  38. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  39. $this->assertEquals('MyFunc', $function->name());
  40. $this->assertEquals('MyFunc(b)', $function->sql(new ValueBinder));
  41. }
  42. /**
  43. * Tests generating a SUM() function
  44. *
  45. * @return void
  46. */
  47. public function testSum() {
  48. $function = $this->functions->sum('total');
  49. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  50. $this->assertEquals('SUM(total)', $function->sql(new ValueBinder));
  51. }
  52. /**
  53. * Tests generating a AVG() function
  54. *
  55. * @return void
  56. */
  57. public function testAvg() {
  58. $function = $this->functions->avg('salary');
  59. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  60. $this->assertEquals('AVG(salary)', $function->sql(new ValueBinder));
  61. }
  62. /**
  63. * Tests generating a MAX() function
  64. *
  65. * @return void
  66. */
  67. public function testMAX() {
  68. $function = $this->functions->max('created');
  69. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  70. $this->assertEquals('MAX(created)', $function->sql(new ValueBinder));
  71. }
  72. /**
  73. * Tests generating a MIN() function
  74. *
  75. * @return void
  76. */
  77. public function testMin() {
  78. $function = $this->functions->min('created');
  79. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  80. $this->assertEquals('MIN(created)', $function->sql(new ValueBinder));
  81. }
  82. /**
  83. * Tests generating a COUNT() function
  84. *
  85. * @return void
  86. */
  87. public function testCount() {
  88. $function = $this->functions->count('*');
  89. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  90. $this->assertEquals('COUNT(*)', $function->sql(new ValueBinder));
  91. }
  92. /**
  93. * Tests generating a CONCAT() function
  94. *
  95. * @return void
  96. */
  97. public function testConcat() {
  98. $function = $this->functions->concat(['title' => 'literal', ' is a string']);
  99. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  100. $this->assertEquals("CONCAT(title, :c0)", $function->sql(new ValueBinder));
  101. }
  102. /**
  103. * Tests generating a COALESCE() function
  104. *
  105. * @return void
  106. */
  107. public function testCoalesce() {
  108. $function = $this->functions->coalesce(['NULL' => 'literal', '1', '2']);
  109. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  110. $this->assertEquals("COALESCE(NULL, :c0, :c1)", $function->sql(new ValueBinder));
  111. }
  112. /**
  113. * Tests generating a NOW(), CURRENT_TIME() and CURRENT_DATE() function
  114. *
  115. * @return void
  116. */
  117. public function testNow() {
  118. $function = $this->functions->now();
  119. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  120. $this->assertEquals("NOW()", $function->sql(new ValueBinder));
  121. $function = $this->functions->now('date');
  122. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  123. $this->assertEquals("CURRENT_DATE()", $function->sql(new ValueBinder));
  124. $function = $this->functions->now('time');
  125. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  126. $this->assertEquals("CURRENT_TIME()", $function->sql(new ValueBinder));
  127. }
  128. }