Skip to content

Commit 76c01e7

Browse files
committed
PCBC-745: Add test coverage for *Options->cas() argument
Change-Id: I53e80967ebe13afc8c52304418e0f2967a8abff4 Reviewed-on: http://review.couchbase.org/c/php-couchbase/+/147745 Tested-by: Build Bot <[email protected]> Reviewed-by: Sergey Avseyev <[email protected]>
1 parent 960c8d1 commit 76c01e7

File tree

5 files changed

+55
-33
lines changed

5 files changed

+55
-33
lines changed

api/couchbase.php

-10
Original file line numberDiff line numberDiff line change
@@ -3701,16 +3701,6 @@ public function expiry(mixed $arg): UpsertOptions
37013701
{
37023702
}
37033703

3704-
/**
3705-
* Sets the cas value for the operation.
3706-
*
3707-
* @param string $arg the cas value
3708-
* @return UpsertOptions
3709-
*/
3710-
public function cas(string $arg): UpsertOptions
3711-
{
3712-
}
3713-
37143704
/**
37153705
* Sets the durability level to enforce when writing the document.
37163706
*

src/couchbase/bucket/remove.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ PHP_METHOD(RemoveOptions, cas)
8585
RETURN_NULL();
8686
}
8787
zend_string *decoded = php_base64_decode_str(arg);
88+
if (decoded && ZSTR_LEN(decoded) == sizeof(uint64_t)) {
89+
pcbc_update_property_str(pcbc_remove_options_ce, getThis(), ("cas"), arg);
90+
} else {
91+
throw_lcb_exception(LCB_ERR_INVALID_ARGUMENT, NULL);
92+
}
8893
if (decoded) {
89-
if (ZSTR_LEN(decoded) == sizeof(uint64_t)) {
90-
pcbc_update_property_str(pcbc_remove_options_ce, getThis(), ("cas"), arg);
91-
}
9294
zend_string_free(decoded);
9395
}
9496
RETURN_ZVAL(getThis(), 1, 0);

src/couchbase/bucket/store.c

+3-16
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,8 @@ zend_class_entry *pcbc_upsert_options_ce;
259259

260260
PHP_METHOD(UpsertOptions, cas)
261261
{
262-
zend_string *arg;
263-
int rv = zend_parse_parameters_throw(ZEND_NUM_ARGS(), "S", &arg);
264-
if (rv == FAILURE) {
265-
RETURN_NULL();
266-
}
267-
zend_string *decoded = php_base64_decode_str(arg);
268-
if (decoded && ZSTR_LEN(decoded) == sizeof(uint64_t)) {
269-
pcbc_update_property_str(pcbc_upsert_options_ce, getThis(), ("cas"), arg);
270-
} else {
271-
throw_lcb_exception(LCB_ERR_INVALID_ARGUMENT, NULL);
272-
}
273-
if (decoded) {
274-
zend_string_free(decoded);
275-
}
276-
RETURN_ZVAL(getThis(), 1, 0);
262+
zend_parse_parameters_none_throw();
263+
RETURN_NULL();
277264
}
278265

279266
PHP_METHOD(UpsertOptions, timeout)
@@ -343,7 +330,7 @@ ZEND_END_ARG_INFO()
343330

344331
// clang-format off
345332
static const zend_function_entry pcbc_upsert_options_methods[] = {
346-
PHP_ME(UpsertOptions, cas, ai_UpsertOptions_cas, ZEND_ACC_PUBLIC)
333+
PHP_ME(UpsertOptions, cas, ai_UpsertOptions_cas, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
347334
PHP_ME(UpsertOptions, timeout, ai_UpsertOptions_timeout, ZEND_ACC_PUBLIC)
348335
PHP_ME(UpsertOptions, expiry, ai_UpsertOptions_expiry, ZEND_ACC_PUBLIC)
349336
PHP_ME(UpsertOptions, durabilityLevel, ai_UpsertOptions_durabilityLevel, ZEND_ACC_PUBLIC)

src/couchbase/bucket/subdoc.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,12 @@ PHP_METHOD(MutateInOptions, cas)
336336
RETURN_NULL();
337337
}
338338
zend_string *decoded = php_base64_decode_str(arg);
339+
if (decoded && ZSTR_LEN(decoded) == sizeof(uint64_t)) {
340+
pcbc_update_property_str(pcbc_mutate_in_options_ce, getThis(), ("cas"), arg);
341+
} else {
342+
throw_lcb_exception(LCB_ERR_INVALID_ARGUMENT, NULL);
343+
}
339344
if (decoded) {
340-
if (ZSTR_LEN(decoded) == sizeof(uint64_t)) {
341-
pcbc_update_property_str(pcbc_mutate_in_options_ce, getThis(), ("cas"), arg);
342-
}
343345
zend_string_free(decoded);
344346
}
345347
RETURN_ZVAL(getThis(), 1, 0);

tests/BucketTest.php

+42-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use \Couchbase\ClusterOptions;
66
use \Couchbase\GetOptions;
77
use \Couchbase\UpsertOptions;
8+
use \Couchbase\ReplaceOptions;
9+
use \Couchbase\RemoveOptions;
10+
use \Couchbase\MutateInOptions;
811

912
class BucketTest extends CouchbaseTestCase {
10-
1113
/**
1214
* Test that connections with invalid details fail.
1315
*/
@@ -79,6 +81,45 @@ function testBasicGet($c, $key) {
7981
return $key;
8082
}
8183

84+
/**
85+
* Verifies that library-generated CAS values work as expected
86+
*
87+
* @depends testConnect
88+
*/
89+
function testGoodCas($c) {
90+
$key = $this->makeKey('goodCas');
91+
92+
$res = $c->upsert($key, ['name' => 'bob']);
93+
$this->assertNotNull($res->cas());
94+
$cas1 = $res->cas();
95+
var_dump($res->cas());
96+
97+
$options = new ReplaceOptions();
98+
$options->cas($cas1);
99+
$res = $c->replace($key, ['name' => 'john'], $options);
100+
$this->assertNotNull($res->cas());
101+
$cas2 = $res->cas();
102+
$this->assertNotEquals($cas1, $cas2);
103+
104+
$res = $c->get($key);
105+
$this->assertEquals($cas2, $res->cas());
106+
$this->assertEquals($res->content(), ['name' => 'john']);
107+
}
108+
109+
function testBadCas() {
110+
// strings are not CAS
111+
$options = new RemoveOptions();
112+
$this->wrapException(function() use($options) {
113+
$options->cas("0xdeadbeef");
114+
}, '\Couchbase\BadInputException');
115+
116+
// Application should not attempt to generate CAS literals
117+
$options = new MutateInOptions();
118+
$this->wrapException(function() use($options) {
119+
$options->cas("776t3g==");
120+
}, '\Couchbase\BadInputException');
121+
}
122+
82123
/**
83124
* Test reaction on empty value
84125
*

0 commit comments

Comments
 (0)