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