WindowQueryTests.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\QueryTests;
  16. use Cake\Database\Expression\QueryExpression;
  17. use Cake\Database\Expression\WindowExpression;
  18. use Cake\Database\Query;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\TestSuite\TestCase;
  21. use RuntimeException;
  22. /**
  23. * Tests WindowExpression class
  24. */
  25. class WindowQueryTests extends TestCase
  26. {
  27. protected $fixtures = [
  28. 'core.Comments',
  29. ];
  30. public $autoFixtures = false;
  31. /**
  32. * @var \Cake\Database\Connection
  33. */
  34. protected $connection = null;
  35. /**
  36. * @var bool
  37. */
  38. protected $autoQuote;
  39. /**
  40. * @var bool
  41. */
  42. protected $skipTests = false;
  43. public function setUp(): void
  44. {
  45. parent::setUp();
  46. $this->connection = ConnectionManager::get('test');
  47. $this->autoQuote = $this->connection->getDriver()->isAutoQuotingEnabled();
  48. $driver = $this->connection->getDriver();
  49. if (
  50. $driver instanceof \Cake\Database\Driver\Mysql ||
  51. $driver instanceof \Cake\Database\Driver\Sqlite
  52. ) {
  53. $this->skipTests = !$this->connection->getDriver()->supportsWindowFunctions();
  54. } else {
  55. $this->skipTests = false;
  56. }
  57. }
  58. public function tearDown(): void
  59. {
  60. parent::tearDown();
  61. }
  62. /**
  63. * Tests window sql generation.
  64. *
  65. * @return void
  66. */
  67. public function testWindowSql()
  68. {
  69. $query = new Query($this->connection);
  70. $sql = $query
  71. ->select('*')
  72. ->window('name', new WindowExpression())
  73. ->sql();
  74. $this->assertRegExpSql('SELECT \* WINDOW <name> AS \(\)', $sql, !$this->autoQuote);
  75. $sql = $query
  76. ->window('name2', new WindowExpression('name'))
  77. ->sql();
  78. $this->assertRegExpSql('SELECT \* WINDOW <name> AS \(\), <name2> AS \(<name>\)', $sql, !$this->autoQuote);
  79. $sql = $query
  80. ->window('name', function ($window, $query) {
  81. return $window->name('name3');
  82. }, true)
  83. ->sql();
  84. $this->assertEqualsSql('SELECT * WINDOW name AS (name3)', $sql);
  85. }
  86. public function testMissingWindow()
  87. {
  88. $this->expectException(RuntimeException::class);
  89. $this->expectExceptionMessage('You must return a `WindowExpression`');
  90. (new Query($this->connection))->window('name', function () {
  91. return new QueryExpression();
  92. });
  93. }
  94. public function testPartitions()
  95. {
  96. $this->skipIf($this->skipTests);
  97. $this->loadFixtures('Comments');
  98. $query = new Query($this->connection);
  99. $result = $query
  100. ->select(['num_rows' => $query->func()->count('*')->over()])
  101. ->from('comments')
  102. ->execute()
  103. ->fetchAll();
  104. $this->assertCount(6, $result);
  105. $query = new Query($this->connection);
  106. $result = $query
  107. ->select(['num_rows' => $query->func()->count('*')->partition('article_id')])
  108. ->from('comments')
  109. ->order(['article_id'])
  110. ->execute()
  111. ->fetchAll('assoc');
  112. $this->assertEquals(4, $result[0]['num_rows']);
  113. $query = new Query($this->connection);
  114. $result = $query
  115. ->select(['num_rows' => $query->func()->count('*')->partition('article_id')->order('updated')])
  116. ->from('comments')
  117. ->order(['updated'])
  118. ->execute()
  119. ->fetchAll('assoc');
  120. $this->assertEquals(1, $result[0]['num_rows']);
  121. $this->assertEquals(4, $result[3]['num_rows']);
  122. $this->assertEquals(1, $result[4]['num_rows']);
  123. }
  124. /**
  125. * Tests adding named windows to the query.
  126. *
  127. * @return void
  128. */
  129. public function testNamedWindow()
  130. {
  131. $skip = $this->skipTests;
  132. if (!$skip) {
  133. $skip = $this->connection->getDriver() instanceof \Cake\Database\Driver\Sqlserver;
  134. }
  135. $this->skipIf($skip);
  136. $this->loadFixtures('Comments');
  137. $query = new Query($this->connection);
  138. $result = $query
  139. ->select(['num_rows' => $query->func()->count('*')->over('window1')])
  140. ->from('comments')
  141. ->window('window1', (new WindowExpression())->partition('article_id'))
  142. ->order(['article_id'])
  143. ->execute()
  144. ->fetchAll('assoc');
  145. $this->assertEquals(4, $result[0]['num_rows']);
  146. }
  147. public function testWindowChaining()
  148. {
  149. $skip = $this->skipTests;
  150. if (!$skip) {
  151. $driver = $this->connection->getDriver();
  152. $skip = $driver instanceof \Cake\Database\Driver\Sqlserver;
  153. if ($driver instanceof \Cake\Database\Driver\Sqlite) {
  154. $skip = version_compare($driver->getVersion(), '3.28.0', '<');
  155. }
  156. }
  157. $this->skipIf($skip);
  158. $this->loadFixtures('Comments');
  159. $query = new Query($this->connection);
  160. $result = $query
  161. ->select(['num_rows' => $query->func()->count('*')->over('window2')])
  162. ->from('comments')
  163. ->window('window1', (new WindowExpression())->partition('article_id'))
  164. ->window('window2', new WindowExpression('window1'))
  165. ->order(['article_id'])
  166. ->execute()
  167. ->fetchAll('assoc');
  168. $this->assertEquals(4, $result[0]['num_rows']);
  169. }
  170. }