ExpressionTypeCastingIntegrationTest.php 5.9 KB

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