ExpressionTypeCastingIntegrationTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 3.3.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database;
  16. use Cake\Database\Driver\Sqlserver;
  17. use Cake\Database\Expression\QueryExpression;
  18. use Cake\Database\Query;
  19. use Cake\Database\TypeFactory;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. use TestApp\Database\Type\OrderedUuidType;
  23. use TestApp\Database\Type\UuidValue;
  24. /**
  25. * Tests for Expression objects casting values to other expressions
  26. * using the type classes
  27. */
  28. class ExpressionTypeCastingIntegrationTest extends TestCase
  29. {
  30. protected $fixtures = ['core.OrderedUuidItems'];
  31. /**
  32. * @var \Cake\Database\Connection
  33. */
  34. protected $connection;
  35. public function setUp(): void
  36. {
  37. parent::setUp();
  38. $this->connection = ConnectionManager::get('test');
  39. $this->skipIf($this->connection->getDriver() instanceof Sqlserver, 'This tests uses functions specific to other drivers');
  40. TypeFactory::map('ordered_uuid', OrderedUuidType::class);
  41. }
  42. protected function _insert()
  43. {
  44. $query = $this->connection->newQuery();
  45. $query
  46. ->insert(['id', 'published', 'name'], ['id' => 'ordered_uuid'])
  47. ->into('ordered_uuid_items')
  48. ->values(['id' => '481fc6d0-b920-43e0-a40d-6d1740cf8569', 'published' => 0, 'name' => 'Item 1'])
  49. ->values(['id' => '48298a29-81c0-4c26-a7fb-413140cf8569', 'published' => 0, 'name' => 'Item 2'])
  50. ->values(['id' => '482b7756-8da0-419a-b21f-27da40cf8569', 'published' => 0, 'name' => 'Item 3']);
  51. $query->execute();
  52. }
  53. /**
  54. * Tests inserting a value that is to be converted to an expression
  55. * automatically
  56. *
  57. * @return void
  58. */
  59. public function testInsert()
  60. {
  61. $this->_insert();
  62. $query = $this->connection->newQuery()
  63. ->select('id')
  64. ->from('ordered_uuid_items')
  65. ->order('id')
  66. ->setDefaultTypes(['id' => 'ordered_uuid']);
  67. $query->setSelectTypeMap($query->getTypeMap());
  68. $results = $query->execute()->fetchAll('assoc');
  69. $this->assertEquals(new UuidValue('419a8da0482b7756b21f27da40cf8569'), $results[0]['id']);
  70. $this->assertEquals(new UuidValue('43e0b920481fc6d0a40d6d1740cf8569'), $results[1]['id']);
  71. $this->assertEquals(new UuidValue('4c2681c048298a29a7fb413140cf8569'), $results[2]['id']);
  72. }
  73. /**
  74. * Test selecting with a custom expression type using conditions
  75. *
  76. * @return void
  77. */
  78. public function testSelectWithConditions()
  79. {
  80. $this->_insert();
  81. $result = $this->connection->newQuery()
  82. ->select('id')
  83. ->from('ordered_uuid_items')
  84. ->where(['id' => '48298a29-81c0-4c26-a7fb-413140cf8569'], ['id' => 'ordered_uuid'])
  85. ->execute()
  86. ->fetchAll('assoc');
  87. $this->assertCount(1, $result);
  88. $this->assertSame('4c2681c048298a29a7fb413140cf8569', $result[0]['id']);
  89. }
  90. /**
  91. * Tests Select using value object in conditions
  92. *
  93. * @return void
  94. */
  95. public function testSelectWithConditionsValueObject()
  96. {
  97. $this->_insert();
  98. $result = $this->connection->newQuery()
  99. ->select('id')
  100. ->from('ordered_uuid_items')
  101. ->where(['id' => new UuidValue('48298a29-81c0-4c26-a7fb-413140cf8569')], ['id' => 'ordered_uuid'])
  102. ->execute()
  103. ->fetchAll('assoc');
  104. $this->assertCount(1, $result);
  105. $this->assertSame('4c2681c048298a29a7fb413140cf8569', $result[0]['id']);
  106. }
  107. /**
  108. * Tests using the expression type in with an IN condition
  109. *
  110. * @var string
  111. */
  112. public function testSelectWithInCondition()
  113. {
  114. $this->_insert();
  115. $result = $this->connection->newQuery()
  116. ->select('id')
  117. ->from('ordered_uuid_items')
  118. ->where(
  119. ['id' => ['48298a29-81c0-4c26-a7fb-413140cf8569', '482b7756-8da0-419a-b21f-27da40cf8569']],
  120. ['id' => 'ordered_uuid[]']
  121. )
  122. ->order('id')
  123. ->execute()
  124. ->fetchAll('assoc');
  125. $this->assertCount(2, $result);
  126. $this->assertSame('419a8da0482b7756b21f27da40cf8569', $result[0]['id']);
  127. $this->assertSame('419a8da0482b7756b21f27da40cf8569', $result[0]['id']);
  128. }
  129. /**
  130. * Tests using an expression type in a between condition
  131. *
  132. * @return void
  133. */
  134. public function testSelectWithBetween()
  135. {
  136. $this->_insert();
  137. $result = $this->connection->newQuery()
  138. ->select('id')
  139. ->from('ordered_uuid_items')
  140. ->where(function (QueryExpression $exp) {
  141. return $exp->between(
  142. 'id',
  143. '482b7756-8da0-419a-b21f-27da40cf8569',
  144. '48298a29-81c0-4c26-a7fb-413140cf8569',
  145. 'ordered_uuid'
  146. );
  147. })
  148. ->execute()
  149. ->fetchAll('assoc');
  150. $this->assertCount(3, $result);
  151. }
  152. /**
  153. * Tests using an expression type inside a function expression
  154. *
  155. * @return void
  156. */
  157. public function testSelectWithFunction()
  158. {
  159. $this->_insert();
  160. $result = $this->connection->newQuery()
  161. ->select('id')
  162. ->from('ordered_uuid_items')
  163. ->where(function (QueryExpression $exp, Query $q) {
  164. return $exp->eq(
  165. 'id',
  166. $q->func()->concat(['48298a29-81c0-4c26-a7fb', '-413140cf8569'], []),
  167. 'ordered_uuid'
  168. );
  169. })
  170. ->execute()
  171. ->fetchAll('assoc');
  172. $this->assertCount(1, $result);
  173. $this->assertSame('4c2681c048298a29a7fb413140cf8569', $result[0]['id']);
  174. }
  175. }