Skip to content

Commit 2b04cd9

Browse files
DOC-5670 added PHP hash examples
1 parent 0d697ed commit 2b04cd9

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

local_examples/php/DtHashTest.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// EXAMPLE: hash_tutorial
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Predis\Client as PredisClient;
7+
8+
class DtHashTest
9+
// REMOVE_START
10+
extends PredisTestCase
11+
// REMOVE_END
12+
{
13+
public function testDtHash() {
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_all
26+
$res1 = $r->hmset('bike:1', [
27+
'model' => 'Deimos',
28+
'brand' => 'Ergonom',
29+
'type' => 'Enduro bikes',
30+
'price' => 4972,
31+
]);
32+
33+
echo $res1 . PHP_EOL;
34+
// >>> 4
35+
36+
$res2 = $r->hget('bike:1', 'model');
37+
echo $res2 . PHP_EOL;
38+
// >>> Deimos
39+
40+
$res3 = $r->hget('bike:1', 'price');
41+
echo $res3 . PHP_EOL;
42+
// >>> 4972
43+
44+
$res4 = $r->hgetall('bike:1');
45+
echo json_encode($res3) . PHP_EOL;
46+
// >>> {"name":"Deimos","brand":"Ergonom","type":"Enduro bikes","price":"4972"}
47+
// STEP_END
48+
// REMOVE_START
49+
$this->assertEquals('OK', $res1);
50+
$this->assertEquals('Deimos', $res2);
51+
$this->assertEquals('4972', $res3);
52+
$this->assertEquals(
53+
['model' => 'Deimos', 'brand' => 'Ergonom', 'type' => 'Enduro bikes', 'price' => '4972'],
54+
$res4
55+
);
56+
// REMOVE_END
57+
58+
// STEP_START hmget
59+
$res5 = $r->hmget('bike:1', ['model', 'price']);
60+
echo json_encode($res5) . PHP_EOL;
61+
// >>> ["Deimos","4972"]
62+
// STEP_END
63+
// REMOVE_START
64+
$this->assertEquals(['Deimos', '4972'], $res5);
65+
// REMOVE_END
66+
67+
// STEP_START hincrby
68+
$res6 = $r->hincrby('bike:1', 'price', 100);
69+
echo $res6 . PHP_EOL;
70+
// >>> 5072
71+
72+
$res7 = $r->hincrby('bike:1', 'price', -100);
73+
echo $res7 . PHP_EOL;
74+
// >>> 4972
75+
// STEP_END
76+
// REMOVE_START
77+
$this->assertEquals(5072, $res6);
78+
$this->assertEquals(4972, $res7);
79+
// REMOVE_END
80+
81+
// STEP_START incrby_get_mget
82+
$res8 = $r->hincrby('bike:1:stats', 'rides', 1);
83+
echo $res8 . PHP_EOL;
84+
// >>> 1
85+
86+
$res9 = $r->hincrby('bike:1:stats', 'rides', 1);
87+
echo $res9 . PHP_EOL;
88+
// >>> 2
89+
90+
$res10 = $r->hincrby('bike:1:stats', 'rides', 1);
91+
echo $res10 . PHP_EOL;
92+
// >>> 3
93+
94+
$res11 = $r->hincrby('bike:1:stats', 'crashes', 1);
95+
echo $res11 . PHP_EOL;
96+
// >>> 1
97+
98+
$res12 = $r->hincrby('bike:1:stats', 'owners', 1);
99+
echo $res12 . PHP_EOL;
100+
// >>> 1
101+
102+
$res13 = $r->hget('bike:1:stats', 'rides');
103+
echo $res13 . PHP_EOL;
104+
// >>> 3
105+
106+
$res14 = $r->hmget('bike:1:stats', ['crashes', 'owners']);
107+
echo json_encode($res14) . PHP_EOL;
108+
// >>> ["1","1"]
109+
// STEP_END
110+
// REMOVE_START
111+
$this->assertEquals(1, $res8);
112+
$this->assertEquals(2, $res9);
113+
$this->assertEquals(3, $res10);
114+
$this->assertEquals(1, $res11);
115+
$this->assertEquals(1, $res12);
116+
$this->assertEquals(3, $res13);
117+
$this->assertEquals([1, 1], $res14);
118+
// REMOVE_END
119+
}
120+
}

0 commit comments

Comments
 (0)