21
21
/**
22
22
* A base class for collections integration tests
23
23
*/
24
- abstract class CollectionsIntegrationTest extends BasicIntegrationTest {
24
+ abstract class CollectionsIntegrationTest extends DatatypeIntegrationTests {
25
25
/**
26
26
* Create user types after initializing cluster and session
27
27
*/
@@ -41,30 +41,6 @@ protected function setUp() {
41
41
}
42
42
}
43
43
44
- /**
45
- * Scalar Cassandra types to be used by data providers
46
- */
47
- public function scalarCassandraTypes () {
48
- return array (
49
- array (Type::ascii (), array ("a " , "b " , "c " )),
50
- array (Type::bigint (), array (new Bigint ("1 " ), new Bigint ("2 " ), new Bigint ("3 " ))),
51
- array (Type::blob (), array (new Blob ("x " ), new Blob ("y " ), new Blob ("z " ))),
52
- array (Type::boolean (), array (true , false , true , false )),
53
- array (Type::decimal (), array (new Decimal (1.1 ), new Decimal (2.2 ), new Decimal (3.3 ))),
54
- array (Type::double (), array (1.1 , 2.2 , 3.3 , 4.4 )),
55
- array (Type::float (), array (new Float (1.0 ), new Float (2.2 ), new Float (2.2 ))),
56
- array (Type::inet (), array (new Inet ("127.0.0.1 " ), new Inet ("127.0.0.2 " ), new Inet ("127.0.0.3 " ))),
57
- array (Type::text (), array ("a " , "b " , "c " , "x " , "y " , "z " )),
58
- array (Type::timestamp (), array (new Timestamp (123 ), new Timestamp (456 ), new Timestamp (789 ))),
59
- array (Type::timeuuid (), array (new Timeuuid (0 ), new Timeuuid (1 ), new Timeuuid (2 ))),
60
- array (Type::uuid (), array (new Uuid ("03398c99-c635-4fad-b30a-3b2c49f785c2 " ),
61
- new Uuid ("03398c99-c635-4fad-b30a-3b2c49f785c3 " ),
62
- new Uuid ("03398c99-c635-4fad-b30a-3b2c49f785c4 " ))),
63
- array (Type::varchar (), array ("a " , "b " , "c " , "x " , "y " , "z " )),
64
- array (Type::varint (), array (new Varint (1 ), new Varint (2 ), new Varint (3 ))),
65
- );
66
- }
67
-
68
44
/**
69
45
* Composite Cassandra types (list, map, set, tuple, and UDT) to be used by
70
46
* data providers
@@ -131,148 +107,4 @@ public function nestedCassandraTypes() {
131
107
132
108
return $ nestedCassandraTypes ;
133
109
}
134
-
135
- /**
136
- * Create a table using $type for the value's type and insert $value using
137
- * positional parameters.
138
- *
139
- * @param $type Cassandra\Type
140
- * @param $value mixed
141
- */
142
- public function createTableInsertAndVerifyValueByIndex ($ type , $ value ) {
143
- $ key = "key " ;
144
- $ options = new ExecutionOptions (array ('arguments ' => array ($ key , $ value )));
145
- $ this ->createTableInsertAndVerifyValue ($ type , $ options , $ key , $ value );
146
- }
147
-
148
- /**
149
- * Create a table using $type for the value's type and insert $value using
150
- * named parameters.
151
- *
152
- * @param $type Cassandra\Type
153
- * @param $value mixed
154
- */
155
- public function createTableInsertAndVerifyValueByName ($ type , $ value ) {
156
- $ key = "key " ;
157
- $ options = new ExecutionOptions (array ('arguments ' => array ("key " => $ key , "value " => $ value )));
158
- $ this ->createTableInsertAndVerifyValue ($ type , $ options , $ key , $ value );
159
- }
160
-
161
- /**
162
- * Create a user type in the current keyspace
163
- *
164
- * @param $userType Cassandra\Type\UserType
165
- */
166
- public function createUserType ($ userType ) {
167
- $ query = "CREATE TYPE IF NOT EXISTS %s (%s) " ;
168
- $ fieldsString = implode (", " , array_map (function ($ name , $ type ) {
169
- return "$ name " . self ::typeString ($ type );
170
- }, array_keys ($ userType ->types ()), $ userType ->types ()));
171
- $ query = sprintf ($ query , $ this ->userTypeString ($ userType ), $ fieldsString );
172
- $ this ->session ->execute (new SimpleStatement ($ query ));
173
- }
174
-
175
- /**
176
- * Create a table named for the CQL $type parameter
177
- *
178
- * @param $type Cassandra\Type
179
- * @return string Table name generated from $type
180
- */
181
- public function createTable ($ type ) {
182
- $ query = "CREATE TABLE IF NOT EXISTS %s (key text PRIMARY KEY, value %s) " ;
183
-
184
- $ cqlType = $ this ->typeString ($ type );
185
- $ tableName = "table_ " . str_replace (array ("- " ), "" , (string )(new Uuid ()));
186
-
187
- $ query = sprintf ($ query , $ tableName , $ cqlType );
188
-
189
- $ this ->session ->execute (new SimpleStatement ($ query ));
190
-
191
- return $ tableName ;
192
- }
193
-
194
- /**
195
- * Create a new table with specified type and insert and verify value
196
- *
197
- * @param $type Cassandra\Type
198
- * @param $options Cassandra\ExecutionOptions
199
- * @param $key string
200
- * @param $value mixed
201
- */
202
- protected function createTableInsertAndVerifyValue ($ type , $ options , $ key , $ value ) {
203
- $ tableName = $ this ->createTable ($ type );
204
-
205
- $ this ->insertValue ($ tableName , $ options );
206
-
207
- $ this ->verifyValue ($ tableName , $ type , $ key , $ value );
208
- }
209
-
210
- /**
211
- * Insert a value into table
212
- *
213
- * @param $tableName string
214
- * @param $options Cassandra\ExecutionOptions
215
- */
216
- protected function insertValue ($ tableName , $ options ) {
217
- $ insertQuery = "INSERT INTO $ tableName (key, value) VALUES (?, ?) " ;
218
-
219
- $ this ->session ->execute (new SimpleStatement ($ insertQuery ), $ options );
220
- }
221
-
222
- /**
223
- * Verify value
224
- *
225
- * @param $tableName string
226
- * @param $type Cassandra\Type
227
- * @param $key string
228
- * @param $value mixed
229
- */
230
- protected function verifyValue ($ tableName , $ type , $ key , $ value ) {
231
- $ selectQuery = "SELECT * FROM $ tableName WHERE key = ? " ;
232
-
233
- $ options = new ExecutionOptions (array ('arguments ' => array ($ key )));
234
-
235
- $ result = $ this ->session ->execute (new SimpleStatement ($ selectQuery ), $ options );
236
-
237
- $ this ->assertEquals (count ($ result ), 1 );
238
-
239
- $ row = $ result ->first ();
240
-
241
- $ this ->assertEquals ($ row ['value ' ], $ value );
242
- $ this ->assertTrue ($ row ['value ' ] == $ value );
243
- if ($ row ['value ' ]) {
244
- $ this ->assertEquals (count ($ row ['value ' ]), count ($ value ));
245
- $ this ->assertEquals ($ row ['value ' ]->type (), $ type );
246
- }
247
- }
248
-
249
- /**
250
- * Generate a type string suitable for creating a new table or user type
251
- * using CQL
252
- *
253
- * @param $type Cassandra\Type
254
- * @return string String representation of type
255
- */
256
- public static function typeString ($ type ) {
257
- if ($ type instanceof Type \Tuple || $ type instanceof Type \Collection ||
258
- $ type instanceof Type \Map || $ type instanceof Type \Set ||
259
- $ type instanceof Type \UserType) {
260
- return sprintf ("frozen<%s> " , $ type );
261
- } else {
262
- return (string )$ type ;
263
- }
264
- }
265
-
266
- /**
267
- * Generate a user type name string suitable for creating a new table or
268
- * user type using CQL
269
- *
270
- * @param $userType Cassandra\Type
271
- * @return string String representation of the UserType
272
- */
273
- public static function userTypeString ($ userType ) {
274
- return sprintf ("%s " , implode ("_ " , array_map (function ($ name , $ type ) {
275
- return $ name . str_replace (array ("frozen " , "< " , " " , ", " , "> " ), "" , $ type );
276
- }, array_keys ($ userType ->types ()), $ userType ->types ())));
277
- }
278
110
}
0 commit comments