AggregatesQueryTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\Driver\Postgres;
  17. use Cake\Database\Driver\Sqlite;
  18. use Cake\Database\Query\SelectQuery;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Tests AggregateExpression queries
  23. */
  24. class AggregatesQueryTest extends TestCase
  25. {
  26. protected array $fixtures = [
  27. 'core.Comments',
  28. ];
  29. /**
  30. * @var \Cake\Database\Connection
  31. */
  32. protected $connection = null;
  33. /**
  34. * @var bool
  35. */
  36. protected $skipTests = false;
  37. public function setUp(): void
  38. {
  39. parent::setUp();
  40. $this->connection = ConnectionManager::get('test');
  41. }
  42. public function tearDown(): void
  43. {
  44. parent::tearDown();
  45. }
  46. /**
  47. * Tests filtering aggregate function rows.
  48. */
  49. public function testFilters(): void
  50. {
  51. $skip = !($this->connection->getDriver() instanceof Postgres);
  52. if ($this->connection->getDriver() instanceof Sqlite) {
  53. $skip = version_compare($this->connection->getDriver()->version(), '3.30.0', '<');
  54. }
  55. $this->skipif($skip);
  56. $query = new SelectQuery($this->connection);
  57. $result = $query
  58. ->select(['num_rows' => $query->func()->count('*')->filter(['article_id' => 2])])
  59. ->from('comments')
  60. ->execute()
  61. ->fetchAll('assoc');
  62. $this->assertSame(2, (int)$result[0]['num_rows']);
  63. }
  64. }