FunctionsBuilderTest.php 12 KB

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