CommonTableExpressionTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.1.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database\Expression;
  17. use Cake\Database\Expression\CommonTableExpression;
  18. use Cake\Database\Expression\IdentifierExpression;
  19. use Cake\Database\ValueBinder;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. class CommonTableExpressionTest extends TestCase
  23. {
  24. /**
  25. * @var \Cake\Database\Connection
  26. */
  27. protected $connection;
  28. public function setUp(): void
  29. {
  30. parent::setUp();
  31. $this->connection = ConnectionManager::get('test');
  32. }
  33. public function tearDown(): void
  34. {
  35. parent::tearDown();
  36. unset($this->connection);
  37. }
  38. /**
  39. * Tests constructing CommonTableExpressions.
  40. */
  41. public function testCteConstructor(): void
  42. {
  43. $cte = new CommonTableExpression('test', $this->connection->selectQuery());
  44. $this->assertEqualsSql('test AS ()', $cte->sql(new ValueBinder()));
  45. $cte = (new CommonTableExpression())
  46. ->name('test')
  47. ->query($this->connection->selectQuery());
  48. $this->assertEqualsSql('test AS ()', $cte->sql(new ValueBinder()));
  49. }
  50. /**
  51. * Tests setting fields.
  52. */
  53. public function testFields(): void
  54. {
  55. $cte = (new CommonTableExpression('test', $this->connection->selectQuery()))
  56. ->field('col1')
  57. ->field([new IdentifierExpression('col2')]);
  58. $this->assertEqualsSql('test(col1, col2) AS ()', $cte->sql(new ValueBinder()));
  59. }
  60. /**
  61. * Tests setting CTE materialized
  62. */
  63. public function testMaterialized(): void
  64. {
  65. $cte = (new CommonTableExpression('test', $this->connection->selectQuery()))
  66. ->materialized();
  67. $this->assertEqualsSql('test AS MATERIALIZED ()', $cte->sql(new ValueBinder()));
  68. $cte->notMaterialized();
  69. $this->assertEqualsSql('test AS NOT MATERIALIZED ()', $cte->sql(new ValueBinder()));
  70. }
  71. /**
  72. * Tests setting CTE as recursive.
  73. */
  74. public function testRecursive(): void
  75. {
  76. $cte = (new CommonTableExpression('test', $this->connection->selectQuery()))
  77. ->recursive();
  78. $this->assertTrue($cte->isRecursive());
  79. }
  80. /**
  81. * Tests setting query using closures.
  82. */
  83. public function testQueryClosures(): void
  84. {
  85. $cte = new CommonTableExpression('test', function () {
  86. return $this->connection->selectQuery();
  87. });
  88. $this->assertEqualsSql('test AS ()', $cte->sql(new ValueBinder()));
  89. $cte->query(function () {
  90. return $this->connection->selectQuery('1');
  91. });
  92. $this->assertEqualsSql('test AS (SELECT 1)', $cte->sql(new ValueBinder()));
  93. }
  94. /**
  95. * Tests traversing CommonTableExpression.
  96. */
  97. public function testTraverse(): void
  98. {
  99. $query = $this->connection->selectQuery('1');
  100. $cte = (new CommonTableExpression('test', $query))->field('field');
  101. $expressions = [];
  102. $cte->traverse(function ($expression) use (&$expressions): void {
  103. $expressions[] = $expression;
  104. });
  105. $this->assertEquals(new IdentifierExpression('test'), $expressions[0]);
  106. $this->assertEquals(new IdentifierExpression('field'), $expressions[1]);
  107. $this->assertEquals($query, $expressions[2]);
  108. }
  109. /**
  110. * Tests cloning CommonTableExpression
  111. */
  112. public function testClone(): void
  113. {
  114. $cte = new CommonTableExpression('test', function () {
  115. return $this->connection->selectQuery('1');
  116. });
  117. $cte2 = (clone $cte)->name('test2');
  118. $this->assertNotSame($cte->sql(new ValueBinder()), $cte2->sql(new ValueBinder()));
  119. $cte2 = (clone $cte)->field('col1');
  120. $this->assertNotSame($cte->sql(new ValueBinder()), $cte2->sql(new ValueBinder()));
  121. }
  122. }