ForwardsCompatibilityTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.5.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database\QueryTests;
  17. use Cake\Database\Connection;
  18. use Cake\Database\Query\DeleteQuery;
  19. use Cake\Database\Query\InsertQuery;
  20. use Cake\Database\Query\SelectQuery;
  21. use Cake\Database\Query\UpdateQuery;
  22. use Cake\Datasource\ConnectionManager;
  23. use Cake\TestSuite\TestCase;
  24. class ForwardsCompatibilityTest extends TestCase
  25. {
  26. protected $fixtures = [
  27. 'core.Articles',
  28. 'core.Authors',
  29. ];
  30. public static function queryProvider()
  31. {
  32. return [
  33. [fn (Connection $connection) => new DeleteQuery($connection), 'delete'],
  34. [fn (Connection $connection) => new InsertQuery($connection), 'insert'],
  35. [fn (Connection $connection) => new SelectQuery($connection), 'select'],
  36. [fn (Connection $connection) => new UpdateQuery($connection), 'update'],
  37. ];
  38. }
  39. /**
  40. * @dataProvider queryProvider
  41. */
  42. public function testAsInsert($queryFactory)
  43. {
  44. $query = $queryFactory(ConnectionManager::get('test'));
  45. $scenario = function () use ($query) {
  46. $statement = $query
  47. ->insert(['author_id', 'title', 'body', 'published'])
  48. ->into('articles')
  49. ->values([1, 'custom article', 'so long'])
  50. ->execute();
  51. $this->assertEquals(1, $statement->rowCount());
  52. $statement->closeCursor();
  53. };
  54. if ($query instanceof InsertQuery) {
  55. return $scenario();
  56. }
  57. $this->deprecated($scenario);
  58. }
  59. /**
  60. * @dataProvider queryProvider
  61. */
  62. public function testAsUpdate($queryFactory)
  63. {
  64. $query = $queryFactory(ConnectionManager::get('test'));
  65. $scenario = function () use ($query) {
  66. $statement = $query
  67. ->update('articles')
  68. ->set(['title' => 'Updated'])
  69. ->where(['title' => 'First Article'])
  70. ->execute();
  71. $this->assertEquals(1, $statement->rowCount());
  72. $statement->closeCursor();
  73. };
  74. if ($query instanceof UpdateQuery) {
  75. return $scenario();
  76. }
  77. $this->deprecated($scenario);
  78. }
  79. /**
  80. * @dataProvider queryProvider
  81. */
  82. public function testAsDelete($queryFactory)
  83. {
  84. $query = $queryFactory(ConnectionManager::get('test'));
  85. $scenario = function () use ($query) {
  86. $statement = $query
  87. ->delete('articles')
  88. ->where(['title' => 'First Article'])
  89. ->epilog('')
  90. ->execute();
  91. $this->assertEquals(1, $statement->rowCount());
  92. $statement->closeCursor();
  93. };
  94. if ($query instanceof DeleteQuery) {
  95. return $scenario();
  96. }
  97. $this->deprecated($scenario);
  98. }
  99. /**
  100. * @dataProvider queryProvider
  101. */
  102. public function testAsSelect($queryFactory)
  103. {
  104. $query = $queryFactory(ConnectionManager::get('test'));
  105. $scenario = function () use ($query) {
  106. $statement = $query
  107. ->select(['title', 'body'])
  108. ->from(['Articles' => 'articles'])
  109. ->where(['title' => 'First Article'])
  110. ->execute();
  111. $this->assertEquals(1, $statement->rowCount());
  112. $statement->closeCursor();
  113. };
  114. if ($query instanceof SelectQuery) {
  115. return $scenario();
  116. }
  117. $this->deprecated($scenario);
  118. }
  119. }