Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions ext/src/Timeuuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ zend_class_entry *php_driver_timeuuid_ce = NULL;
void
php_driver_timeuuid_init(INTERNAL_FUNCTION_PARAMETERS)
{
long timestamp;
php_driver_uuid *self;
zval *param;
int version;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &timestamp) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &param) == FAILURE) {
return;
}

Expand All @@ -41,14 +42,34 @@ php_driver_timeuuid_init(INTERNAL_FUNCTION_PARAMETERS)
self = PHP_DRIVER_GET_UUID(return_value);
}


if (ZEND_NUM_ARGS() == 0) {
php_driver_uuid_generate_time(&self->uuid TSRMLS_CC);
} else {
if (timestamp < 0) {
zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Timestamp must be a positive integer, %d given", timestamp);
return;
}
php_driver_uuid_generate_from_time(timestamp, &self->uuid TSRMLS_CC);

switch (Z_TYPE_P(param)) {
case IS_LONG:
if (Z_LVAL_P(param) < 0) {
zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Timestamp must be a positive integer, %d given", Z_LVAL_P(param));
return;
}
php_driver_uuid_generate_from_time(Z_LVAL_P(param), &self->uuid TSRMLS_CC);
break;
case IS_STRING:
if (cass_uuid_from_string(Z_STRVAL_P(param), &self->uuid) != CASS_OK) {
zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid UUID: '%.*s'", Z_STRLEN_P(param), Z_STRVAL_P(param));
return;
}

version = cass_uuid_version(self->uuid);
if (version != 1) {
zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "UUID must be of type 1, type %d given", version);
}
break;
default:
zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid argument - integer or string expected");
}

}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/unit/Cassandra/TimeUuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,42 @@ public function notEqualTypes()
array(new TimeUuid(0), new TimeUuid(2))
);
}

/**
* TimeUuid can be created from string
*/
public function testInitFromStringType1()
{
new TimeUuid('5f344f20-52a3-11e7-915b-5f4f349b532d');
}

/**
* TimeUuid cannot be created from UUID type 4
* @expectedException Cassandra\Exception\InvalidArgumentException
* @expectedExceptionMessage UUID must be of type 1, type 4 given
*/
public function testInitFromStringType4()
{
new TimeUuid('65f9e722-036a-4029-b03b-a9046b23b4c9');
}

/**
* TimeUuid cannot be created from invalid string
* @expectedException Cassandra\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid UUID
*/
public function testInitFromInvalidString()
{
new TimeUuid('invalid');
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case for constructing with a different type of object (say a DateTime) to verify the default case of your switch?


/**
* TimeUuid requires string or integer in constructor
* @expectedException Cassandra\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid argument
*/
public function testInitInvalidArgument()
{
new TimeUuid(new \Datetime());
}
}