Skip to content

Commit

Permalink
finish refactoring test cases and couchbase_remove
Browse files Browse the repository at this point in the history
  • Loading branch information
janl committed Sep 6, 2011
1 parent d453352 commit 4fb4ba4
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 33 deletions.
29 changes: 20 additions & 9 deletions couchbase.c
Expand Up @@ -22,6 +22,7 @@ static zend_function_entry couchbase_functions[] = {
PHP_FE(couchbase_remove, NULL)
PHP_FE(couchbase_set_storage_callback, NULL)
PHP_FE(couchbase_set_get_callback, NULL)
PHP_FE(couchbase_set_remove_callback, NULL)
{NULL, NULL, NULL}
};

Expand Down Expand Up @@ -62,6 +63,10 @@ static void get_callback(libcouchbase_t instance,
const void *key, size_t nkey,
const void *bytes, size_t nbytes,
uint32_t flags, uint64_t cas);
void remove_callback(libcouchbase_t instance,
const void *cookie,
libcouchbase_error_t error,
const void *key, size_t nkey);

enum php_libcouchbase_error_t {
COUCHBASE_SUCCESS,
Expand Down Expand Up @@ -229,6 +234,7 @@ PHP_FUNCTION(couchbase_create)
libcouchbase_set_cookie(instance, callbacks);
libcouchbase_set_storage_callback(instance, storage_callback);
libcouchbase_set_get_callback(instance, get_callback);
libcouchbase_set_remove_callback(instance, remove_callback);
}

PHP_FUNCTION(couchbase_execute)
Expand Down Expand Up @@ -258,6 +264,11 @@ PHP_FUNCTION(couchbase_set_get_callback)
couchbase_set_callback(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
}

