Skip to content

Commit 0fc2707

Browse files
DOC-4423 added PHP list and set command examples (#2098)
1 parent 095cc2a commit 0fc2707

File tree

2 files changed

+232
-0
lines changed

2 files changed

+232
-0
lines changed

local_examples/php/CmdListTest.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// EXAMPLE: cmds_list
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Predis\Client as PredisClient;
7+
8+
class CmdListTest
9+
// REMOVE_START
10+
extends PredisTestCase
11+
// REMOVE_END
12+
{
13+
public function testCmdList() {
14+
$r = new PredisClient([
15+
'scheme' => 'tcp',
16+
'host' => '127.0.0.1',
17+
'port' => 6379,
18+
'password' => '',
19+
'database' => 0,
20+
]);
21+
// REMOVE_START
22+
$r->flushall();
23+
// REMOVE_END
24+
25+
// STEP_START lpush
26+
$res1 = $r->lpush('mylist', 'world');
27+
echo $res1 . PHP_EOL;
28+
// >>> 1
29+
30+
$res2 = $r->lpush('mylist', 'hello');
31+
echo $res2 . PHP_EOL;
32+
// >>> 2
33+
34+
$res3 = $r->lrange('mylist', 0, -1);
35+
echo json_encode($res3) . PHP_EOL;
36+
// >>> ["hello","world"]
37+
// STEP_END
38+
// REMOVE_START
39+
$this->assertEquals(1, $res1);
40+
$this->assertEquals(2, $res2);
41+
$this->assertEquals(["hello", "world"], $res3);
42+
$r->del('mylist');
43+
// REMOVE_END
44+
45+
// STEP_START lrange
46+
$res4 = $r->rpush('mylist', 'one');
47+
echo $res4 . PHP_EOL;
48+
// >>> 1
49+
50+
$res5 = $r->rpush('mylist', 'two');
51+
echo $res5 . PHP_EOL;
52+
// >>> 2
53+
54+
$res6 = $r->rpush('mylist', 'three');
55+
echo $res6 . PHP_EOL;
56+
// >>> 3
57+
58+
$res7 = $r->lrange('mylist', 0, 0);
59+
echo json_encode($res7) . PHP_EOL;
60+
// >>> ["one"]
61+
62+
$res8 = $r->lrange('mylist', -3, 2);
63+
echo json_encode($res8) . PHP_EOL;
64+
// >>> ["one","two","three"]
65+
66+
$res9 = $r->lrange('mylist', -100, 100);
67+
echo json_encode($res9) . PHP_EOL;
68+
// >>> ["one","two","three"]
69+
70+
$res10 = $r->lrange('mylist', 5, 10);
71+
echo json_encode($res10) . PHP_EOL;
72+
// >>> []
73+
// STEP_END
74+
// REMOVE_START
75+
$this->assertEquals(["one"], $res7);
76+
$this->assertEquals(["one", "two", "three"], $res8);
77+
$this->assertEquals(["one", "two", "three"], $res9);
78+
$this->assertEquals([], $res10);
79+
$r->del('mylist');
80+
// REMOVE_END
81+
82+
// STEP_START llen
83+
$res11 = $r->lpush('mylist', 'World');
84+
echo $res11 . PHP_EOL;
85+
// >>> 1
86+
87+
$res12 = $r->lpush('mylist', 'Hello');
88+
echo $res12 . PHP_EOL;
89+
// >>> 2
90+
91+
$res13 = $r->llen('mylist');
92+
echo $res13 . PHP_EOL;
93+
// >>> 2
94+
// STEP_END
95+
// REMOVE_START
96+
$this->assertEquals(2, $res13);
97+
$r->del('mylist');
98+
// REMOVE_END
99+
100+
// STEP_START rpush
101+
$res14 = $r->rpush('mylist', 'hello');
102+
echo $res14 . PHP_EOL;
103+
// >>> 1
104+
105+
$res15 = $r->rpush('mylist', 'world');
106+
echo $res15 . PHP_EOL;
107+
// >>> 2
108+
109+
$res16 = $r->lrange('mylist', 0, -1);
110+
echo json_encode($res16) . PHP_EOL;
111+
// >>> ["hello","world"]
112+
// STEP_END
113+
// REMOVE_START
114+
$this->assertEquals(["hello", "world"], $res16);
115+
$r->del('mylist');
116+
// REMOVE_END
117+
118+
// STEP_START lpop
119+
$res17 = $r->rpush('mylist', 'one', 'two', 'three', 'four', 'five');
120+
echo $res17 . PHP_EOL;
121+
// >>> 5
122+
123+
$res18 = $r->lpop('mylist');
124+
echo $res18 . PHP_EOL;
125+
// >>> one
126+
127+
$res19 = $r->lpop('mylist', 2);
128+
echo json_encode($res19) . PHP_EOL;
129+
// >>> ["two","three"]
130+
131+
$res20 = $r->lrange('mylist', 0, -1);
132+
echo json_encode($res20) . PHP_EOL;
133+
// >>> ["four","five"]
134+
// STEP_END
135+
// REMOVE_START
136+
$this->assertEquals(5, $res17);
137+
$this->assertEquals('one', $res18);
138+
$this->assertEquals(['two', 'three'], $res19);
139+
$this->assertEquals(['four', 'five'], $res20);
140+
$r->del('mylist');
141+
// REMOVE_END
142+
143+
// STEP_START rpop
144+
$res21 = $r->rpush('mylist', 'one', 'two', 'three', 'four', 'five');
145+
echo $res21 . PHP_EOL;
146+
// >>> 5
147+
148+
$res22 = $r->rpop('mylist');
149+
echo $res22 . PHP_EOL;
150+
// >>> five
151+
152+
$res23 = $r->rpop('mylist', 2);
153+
echo json_encode($res23) . PHP_EOL;
154+
// >>> ["four","three"]
155+
156+
$res24 = $r->lrange('mylist', 0, -1);
157+
echo json_encode($res24) . PHP_EOL;
158+
// >>> ["one","two"]
159+
// STEP_END
160+
// REMOVE_START
161+
$this->assertEquals(5, $res21);
162+
$this->assertEquals('five', $res22);
163+
$this->assertEquals(['four', 'three'], $res23);
164+
$this->assertEquals(['one', 'two'], $res24);
165+
$r->del('mylist');
166+
// REMOVE_END
167+
}
168+
}
169+

local_examples/php/CmdSetTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// EXAMPLE: cmds_set
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Predis\Client as PredisClient;
7+
8+
class CmdSetTest
9+
// REMOVE_START
10+
extends PredisTestCase
11+
// REMOVE_END
12+
{
13+
public function testCmdSet() {
14+
$r = new PredisClient([
15+
'scheme' => 'tcp',
16+
'host' => '127.0.0.1',
17+
'port' => 6379,
18+
'password' => '',
19+
'database' => 0,
20+
]);
21+
// REMOVE_START
22+
$r->flushall();
23+
// REMOVE_END
24+
25+
// STEP_START sadd
26+
$res1 = $r->sadd('myset', ['Hello', 'World']);
27+
echo $res1 . PHP_EOL;
28+
// >>> 2
29+
30+
$res2 = $r->sadd('myset', ['World']);
31+
echo $res2 . PHP_EOL;
32+
// >>> 0
33+
34+
$res3 = $r->smembers('myset');
35+
sort($res3);
36+
echo json_encode($res3) . PHP_EOL;
37+
// >>> ["Hello","World"]
38+
// STEP_END
39+
// REMOVE_START
40+
$this->assertEquals(2, $res1);
41+
$this->assertEquals(0, $res2);
42+
$this->assertEquals(['Hello', 'World'], $res3);
43+
$r->del('myset');
44+
// REMOVE_END
45+
46+
// STEP_START smembers
47+
$res4 = $r->sadd('myset', ['Hello', 'World']);
48+
echo $res4 . PHP_EOL;
49+
// >>> 2
50+
51+
$res5 = $r->smembers('myset');
52+
sort($res5);
53+
echo json_encode($res5) . PHP_EOL;
54+
// >>> ["Hello","World"]
55+
// STEP_END
56+
// REMOVE_START
57+
$this->assertEquals(2, $res4);
58+
$this->assertEquals(['Hello', 'World'], $res5);
59+
$r->del('myset');
60+
// REMOVE_END
61+
}
62+
}
63+

0 commit comments

Comments
 (0)