Completed
Pull Request — master (#1)
by Alexander
02:16
created

shouldConnectAndAcquireAExistingLock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 18
rs 9.4285
cc 1
eloc 10
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 View Code Duplication
    public function shouldConnectAndAcquireALock()
1 ignored issue
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...
102
    {
103
        $id = uniqid();
104
105
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [
106
            'host'     => $this->redisHost,
107
            'port'     => 6379,
108
            'database' => $this->redisDatabase,
109
        ];
110
111
        $locker = $this->lockFactory->createLocker($id);
112
113
        $redis = $this->getRedisClient();
114
115
        $redis->set($id, 'testvalue');
116
117
        self::assertTrue($locker->acquire());
118
    }
119
120
121
    /**
122
     * @test
123
     */
124 View Code Duplication
    public function shouldConnectAndAcquireAExistingLock()
1 ignored issue
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...
125
    {
126
        $id = uniqid();
127
128
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [
129
            'host'     => $this->redisHost,
130
            'port'     => 6379,
131
            'database' => $this->redisDatabase,
132
        ];
133
134
        $locker = $this->lockFactory->createLocker($id);
135
136
        self::assertTrue($locker->acquire());
137
138
        $redis = $this->getRedisClient();
139
140
        self::assertTrue($redis->exists($id));
141
    }
142
143
    /**
144
     * @test
145
     */
146 View Code Duplication
    public function shouldConnectAndCheckIfLockIsAcquired()
1 ignored issue
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...
147
    {
148
        $id = uniqid();
149
150
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [
151
            'host'     => $this->redisHost,
152
            'port'     => 6379,
153
            'database' => $this->redisDatabase,
154
        ];
155
156
        $locker = $this->lockFactory->createLocker($id);
157
158
        $redis = $this->getRedisClient();
159
160
        $redis->set($id, 'testvalue');
161
162
        self::assertTrue($locker->isAcquired());
163
    }
164
165
    /**
166
     * @test
167
     */
168 View Code Duplication
    public function shouldConnectAndDestroyALock()
1 ignored issue
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...
169
    {
170
        $id = uniqid();
171
172
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [
173
            'host'     => $this->redisHost,
174
            'port'     => 6379,
175
            'database' => $this->redisDatabase,
176
        ];
177
178
        $locker = $this->lockFactory->createLocker($id);
179
180
        $redis = $this->getRedisClient();
181
182
        $redis->set($id, 'testvalue');
183
184
        $locker->destroy();
185
186
        self::assertFalse($redis->exists($id));
187
    }
188
189
    /**
190
     * @test
191
     */
192 View Code Duplication
    public function shouldConnectAndDestroyANotExistingLock()
1 ignored issue
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...
193
    {
194
        $id = uniqid();
195
196
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock'] = [
197
            'host'     => $this->redisHost,
198
            'port'     => 6379,
199
            'database' => $this->redisDatabase,
200
        ];
201
202
        $locker = $this->lockFactory->createLocker($id);
203
204
        $redis = $this->getRedisClient();
205
206
        $locker->destroy();
207
208
        self::assertFalse($redis->exists($id));
209
    }
210
211
    protected function setUp()
212
    {
213
        $this->testExtensionsToLoad[] = 'typo3conf/ext/redis_lock_strategy';
214
        $this->redisHost              = getenv('typo3RedisHost');
215
        $this->redisDatabase          = getenv('typo3RedisDatabase');
216
217
        parent::setUp();
218
219
        $this->lockFactory = GeneralUtility::makeInstance(LockFactory::class);
220
    }
221
222
    protected function tearDown()
223
    {
224
        parent::tearDown();
225
226
        $this->getRedisClient()->flushDB();
227
    }
228
229
    /**
230
     * @return \Redis
231
     */
232
    private function getRedisClient()
233
    {
234
        $redis = new \Redis();
235
        $redis->connect($this->redisHost);
236
        $redis->select($this->redisDatabase);
237
238
        return $redis;
239
    }
240
241
}
242