Completed
Pull Request — master (#1)
by Alexander
03:01
created

shouldConnectAndCheckIfLockIsAcquired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Tourstream\RedisLockStrategy\Tests;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017 Alexander Miehe ([email protected])
9
 *  All rights reserved
10
 *
11
 *  You may not remove or change the name of the author above. See:
12
 *  http://www.gnu.org/licenses/gpl-faq.html#IWantCredit
13
 *
14
 *  This script is part of the Typo3 project. The Typo3 project is
15
 *  free software; you can redistribute it and/or modify
16
 *  it under the terms of the GNU General Public License as published by
17
 *  the Free Software Foundation; either version 3 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  The GNU General Public License can be found at
21
 *  http://www.gnu.org/copyleft/gpl.html.
22
 *  A copy is found in the LICENSE and distributed with these scripts.
23
 *
24
 *
25
 *  This script is distributed in the hope that it will be useful,
26
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 *  GNU General Public License for more details.
29
 *
30
 *  This copyright notice MUST APPEAR in all copies of the script!
31
 ***************************************************************/
32
33
use TYPO3\CMS\Core\Locking\LockFactory;
34
use TYPO3\CMS\Core\Tests\FunctionalTestCase;
35
use TYPO3\CMS\Core\Utility\GeneralUtility;
36
37
/**
38
 * @author Alexander Miehe <[email protected]>
39
 *
40
 * @covers \Tourstream\RedisLockStrategy\RedisLockStrategy
41
 */
42
class RedisLockStrategyTest extends FunctionalTestCase
43
{
44
    /**
45
     * @var LockFactory
46
     */
47
    private $lockFactory;
48
    private $redisHost;
49
    private $redisDatabase;
50
51
    /**
52
     * @test
53
     * @expectedException \TYPO3\CMS\Core\Locking\Exception
54
     * @expectedExceptionMessage no configuration for redis lock strategy found
55
     */
56
    public function shouldThrowExceptionBecauseConfigIsMissing()
57
    {
58
        $this->lockFactory->createLocker('test');
59
    }
60
61
    /**
62
     * @test
63
     * @expectedException \TYPO3\CMS\Core\Locking\Exception
64
     * @expectedExceptionMessage no configuration for redis lock strategy found
65
     */
66
    public function shouldThrowExceptionBecauseConfigIsNotAnArray()
67
    {
68
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = 'test';
69
        $this->lockFactory->createLocker('test');
70
    }
71
72
    /**
73
     * @test
74
     * @expectedException \TYPO3\CMS\Core\Locking\Exception
75
     * @expectedExceptionMessage no host for redis lock strategy found
76
     */
77
    public function shouldThrowExceptionBecauseConfigHasNoHost()
78
    {
79
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [];
80
81
        $this->lockFactory->createLocker('test');
82
    }
83
84
    /**
85
     * @test
86
     * @expectedException \TYPO3\CMS\Core\Locking\Exception
87
     * @expectedExceptionMessage no database for redis lock strategy found
88
     */
89
    public function shouldThrowExceptionBecauseConfigHasNoDatabase()
90
    {
91
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [
92
            'host' => $this->redisHost,
93
        ];
94
95
        $this->lockFactory->createLocker('test');
96
    }
97
98
    /**
99
     * @test
100
     */
101
    public function shouldConnectAndAcquireAExistingLock()
102
    {
103
        $id = uniqid();
104
105
        $locker = $this->getLocker($id);
106
107
        $redis = $this->getRedisClient();
108
109
        $redis->set($id, 'testvalue');
110
111
        self::assertTrue($locker->acquire());
112
    }
113
114
115
    /**
116
     * @test
117
     */
118 View Code Duplication
    public function shouldConnectAndAcquireALock()
119
    {
120
        $id = uniqid();
121
122
        $locker = $this->getLocker($id);
123
124
        self::assertTrue($locker->acquire());
125
126
        $redis = $this->getRedisClient();
127
128
        self::assertTrue($redis->exists($id));
129
    }
130
131
    /**
132
     * @test
133
     */
134
    public function shouldConnectAndCheckIfLockIsAcquired()
135
    {
136
137
        $id = uniqid();
138
139
        $locker = $this->getLocker($id);
140
141
        $redis = $this->getRedisClient();
142
143
        $redis->set($id, 'testvalue');
144
145
        self::assertTrue($locker->isAcquired());
146
    }
147
148
    /**
149
     * @test
150
     */
151 View Code Duplication
    public function shouldConnectAndDestroyALock()
152
    {
153
        $id = uniqid();
154
155
        $locker = $this->getLocker($id);
156
157
        $redis = $this->getRedisClient();
158
159
        $redis->set($id, 'testvalue');
160
161
        $locker->destroy();
162
163
        self::assertFalse($redis->exists($id));
164
    }
165
166
    /**
167
     * @test
168
     */
169 View Code Duplication
    public function shouldConnectAndDestroyANotExistingLock()
170
    {
171
        $id = uniqid();
172
173
        $locker = $this->getLocker($id);
174
175
        $redis = $this->getRedisClient();
176
177
        $locker->destroy();
178
179
        self::assertFalse($redis->exists($id));
180
    }
181
182
    protected function setUp()
183
    {
184
        $this->testExtensionsToLoad[] = 'typo3conf/ext/redis_lock_strategy';
185
        $this->redisHost              = getenv('typo3RedisHost');
186
        $this->redisDatabase          = getenv('typo3RedisDatabase');
187
188
        parent::setUp();
189
190
        $this->lockFactory = GeneralUtility::makeInstance(LockFactory::class);
191
    }
192
193
    protected function tearDown()
194
    {
195
        parent::tearDown();
196
197
        $this->getRedisClient()->flushDB();
198
    }
199
200
    /**
201
     * @return \Redis
202
     */
203
    private function getRedisClient()
204
    {
205
        $redis = new \Redis();
206
        $redis->connect($this->redisHost);
207
        $redis->select($this->redisDatabase);
208
209
        return $redis;
210
    }
211
212
    /**
213
     * @param string $id Locker id
214
     *
215
     * @return \TYPO3\CMS\Core\Locking\LockingStrategyInterface
216
     */
217
    private function getLocker($id)
218
    {
219
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [
220
            'host'     => $this->redisHost,
221
            'port'     => 6379,
222
            'database' => $this->redisDatabase,
223
        ];
224
225
        return $this->lockFactory->createLocker($id);
226
    }
227
228
}
229