ExpressionTypeCastingIntegrationTest.php 7.1 KB

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