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