CommonTableExpressionTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * @return void
  42. */
  43. public function testCteConstructor()
  44. {
  45. $cte = new CommonTableExpression('test', $this->connection->newQuery());
  46. $this->assertEqualsSql('test AS ()', $cte->sql(new ValueBinder()));
  47. $cte = (new CommonTableExpression())
  48. ->name('test')
  49. ->query($this->connection->newQuery());
  50. $this->assertEqualsSql('test AS ()', $cte->sql(new ValueBinder()));
  51. }
  52. /**
  53. * Tests setting fields.
  54. *
  55. * @return void
  56. */
  57. public function testFields(): void
  58. {
  59. $cte = (new CommonTableExpression('test', $this->connection->newQuery()))
  60. ->field('col1')
  61. ->field([new IdentifierExpression('col2')]);
  62. $this->assertEqualsSql('test(col1, col2) AS ()', $cte->sql(new ValueBinder()));
  63. }
  64. /**
  65. * Tests setting CTE materialized
  66. *
  67. * @return void
  68. */
  69. public function testMaterialized()
  70. {
  71. $cte = (new CommonTableExpression('test', $this->connection->newQuery()))
  72. ->materialized();
  73. $this->assertEqualsSql('test AS MATERIALIZED ()', $cte->sql(new ValueBinder()));
  74. $cte->notMaterialized();
  75. $this->assertEqualsSql('test AS NOT MATERIALIZED ()', $cte->sql(new ValueBinder()));
  76. }
  77. /**
  78. * Tests setting CTE as recursive.
  79. *
  80. * @return void
  81. */
  82. public function testRecursive()
  83. {
  84. $cte = (new CommonTableExpression('test', $this->connection->newQuery()))
  85. ->recursive();
  86. $this->assertTrue($cte->isRecursive());
  87. }
  88. /**
  89. * Tests setting query using closures.
  90. *
  91. * @return void
  92. */
  93. public function testQueryClosures()
  94. {
  95. $cte = new CommonTableExpression('test', function () {
  96. return $this->connection->newQuery();
  97. });
  98. $this->assertEqualsSql('test AS ()', $cte->sql(new ValueBinder()));
  99. $cte->query(function () {
  100. return $this->connection->newQuery()->select('1');
  101. });
  102. $this->assertEqualsSql('test AS (SELECT 1)', $cte->sql(new ValueBinder()));
  103. }
  104. /**
  105. * Tests traversing CommonTableExpression.
  106. *
  107. * @return void
  108. */
  109. public function testTraverse()
  110. {
  111. $query = $this->connection->newQuery()->select('1');
  112. $field = new IdentifierExpression('field');
  113. $cte = (new CommonTableExpression('test', $query))->field($field);
  114. $expressions = [];
  115. $cte->traverse(function ($expression) use (&$expressions) {
  116. $expressions[] = $expression;
  117. });
  118. $this->assertEquals($field, $expressions[0]);
  119. $this->assertEquals($query, $expressions[1]);
  120. }
  121. /**
  122. * Tests cloning CommonTableExpression
  123. */
  124. public function testClone(): void
  125. {
  126. $cte = new CommonTableExpression('test', function () {
  127. return $this->connection->newQuery()->select('1');
  128. });
  129. $cte2 = (clone $cte)->field('col1');
  130. $this->assertNotSame($cte->sql(new ValueBinder()), $cte2->sql(new ValueBinder()));
  131. }
  132. }