UpdateSqlGenerationTest.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\Tests\ORM\Query;
  22. require_once __DIR__ . '/../../TestInit.php';
  23. /**
  24. * Test case for testing the saving and referencing of query identifiers.
  25. *
  26. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  27. * @author Janne Vanhala <jpvanhal@cc.hut.fi>
  28. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  29. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  30. * @link http://www.phpdoctrine.org
  31. * @since 2.0
  32. * @version $Revision$
  33. * @todo 1) [romanb] We might want to split the SQL generation tests into multiple
  34. * testcases later since we'll have a lot of them and we might want to have special SQL
  35. * generation tests for some dbms specific SQL syntaxes.
  36. */
  37. class UpdateSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
  38. {
  39. private $_em;
  40. protected function setUp() {
  41. $this->_em = $this->_getTestEntityManager();
  42. }
  43. public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
  44. {
  45. try {
  46. $query = $this->_em->createQuery($dqlToBeTested);
  47. parent::assertEquals($sqlToBeConfirmed, $query->getSql());
  48. $query->free();
  49. } catch (\Exception $e) {
  50. $this->fail($e->getMessage());
  51. }
  52. }
  53. public function testSupportsQueriesWithoutWhere()
  54. {
  55. $this->assertSqlGeneration(
  56. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1',
  57. 'UPDATE cms_users SET name = ?'
  58. );
  59. }
  60. public function testSupportsMultipleFieldsWithoutWhere()
  61. {
  62. $this->assertSqlGeneration(
  63. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1, u.username = ?2',
  64. 'UPDATE cms_users SET name = ?, username = ?'
  65. );
  66. }
  67. public function testSupportsWhereClauses()
  68. {
  69. $this->assertSqlGeneration(
  70. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1 WHERE u.id = ?2',
  71. 'UPDATE cms_users SET name = ? WHERE id = ?'
  72. );
  73. }
  74. public function testSupportsWhereClausesOnTheUpdatedField()
  75. {
  76. $this->assertSqlGeneration(
  77. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1 WHERE u.name = ?2',
  78. 'UPDATE cms_users SET name = ? WHERE name = ?'
  79. );
  80. }
  81. public function testSupportsMultipleWhereClause()
  82. {
  83. $this->assertSqlGeneration(
  84. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1 WHERE u.name = ?2 AND u.status = ?3',
  85. 'UPDATE cms_users SET name = ? WHERE name = ? AND status = ?'
  86. );
  87. }
  88. public function testSupportsInClause()
  89. {
  90. $this->assertSqlGeneration(
  91. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1 WHERE u.id IN (1, 3, 4)',
  92. 'UPDATE cms_users SET name = ? WHERE id IN (1, 3, 4)'
  93. );
  94. }
  95. public function testSupportsParametrizedInClause()
  96. {
  97. $this->assertSqlGeneration(
  98. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1 WHERE u.id IN (?2, ?3, ?4)',
  99. 'UPDATE cms_users SET name = ? WHERE id IN (?, ?, ?)'
  100. );
  101. }
  102. public function testSupportsNotInClause()
  103. {
  104. $this->assertSqlGeneration(
  105. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1 WHERE u.id NOT IN (1, 3, 4)',
  106. 'UPDATE cms_users SET name = ? WHERE id NOT IN (1, 3, 4)'
  107. );
  108. }
  109. public function testSupportsGreatherThanClause()
  110. {
  111. $this->assertSqlGeneration(
  112. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = ?1 WHERE u.id > ?2',
  113. 'UPDATE cms_users SET status = ? WHERE id > ?'
  114. );
  115. }
  116. public function testSupportsGreatherThanOrEqualToClause()
  117. {
  118. $this->assertSqlGeneration(
  119. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = ?1 WHERE u.id >= ?2',
  120. 'UPDATE cms_users SET status = ? WHERE id >= ?'
  121. );
  122. }
  123. public function testSupportsLessThanClause()
  124. {
  125. $this->assertSqlGeneration(
  126. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = ?1 WHERE u.id < ?2',
  127. 'UPDATE cms_users SET status = ? WHERE id < ?'
  128. );
  129. }
  130. public function testSupportsLessThanOrEqualToClause()
  131. {
  132. $this->assertSqlGeneration(
  133. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = ?1 WHERE u.id <= ?2',
  134. 'UPDATE cms_users SET status = ? WHERE id <= ?'
  135. );
  136. }
  137. public function testSupportsBetweenClause()
  138. {
  139. $this->assertSqlGeneration(
  140. 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = ?1 WHERE u.id BETWEEN :from AND :to',
  141. 'UPDATE cms_users SET status = ? WHERE id BETWEEN ? AND ?'
  142. );
  143. }
  144. public function testSingleValuedAssociationFieldInWhere()
  145. {
  146. $this->assertSqlGeneration(
  147. "UPDATE Doctrine\Tests\Models\CMS\CmsPhonenumber p SET p.phonenumber = 1234 WHERE p.user = ?1",
  148. "UPDATE cms_phonenumbers SET phonenumber = 1234 WHERE user_id = ?"
  149. );
  150. }
  151. public function testSingleValuedAssociationFieldInSetClause()
  152. {
  153. $this->assertSqlGeneration(
  154. "update Doctrine\Tests\Models\CMS\CmsComment c set c.article = null where c.article=?1",
  155. "UPDATE cms_comments SET article_id = NULL WHERE article_id = ?"
  156. );
  157. }
  158. /**
  159. * @group DDC-980
  160. */
  161. public function testSubselectTableAliasReferencing()
  162. {
  163. $this->assertSqlGeneration(
  164. "UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = 'inactive' WHERE SIZE(u.groups) = 10",
  165. "UPDATE cms_users SET status = 'inactive' WHERE (SELECT COUNT(*) FROM cms_users_groups c0_ WHERE c0_.user_id = cms_users.id) = 10"
  166. );
  167. }
  168. }