PHP_FUNCTION(couchbase_set_remove_callback)
{
couchbase_set_callback(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
}

static void couchbase_set_callback(INTERNAL_FUNCTION_PARAMETERS, int type)
{
zval *zinstance;
Expand All @@ -283,6 +294,9 @@ static void couchbase_set_callback(INTERNAL_FUNCTION_PARAMETERS, int type)
case 2:
callbacks->get = zcallback;
break;
case 3:
callbacks->remove = zcallback;
break;
}

libcouchbase_set_cookie(php_instance->instance, callbacks);
Expand Down Expand Up @@ -507,7 +521,8 @@ void remove_callback(libcouchbase_t instance,
libcouchbase_error_t error,
const void *key, size_t nkey)
{
zval *zcallback = (zval *)cookie;
php_couchbase_callbacks *callbacks = (php_couchbase_callbacks *)libcouchbase_get_cookie(instance);
zval *zcallback = callbacks->remove;
char *callback_name;

if (Z_TYPE_P(zcallback) != IS_NULL) {
Expand Down Expand Up @@ -547,6 +562,7 @@ void remove_callback(libcouchbase_t instance,
fci.param_count = 2;
fci.params = argv;
fci.no_separation = 0;
fci.object_ptr = NULL;

if (zend_call_function(&fci, NULL TSRMLS_CC) != SUCCESS ) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the storage callback");
Expand All @@ -563,14 +579,12 @@ void remove_callback(libcouchbase_t instance,
PHP_FUNCTION(couchbase_remove)
{
zval *zinstance;
zval *zcallback = NULL;

const char *key = NULL; int key_len;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsz",
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs",
&zinstance,
&key, &key_len,
&zcallback
&key, &key_len
) == FAILURE) {
RETURN_FALSE;
}
Expand All @@ -581,11 +595,8 @@ PHP_FUNCTION(couchbase_remove)

libcouchbase_error_t error;

Z_ADDREF_P(zcallback);
(void)libcouchbase_set_remove_callback(php_instance->instance, remove_callback);

error = libcouchbase_remove(php_instance->instance,
zcallback,
NULL,
(const void * const *)key, (size_t)strlen(key),
0);
if(error != LIBCOUCHBASE_SUCCESS) {
Expand Down
2 changes: 2 additions & 0 deletions php_couchbase.h
Expand Up @@ -18,6 +18,7 @@ PHP_FUNCTION(couchbase_remove);
// callbacks
PHP_FUNCTION(couchbase_set_storage_callback);
PHP_FUNCTION(couchbase_set_get_callback);
PHP_FUNCTION(couchbase_set_remove_callback);

typedef struct _php_couchbase_instance {
libcouchbase_t instance;
Expand All @@ -27,6 +28,7 @@ typedef struct _php_couchbase_instance {
typedef struct _php_couchbase_callbacks {
zval *storage;
zval *get;
zval *remove;
} php_couchbase_callbacks;

#define PHP_COUCHBASE_INSTANCE "Couchbase Instance"
Expand Down
47 changes: 34 additions & 13 deletions tests/0005-add.phpt
Expand Up @@ -9,25 +9,47 @@ $host = "localhost:9000";
$cb = couchbase_create($host, "Administrator", "asdasd", "default");
var_dump($cb);

couchbase_set($cb, "k", "x", function($error, $key) {
function storage_callback($error, $key)
{
if($error !== NULL) {
var_dump($error);
}
var_dump($key);
});
}

couchbase_add($cb, "k", "y", function($error, $key) {
var_dump($key);
couchbase_set_storage_callback($cb, "storage_callback");

couchbase_set($cb, "k", "x");

couchbase_execute($cb);

function storage_callback2($error, $key)
{
var_dump($error == COUCHBASE_KEY_EEXISTS);
});
var_dump($key);
}

couchbase_set_storage_callback($cb, "storage_callback2");

couchbase_remove($cb, "l", function($error, $key) {
couchbase_add($cb, "k", "y");

function remove_callback($error, $key)
{
if($error !== NULL) {
var_dump($error);
}
var_dump($key);
});
}

couchbase_set_remove_callback($cb, "remove_callback");

couchbase_remove($cb, "l");

couchbase_execute($cb);

couchbase_add($cb, "l", "z", function($error, $key) {
var_dump($key);
var_dump($error);
});
couchbase_set_storage_callback($cb, "storage_callback");

couchbase_add($cb, "l", "z");

couchbase_execute($cb);

Expand All @@ -36,9 +58,8 @@ var_dump("end");
--EXPECTF--
resource(%d) of type (Couchbase Instance)
string(1) "k"
string(1) "k"
bool(true)
string(1) "k"
string(1) "l"
string(1) "l"
NULL
string(3) "end"
49 changes: 38 additions & 11 deletions tests/0006-remove.phpt
Expand Up @@ -9,39 +9,66 @@ $host = "localhost:9000";
$cb = couchbase_create($host, "Administrator", "asdasd", "default");
var_dump($cb);

couchbase_set($cb, "k", "x", function($error, $key) {
function storage_callback($error, $key)
{
if($error !== NULL) {
var_dump($error);
}
var_dump($key);
});
}

couchbase_mget($cb, "k", function($error, $key, $value) {
couchbase_set_storage_callback($cb, "storage_callback");

couchbase_set($cb, "k", "x");

function get_callback($error, $key, $value)
{
if($error !== NULL) {
var_dump($error);
}
var_dump($key);
var_dump($value);
});
}

couchbase_set_get_callback($cb, "get_callback");

couchbase_mget($cb, "k");

couchbase_execute($cb);

couchbase_remove($cb, "k", function($error, $key) {
function remove_callback($error, $key)
{
if($error !== NULL) {
var_dump($error);
}
var_dump($key);
var_dump($error);
});
}

couchbase_set_remove_callback($cb, "remove_callback");

couchbase_remove($cb, "k");

couchbase_execute($cb);

couchbase_mget($cb, "k", function($error, $key, $value) {
function get_callback2($error, $key, $value) {
var_dump($error === COUCHBASE_KEY_ENOENT);
var_dump($value);
});
};

couchbase_execute($cb);
couchbase_set_get_callback($cb, "get_callback2");

couchbase_mget($cb, "k");

couchbase_execute($cb);

var_dump("end");
?>
--EXPECTF--
resource(%d) of type (Couchbase Instance)
string(1) "k"
string(1) "k"
string(1) "x"
string(1) "k"
NULL
bool(true)
NULL
string(3) "end"

0 comments on commit 4fb4ba4

Please sign in to comment.