FunctionsBuilderTest.php 11 KB

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