Skip to content

Commit fc92f93

Browse files
DOC-5544 added string data type example for PHP
1 parent dd360c4 commit fc92f93

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

local_examples/php/DtStringTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// EXAMPLE: set_tutorial
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Predis\Client as PredisClient;
7+
8+
class DtStringTest
9+
// REMOVE_START
10+
extends PredisTestCase
11+
// REMOVE_END
12+
{
13+
public function testDtString() {
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 set_get
26+
$res1 = $r->set('bike:1', 'Deimos');
27+
echo "$res1" . PHP_EOL;
28+
// >>> OK
29+
30+
$res2 = $r->get('bike:1');
31+
echo "$res2" . PHP_EOL;
32+
// >>> Deimos
33+
// STEP_END
34+
// REMOVE_START
35+
$this->assertEquals('OK', $res1);
36+
$this->assertEquals('Deimos', $res2);
37+
// REMOVE_END
38+
39+
// STEP_START setnx_xx
40+
$res3 = $r->set('bike:1', 'bike', 'nx');
41+
echo "$res3" . PHP_EOL;
42+
// >>> (null)
43+
44+
echo $r->get('bike:1') . PHP_EOL;
45+
// >>> Deimos
46+
47+
$res4 = $r->set('bike:1', 'bike', 'xx');
48+
echo "$res4" . PHP_EOL;
49+
// >>> OK
50+
// STEP_END
51+
// REMOVE_START
52+
$this->assertEquals(null, $res3);
53+
$this->assertEquals('OK', $res4);
54+
$this->assertEquals('bike', $r->get('bike:1'));
55+
// REMOVE_END
56+
57+
// STEP_START mset
58+
$res5 = $r->mset([
59+
'bike:1' => 'Deimos', 'bike:2' => 'Ares', 'bike:3' => 'Vanth'
60+
]);
61+
echo "$res5" . PHP_EOL;
62+
// >>> OK
63+
64+
$res6 = $r->mget(['bike:1', 'bike:2', 'bike:3']);
65+
echo json_encode($res6) . PHP_EOL;
66+
// >>> ["Deimos","Ares","Vanth"]
67+
// STEP_END
68+
// REMOVE_START
69+
$this->assertEquals('OK', $res5);
70+
$this->assertEquals(['Deimos', 'Ares', 'Vanth'], $res6);
71+
// REMOVE_END
72+
73+
// STEP_START incr
74+
$r->set('total_crashes', 0);
75+
$res7 = $r->incr('total_crashes');
76+
echo "$res7" . PHP_EOL;
77+
// >>> 1
78+
79+
$res8 = $r->incrby('total_crashes', 10);
80+
echo "$res8" . PHP_EOL;
81+
// >>> 11
82+
// STEP_END
83+
// REMOVE_START
84+
$this->assertEquals(1, $res7);
85+
$this->assertEquals(11, $res8);
86+
// REMOVE_END
87+
}
88+
}

0 commit comments

Comments
 (0)