ExpressionTypeCastingIntegrationTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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(): void
  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. public function testInsert(): void
  58. {
  59. $this->_insert();
  60. $query = $this->connection->newQuery()
  61. ->select('id')
  62. ->from('ordered_uuid_items')
  63. ->order('id')
  64. ->setDefaultTypes(['id' => 'ordered_uuid']);
  65. $query->setSelectTypeMap($query->getTypeMap());
  66. $results = $query->execute()->fetchAll('assoc');
  67. $this->assertEquals(new UuidValue('419a8da0482b7756b21f27da40cf8569'), $results[0]['id']);
  68. $this->assertEquals(new UuidValue('43e0b920481fc6d0a40d6d1740cf8569'), $results[1]['id']);
  69. $this->assertEquals(new UuidValue('4c2681c048298a29a7fb413140cf8569'), $results[2]['id']);
  70. }
  71. /**
  72. * Test selecting with a custom expression type using conditions
  73. */
  74. public function testSelectWithConditions(): void
  75. {
  76. $this->_insert();
  77. $result = $this->connection->newQuery()
  78. ->select('id')
  79. ->from('ordered_uuid_items')
  80. ->where(['id' => '48298a29-81c0-4c26-a7fb-413140cf8569'], ['id' => 'ordered_uuid'])
  81. ->execute()
  82. ->fetchAll('assoc');
  83. $this->assertCount(1, $result);
  84. $this->assertSame('4c2681c048298a29a7fb413140cf8569', $result[0]['id']);
  85. }
  86. /**
  87. * Tests Select using value object in conditions
  88. */
  89. public function testSelectWithConditionsValueObject(): void
  90. {
  91. $this->_insert();
  92. $result = $this->connection->newQuery()
  93. ->select('id')
  94. ->from('ordered_uuid_items')
  95. ->where(['id' => new UuidValue('48298a29-81c0-4c26-a7fb-413140cf8569')], ['id' => 'ordered_uuid'])
  96. ->execute()
  97. ->fetchAll('assoc');
  98. $this->assertCount(1, $result);
  99. $this->assertSame('4c2681c048298a29a7fb413140cf8569', $result[0]['id']);
  100. }
  101. /**
  102. * Tests using the expression type in with an IN condition
  103. *
  104. * @var string
  105. */
  106. public function testSelectWithInCondition(): void
  107. {
  108. $this->_insert();
  109. $result = $this->connection->newQuery()
  110. ->select('id')
  111. ->from('ordered_uuid_items')
  112. ->where(
  113. ['id' => ['48298a29-81c0-4c26-a7fb-413140cf8569', '482b7756-8da0-419a-b21f-27da40cf8569']],
  114. ['id' => 'ordered_uuid[]']
  115. )
  116. ->order('id')
  117. ->execute()
  118. ->fetchAll('assoc');
  119. $this->assertCount(2, $result);
  120. $this->assertSame('419a8da0482b7756b21f27da40cf8569', $result[0]['id']);
  121. $this->assertSame('419a8da0482b7756b21f27da40cf8569', $result[0]['id']);
  122. }
  123. /**
  124. * Tests using an expression type in a between condition
  125. */
  126. public function testSelectWithBetween(): void
  127. {
  128. $this->_insert();
  129. $result = $this->connection->newQuery()
  130. ->select('id')
  131. ->from('ordered_uuid_items')
  132. ->where(function (QueryExpression $exp) {
  133. return $exp->between(
  134. 'id',
  135. '482b7756-8da0-419a-b21f-27da40cf8569',
  136. '48298a29-81c0-4c26-a7fb-413140cf8569',
  137. 'ordered_uuid'
  138. );
  139. })
  140. ->execute()
  141. ->fetchAll('assoc');
  142. $this->assertCount(3, $result);
  143. }
  144. /**
  145. * Tests using an expression type inside a function expression
  146. */
  147. public function testSelectWithFunction(): void
  148. {
  149. $this->_insert();
  150. $result = $this->connection->newQuery()
  151. ->select('id')
  152. ->from('ordered_uuid_items')
  153. ->where(function (QueryExpression $exp, Query $q) {
  154. return $exp->eq(
  155. 'id',
  156. $q->func()->concat(['48298a29-81c0-4c26-a7fb', '-413140cf8569'], []),
  157. 'ordered_uuid'
  158. );
  159. })
  160. ->execute()
  161. ->fetchAll('assoc');
  162. $this->assertCount(1, $result);
  163. $this->assertSame('4c2681c048298a29a7fb413140cf8569', $result[0]['id']);
  164. }
  165. }