-
Notifications
You must be signed in to change notification settings - Fork 151
/
CollectionTest.php
305 lines (273 loc) · 10.6 KB
/
CollectionTest.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
/**
* Copyright 2015-2017 DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Cassandra;
/**
* @requires extension cassandra
*/
class CollectionTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage type must be a string or an instance of Cassandra\Type, an instance of stdClass given
*/
public function testInvalidType()
{
new Collection(new \stdClass());
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Unsupported type 'custom type'
*/
public function testUnsupportedStringType()
{
new Collection('custom type');
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage type must be a valid Cassandra\Type,
* an instance of Cassandra\Type\UnsupportedType given
*/
public function testUnsupportedType()
{
new Collection(new Type\UnsupportedType());
}
/**
* @dataProvider cassandraTypes
*/
public function testSupportsAllCassandraStringTypes($type)
{
$list = new Collection($type);
$this->assertEquals($type, $list->type()->valueType());
}
public function cassandraTypes()
{
return array(
array(\Cassandra::TYPE_ASCII),
array(\Cassandra::TYPE_BIGINT),
array(\Cassandra::TYPE_BLOB),
array(\Cassandra::TYPE_BOOLEAN),
array(\Cassandra::TYPE_COUNTER),
array(\Cassandra::TYPE_DECIMAL),
array(\Cassandra::TYPE_DOUBLE),
array(\Cassandra::TYPE_FLOAT),
array(\Cassandra::TYPE_INT),
array(\Cassandra::TYPE_TEXT),
array(\Cassandra::TYPE_TIMESTAMP),
array(\Cassandra::TYPE_UUID),
array(\Cassandra::TYPE_VARCHAR),
array(\Cassandra::TYPE_VARINT),
array(\Cassandra::TYPE_TIMEUUID),
array(\Cassandra::TYPE_INET),
);
}
/**
* @dataProvider scalarTypes
*/
public function testScalarKeys($type, $value)
{
$list = Type::collection($type)->create();
$list->add($value);
$this->assertEquals(1, count($list));
$this->assertEquals($list->get(0), $value);
$this->assertEquals($list->find($value), 0);
}
public function scalarTypes()
{
return array(
array(Type::ascii(), "ascii"),
array(Type::bigint(), new Bigint("9223372036854775807")),
array(Type::blob(), new Blob("blob")),
array(Type::boolean(), true),
array(Type::counter(), new Bigint(123)),
array(Type::decimal(), new Decimal("3.14159265359")),
array(Type::double(), 3.14159),
array(Type::float(), new Float(3.14159)),
array(Type::inet(), new Inet("127.0.0.1")),
array(Type::int(), 123),
array(Type::text(), "text"),
array(Type::timestamp(), new Timestamp(123)),
array(Type::timeuuid(), new Timeuuid(0)),
array(Type::uuid(), new Uuid("03398c99-c635-4fad-b30a-3b2c49f785c2")),
array(Type::varchar(), "varchar"),
array(Type::varint(), new Varint("9223372036854775808")),
array(Type::duration(), new Duration(1, 2, 3))
);
}
/**
* @dataProvider compositeTypes
*/
public function testCompositeKeys($type)
{
$list = Type::collection($type)->create();
$list->add($type->create("a", "1", "b", "2"));
$this->assertEquals($list->find($type->create("a", "1", "b", "2")), 0);
$this->assertEquals(1, count($list));
$list->add($type->create("c", "3", "d", "4", "e", "5"));
$this->assertEquals($list->find($type->create("c", "3", "d", "4", "e", "5")), 1);
$this->assertEquals(2, count($list));
$list->remove(0);
$this->assertNull($list->find($type->create("a", "1", "b", "2")));
$this->assertEquals(1, count($list));
$list->remove(1);
$this->assertNull($list->find($type->create("c", "3", "d", "4", "e", "5")));
$this->assertEquals(0, count($list));
}
public function compositeTypes()
{
return array(
array(Type::map(Type::varchar(), Type::varchar())),
array(Type::set(Type::varchar())),
array(Type::collection(Type::varchar()))
);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage argument must be an instance of Cassandra\Varint, an instance of Cassandra\Decimal given
*/
public function testValidatesTypesOfElements()
{
$list = new Collection(\Cassandra::TYPE_VARINT);
$list->add(new Decimal('123'));
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Invalid value: null is not supported inside collections
*/
public function testSupportsNullValues()
{
$list = new Collection(\Cassandra::TYPE_VARINT);
$list->add(null);
}
public function testAddsAllElements()
{
$list = new Collection(\Cassandra::TYPE_VARINT);
$list->add(new Varint('1'), new Varint('2'), new Varint('3'),
new Varint('4'), new Varint('5'), new Varint('6'),
new Varint('7'), new Varint('8'));
$this->assertEquals(8, $list->count());
$this->assertEquals(
array(
new Varint('1'), new Varint('2'), new Varint('3'),
new Varint('4'), new Varint('5'), new Varint('6'),
new Varint('7'), new Varint('8')
),
$list->values()
);
}
public function testReturnsNullWhenCannotFindIndex()
{
$list = new Collection(\Cassandra::TYPE_VARINT);
$this->assertSame(null, $list->find(new Varint('1')));
}
public function testFindsIndexOfAnElement()
{
$list = new Collection(\Cassandra::TYPE_VARINT);
$list->add(new Varint('1'), new Varint('2'), new Varint('3'),
new Varint('4'), new Varint('5'), new Varint('6'),
new Varint('7'), new Varint('8'));
$this->assertEquals(0, $list->find(new Varint('1')));
$this->assertEquals(1, $list->find(new Varint('2')));
$this->assertEquals(2, $list->find(new Varint('3')));
$this->assertEquals(3, $list->find(new Varint('4')));
$this->assertEquals(4, $list->find(new Varint('5')));
$this->assertEquals(5, $list->find(new Varint('6')));
$this->assertEquals(6, $list->find(new Varint('7')));
$this->assertEquals(7, $list->find(new Varint('8')));
}
public function testGetsElementByIndex()
{
$list = new Collection(\Cassandra::TYPE_VARINT);
$list->add(new Varint('1'), new Varint('2'), new Varint('3'),
new Varint('4'), new Varint('5'), new Varint('6'),
new Varint('7'), new Varint('8'));
$this->assertEquals(new Varint('1'), $list->get(0));
$this->assertEquals(new Varint('2'), $list->get(1));
$this->assertEquals(new Varint('3'), $list->get(2));
$this->assertEquals(new Varint('4'), $list->get(3));
$this->assertEquals(new Varint('5'), $list->get(4));
$this->assertEquals(new Varint('6'), $list->get(5));
$this->assertEquals(new Varint('7'), $list->get(6));
$this->assertEquals(new Varint('8'), $list->get(7));
}
public function testSupportsForeachIteration()
{
$values = array(new Varint('1'), new Varint('2'), new Varint('3'),
new Varint('4'), new Varint('5'), new Varint('6'),
new Varint('7'), new Varint('8'));
$list = new Collection(\Cassandra::TYPE_VARINT);
foreach ($values as $value) {
$list->add($value);
}
$index = 0;
foreach ($list as $value) {
$this->assertEquals($values[$index], $value);
$index++;
}
$index = 0;
foreach ($list as $key => $value) {
$this->assertEquals($key, $index);
$this->assertEquals($values[$key], $value);
$index++;
}
}
/**
* @dataProvider equalTypes
*/
public function testCompareEquals($value1, $value2)
{
$this->assertEquals($value1, $value2);
$this->assertTrue($value1 == $value2);
}
public function equalTypes()
{
$setType = Type::set(Type::int());
return array(
array(Type::collection(Type::int())->create(),
Type::collection(Type::int())->create()),
array(Type::collection(Type::int())->create(1, 2, 3),
Type::collection(Type::int())->create(1, 2, 3)),
array(Type::collection(Type::varchar())->create('a', 'b', 'c'),
Type::collection(Type::varchar())->create('a', 'b', 'c')),
array(Type::collection($setType)->create($setType->create(1, 2, 3)),
Type::collection($setType)->create($setType->create(1, 2, 3))),
);
}
/**
* @dataProvider notEqualTypes
*/
public function testCompareNotEquals($value1, $value2)
{
$this->assertNotEquals($value1, $value2);
$this->assertFalse($value1 == $value2);
}
public function notEqualTypes()
{
$setType = Type::set(Type::int());
return array(
array(Type::collection(Type::int())->create(),
Type::collection(Type::varchar())->create()),
array(Type::collection(Type::int())->create(1, 2, 3),
Type::collection(Type::int())->create(4, 5, 6)),
array(Type::collection(Type::int())->create(1, 2, 3),
Type::collection(Type::int())->create(1)),
array(Type::collection(Type::varchar())->create('a', 'b', 'c'),
Type::collection(Type::varchar())->create('a', 'b', 'd')),
array(Type::collection($setType)->create($setType->create(1, 2, 3)),
Type::collection($setType)->create($setType->create(4, 5, 6))),
);
}
}