FunctionsBuilderTest.php 11 KB

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