Skip to content

Commit b5e37b0

Browse files
committed
PHP-118 - Unable to bind list values using global type constants
1 parent f4cebf6 commit b5e37b0

File tree

5 files changed

+80
-18
lines changed

5 files changed

+80
-18
lines changed

ext/src/Collection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ PHP_METHOD(Collection, __construct)
9797
CassValueType value_type;
9898
if (!php_driver_value_type(Z_STRVAL_P(type), &value_type TSRMLS_CC))
9999
return;
100-
self->type = php_driver_type_set_from_value_type(value_type TSRMLS_CC);
100+
self->type = php_driver_type_collection_from_value_type(value_type TSRMLS_CC);
101101
} else if (Z_TYPE_P(type) == IS_OBJECT &&
102102
instanceof_function(Z_OBJCE_P(type), php_driver_type_ce TSRMLS_CC)) {
103103
if (!php_driver_type_validate(type, "type" TSRMLS_CC)) {

tests/integration/Cassandra/CollectionIntegrationTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,23 @@ public function testScalarTypes($type, $value) {
3838
* Data provider for lists with scalar types
3939
*/
4040
public function collectionWithScalarTypes() {
41-
return array_map(function ($cassandraType) {
42-
$listType = Type::collection($cassandraType[0]);
43-
$list = $listType->create();
44-
foreach ($cassandraType[1] as $value) {
45-
$list->add($value);
46-
}
47-
return array($listType, $list);
48-
}, $this->scalarCassandraTypes());
41+
return array_merge(
42+
array_map(function ($cassandraType) {
43+
$listType = Type::collection($cassandraType[0]);
44+
$list = $listType->create();
45+
foreach ($cassandraType[1] as $value) {
46+
$list->add($value);
47+
}
48+
return array($listType, $list);
49+
}, $this->scalarCassandraTypes()),
50+
array_map(function ($cassandraType) {
51+
$list = new Collection($cassandraType[0]);
52+
foreach ($cassandraType[1] as $value) {
53+
$list->add($value);
54+
}
55+
return array($list->type(), $list);
56+
}, $this->constantScalarCassandraTypes())
57+
);
4958
}
5059

5160
/**

tests/integration/Cassandra/DatatypeIntegrationTests.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function scalarCassandraTypes() {
4040
new Duration(-1, 0, -(2 ** 31)),
4141
new Duration((2 ** 31) - 1, 1, 0),
4242
new Duration(-(2 ** 31), -1, 0))),
43+
array(Type::int(), array(1, 2, 99)),
4344
array(Type::float(), array(new Float(1.0), new Float(2.2), new Float(2.2))),
4445
array(Type::inet(), array(new Inet("127.0.0.1"), new Inet("127.0.0.2"), new Inet("127.0.0.3"))),
4546
array(Type::smallint(), array(Smallint::min(), Smallint::max(), new Smallint(0), new Smallint(74))),
@@ -56,6 +57,40 @@ public function scalarCassandraTypes() {
5657
);
5758
}
5859

60+
/**
61+
* Global constant scalar Cassandra types to be used by data providers
62+
*/
63+
public function constantScalarCassandraTypes() {
64+
$constants = array(
65+
\Cassandra::TYPE_TEXT,
66+
\Cassandra::TYPE_ASCII,
67+
\Cassandra::TYPE_VARCHAR,
68+
\Cassandra::TYPE_BIGINT,
69+
\Cassandra::TYPE_SMALLINT,
70+
\Cassandra::TYPE_TINYINT,
71+
\Cassandra::TYPE_BLOB,
72+
\Cassandra::TYPE_BOOLEAN,
73+
\Cassandra::TYPE_DECIMAL,
74+
\Cassandra::TYPE_DOUBLE,
75+
\Cassandra::TYPE_FLOAT,
76+
\Cassandra::TYPE_INT,
77+
\Cassandra::TYPE_TIMESTAMP,
78+
\Cassandra::TYPE_UUID,
79+
\Cassandra::TYPE_VARINT,
80+
\Cassandra::TYPE_TIMEUUID,
81+
\Cassandra::TYPE_INET
82+
);
83+
$scalarCassandraTypes = $this->scalarCassandraTypes();
84+
85+
return array_map(function($type) use ($scalarCassandraTypes) {
86+
$match = array_filter($scalarCassandraTypes, function($item) use ($type) {
87+
return (string)$item[0] === $type;
88+
});
89+
assert(isset($match) && count($match) > 0);
90+
return array($type, current($match)[1]);
91+
}, $constants);
92+
}
93+
5994
/**
6095
* Create a table using $type for the value's type and insert $value using
6196
* positional parameters.

tests/integration/Cassandra/MapIntegrationTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,16 @@ function($cassandraType) {
6767
return array($mapType, $map);
6868
}, $this->scalarCassandraTypes());
6969

70-
return array_merge($mapKeyTypes, $mapValueTypes);
70+
$mapConstantScalarTypes = array_map(function ($cassandraType) {
71+
$map = new Map($cassandraType[0], $cassandraType[0]);
72+
$values = $cassandraType[1];
73+
for ($i = 0; $i < count($cassandraType[1]); $i++) {
74+
$map->set($values[$i], $values[$i]);
75+
}
76+
return array($map->type(), $map);
77+
}, $this->constantScalarCassandraTypes());
78+
79+
return array_merge($mapKeyTypes, $mapValueTypes, $mapConstantScalarTypes);
7180
}
7281

7382
/**

tests/integration/Cassandra/SetIntegrationTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,23 @@ function($cassandraType) {
4747
}
4848
);
4949

50-
return array_map(function ($cassandraType) {
51-
$setType = Type::set($cassandraType[0]);
52-
$set = $setType->create();
53-
foreach ($cassandraType[1] as $value) {
54-
$set->add($value);
55-
}
56-
return array($setType, $set);
57-
}, $scalarCassandraTypes);
50+
return array_merge(
51+
array_map(function ($cassandraType) {
52+
$setType = Type::set($cassandraType[0]);
53+
$set = $setType->create();
54+
foreach ($cassandraType[1] as $value) {
55+
$set->add($value);
56+
}
57+
return array($setType, $set);
58+
}, $scalarCassandraTypes),
59+
array_map(function ($cassandraType) {
60+
$set = new Set($cassandraType[0]);
61+
foreach ($cassandraType[1] as $value) {
62+
$set->add($value);
63+
}
64+
return array($set->type(), $set);
65+
}, $this->constantScalarCassandraTypes())
66+
);
5867
}
5968

6069
/**

0 commit comments

Comments
 (0)