FunctionsBuilderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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\Expression\AggregateExpression;
  17. use Cake\Database\Expression\FunctionExpression;
  18. use Cake\Database\Expression\IdentifierExpression;
  19. use Cake\Database\FunctionsBuilder;
  20. use Cake\Database\ValueBinder;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Tests FunctionsBuilder class
  24. */
  25. class FunctionsBuilderTest extends TestCase
  26. {
  27. /**
  28. * @var \Cake\Database\FunctionsBuilder
  29. */
  30. protected $functions;
  31. /**
  32. * Setups a mock for FunctionsBuilder
  33. */
  34. public function setUp(): void
  35. {
  36. parent::setUp();
  37. $this->functions = new FunctionsBuilder();
  38. }
  39. /**
  40. * Tests generating a generic function call
  41. */
  42. public function testArbitrary(): void
  43. {
  44. $function = $this->functions->MyFunc(['b' => 'literal']);
  45. $this->assertInstanceOf(FunctionExpression::class, $function);
  46. $this->assertSame('MyFunc', $function->getName());
  47. $this->assertSame('MyFunc(b)', $function->sql(new ValueBinder()));
  48. $function = $this->functions->MyFunc(['b'], ['string'], 'integer');
  49. $this->assertSame('integer', $function->getReturnType());
  50. }
  51. /**
  52. * Tests generating a generic aggregate call
  53. */
  54. public function testArbitraryAggregate(): void
  55. {
  56. $function = $this->functions->aggregate('MyFunc', ['b' => 'literal']);
  57. $this->assertInstanceOf(AggregateExpression::class, $function);
  58. $this->assertSame('MyFunc', $function->getName());
  59. $this->assertSame('MyFunc(b)', $function->sql(new ValueBinder()));
  60. $function = $this->functions->aggregate('MyFunc', ['b'], ['string'], 'integer');
  61. $this->assertSame('integer', $function->getReturnType());
  62. }
  63. /**
  64. * Tests generating a SUM() function
  65. */
  66. public function testSum(): void
  67. {
  68. $function = $this->functions->sum('total');
  69. $this->assertInstanceOf(AggregateExpression::class, $function);
  70. $this->assertSame('SUM(total)', $function->sql(new ValueBinder()));
  71. $this->assertSame('float', $function->getReturnType());
  72. $function = $this->functions->sum('total', ['integer']);
  73. $this->assertInstanceOf(AggregateExpression::class, $function);
  74. $this->assertSame('SUM(total)', $function->sql(new ValueBinder()));
  75. $this->assertSame('integer', $function->getReturnType());
  76. }
  77. /**
  78. * Tests generating a AVG() function
  79. */
  80. public function testAvg(): void
  81. {
  82. $function = $this->functions->avg('salary');
  83. $this->assertInstanceOf(AggregateExpression::class, $function);
  84. $this->assertSame('AVG(salary)', $function->sql(new ValueBinder()));
  85. $this->assertSame('float', $function->getReturnType());
  86. }
  87. /**
  88. * Tests generating a MAX() function
  89. */
  90. public function testMax(): void
  91. {
  92. $function = $this->functions->max('total');
  93. $this->assertInstanceOf(AggregateExpression::class, $function);
  94. $this->assertSame('MAX(total)', $function->sql(new ValueBinder()));
  95. $this->assertSame('float', $function->getReturnType());
  96. $function = $this->functions->max('created', ['datetime']);
  97. $this->assertInstanceOf(AggregateExpression::class, $function);
  98. $this->assertSame('MAX(created)', $function->sql(new ValueBinder()));
  99. $this->assertSame('datetime', $function->getReturnType());
  100. }
  101. /**
  102. * Tests generating a MIN() function
  103. */
  104. public function testMin(): void
  105. {
  106. $function = $this->functions->min('created', ['date']);
  107. $this->assertInstanceOf(AggregateExpression::class, $function);
  108. $this->assertSame('MIN(created)', $function->sql(new ValueBinder()));
  109. $this->assertSame('date', $function->getReturnType());
  110. }
  111. /**
  112. * Tests generating a COUNT() function
  113. */
  114. public function testCount(): void
  115. {
  116. $function = $this->functions->count('*');
  117. $this->assertInstanceOf(AggregateExpression::class, $function);
  118. $this->assertSame('COUNT(*)', $function->sql(new ValueBinder()));
  119. $this->assertSame('integer', $function->getReturnType());
  120. }
  121. /**
  122. * Tests generating a CONCAT() function
  123. */
  124. public function testConcat(): void
  125. {
  126. $function = $this->functions->concat(['title' => 'literal', ' is a string']);
  127. $this->assertInstanceOf(FunctionExpression::class, $function);
  128. $this->assertSame('CONCAT(title, :param0)', $function->sql(new ValueBinder()));
  129. $this->assertSame('string', $function->getReturnType());
  130. }
  131. /**
  132. * Tests generating a COALESCE() function
  133. */
  134. public function testCoalesce(): void
  135. {
  136. $function = $this->functions->coalesce(['NULL' => 'literal', '1', 'a'], ['a' => 'date']);
  137. $this->assertInstanceOf(FunctionExpression::class, $function);
  138. $this->assertSame('COALESCE(NULL, :param0, :param1)', $function->sql(new ValueBinder()));
  139. $this->assertSame('date', $function->getReturnType());
  140. }
  141. /**
  142. * Tests generating a CAST() function
  143. */
  144. public function testCast(): void
  145. {
  146. $function = $this->functions->cast('field', 'varchar');
  147. $this->assertInstanceOf(FunctionExpression::class, $function);
  148. $this->assertSame('CAST(field AS varchar)', $function->sql(new ValueBinder()));
  149. $this->assertSame('string', $function->getReturnType());
  150. $function = $this->functions->cast($this->functions->now(), 'varchar');
  151. $this->assertInstanceOf(FunctionExpression::class, $function);
  152. $this->assertSame('CAST(NOW() AS varchar)', $function->sql(new ValueBinder()));
  153. $this->assertSame('string', $function->getReturnType());
  154. }
  155. /**
  156. * Tests generating a NOW(), CURRENT_TIME() and CURRENT_DATE() function
  157. */
  158. public function testNow(): void
  159. {
  160. $function = $this->functions->now();
  161. $this->assertInstanceOf(FunctionExpression::class, $function);
  162. $this->assertSame('NOW()', $function->sql(new ValueBinder()));
  163. $this->assertSame('datetime', $function->getReturnType());
  164. $function = $this->functions->now('date');
  165. $this->assertInstanceOf(FunctionExpression::class, $function);
  166. $this->assertSame('CURRENT_DATE()', $function->sql(new ValueBinder()));
  167. $this->assertSame('date', $function->getReturnType());
  168. $function = $this->functions->now('time');
  169. $this->assertInstanceOf(FunctionExpression::class, $function);
  170. $this->assertSame('CURRENT_TIME()', $function->sql(new ValueBinder()));
  171. $this->assertSame('time', $function->getReturnType());
  172. }
  173. /**
  174. * Tests generating a EXTRACT() function
  175. */
  176. public function testExtract(): void
  177. {
  178. $function = $this->functions->extract('day', 'created');
  179. $this->assertInstanceOf(FunctionExpression::class, $function);
  180. $this->assertSame('EXTRACT(day FROM created)', $function->sql(new ValueBinder()));
  181. $this->assertSame('integer', $function->getReturnType());
  182. $function = $this->functions->datePart('year', 'modified');
  183. $this->assertInstanceOf(FunctionExpression::class, $function);
  184. $this->assertSame('EXTRACT(year FROM modified)', $function->sql(new ValueBinder()));
  185. $this->assertSame('integer', $function->getReturnType());
  186. }
  187. /**
  188. * Tests generating a DATE_ADD() function
  189. */
  190. public function testDateAdd(): void
  191. {
  192. $function = $this->functions->dateAdd('created', -3, 'day');
  193. $this->assertInstanceOf(FunctionExpression::class, $function);
  194. $this->assertSame('DATE_ADD(created, INTERVAL -3 day)', $function->sql(new ValueBinder()));
  195. $this->assertSame('datetime', $function->getReturnType());
  196. $function = $this->functions->dateAdd(new IdentifierExpression('created'), -3, 'day');
  197. $this->assertInstanceOf(FunctionExpression::class, $function);
  198. $this->assertSame('DATE_ADD(created, INTERVAL -3 day)', $function->sql(new ValueBinder()));
  199. }
  200. /**
  201. * Tests generating a DAYOFWEEK() function
  202. */
  203. public function testDayOfWeek(): void
  204. {
  205. $function = $this->functions->dayOfWeek('created');
  206. $this->assertInstanceOf(FunctionExpression::class, $function);
  207. $this->assertSame('DAYOFWEEK(created)', $function->sql(new ValueBinder()));
  208. $this->assertSame('integer', $function->getReturnType());
  209. $function = $this->functions->weekday('created');
  210. $this->assertInstanceOf(FunctionExpression::class, $function);
  211. $this->assertSame('DAYOFWEEK(created)', $function->sql(new ValueBinder()));
  212. $this->assertSame('integer', $function->getReturnType());
  213. }
  214. /**
  215. * Tests generating a RAND() function
  216. */
  217. public function testRand(): void
  218. {
  219. $function = $this->functions->rand();
  220. $this->assertInstanceOf(FunctionExpression::class, $function);
  221. $this->assertSame('RAND()', $function->sql(new ValueBinder()));
  222. $this->assertSame('float', $function->getReturnType());
  223. }
  224. /**
  225. * Tests generating a ROW_NUMBER() window function
  226. */
  227. public function testRowNumber(): void
  228. {
  229. $function = $this->functions->rowNumber();
  230. $this->assertInstanceOf(AggregateExpression::class, $function);
  231. $this->assertSame('ROW_NUMBER() OVER ()', $function->sql(new ValueBinder()));
  232. $this->assertSame('integer', $function->getReturnType());
  233. }
  234. /**
  235. * Tests generating a LAG() window function
  236. */
  237. public function testLag(): void
  238. {
  239. $function = $this->functions->lag('field', 1);
  240. $this->assertInstanceOf(AggregateExpression::class, $function);
  241. $this->assertSame('LAG(field, 1) OVER ()', $function->sql(new ValueBinder()));
  242. $this->assertSame('float', $function->getReturnType());
  243. $function = $this->functions->lag('field', 1, 10, 'integer');
  244. $this->assertInstanceOf(AggregateExpression::class, $function);
  245. $this->assertSame('LAG(field, 1, :param0) OVER ()', $function->sql(new ValueBinder()));
  246. $this->assertSame('integer', $function->getReturnType());
  247. }
  248. /**
  249. * Tests generating a LAG() window function
  250. */
  251. public function testLead(): void
  252. {
  253. $function = $this->functions->lead('field', 1);
  254. $this->assertInstanceOf(AggregateExpression::class, $function);
  255. $this->assertSame('LEAD(field, 1) OVER ()', $function->sql(new ValueBinder()));
  256. $this->assertSame('float', $function->getReturnType());
  257. $function = $this->functions->lead('field', 1, 10, 'integer');
  258. $this->assertInstanceOf(AggregateExpression::class, $function);
  259. $this->assertSame('LEAD(field, 1, :param0) OVER ()', $function->sql(new ValueBinder()));
  260. $this->assertSame('integer', $function->getReturnType());
  261. }
  262. }