FunctionsBuilderTest.php 4.7 KB

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