ExpressionTypeCastingIntegrationTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The Open Group Test Suite License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.3.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Database;
  15. use Cake\Database\Driver;
  16. use Cake\Database\Driver\Sqlserver;
  17. use Cake\Database\Expression\FunctionExpression;
  18. use Cake\Database\Type;
  19. use Cake\Database\Type\ExpressionTypeInterface;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Value object for testing mappings.
  24. */
  25. class UuidValue
  26. {
  27. public $value;
  28. public function __construct($value)
  29. {
  30. $this->value = $value;
  31. }
  32. }
  33. /**
  34. * Custom type class that maps between value objects, and SQL expressions.
  35. */
  36. class OrderedUuidType extends Type implements ExpressionTypeInterface
  37. {
  38. public function toPHP($value, Driver $d)
  39. {
  40. return new UuidValue($value);
  41. }
  42. public function toExpression($value)
  43. {
  44. if ($value instanceof UuidValue) {
  45. $value = $value->value;
  46. }
  47. $substr = function ($start, $length = null) use ($value) {
  48. return new FunctionExpression(
  49. 'SUBSTR',
  50. $length === null ? [$value, $start] : [$value, $start, $length],
  51. ['string', 'integer', 'integer']
  52. );
  53. };
  54. return new FunctionExpression(
  55. 'CONCAT',
  56. [$substr(15, 4), $substr(10, 4), $substr(1, 8), $substr(20, 4), $substr(25)]
  57. );
  58. }
  59. }
  60. /**
  61. * Tests for Expression objects casting values to other expressions
  62. * using the type classes
  63. *
  64. */
  65. class ExpressionTypeCastingIntegrationTest extends TestCase
  66. {
  67. public $fixtures = ['core.ordered_uuid_items'];
  68. public function setUp()
  69. {
  70. parent::setUp();
  71. $this->connection = ConnectionManager::get('test');
  72. $this->skipIf($this->connection->driver() instanceof Sqlserver, 'This tests uses functions specific to other drivers');
  73. Type::map('ordered_uuid', OrderedUuidType::class);
  74. }
  75. protected function _insert()
  76. {
  77. $query = $this->connection->newQuery();
  78. $query
  79. ->insert(['id', 'published', 'name'], ['id' => 'ordered_uuid'])
  80. ->into('ordered_uuid_items')
  81. ->values(['id' => '481fc6d0-b920-43e0-a40d-6d1740cf8569', 'published' => 0, 'name' => 'Item 1'])
  82. ->values(['id' => '48298a29-81c0-4c26-a7fb-413140cf8569', 'published' => 0, 'name' => 'Item 2'])
  83. ->values(['id' => '482b7756-8da0-419a-b21f-27da40cf8569', 'published' => 0, 'name' => 'Item 3']);
  84. $query->execute();
  85. }
  86. /**
  87. * Tests inserting a value that is to be converted to an expression
  88. * automatically
  89. *
  90. * @return void
  91. */
  92. public function testInsert()
  93. {
  94. $this->_insert();
  95. $query = $this->connection->newQuery()
  96. ->select('id')
  97. ->from('ordered_uuid_items')
  98. ->order('id')
  99. ->defaultTypes(['id' => 'ordered_uuid']);
  100. $query->selectTypeMap($query->typeMap());
  101. $results = $query->execute()->fetchAll('assoc');
  102. $this->assertEquals(new UuidValue('419a8da0482b7756b21f27da40cf8569'), $results[0]['id']);
  103. $this->assertEquals(new UuidValue('43e0b920481fc6d0a40d6d1740cf8569'), $results[1]['id']);
  104. $this->assertEquals(new UuidValue('4c2681c048298a29a7fb413140cf8569'), $results[2]['id']);
  105. }
  106. /**
  107. * Test selecting with a custom expression type using conditions
  108. *
  109. * @return void
  110. */
  111. public function testSelectWithConditions()
  112. {
  113. $this->_insert();
  114. $result = $this->connection->newQuery()
  115. ->select('id')
  116. ->from('ordered_uuid_items')
  117. ->where(['id' => '48298a29-81c0-4c26-a7fb-413140cf8569'], ['id' => 'ordered_uuid'])
  118. ->execute()
  119. ->fetchAll('assoc');
  120. $this->assertCount(1, $result);
  121. $this->assertEquals('4c2681c048298a29a7fb413140cf8569', $result[0]['id']);
  122. }
  123. /**
  124. * Tests Select using value object in conditions
  125. *
  126. * @return void
  127. */
  128. public function testSelectWithConditionsValueObject()
  129. {
  130. $this->_insert();
  131. $result = $this->connection->newQuery()
  132. ->select('id')
  133. ->from('ordered_uuid_items')
  134. ->where(['id' => new UuidValue('48298a29-81c0-4c26-a7fb-413140cf8569')], ['id' => 'ordered_uuid'])
  135. ->execute()
  136. ->fetchAll('assoc');
  137. $this->assertCount(1, $result);
  138. $this->assertEquals('4c2681c048298a29a7fb413140cf8569', $result[0]['id']);
  139. }
  140. /**
  141. * Tests using the expression type in with an IN condition
  142. *
  143. * @var string
  144. */
  145. public function testSelectWithInCondition()
  146. {
  147. $this->_insert();
  148. $result = $this->connection->newQuery()
  149. ->select('id')
  150. ->from('ordered_uuid_items')
  151. ->where(
  152. ['id' => ['48298a29-81c0-4c26-a7fb-413140cf8569', '482b7756-8da0-419a-b21f-27da40cf8569']],
  153. ['id' => 'ordered_uuid[]']
  154. )
  155. ->order('id')
  156. ->execute()
  157. ->fetchAll('assoc');
  158. $this->assertCount(2, $result);
  159. $this->assertEquals('419a8da0482b7756b21f27da40cf8569', $result[0]['id']);
  160. $this->assertEquals('419a8da0482b7756b21f27da40cf8569', $result[0]['id']);
  161. }
  162. /**
  163. * Tests using an expression type in a between condition
  164. *
  165. * @return void
  166. */
  167. public function testSelectWithBetween()
  168. {
  169. $this->_insert();
  170. $result = $this->connection->newQuery()
  171. ->select('id')
  172. ->from('ordered_uuid_items')
  173. ->where(function ($exp) {
  174. return $exp->between(
  175. 'id',
  176. '482b7756-8da0-419a-b21f-27da40cf8569',
  177. '48298a29-81c0-4c26-a7fb-413140cf8569',
  178. 'ordered_uuid'
  179. );
  180. })
  181. ->execute()
  182. ->fetchAll('assoc');
  183. $this->assertCount(3, $result);
  184. }
  185. /**
  186. * Tests using an expression type inside a function expression
  187. *
  188. * @return void
  189. */
  190. public function testSelectWithFunction()
  191. {
  192. $this->_insert();
  193. $result = $this->connection->newQuery()
  194. ->select('id')
  195. ->from('ordered_uuid_items')
  196. ->where(function ($exp, $q) {
  197. return $exp->eq(
  198. 'id',
  199. $q->func()->concat(['48298a29-81c0-4c26-a7fb', '-413140cf8569'], []),
  200. 'ordered_uuid'
  201. );
  202. })
  203. ->execute()
  204. ->fetchAll('assoc');
  205. $this->assertCount(1, $result);
  206. $this->assertEquals('4c2681c048298a29a7fb413140cf8569', $result[0]['id']);
  207. }
  208. }