1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SymfonyBundles\RedisBundle\Tests\Redis; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SymfonyBundles\RedisBundle\Tests\TestCase; |
7
|
|
|
use SymfonyBundles\RedisBundle\Redis\ClientInterface; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use SymfonyBundles\RedisBundle\DependencyInjection\RedisExtension; |
10
|
|
|
|
11
|
|
|
class ClientTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function testDefaultClass(): void |
14
|
|
|
{ |
15
|
|
|
$client = $this->getClient(['sb_redis' => []]); |
16
|
|
|
|
17
|
|
|
$this->assertInstanceOf(ClientInterface::class, $client); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testPush(): void |
21
|
|
|
{ |
22
|
|
|
$client = $this->getClient(); |
23
|
|
|
|
24
|
|
|
$client->remove('mykey'); |
25
|
|
|
|
26
|
|
|
$this->assertSame(0, $client->count('mykey')); |
27
|
|
|
$this->assertSame(1, $client->push('mykey', 'foo')); |
28
|
|
|
$this->assertSame(3, $client->push('mykey', 'bar', 'baz')); |
29
|
|
|
$this->assertSame(3, $client->count('mykey')); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testPop(): void |
33
|
|
|
{ |
34
|
|
|
$client = $this->getClient(); |
35
|
|
|
|
36
|
|
|
$client->remove('mykey'); |
37
|
|
|
|
38
|
|
|
$this->assertSame(0, $client->count('mykey')); |
39
|
|
|
$this->assertSame(3, $client->push('mykey', 'foo', 'bar', 'baz')); |
40
|
|
|
$this->assertSame('foo', $client->pop('mykey')); |
41
|
|
|
$this->assertSame('bar', $client->pop('mykey')); |
42
|
|
|
$this->assertSame('baz', $client->pop('mykey')); |
43
|
|
|
$this->assertSame(0, $client->count('mykey')); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testSingleConnection(): void |
47
|
|
|
{ |
48
|
|
|
$client = $this->getClient([ |
49
|
|
|
'sb_redis' => [ |
50
|
|
|
'clients' => [ |
51
|
|
|
'test' => [ |
52
|
|
|
'$options' => [], |
53
|
|
|
'$parameters' => ['tcp://127.0.0.1:6379'], |
54
|
|
|
], |
55
|
|
|
], |
56
|
|
|
], |
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
$this->assertSame('OK', $client->flushall()->getPayload()); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* getClient. |
64
|
|
|
* |
65
|
|
|
* @param array[] $config |
66
|
|
|
* |
67
|
|
|
* @return ClientInterface |
68
|
|
|
* |
69
|
|
|
* @throws Exception |
70
|
|
|
*/ |
71
|
|
|
private function getClient(array $config = []): ClientInterface |
72
|
|
|
{ |
73
|
|
|
$client = $this->loadExtension($config)->get(ClientInterface::class); |
74
|
|
|
|
75
|
|
|
if (!$client instanceof ClientInterface) { |
76
|
|
|
throw new Exception('Client not instanceof ClientInterface'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $client; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* loadExtension. |
84
|
|
|
* |
85
|
|
|
* @param array[] $config |
86
|
|
|
* |
87
|
|
|
* @return ContainerBuilder |
88
|
|
|
*/ |
89
|
|
|
private function loadExtension(array $config = []): ContainerBuilder |
90
|
|
|
{ |
91
|
|
|
$defaults = [ |
92
|
|
|
'sb_redis' => [ |
93
|
|
|
'clients' => [ |
94
|
|
|
'test' => [], |
95
|
|
|
], |
96
|
|
|
], |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
$extension = new RedisExtension(); |
100
|
|
|
$container = new ContainerBuilder(); |
101
|
|
|
|
102
|
|
|
$extension->load(array_merge_recursive($defaults, $config), $container); |
103
|
|
|
|
104
|
|
|
return $container; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|