Completed
Pull Request — master (#8)
by
unknown
01:30
created

RedisLockStrategyTest::shouldAcquireAndRelease()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 8.8571
cc 1
eloc 19
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\Utility\GeneralUtility;
35
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
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\LockCreateException
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\LockCreateException
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\LockCreateException
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\LockCreateException
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
        $subject = uniqid();
104
105
        $locker = $this->getLocker($subject);
106
107
        self::assertTrue($locker->acquire());
108
    }
109
110
111
    /**
112
     * @test
113
     */
114 View Code Duplication
    public function shouldConnectAndAcquireALock()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        $subject = uniqid();
117
118
        $locker = $this->getLocker($subject);
119
120
        $redis = $this->getRedisClient();
121
122
        self::assertTrue($redis->exists($subject));
123
124
        self::assertTrue($locker->acquire());
125
    }
126
127
    /**
128
     * @test
129
     */
130
    public function shouldConnectAndCheckIfLockIsAcquired()
131
    {
132
        $subject = uniqid();
133
134
        $locker = $this->getLocker($subject);
135
136
        $locker->acquire();
137
138
        self::assertTrue($locker->isAcquired());
139
    }
140
141
    /**
142
     * @test
143
     */
144 View Code Duplication
    public function shouldConnectAndDestroyALock()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        $subject = uniqid();
147
148
        $locker = $this->getLocker($subject);
149
150
        $redis = $this->getRedisClient();
151
152
        $redis->set($subject, 'testvalue');
153
154
        $locker->destroy();
155
156
        self::assertFalse($redis->exists($subject));
157
    }
158
159
    /**
160
     * @test
161
     */
162 View Code Duplication
    public function shouldConnectAndDestroyANotExistingLock()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164
        $subject = uniqid();
165
166
        $locker = $this->getLocker($subject);
167
168
        $redis = $this->getRedisClient();
169
170
        $locker->destroy();
171
172
        self::assertFalse($redis->exists($subject));
173
    }
174
175
    /**
176
     * @test
177
     */
178
    public function shouldAcquireAndRelease()
179
    {
180
        $subject = uniqid();
181
182
        $locker1 = $this->getLocker($subject);
183
        $locker2 = $this->getLocker($subject);
184
        $locker3 = $this->getLocker($subject);
185
186
        self::assertTrue($locker1->acquire());
187
        self::assertFalse($locker2->acquire());
188
        self::assertFalse($locker3->acquire());
189
190
        self::assertTrue($locker1->isAcquired());
191
        self::assertFalse($locker2->isAcquired());
192
        self::assertFalse($locker3->isAcquired());
193
194
        self::assertTrue($locker1->release());
195
        self::assertTrue($locker2->isAcquired());
196
197
        self::assertTrue($locker2->release());
198
        self::assertTrue($locker3->isAcquired());
199
200
        self::assertTrue($locker3->release());
201
        self::assertFalse($locker1->isAcquired());
202
        self::assertFalse($locker2->isAcquired());
203
        self::assertFalse($locker3->isAcquired());
204
    }
205
206
    protected function setUp()
207
    {
208
        $this->testExtensionsToLoad[] = 'typo3conf/ext/redis_lock_strategy';
209
        $this->redisHost              = getenv('typo3RedisHost');
210
        $this->redisDatabase          = getenv('typo3RedisDatabase');
211
212
        parent::setUp();
213
214
        $this->lockFactory = GeneralUtility::makeInstance(LockFactory::class);
215
    }
216
217
    protected function tearDown()
218
    {
219
        parent::tearDown();
220
221
        $this->getRedisClient()->flushDB();
222
    }
223
224
    /**
225
     * @return \Redis
226
     */
227
    private function getRedisClient()
228
    {
229
        $redis = new \Redis();
230
        $redis->connect($this->redisHost);
231
        $redis->select($this->redisDatabase);
232
233
        return $redis;
234
    }
235
236
    /**
237
     * @param string $id Locker id
238
     *
239
     * @return \TYPO3\CMS\Core\Locking\LockingStrategyInterface
240
     */
241
    private function getLocker($id)
242
    {
243
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [
244
            'host'     => $this->redisHost,
245
            'port'     => 6379,
246
            'database' => $this->redisDatabase,
247
        ];
248
249
        return $this->lockFactory->createLocker($id);
250
    }
251
252
}
253