forked from brett19/php-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathDatastructuresTest.php
More file actions
108 lines (85 loc) · 3.97 KB
/
DatastructuresTest.php
File metadata and controls
108 lines (85 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
require_once('CouchbaseTestCase.php');
class DatastructuresTest extends CouchbaseTestCase {
private $cluster;
private $bucket;
protected function setUp() {
$this->cluster = new \Couchbase\Cluster($this->testDsn);
$this->bucket = $this->cluster->openBucket($this->testBucket);
$this->setTimeouts($this->bucket);
}
function testMap() {
$key = $this->makeKey("datastructuresMap");
$this->bucket->upsert($key, ["name" => "John"]);
$this->assertEquals(1, $this->bucket->mapSize($key));
$this->assertEquals("John", $this->bucket->mapGet($key, "name"));
$this->bucket->mapAdd($key, "age", 42);
$this->assertEquals(2, $this->bucket->mapSize($key));
$doc = $this->bucket->get($key);
$this->assertEquals("John", $doc->value->name);
$this->assertEquals(42, $doc->value->age);
$this->bucket->mapRemove($key, "name");
$this->assertEquals(1, $this->bucket->mapSize($key));
$doc = $this->bucket->get($key);
$this->assertObjectNotHasAttribute("name", $doc);
$this->assertEquals(42, $doc->value->age);
}
function testList() {
$key = $this->makeKey("datastructuresList");
$this->bucket->upsert($key, [1, 2]);
$this->assertEquals(2, $this->bucket->listSize($key));
$this->bucket->listPush($key, 3);
$this->assertEquals(3, $this->bucket->listSize($key));
$doc = $this->bucket->get($key);
$this->assertEquals([1, 2, 3], $doc->value);
$this->bucket->listShift($key, 42);
$this->assertEquals(4, $this->bucket->listSize($key));
$doc = $this->bucket->get($key);
$this->assertEquals([42, 1, 2, 3], $doc->value);
$this->bucket->listRemove($key, 0);
$this->assertEquals(3, $this->bucket->listSize($key));
$doc = $this->bucket->get($key);
$this->assertEquals([1, 2, 3], $doc->value);
$res = $this->bucket->listGet($key, 2);
$this->assertEquals(3, $res);
$this->bucket->listSet($key, 1, 42);
$this->assertEquals(3, $this->bucket->listSize($key));
$doc = $this->bucket->get($key);
$this->assertEquals([1, 42, 3], $doc->value);
}
function testSet() {
$key = $this->makeKey("datastructuresSet");
$this->bucket->upsert($key, ["hello"]);
$this->assertEquals(1, $this->bucket->setSize($key));
$this->bucket->setAdd($key, "world");
$this->assertEquals(2, $this->bucket->setSize($key));
$doc = $this->bucket->get($key);
$this->assertEquals(["hello", "world"], $doc->value);
$this->bucket->setAdd($key, "hello");
$this->assertEquals(2, $this->bucket->setSize($key));
$doc = $this->bucket->get($key);
$this->assertEquals(["hello", "world"], $doc->value);
$res = $this->bucket->setExists($key, "hello");
$this->assertTrue($res, "Expected set to contain \"hello\"");
$res = $this->bucket->setRemove($key, "hello");
$this->assertTrue($res, "Expected successful removal of \"hello\"");
$res = $this->bucket->setExists($key, "hello");
$this->assertFalse($res, "Expected set not to contain \"hello\"");
$res = $this->bucket->setRemove($key, "hello");
$this->assertFalse($res, "Expected failure removal of \"hello\"");
}
function testQueue() {
$key = $this->makeKey("datastructuresQueue");
$this->bucket->upsert($key, ["hello"]);
$this->assertEquals(1, $this->bucket->queueSize($key));
$this->bucket->queueAdd($key, "world");
$this->assertEquals(2, $this->bucket->queueSize($key));
$doc = $this->bucket->get($key);
$this->assertEquals(["world", "hello"], $doc->value);
$res = $this->bucket->queueRemove($key);
$this->assertEquals("hello", $res);
$this->assertEquals(1, $this->bucket->queueSize($key));
$doc = $this->bucket->get($key);
$this->assertEquals(["world"], $doc->value);
}
}