Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $assertionsEnabled = (int) ini_get('zend.assertions');
- if ($assertionsEnabled !== 1) {
- die('zend.assertions must be on');
- }
- $cb = new CouchbaseCluster('127.0.0.1:8091');
- $bucket = $cb->openBucket('clients');
- function compareResponse($value, $response, $tag, $strongTypes = false)
- {
- assert($response->error === null, "$tag: Response has error");
- assert($value === null || $response->value !== null, "$tag: Response does not have value");
- if ($strongTypes) {
- $expectedType = gettype($value);
- $returnedType = gettype($response->value);
- assert($expectedType === $returnedType, "Expected a $expectedType but got a $returnedType");
- }
- switch (gettype($value)) {
- case 'array':
- $returnedValued = (array) $response->value;
- // no break
- case 'object':
- $returnedValued = $returnedValued ?? $response->value;
- assert($returnedValued == $value, "$tag: values differ");
- break;
- default:
- $same = $strongTypes ?
- $response->value === $value :
- $response->value == $value;
- assert($same, "$tag: expected <$value> go <{$response->value}>");
- }
- }
- $testUpsert = function ($key, $value, $tag) use ($bucket) {
- $bucket->upsert($key, $value);
- $response = $bucket->get($key);
- // var_dump($response);
- compareResponse($value, $response, $tag);
- };
- // TEST: upsert and get
- $testUpsert('cb-test-upsert-1', '{"json":"json-tes224t"}', 'test upsert string');
- $testUpsert('cb-test-upsert-4', 333, 'test upsert int');
- $testUpsert('cb-test-upsert-2', ['json' => 'json-tes224t'], 'test upsert array');
- $testUpsert('cb-test-upsert-3', (object) ['json' => 'json-tes224t'], 'test upsert object');
- // ENDTEST: upsert and get
- // TEST: Remove
- $bucket->upsert('test-remove', 1);
- compareResponse(1, $bucket->get('test-remove'), 'test remove insert', true);
- $bucket->remove('test-remove');
- try {
- $bucket->get('test-remove');
- } catch (CouchbaseException $e) {
- } finally {
- assert(isset($e) && strpos($e->getMessage(), 'The key does not exist on the server') !== false);
- }
- // ENDTEST: remove
- // TEST: counter
- $bucket->remove('cb-test-counter');
- $counter = $bucket->counter('cb-test-counter', 5, ['initial' => 33, 'expiry']);
- compareResponse(33, $counter, 'test counter initial');
- $counter = $bucket->counter('cb-test-counter', 5, ['initial' => 33, 'expiry']);
- compareResponse(38, $counter, 'test counter increment');
- $counter = $bucket->counter('cb-test-counter', -2, ['initial' => 33, 'expiry']);
- compareResponse(36, $counter, 'test counter decrement');
- $counter = $bucket->get('cb-test-counter');
- compareResponse(36, $counter, 'test counter get()');
- $bucket->remove('Service\\ServiceAccount_visits_vm.flying.com_2016-04-12');
- $counter = $bucket->counter('Service\\ServiceAccount_visits_vm.flying.com_2016-04-12',
- 1, ['initial' => 1, 'expiry' => 0]);
- compareResponse(1, $counter, 'test counter initial 2');
- $counter = $bucket->counter('Service\\ServiceAccount_visits_vm.flying.com_2016-04-12',
- 1, ['initial' => 1, 'expiry' => 0]);
- compareResponse(2, $counter, 'test counter increment 2');
- $counter = $bucket->counter('Service\\ServiceAccount_visits_vm.flying.com_2016-04-12',
- -1, ['initial' => 1, 'expiry' => 0]);
- compareResponse(1, $counter, 'test counter decrement 2');
- $counter = $bucket->get('Service\\ServiceAccount_visits_vm.flying.com_2016-04-12');
- compareResponse(1, $counter, 'test counter get()');
- // ENDTEST: counter
- // TEST: lock and unlock
- $bucket->upsert('test-lock', '12');
- $response = $bucket->getAndLock('test-lock', 2);
- compareResponse('12', $response, 'test lock lock()');
- $lockedResponse = $bucket->get('test-lock');
- compareResponse('12', $lockedResponse, 'test lock get()');
- try {
- $bucket->upsert('test-lock', 'should-throw');
- } catch (CouchbaseException $e) {
- } finally {
- assert(isset($e) && strpos($e->getMessage(), 'If you have supplied a CAS then the key exists with a CAS value different than specified') !== false);
- }
- sleep(3);
- $bucket->upsert('test-lock', '13');
- $response = $bucket->getAndLock('test-lock', 5);
- compareResponse('13', $response, 'test lock auto unlock');
- $bucket->unlock('test-lock', ['cas' => $response->cas]);
- $bucket->upsert('test-lock', '14');
- // ENDTEST: lock and unlock
- phpinfo();
Add Comment
Please, Sign In to add comment