Completed
Push — master ( 531043...462e40 )
by Aimeos
01:36
created

RedisTest::testSetMultiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
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-2016
7
 */
8
9
10
namespace Aimeos\MW\Cache;
11
12
13
class RedisTest extends \PHPUnit_Framework_TestCase
14
{
15
	private $mock;
16
	private $object;
17
18
19
	protected function setUp()
20
	{
21
		$methods = array(
22
			'del', 'execute', 'expireat', 'flushdb',
23
			'get', 'mget', 'mset', 'pipeline',
24
			'sadd', 'set', 'smembers'
25
		);
26
27
		$this->mock = $this->getMockBuilder( '\\Predis\\Client' )->setMethods( $methods )->getMock();
28
		$this->object = new \Aimeos\MW\Cache\Redis( array( 'siteid' => 1 ), $this->mock );
29
	}
30
31
32
	protected function tearDown()
33
	{
34
		unset( $this->object, $this->mock );
35
	}
36
37
38
	public function testDelete()
39
	{
40
		$this->mock->expects( $this->once() )->method( 'del' )
41
			->with( $this->equalTo( array( '1-test' ) ) );
42
43
		$this->object->delete( 'test' );
44
	}
45
46
47
	public function testDeleteMultiple()
48
	{
49
		$this->mock->expects( $this->once() )->method( 'del' )
50
			->with( $this->equalTo( array( '1-test' ) ) );
51
52
		$this->object->deleteMultiple( array( 'test' ) );
53
	}
54
55
56
	public function testDeleteByTags()
57
	{
58
		$this->mock->expects( $this->once() )->method( 'pipeline' )
59
			->will( $this->returnValue( $this->mock ) );
60
61
		$this->mock->expects( $this->exactly( 2 ) )->method( 'smembers' );
62
63
		$this->mock->expects( $this->once() )->method( 'execute' )
64
			->will( $this->returnValue( array( '1-tag:1' => array( '1-key:1', '1-key:2' ) ) ) );
65
66
		$this->mock->expects( $this->once() )->method( 'del' )
67
			->with( $this->equalTo( array( '1-key:1', '1-key:2', '1-tag:tag1', '1-tag:tag2' ) ) );
68
69
		$this->object->deleteByTags( array( 'tag1', 'tag2' ) );
70
	}
71
72
73
	public function testClear()
74
	{
75
		$this->mock->expects( $this->once() )->method( 'flushdb' );
76
		$this->object->clear();
77
	}
78
79
80
	public function testGet()
81
	{
82
		$this->mock->expects( $this->once() )->method( 'get' )
83
			->will( $this->returnValue( 'test' ) );
84
85
		$this->assertEquals( 'test', $this->object->get( 't:1' ) );
86
	}
87
88
89
	public function testGetDefault()
90
	{
91
		$this->mock->expects( $this->once() )->method( 'get' );
92
93
		$this->assertFalse( $this->object->get( 't:1', false ) );
94
	}
95
96
97
	public function testGetExpired()
98
	{
99
		$this->mock->expects( $this->once() )->method( 'get' );
100
101
		$this->assertEquals( null, $this->object->get( 't:1' ) );
102
	}
103
104
105
	public function testGetMultiple()
106
	{
107
		$this->mock->expects( $this->once() )->method( 'mget' )
108
			->will( $this->returnValue( array( 0 => 'test' ) ) );
109
110
		$this->assertEquals( array( 't:1' => 'test' ), $this->object->getMultiple( array( 't:1' ) ) );
0 ignored issues
show
Documentation introduced by
array('t:1') is of type array<integer,string,{"0":"string"}>, but the function expects a object<Aimeos\MW\Cache\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
	}
112
113
114
	public function testGetMultipleByTags()
115
	{
116
		$this->mock->expects( $this->once() )->method( 'pipeline' )
117
			->will( $this->returnValue( $this->mock ) );
118
119
		$this->mock->expects( $this->exactly( 2 ) )->method( 'smembers' );
120
121
		$this->mock->expects( $this->once() )->method( 'execute' )
122
			->will( $this->returnValue( array( '1-tag:1' => array( '1-t:1', '1-t:2' ) ) ) );
123
124
		$this->mock->expects( $this->once() )->method( 'mget' )
125
			->will( $this->returnValue( array( 0 => 'test1', 1 => 'test2' ) ) );
126
127
		$expected = array( 't:1' => 'test1', 't:2' => 'test2' );
128
		$result = $this->object->getMultipleByTags( array( 'tag1', 'tag2' ) );
129
130
		$this->assertEquals( $expected, $result );
131
	}
132
133
134
	public function testSet()
135
	{
136
		$this->mock->expects( $this->once() )->method( 'pipeline' )
137
			->will( $this->returnValue( $this->mock ) );
138
139
		$this->mock->expects( $this->once() )->method( 'set' )
140
			->with( $this->equalTo( '1-t:1' ), $this->equalTo( 'test 1' ) );
141
142
		$this->mock->expects( $this->exactly( 2 ) )->method( 'sadd' );
143
144
		$this->mock->expects( $this->once() )->method( 'execute' );
145
146
		$this->mock->expects( $this->once() )->method( 'expireat' )
147
			->with( $this->equalTo( '1-t:1' ), $this->greaterThan( 0 ) );
148
149
		$this->object->set( 't:1', 'test 1', '2000-01-01 00:00:00', array( 'tag1', 'tag2' ) );
150
	}
151
152
153
	public function testSetMultiple()
154
	{
155
		$this->mock->expects( $this->once() )->method( 'pipeline' )
156
			->will( $this->returnValue( $this->mock ) );
157
158
		$this->mock->expects( $this->once() )->method( 'mset' )
159
			->with( $this->equalTo( array( '1-t:1' => 'test 1' ) ) );
160
161
		$this->mock->expects( $this->exactly( 2 ) )->method( 'sadd' );
162
163
		$this->mock->expects( $this->once() )->method( 'execute' );
164
165
		$this->mock->expects( $this->once() )->method( 'expireat' )
166
			->with( $this->equalTo( '1-t:1' ), $this->greaterThan( 0 ) );
167
168
		$this->object->setMultiple( array( 't:1' => 'test 1' ), array( 't:1' => '2000-01-01 00:00:00' ), array( 'tag1', 'tag2' ) );
0 ignored issues
show
Documentation introduced by
array('t:1' => 'test 1') is of type array<string,string,{"t:1":"string"}>, but the function expects a object<Aimeos\MW\Cache\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
169
	}
170
}
171