Completed
Pull Request — master (#31)
by
unknown
01:19
created

RedisTest::removeMultiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
4
 * @license    GNU General Public License version 2 or later; see LICENSE
5
 */
6
7
namespace Joomla\Cache\Tests;
8
9
use Joomla\Cache;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * Tests for the Joomla\Cache\Redis class.
14
 *
15
 * @since  1.0
16
 */
17
class RedisTest extends TestCase
18
{
19
	/**
20
	 * @var	Cache\Redis
21
	 * @since  1.0
22
	 */
23
	private $instance;
24
25
	/**
26
	 * Tests for the correct Psr\Cache return values.
27
	 *
28
	 * @return  void
29
	 *
30
	 * @coversNothing
31
	 * @since   1.0
32
	 */
33
	public function testPsrCache()
34
	{
35
		$this->assertInternalType('boolean', $this->instance->clear(), 'Checking clear.');
36
		$this->assertInstanceOf('\Psr\Cache\CacheItemInterface', $this->instance->get('foo'), 'Checking get.');
37
		$this->assertInternalType('array', $this->instance->getMultiple(array('foo')), 'Checking getMultiple.');
38
		$this->assertInternalType('boolean', $this->instance->remove('foo'), 'Checking remove.');
39
		$this->assertInternalType('array', $this->instance->removeMultiple(array('foo')), 'Checking removeMultiple.');
40
		$this->assertInternalType('boolean', $this->instance->set('for', 'bar'), 'Checking set.');
41
		$this->assertInternalType('boolean', $this->instance->setMultiple(array('foo' => 'bar')), 'Checking setMultiple.');
42
	}
43
44
	/**
45
	 * Tests the Joomla\Cache\Redis::get and Joomla\Cache\Redis::set methods.
46
	 *
47
	 * @return  void
48
	 *
49
	 * @covers  Joomla\Cache\Redis::get
50
	 * @covers  Joomla\Cache\Redis::set
51
	 * @covers  Joomla\Cache\Redis::connect
52
	 * @since   1.0
53
	 */
54
	public function testGetAndSet()
55
	{
56
		$this->assertTrue(
57
			$this->instance->set('foo', 'bar'),
58
			'Should store the data properly'
59
		);
60
61
		$this->assertEquals(
62
			'bar',
63
			$this->instance->get('foo')->getValue(),
64
			'Checking get'
65
		);
66
	}
67
68
	/**
69
	 * Tests the Joomla\Cache\Redis::get and Joomla\Cache\Redis::set methods with timeout
70
	 *
71
	 * @return  void
72
	 *
73
	 * @covers  Joomla\Cache\Redis::get
74
	 * @covers  Joomla\Cache\Redis::set
75
	 * @covers  Joomla\Cache\Redis::connect
76
	 * @since   1.0
77
	 */
78
	public function testGetAndSetWithTimeout()
79
	{
80
		$this->assertTrue(
81
			$this->instance->set('foo', 'bar', 1),
82
			'Should store the data properly'
83
		);
84
85
		sleep(2);
86
87
		$this->assertFalse(
88
			$this->instance->get('foo')->isHit(),
89
			'Checks expired get.'
90
		);
91
	}
92
93
	/**
94
	 * Tests the Joomla\Cache\Redis::clear method.
95
	 *
96
	 * @return  void
97
	 *
98
	 * @covers  Joomla\Cache\Redis::clear
99
	 * @covers  Joomla\Cache\Redis::connect
100
	 * @since   1.0
101
	 */
102 View Code Duplication
	public function testClear()
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...
103
	{
104
		$this->instance->set('foo', 'bar');
105
		$this->instance->set('boo', 'car');
106
107
		$this->instance->clear();
108
109
		$this->assertFalse(
110
			$this->instance->get('foo')->isHit(),
111
			'Item should have been removed'
112
		);
113
114
		$this->assertFalse(
115
			$this->instance->get('goo')->isHit(),
116
			'Item should have been removed'
117
		);
118
	}
119
120
	/**
121
	 * Tests the Joomla\Cache\Redis::exists method.
122
	 *
123
	 * @return  void
124
	 *
125
	 * @covers  Joomla\Cache\Redis::connect
126
	 * @covers  Joomla\Cache\Redis::exists
127
	 * @since   1.0
128
	 */
129
	public function testExists()
130
	{
131
		$this->assertFalse(
132
			$this->instance->exists('foo'),
133
			'Item should not exist'
134
		);
135
136
		$this->instance->set('foo', 'bar');
137
138
		$this->assertTrue(
139
			$this->instance->exists('foo'),
140
			'Item should exist'
141
		);
142
	}
143
144
	/**
145
	 * Tests the Joomla\Cache\Redis::remove method.
146
	 *
147
	 * @return  void
148
	 *
149
	 * @covers  Joomla\Cache\Redis::connect
150
	 * @covers  Joomla\Cache\Redis::remove
151
	 * @since   1.0
152
	 */
153
154
	public function testRemove()
155
	{
156
		$this->instance->set('foo', 'bar');
157
		$this->assertTrue(
158
			$this->instance->get('foo')->isHit(),
159
			'Item should exist'
160
		);
161
162
		$this->instance->remove('foo');
163
164
		$this->assertFalse(
165
			$this->instance->get('foo')->isHit(),
166
			'Item should have been removed'
167
		);
168
	}
169
170
	/**
171
	 * Tests the Joomla\Cache\Redis::getMultiple method.
172
	 *
173
	 * @return  void
174
	 *
175
	 * @covers  Joomla\Cache\Redis::getMultiple
176
	 * @since   1.0
177
	 */
178
	public function testGetMultiple()
179
	{
180
		$this->instance->set('foo', 'bar');
181
		$this->instance->set('boo', 'bar');
182
183
		$fooResult = $this->instance->getMultiple(array('foo', 'boo'));
184
185
		$this->assertArrayHasKey('foo', $fooResult, 'Missing array key');
186
		$this->assertArrayHasKey('boo', $fooResult, 'Missing array key');
187
188
		$this->assertInstanceOf(
189
			'Joomla\Cache\Item',
190
			$fooResult['foo'],
191
			'Expected instance of Joomla\Cache\Item'
192
		);
193
		$this->assertInstanceOf(
194
			'Joomla\Cache\Item',
195
			$fooResult['boo'],
196
			'Expected instance of Joomla\Cache\Item'
197
		);
198
199
		$this->assertTrue(
200
			$fooResult['foo']->isHit(),
201
			'Item should be returned from cache'
202
		);
203
		$this->assertTrue(
204
			$fooResult['boo']->isHit(),
205
			'Item should be returned from cache'
206
		);
207
	}
208
209
	/**
210
	 * Tests the Joomla\Cache\Redis::setMultiple method.
211
	 *
212
	 * @return  void
213
	 *
214
	 * @covers  Joomla\Cache\Redis::setMultiple
215
	 * @since   1.0
216
	 */
217
	public function testSetMultiple()
218
	{
219
		$data = array('foo' => 'bar', 'boo' => 'bar');
220
221
		$this->instance->setMultiple($data);
222
223
		$this->assertEquals(
224
			'bar',
225
			$this->instance->get('foo')->getValue(),
226
			'Item should be cached'
227
			);
228
229
		$this->assertEquals(
230
			'bar', $this->instance->get('boo')->getValue(),
231
			'Item should be cached'
232
		);
233
	}
234
235
	/**
236
	 * Tests the Joomla\Cache\Redis::removeMultiple method.
237
	 *
238
	 * @return  void
239
	 *
240
	 * @covers  Joomla\Cache\Redis::removeMultiple
241
	 * @since   1.0
242
	 */
243 View Code Duplication
	public function removeMultiple()
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...
244
	{
245
		$this->instance->set('foo', 'bar');
246
		$this->instance->set('boo', 'bar');
247
248
		$this->instance->removeMultiple(array('foo', 'bar'));
249
250
		$this->assertFalse(
251
			$this->instance->get('foo')->isHit(),
252
			'Item should have been removed'
253
		);
254
		$this->assertFalse(
255
			$this->instance->get('boo')->isHit(),
256
			'Item should have been removed'
257
		);
258
	}
259
260
	/**
261
	 * Setup the tests.
262
	 *
263
	 * @return  void
264
	 *
265
	 * @covers  Joomla\Cache\Redis::__construct
266
	 * @since   1.0
267
	 */
268
	protected function setUp()
269
	{
270
		// Parse the DSN details for the test server
271
		$dsn = defined('JTEST_CACHE_REDIS_DSN') ? JTEST_CACHE_REDIS_DSN : getenv('JTEST_CACHE_REDIS_DSN');
272
273
		if ($dsn)
274
		{
275
			$options = array();
276
277
			// First let's trim the redis: part off the front of the DSN if it exists.
278
			if (strpos($dsn, 'redis:') === 0)
279
			{
280
				$dsn = substr($dsn, 6);
281
			}
282
283
			if (!is_array($options))
284
			{
285
				$options = array($options);
286
			}
287
288
			// Split the DSN into its parts over semicolons.
289
			$parts = explode(';', $dsn);
290
291
			// Parse each part and populate the options array.
292 View Code Duplication
			foreach ($parts as $part)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
293
			{
294
				list ($k, $v) = explode('=', $part, 2);
295
				switch ($k)
296
				{
297
					case 'host':
298
					case 'port':
299
						$options['redis.' . $k] = $v;
300
						break;
301
				}
302
			}
303
304
			$this->cacheOptions = $options;
0 ignored issues
show
Bug introduced by
The property cacheOptions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
305
		}
306
		else
307
		{
308
			$this->markTestSkipped('No configuration for Redis given');
309
		}
310
311
		parent::setUp();
312
313
		try
314
		{
315
			$this->instance = new Cache\Redis($options);
0 ignored issues
show
Bug introduced by
The variable $options does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Deprecated Code introduced by
The class Joomla\Cache\Redis has been deprecated with message: The joomla/cache package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
316
		}
317
		catch (\Exception $e)
318
		{
319
			$this->markTestSkipped($e->getMessage());
320
		}
321
	}
322
323
	/**
324
	 * Flush all data before each test
325
	 *
326
	 * @return  void
327
	 *
328
	 * @since   1.0
329
	 */
330
	protected function assertPreConditions()
331
	{
332
		if ($this->instance)
333
		{
334
			$this->instance->clear();
335
		}
336
	}
337
338
	/**
339
	 * Teardown the test.
340
	 *
341
	 * @return  void
342
	 *
343
	 * @since   1.0
344
	 */
345
	protected function tearDown()
346
	{
347
		if ($this->instance)
348
		{
349
			$this->instance->clear();
350
		}
351
	}
352
}
353