1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://www.gnu.org/licenses/lgpl.html |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2014 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2025 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
namespace Aimeos\Base\Cache; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class RedisTest extends \PHPUnit\Framework\TestCase |
14
|
|
|
{ |
15
|
|
|
private $mock; |
16
|
|
|
private $object; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
protected function setUp() : void |
20
|
|
|
{ |
21
|
|
|
$methods = array( |
22
|
|
|
'del', 'execute', 'exists', 'expireat', 'flushdb', 'get', |
23
|
|
|
'mget', 'mset', 'sadd', 'set', 'smembers' |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
$this->mock = $this->getMockBuilder( '\\Predis\\Client' ) |
27
|
|
|
->onlyMethods( ['pipeline'] ) |
28
|
|
|
->addMethods( $methods ) |
29
|
|
|
->getMock(); |
30
|
|
|
|
31
|
|
|
$this->object = new \Aimeos\Base\Cache\Redis( [], $this->mock ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
protected function tearDown() : void |
36
|
|
|
{ |
37
|
|
|
unset( $this->object, $this->mock ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
public function testClear() |
42
|
|
|
{ |
43
|
|
|
$this->mock->expects( $this->once() )->method( 'flushdb' )->willReturn( 'OK' ); |
44
|
|
|
$this->assertTrue( $this->object->clear() ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
public function testDelete() |
49
|
|
|
{ |
50
|
|
|
$this->mock->expects( $this->once() )->method( 'del' )->willReturn( 'OK' ) |
51
|
|
|
->with( $this->equalTo( array( 'test' ) ) ); |
52
|
|
|
|
53
|
|
|
$this->assertTrue( $this->object->delete( 'test' ) ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
public function testDeleteMultiple() |
58
|
|
|
{ |
59
|
|
|
$this->mock->expects( $this->once() )->method( 'del' )->willReturn( 'OK' ) |
60
|
|
|
->with( $this->equalTo( array( 'test' ) ) ); |
61
|
|
|
|
62
|
|
|
$this->assertTrue( $this->object->deleteMultiple( array( 'test' ) ) ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
public function testDeleteByTags() |
67
|
|
|
{ |
68
|
|
|
$this->mock->expects( $this->once() )->method( 'pipeline' ) |
69
|
|
|
->willReturn( $this->mock ); |
70
|
|
|
|
71
|
|
|
$this->mock->expects( $this->exactly( 2 ) )->method( 'smembers' ); |
72
|
|
|
|
73
|
|
|
$this->mock->expects( $this->once() )->method( 'execute' ) |
74
|
|
|
->willReturn( array( 'tag:1' => array( 'key:1', 'key:2' ) ) ); |
75
|
|
|
|
76
|
|
|
$this->mock->expects( $this->once() )->method( 'del' )->willReturn( 'OK' ) |
77
|
|
|
->with( $this->equalTo( array( 'key:1', 'key:2', 'tag:tag1', 'tag:tag2' ) ) ); |
78
|
|
|
|
79
|
|
|
$this->assertTrue( $this->object->deleteByTags( array( 'tag1', 'tag2' ) ) ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
public function testGet() |
84
|
|
|
{ |
85
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ) |
86
|
|
|
->willReturn( 'test' ); |
87
|
|
|
|
88
|
|
|
$this->assertEquals( 'test', $this->object->get( 't:1' ) ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
public function testGetDefault() |
93
|
|
|
{ |
94
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ); |
95
|
|
|
|
96
|
|
|
$this->assertFalse( $this->object->get( 't:1', false ) ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
public function testGetExpired() |
101
|
|
|
{ |
102
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ); |
103
|
|
|
|
104
|
|
|
$this->assertEquals( null, $this->object->get( 't:1' ) ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
public function testGetMultiple() |
109
|
|
|
{ |
110
|
|
|
$this->mock->expects( $this->once() )->method( 'mget' ) |
111
|
|
|
->willReturn( array( 0 => 'test' ) ); |
112
|
|
|
|
113
|
|
|
$this->assertEquals( array( 't:1' => 'test' ), $this->object->getMultiple( array( 't:1' ) ) ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
public function testHas() |
118
|
|
|
{ |
119
|
|
|
$this->mock->expects( $this->once() )->method( 'exists' )->willReturn( 1 ); |
120
|
|
|
$this->assertTrue( $this->object->has( 'key' ) ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
public function testSet() |
125
|
|
|
{ |
126
|
|
|
$this->mock->expects( $this->once() )->method( 'pipeline' ) |
127
|
|
|
->willReturn( $this->mock ); |
128
|
|
|
|
129
|
|
|
$this->mock->expects( $this->once() )->method( 'set' ) |
130
|
|
|
->with( $this->equalTo( 't:1' ), $this->equalTo( 'test 1' ) ); |
131
|
|
|
|
132
|
|
|
$this->mock->expects( $this->exactly( 2 ) )->method( 'sadd' ); |
133
|
|
|
|
134
|
|
|
$this->mock->expects( $this->once() )->method( 'execute' )->willReturn( 'OK' ); |
135
|
|
|
|
136
|
|
|
$this->mock->expects( $this->once() )->method( 'expireat' ) |
137
|
|
|
->with( $this->equalTo( 't:1' ), $this->greaterThan( 0 ) ); |
138
|
|
|
|
139
|
|
|
$this->assertTrue( $this->object->set( 't:1', 'test 1', '2000-01-01 00:00:00', ['tag1', 'tag2'] ) ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
public function testSetMultiple() |
144
|
|
|
{ |
145
|
|
|
$this->mock->expects( $this->once() )->method( 'pipeline' ) |
146
|
|
|
->willReturn( $this->mock ); |
147
|
|
|
|
148
|
|
|
$this->mock->expects( $this->once() )->method( 'mset' ) |
149
|
|
|
->with( $this->equalTo( array( 't:1' => 'test 1' ) ) ); |
150
|
|
|
|
151
|
|
|
$this->mock->expects( $this->exactly( 2 ) )->method( 'sadd' ); |
152
|
|
|
|
153
|
|
|
$this->mock->expects( $this->once() )->method( 'execute' )->willReturn( 'OK' ); |
154
|
|
|
|
155
|
|
|
$this->mock->expects( $this->once() )->method( 'expireat' ) |
156
|
|
|
->with( $this->equalTo( 't:1' ), $this->greaterThan( 0 ) ); |
157
|
|
|
|
158
|
|
|
$this->assertTrue( $this->object->setMultiple( ['t:1' => 'test 1'], '2000-01-01 00:00:00', ['tag1', 'tag2'] ) ); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|