Guest User

Untitled

a guest
Apr 12th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.24 KB | None | 0 0
  1. <?php
  2.  
  3. $assertionsEnabled = (int) ini_get('zend.assertions');
  4.  
  5. if ($assertionsEnabled !== 1) {
  6.     die('zend.assertions must be on');
  7. }
  8.  
  9. $cb = new CouchbaseCluster('127.0.0.1:8091');
  10. $bucket = $cb->openBucket('clients');
  11.  
  12.  
  13. function compareResponse($value, $response, $tag, $strongTypes = false)
  14. {
  15.     assert($response->error === null, "$tag: Response has error");
  16.     assert($value === null || $response->value !== null, "$tag: Response does not have value");
  17.  
  18.     if ($strongTypes) {
  19.         $expectedType = gettype($value);
  20.         $returnedType = gettype($response->value);
  21.         assert($expectedType === $returnedType, "Expected a $expectedType but got a $returnedType");
  22.     }
  23.  
  24.     switch (gettype($value)) {
  25.         case 'array':
  26.             $returnedValued = (array) $response->value;
  27.         // no break
  28.         case 'object':
  29.             $returnedValued = $returnedValued ?? $response->value;
  30.             assert($returnedValued == $value, "$tag: values differ");
  31.         break;
  32.         default:
  33.             $same = $strongTypes ?
  34.                 $response->value === $value :
  35.                 $response->value == $value;
  36.             assert($same, "$tag: expected <$value> go <{$response->value}>");
  37.     }
  38. }
  39.  
  40. $testUpsert = function ($key, $value, $tag) use ($bucket) {
  41.     $bucket->upsert($key, $value);
  42.     $response = $bucket->get($key);
  43.     //  var_dump($response);
  44.  
  45.     compareResponse($value, $response, $tag);
  46. };
  47.  
  48. // TEST: upsert and get
  49.     $testUpsert('cb-test-upsert-1', '{"json":"json-tes224t"}', 'test upsert string');
  50.     $testUpsert('cb-test-upsert-4', 333, 'test upsert int');
  51.     $testUpsert('cb-test-upsert-2', ['json' => 'json-tes224t'], 'test upsert array');
  52.     $testUpsert('cb-test-upsert-3', (object) ['json' => 'json-tes224t'], 'test upsert object');
  53. // ENDTEST: upsert and get
  54.  
  55. // TEST: Remove
  56.     $bucket->upsert('test-remove', 1);
  57.     compareResponse(1, $bucket->get('test-remove'), 'test remove insert', true);
  58.     $bucket->remove('test-remove');
  59.     try {
  60.         $bucket->get('test-remove');
  61.     } catch (CouchbaseException $e) {
  62.     } finally {
  63.         assert(isset($e) && strpos($e->getMessage(), 'The key does not exist on the server') !== false);
  64.     }
  65. // ENDTEST: remove
  66.  
  67. // TEST: counter
  68.     $bucket->remove('cb-test-counter');
  69.  
  70.     $counter = $bucket->counter('cb-test-counter', 5, ['initial' => 33, 'expiry']);
  71.     compareResponse(33, $counter, 'test counter initial');
  72.  
  73.     $counter = $bucket->counter('cb-test-counter', 5, ['initial' => 33, 'expiry']);
  74.     compareResponse(38, $counter, 'test counter increment');
  75.  
  76.     $counter = $bucket->counter('cb-test-counter', -2, ['initial' => 33, 'expiry']);
  77.     compareResponse(36, $counter, 'test counter decrement');
  78.  
  79.     $counter = $bucket->get('cb-test-counter');
  80.     compareResponse(36, $counter, 'test counter get()');
  81.  
  82.     $bucket->remove('Service\\ServiceAccount_visits_vm.flying.com_2016-04-12');
  83.  
  84.     $counter = $bucket->counter('Service\\ServiceAccount_visits_vm.flying.com_2016-04-12',
  85.         1, ['initial' => 1, 'expiry' => 0]);
  86.     compareResponse(1, $counter, 'test counter initial 2');
  87.  
  88.  
  89.     $counter = $bucket->counter('Service\\ServiceAccount_visits_vm.flying.com_2016-04-12',
  90.         1, ['initial' => 1, 'expiry' => 0]);
  91.     compareResponse(2, $counter, 'test counter increment 2');
  92.  
  93.  
  94.     $counter = $bucket->counter('Service\\ServiceAccount_visits_vm.flying.com_2016-04-12',
  95.         -1, ['initial' => 1, 'expiry' => 0]);
  96.     compareResponse(1, $counter, 'test counter decrement 2');
  97.  
  98.     $counter = $bucket->get('Service\\ServiceAccount_visits_vm.flying.com_2016-04-12');
  99.     compareResponse(1, $counter, 'test counter get()');
  100. // ENDTEST: counter
  101.  
  102. // TEST: lock and unlock
  103.     $bucket->upsert('test-lock', '12');
  104.     $response = $bucket->getAndLock('test-lock', 2);
  105.     compareResponse('12', $response, 'test lock lock()');
  106.     $lockedResponse = $bucket->get('test-lock');
  107.     compareResponse('12', $lockedResponse, 'test lock get()');
  108.     try {
  109.         $bucket->upsert('test-lock', 'should-throw');
  110.     } catch (CouchbaseException $e) {
  111.     } finally {
  112.         assert(isset($e) && strpos($e->getMessage(), 'If you have supplied a CAS then the key exists with a CAS value different than specified') !== false);
  113.     }
  114.    
  115.     sleep(3);
  116.     $bucket->upsert('test-lock', '13');
  117.     $response = $bucket->getAndLock('test-lock', 5);
  118.     compareResponse('13', $response, 'test lock auto unlock');
  119.     $bucket->unlock('test-lock', ['cas' => $response->cas]);
  120.     $bucket->upsert('test-lock', '14');
  121. // ENDTEST: lock and unlock
  122.  
  123. phpinfo();
Add Comment
Please, Sign In to add comment