RedisTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 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-2025
7
 */
8
9
10
namespace Aimeos\MAdmin\Cache\Manager;
11
12
13
class RedisTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $context;
16
	private $object;
17
18
19
	protected function setUp() : void
20
	{
21
		$this->context = \TestHelper::context();
22
		$this->object = new \Aimeos\MAdmin\Cache\Manager\Redis( $this->context );
23
	}
24
25
26
	protected function tearDown() : void
27
	{
28
		unset( $this->object, $this->context );
29
	}
30
31
32
	public function testClear()
33
	{
34
		$this->assertInstanceOf( \Aimeos\MAdmin\Cache\Manager\Iface::class, $this->object->clear( array( -1 ) ) );
35
	}
36
37
38
	public function testCreateItem()
39
	{
40
		$this->assertInstanceOf( \Aimeos\MAdmin\Cache\Item\Iface::class, $this->object->create() );
41
	}
42
43
44
	public function testGetSearchAttributes()
45
	{
46
		foreach( $this->object->getSearchAttributes() as $attr ) {
47
			$this->assertInstanceOf( '\\Aimeos\\Base\\Criteria\\Attribute\\Iface', $attr );
48
		}
49
	}
50
51
52
	public function testGetSubManager()
53
	{
54
		$this->expectException( \LogicException::class );
55
		$this->object->getSubManager( 'unknown' );
56
	}
57
58
59
	public function testSearchItems()
60
	{
61
		$search = $this->object->filter();
62
		$search->setConditions( $search->compare( '==', 'cache.id', 'unittest' ) );
63
64
		$this->assertEquals( [], $this->object->search( $search )->toArray() );
65
	}
66
67
68
	public function testGetItem()
69
	{
70
		$context = \TestHelper::context();
71
72
		$mockRedis = $this->getMockBuilder( '\\Aimeos\\Base\\Cache\\Redis' )
73
			->disableOriginalConstructor()->onlyMethods( array( 'get' ) )->getMock();
74
75
		$mockRedis->expects( $this->once() )->method( 'get' )->willReturn( 'test value' );
76
77
		$mock = $this->getMockBuilder( '\\Aimeos\\MAdmin\\Cache\\Manager\\Redis' )
78
			->setConstructorArgs( array( $context ) )->onlyMethods( array( 'getCache' ) )->getMock();
79
80
		$mock->expects( $this->once() )->method( 'getCache' )->willReturn( $mockRedis );
81
82
		$this->assertInstanceOf( '\\Aimeos\\MAdmin\\Cache\\Item\\Iface', $mock->get( 'test' ) );
83
	}
84
85
86
	public function testGetItemException()
87
	{
88
		$context = \TestHelper::context();
89
90
		$mockRedis = $this->getMockBuilder( '\\Aimeos\\Base\\Cache\\Redis' )
91
			->disableOriginalConstructor()->onlyMethods( array( 'get' ) )->getMock();
92
93
		$mock = $this->getMockBuilder( '\\Aimeos\\MAdmin\\Cache\\Manager\\Redis' )
94
			->setConstructorArgs( array( $context ) )->onlyMethods( array( 'getCache' ) )->getMock();
95
96
		$mock->expects( $this->once() )->method( 'getCache' )->willReturn( $mockRedis );
97
98
		$this->expectException( '\\Aimeos\\MAdmin\\Cache\\Exception' );
99
		$mock->get( 'test' );
100
	}
101
102
103
	public function testSaveItem()
104
	{
105
		$context = \TestHelper::context();
106
107
		$mockRedis = $this->getMockBuilder( '\\Aimeos\\Base\\Cache\\Redis' )
108
			->disableOriginalConstructor()->onlyMethods( array( 'delete', 'set' ) )->getMock();
109
110
		$mockRedis->expects( $this->once() )->method( 'delete' );
111
		$mockRedis->expects( $this->once() )->method( 'set' );
112
113
		$mock = $this->getMockBuilder( '\\Aimeos\\MAdmin\\Cache\\Manager\\Redis' )
114
			->setConstructorArgs( array( $context ) )->onlyMethods( array( 'getCache' ) )->getMock();
115
116
		$mock->expects( $this->once() )->method( 'getCache' )->willReturn( $mockRedis );
117
118
		$item = $mock->create();
119
		$item->setId( 'test' );
120
121
		$mock->save( $item );
122
	}
123
124
125
	public function testDeleteItems()
126
	{
127
		$mockRedis = $this->getMockBuilder( '\\Aimeos\\Base\\Cache\\Redis' )
128
			->disableOriginalConstructor()->onlyMethods( array( 'deleteMultiple' ) )->getMock();
129
130
		$mock = $this->getMockBuilder( '\\Aimeos\\MAdmin\\Cache\\Manager\\Redis' )
131
			->disableOriginalConstructor()->onlyMethods( array( 'getCache' ) )->getMock();
132
133
		$mock->expects( $this->once() )->method( 'getCache' )->willReturn( $mockRedis );
134
135
		$mock->delete( [] );
136
	}
137
138
139
	public function testGetCache()
140
	{
141
		try {
142
			$this->assertInstanceOf( '\\Aimeos\\Base\\Cache\\Iface', $this->object->getCache() );
143
		} catch( \Aimeos\MAdmin\Cache\Exception $e ) {
144
			$this->markTestSkipped( 'Please install Predis client first' );
145
		}
146
	}
147
}
148