FunctionsBuilderTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The Open Group Test Suite License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database;
  16. use Cake\Database\FunctionsBuilder;
  17. use Cake\Database\ValueBinder;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Tests FunctionsBuilder class
  21. */
  22. class FunctionsBuilderTest extends TestCase
  23. {
  24. /**
  25. * Setups a mock for FunctionsBuilder
  26. *
  27. * @return void
  28. */
  29. public function setUp(): void
  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->assertSame('MyFunc', $function->getName());
  44. $this->assertSame('MyFunc(b)', $function->sql(new ValueBinder()));
  45. $function = $this->functions->MyFunc(['b'], ['string'], 'integer');
  46. $this->assertSame('integer', $function->getReturnType());
  47. }
  48. /**
  49. * Tests generating a SUM() function
  50. *
  51. * @return void
  52. */
  53. public function testSum()
  54. {
  55. $function = $this->functions->sum('total');
  56. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  57. $this->assertSame('SUM(total)', $function->sql(new ValueBinder()));
  58. $this->assertSame('float', $function->getReturnType());
  59. $function = $this->functions->sum('total', ['integer']);
  60. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  61. $this->assertSame('SUM(total)', $function->sql(new ValueBinder()));
  62. $this->assertSame('integer', $function->getReturnType());
  63. }
  64. /**
  65. * Tests generating a AVG() function
  66. *
  67. * @return void
  68. */
  69. public function testAvg()
  70. {
  71. $function = $this->functions->avg('salary');
  72. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  73. $this->assertSame('AVG(salary)', $function->sql(new ValueBinder()));
  74. $this->assertSame('float', $function->getReturnType());
  75. }
  76. /**
  77. * Tests generating a MAX() function
  78. *
  79. * @return void
  80. */
  81. public function testMAX()
  82. {
  83. $function = $this->functions->max('created', ['datetime']);
  84. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  85. $this->assertSame('MAX(created)', $function->sql(new ValueBinder()));
  86. $this->assertSame('datetime', $function->getReturnType());
  87. }
  88. /**
  89. * Tests generating a MIN() function
  90. *
  91. * @return void
  92. */
  93. public function testMin()
  94. {
  95. $function = $this->functions->min('created', ['date']);
  96. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  97. $this->assertSame('MIN(created)', $function->sql(new ValueBinder()));
  98. $this->assertSame('date', $function->getReturnType());
  99. }
  100. /**
  101. * Tests generating a COUNT() function
  102. *
  103. * @return void
  104. */
  105. public function testCount()
  106. {
  107. $function = $this->functions->count('*');
  108. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  109. $this->assertSame('COUNT(*)', $function->sql(new ValueBinder()));
  110. $this->assertSame('integer', $function->getReturnType());
  111. }
  112. /**
  113. * Tests generating a CONCAT() function
  114. *
  115. * @return void
  116. */
  117. public function testConcat()
  118. {
  119. $function = $this->functions->concat(['title' => 'literal', ' is a string']);
  120. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  121. $this->assertSame('CONCAT(title, :param0)', $function->sql(new ValueBinder()));
  122. $this->assertSame('string', $function->getReturnType());
  123. }
  124. /**
  125. * Tests generating a COALESCE() function
  126. *
  127. * @return void
  128. */
  129. public function testCoalesce()
  130. {
  131. $function = $this->functions->coalesce(['NULL' => 'literal', '1', 'a'], ['a' => 'date']);
  132. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  133. $this->assertSame('COALESCE(NULL, :param0, :param1)', $function->sql(new ValueBinder()));
  134. $this->assertSame('date', $function->getReturnType());
  135. }
  136. /**
  137. * Tests generating a NOW(), CURRENT_TIME() and CURRENT_DATE() function
  138. *
  139. * @return void
  140. */
  141. public function testNow()
  142. {
  143. $function = $this->functions->now();
  144. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  145. $this->assertSame('NOW()', $function->sql(new ValueBinder()));
  146. $this->assertSame('datetime', $function->getReturnType());
  147. $function = $this->functions->now('date');
  148. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  149. $this->assertSame('CURRENT_DATE()', $function->sql(new ValueBinder()));
  150. $this->assertSame('date', $function->getReturnType());
  151. $function = $this->functions->now('time');
  152. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  153. $this->assertSame('CURRENT_TIME()', $function->sql(new ValueBinder()));
  154. $this->assertSame('time', $function->getReturnType());
  155. }
  156. /**
  157. * Tests generating a EXTRACT() function
  158. *
  159. * @return void
  160. */
  161. public function testExtract()
  162. {
  163. $function = $this->functions->extract('day', 'created');
  164. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  165. $this->assertSame('EXTRACT(day FROM created)', $function->sql(new ValueBinder()));
  166. $this->assertSame('integer', $function->getReturnType());
  167. $function = $this->functions->datePart('year', 'modified');
  168. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  169. $this->assertSame('EXTRACT(year FROM modified)', $function->sql(new ValueBinder()));
  170. $this->assertSame('integer', $function->getReturnType());
  171. }
  172. /**
  173. * Tests generating a DATE_ADD() function
  174. *
  175. * @return void
  176. */
  177. public function testDateAdd()
  178. {
  179. $function = $this->functions->dateAdd('created', -3, 'day');
  180. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  181. $this->assertSame('DATE_ADD(created, INTERVAL -3 day)', $function->sql(new ValueBinder()));
  182. $this->assertSame('datetime', $function->getReturnType());
  183. }
  184. /**
  185. * Tests generating a DAYOFWEEK() function
  186. *
  187. * @return void
  188. */
  189. public function testDayOfWeek()
  190. {
  191. $function = $this->functions->dayOfWeek('created');
  192. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  193. $this->assertSame('DAYOFWEEK(created)', $function->sql(new ValueBinder()));
  194. $this->assertSame('integer', $function->getReturnType());
  195. $function = $this->functions->weekday('created');
  196. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  197. $this->assertSame('DAYOFWEEK(created)', $function->sql(new ValueBinder()));
  198. $this->assertSame('integer', $function->getReturnType());
  199. }
  200. /**
  201. * Tests generating a RAND() function
  202. *
  203. * @return void
  204. */
  205. public function testRand()
  206. {
  207. $function = $this->functions->rand();
  208. $this->assertInstanceOf('Cake\Database\Expression\FunctionExpression', $function);
  209. $this->assertSame('RAND()', $function->sql(new ValueBinder()));
  210. $this->assertSame('float', $function->getReturnType());
  211. }
  212. }