AggregateExpressionTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 4.1.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Expression;
  16. use Cake\Database\Expression\AggregateExpression;
  17. use Cake\Database\Expression\IdentifierExpression;
  18. use Cake\Database\Expression\QueryExpression;
  19. use Cake\Database\Expression\WindowExpression;
  20. use Cake\Database\ValueBinder;
  21. /**
  22. * Tests FunctionExpression class
  23. */
  24. class AggregateExpressionTest extends FunctionExpressionTest
  25. {
  26. /**
  27. * @var string The expression class to test with
  28. */
  29. protected $expressionClass = AggregateExpression::class;
  30. /**
  31. * Tests annotating an aggregate with an empty window expression
  32. */
  33. public function testEmptyWindow(): void
  34. {
  35. $f = (new AggregateExpression('MyFunction'))->over();
  36. $this->assertSame('MyFunction() OVER ()', $f->sql(new ValueBinder()));
  37. $f = (new AggregateExpression('MyFunction'))->over('name');
  38. $this->assertEqualsSql(
  39. 'MyFunction() OVER name',
  40. $f->sql(new ValueBinder())
  41. );
  42. }
  43. /**
  44. * Tests filter() clauses.
  45. */
  46. public function testFilter(): void
  47. {
  48. $f = (new AggregateExpression('MyFunction'))->filter(['this' => new IdentifierExpression('that')]);
  49. $this->assertEqualsSql(
  50. 'MyFunction() FILTER (WHERE this = that)',
  51. $f->sql(new ValueBinder())
  52. );
  53. $f->filter(function (QueryExpression $q) {
  54. return $q->add(['this2' => new IdentifierExpression('that2')]);
  55. });
  56. $this->assertEqualsSql(
  57. 'MyFunction() FILTER (WHERE (this = that AND this2 = that2))',
  58. $f->sql(new ValueBinder())
  59. );
  60. $f->over();
  61. $this->assertEqualsSql(
  62. 'MyFunction() FILTER (WHERE (this = that AND this2 = that2)) OVER ()',
  63. $f->sql(new ValueBinder())
  64. );
  65. }
  66. /**
  67. * Tests WindowInterface calls are passed to the WindowExpression
  68. */
  69. public function testWindowInterface(): void
  70. {
  71. $f = (new AggregateExpression('MyFunction'))->partition('test');
  72. $this->assertEqualsSql(
  73. 'MyFunction() OVER (PARTITION BY test)',
  74. $f->sql(new ValueBinder())
  75. );
  76. $f = (new AggregateExpression('MyFunction'))->orderBy('test');
  77. $this->assertEqualsSql(
  78. 'MyFunction() OVER (ORDER BY test)',
  79. $f->sql(new ValueBinder())
  80. );
  81. $f = (new AggregateExpression('MyFunction'))->range(null);
  82. $this->assertEqualsSql(
  83. 'MyFunction() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)',
  84. $f->sql(new ValueBinder())
  85. );
  86. $f = (new AggregateExpression('MyFunction'))->range(null, null);
  87. $this->assertEqualsSql(
  88. 'MyFunction() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)',
  89. $f->sql(new ValueBinder())
  90. );
  91. $f = (new AggregateExpression('MyFunction'))->rows(null);
  92. $this->assertEqualsSql(
  93. 'MyFunction() OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)',
  94. $f->sql(new ValueBinder())
  95. );
  96. $f = (new AggregateExpression('MyFunction'))->rows(null, null);
  97. $this->assertEqualsSql(
  98. 'MyFunction() OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)',
  99. $f->sql(new ValueBinder())
  100. );
  101. $f = (new AggregateExpression('MyFunction'))->groups(null);
  102. $this->assertEqualsSql(
  103. 'MyFunction() OVER (GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)',
  104. $f->sql(new ValueBinder())
  105. );
  106. $f = (new AggregateExpression('MyFunction'))->groups(null, null);
  107. $this->assertEqualsSql(
  108. 'MyFunction() OVER (GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)',
  109. $f->sql(new ValueBinder())
  110. );
  111. $f = (new AggregateExpression('MyFunction'))->frame(
  112. AggregateExpression::RANGE,
  113. 2,
  114. AggregateExpression::PRECEDING,
  115. 1,
  116. AggregateExpression::PRECEDING
  117. );
  118. $this->assertEqualsSql(
  119. 'MyFunction() OVER (RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING)',
  120. $f->sql(new ValueBinder())
  121. );
  122. $f = (new AggregateExpression('MyFunction'))->excludeCurrent();
  123. $this->assertEqualsSql(
  124. 'MyFunction() OVER ()',
  125. $f->sql(new ValueBinder())
  126. );
  127. $f = (new AggregateExpression('MyFunction'))->range(null)->excludeCurrent();
  128. $this->assertEqualsSql(
  129. 'MyFunction() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW)',
  130. $f->sql(new ValueBinder())
  131. );
  132. $f = (new AggregateExpression('MyFunction'))->range(null)->excludeGroup();
  133. $this->assertEqualsSql(
  134. 'MyFunction() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE GROUP)',
  135. $f->sql(new ValueBinder())
  136. );
  137. $f = (new AggregateExpression('MyFunction'))->range(null)->excludeTies();
  138. $this->assertEqualsSql(
  139. 'MyFunction() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE TIES)',
  140. $f->sql(new ValueBinder())
  141. );
  142. }
  143. /**
  144. * Tests traversing aggregate expressions.
  145. */
  146. public function testTraverse(): void
  147. {
  148. $w = (new AggregateExpression('MyFunction'))
  149. ->filter(['this' => true])
  150. ->over();
  151. $expressions = [];
  152. $w->traverse(function ($expression) use (&$expressions): void {
  153. $expressions[] = $expression;
  154. });
  155. $this->assertEquals(new QueryExpression(['this' => true]), $expressions[0]);
  156. $this->assertEquals(new WindowExpression(), $expressions[2]);
  157. }
  158. /**
  159. * Tests cloning aggregate expressions
  160. */
  161. public function testCloning(): void
  162. {
  163. $a1 = (new AggregateExpression('MyFunction'))->partition('test');
  164. $a2 = (clone $a1)->partition('new');
  165. $this->assertNotSame($a1->sql(new ValueBinder()), $a2->sql(new ValueBinder()));
  166. }
  167. }