FunctionsBuilderTest.php 4.2 KB

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