ExpressionTypeCastingIntegrationTest.php 6.3 KB

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