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-2022 |
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', 'pipeline', 'sadd', 'set', 'smembers' |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
$this->mock = $this->getMockBuilder( '\\Predis\\Client' )->setMethods( $methods )->getMock(); |
27
|
|
|
$this->object = new \Aimeos\Base\Cache\Redis( [], $this->mock ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
protected function tearDown() : void |
32
|
|
|
{ |
33
|
|
|
unset( $this->object, $this->mock ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
public function testClear() |
38
|
|
|
{ |
39
|
|
|
$this->mock->expects( $this->once() )->method( 'flushdb' )->will( $this->returnValue( 'OK' ) ); |
40
|
|
|
$this->assertTrue( $this->object->clear() ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
public function testDelete() |
45
|
|
|
{ |
46
|
|
|
$this->mock->expects( $this->once() )->method( 'del' )->will( $this->returnValue( 'OK' ) ) |
47
|
|
|
->with( $this->equalTo( array( 'test' ) ) ); |
48
|
|
|
|
49
|
|
|
$this->assertTrue( $this->object->delete( 'test' ) ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
public function testDeleteMultiple() |
54
|
|
|
{ |
55
|
|
|
$this->mock->expects( $this->once() )->method( 'del' )->will( $this->returnValue( 'OK' ) ) |
56
|
|
|
->with( $this->equalTo( array( 'test' ) ) ); |
57
|
|
|
|
58
|
|
|
$this->assertTrue( $this->object->deleteMultiple( array( 'test' ) ) ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
public function testDeleteByTags() |
63
|
|
|
{ |
64
|
|
|
$this->mock->expects( $this->once() )->method( 'pipeline' ) |
65
|
|
|
->will( $this->returnValue( $this->mock ) ); |
66
|
|
|
|
67
|
|
|
$this->mock->expects( $this->exactly( 2 ) )->method( 'smembers' ); |
68
|
|
|
|
69
|
|
|
$this->mock->expects( $this->once() )->method( 'execute' ) |
70
|
|
|
->will( $this->returnValue( array( 'tag:1' => array( 'key:1', 'key:2' ) ) ) ); |
71
|
|
|
|
72
|
|
|
$this->mock->expects( $this->once() )->method( 'del' )->will( $this->returnValue( 'OK' ) ) |
73
|
|
|
->with( $this->equalTo( array( 'key:1', 'key:2', 'tag:tag1', 'tag:tag2' ) ) ); |
74
|
|
|
|
75
|
|
|
$this->assertTrue( $this->object->deleteByTags( array( 'tag1', 'tag2' ) ) ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
public function testGet() |
80
|
|
|
{ |
81
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ) |
82
|
|
|
->will( $this->returnValue( 'test' ) ); |
83
|
|
|
|
84
|
|
|
$this->assertEquals( 'test', $this->object->get( 't:1' ) ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
public function testGetDefault() |
89
|
|
|
{ |
90
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ); |
91
|
|
|
|
92
|
|
|
$this->assertFalse( $this->object->get( 't:1', false ) ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
public function testGetExpired() |
97
|
|
|
{ |
98
|
|
|
$this->mock->expects( $this->once() )->method( 'get' ); |
99
|
|
|
|
100
|
|
|
$this->assertEquals( null, $this->object->get( 't:1' ) ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
public function testGetMultiple() |
105
|
|
|
{ |
106
|
|
|
$this->mock->expects( $this->once() )->method( 'mget' ) |
107
|
|
|
->will( $this->returnValue( array( 0 => 'test' ) ) ); |
108
|
|
|
|
109
|
|
|
$this->assertEquals( array( 't:1' => 'test' ), $this->object->getMultiple( array( 't:1' ) ) ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
public function testHas() |
114
|
|
|
{ |
115
|
|
|
$this->mock->expects( $this->once() )->method( 'exists' )->will( $this->returnValue( 1 ) ); |
116
|
|
|
$this->assertTrue( $this->object->has( 'key' ) ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
public function testSet() |
121
|
|
|
{ |
122
|
|
|
$this->mock->expects( $this->once() )->method( 'pipeline' ) |
123
|
|
|
->will( $this->returnValue( $this->mock ) ); |
124
|
|
|
|
125
|
|
|
$this->mock->expects( $this->once() )->method( 'set' ) |
126
|
|
|
->with( $this->equalTo( 't:1' ), $this->equalTo( 'test 1' ) ); |
127
|
|
|
|
128
|
|
|
$this->mock->expects( $this->exactly( 2 ) )->method( 'sadd' ); |
129
|
|
|
|
130
|
|
|
$this->mock->expects( $this->once() )->method( 'execute' )->will( $this->returnValue( 'OK' ) ); |
131
|
|
|
|
132
|
|
|
$this->mock->expects( $this->once() )->method( 'expireat' ) |
133
|
|
|
->with( $this->equalTo( 't:1' ), $this->greaterThan( 0 ) ); |
134
|
|
|
|
135
|
|
|
$this->assertTrue( $this->object->set( 't:1', 'test 1', '2000-01-01 00:00:00', ['tag1', 'tag2'] ) ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
public function testSetMultiple() |
140
|
|
|
{ |
141
|
|
|
$this->mock->expects( $this->once() )->method( 'pipeline' ) |
142
|
|
|
->will( $this->returnValue( $this->mock ) ); |
143
|
|
|
|
144
|
|
|
$this->mock->expects( $this->once() )->method( 'mset' ) |
145
|
|
|
->with( $this->equalTo( array( 't:1' => 'test 1' ) ) ); |
146
|
|
|
|
147
|
|
|
$this->mock->expects( $this->exactly( 2 ) )->method( 'sadd' ); |
148
|
|
|
|
149
|
|
|
$this->mock->expects( $this->once() )->method( 'execute' )->will( $this->returnValue( 'OK' ) ); |
150
|
|
|
|
151
|
|
|
$this->mock->expects( $this->once() )->method( 'expireat' ) |
152
|
|
|
->with( $this->equalTo( 't:1' ), $this->greaterThan( 0 ) ); |
153
|
|
|
|
154
|
|
|
$this->assertTrue( $this->object->setMultiple( ['t:1' => 'test 1'], '2000-01-01 00:00:00', ['tag1', 'tag2'] ) ); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|