Completed
Push — master ( 8a10f5...330c1d )
by Der Mundschenk
02:58
created

Cache_Test   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 7
1
<?php
2
/**
3
 * This file is part of mundschenk-at/wp-data-storage.
4
 *
5
 * Copyright 2017-2018 Peter Putzer.
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or ( at your option ) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 *
21
 * @package mundschenk-at/wp-data-storage/tests
22
 * @license http://www.gnu.org/licenses/gpl-2.0.html
23
 */
24
25
namespace Mundschenk\Data_Storage\Tests;
26
27
use Mundschenk\Data_Storage\Cache;
28
29
use Brain\Monkey\Actions;
30
use Brain\Monkey\Filters;
31
use Brain\Monkey\Functions;
32
33
use Mockery as m;
34
35
/**
36
 * Mundschenk\Data_Storage\Cache unit test for the singleton methods.
37
 *
38
 * @coversDefaultClass \Mundschenk\Data_Storage\Cache
39
 * @usesDefaultClass \Mundschenk\Data_Storage\Cache
40
 *
41
 * @uses ::__construct
42
 * @uses \Mundschenk\Data_Storage\Abstract_Cache::__construct
43
 */
44
class Cache_Test extends TestCase {
45
46
	const PREFIX          = 'my_prefix_';
47
	const INCREMENTOR_KEY = self::PREFIX . 'cache_incrementor';
48
	const GROUP           = 'some_group';
49
	/**
50
	 * Test fixture.
51
	 *
52
	 * @var \Mundschenk\Data_Storage\Cache
53
	 */
54
	protected $cache;
55
56
	/**
57
	 * Sets up the fixture, for example, opens a network connection.
58
	 * This method is called before a test is executed.
59
	 */
60
	protected function setUp() { // @codingStandardsIgnoreLine
61
		parent::setUp();
62
63
		Functions\expect( 'wp_cache_get' )->once()->with( self::INCREMENTOR_KEY, self::GROUP )->andReturn( 1 );
64
		$this->cache = m::mock( Cache::class, [ self::PREFIX, self::GROUP ] )->makePartial();
65
	}
66
67
	/**
68
	 * Necesssary clean-up work.
69
	 */
70
	protected function tearDown() { // @codingStandardsIgnoreLine
71
		parent::tearDown();
72
	}
73
74
	/**
75
	 * Tests constructor.
76
	 *
77
	 * @covers ::__construct
78
	 *
79
	 * @uses \Mundschenk\Data_Storage\Abstract_Cache::__construct
80
	 */
81
	public function test___construct() {
82
		Functions\expect( 'wp_cache_get' )->once()->with( 'some_prefix_cache_incrementor', self::GROUP )->andReturn( 0 );
83
84
		$cache = m::mock( Cache::class )->makePartial()
85
			->shouldReceive( 'invalidate' )->once()
86
			->getMock();
87
88
		$cache->__construct( 'some_prefix_', self::GROUP );
89
90
		$this->assertInstanceOf( Cache::class, $cache );
91
	}
92
93
	/**
94
	 * Tests invalidate.
95
	 *
96
	 * @covers ::invalidate
97
	 */
98
	public function test_invalidate() {
99
		Functions\expect( 'wp_cache_set' )->once()->with( self::INCREMENTOR_KEY, m::type( 'int' ), self::GROUP, 0 );
100
101
		$this->cache->invalidate();
102
103
		$this->assertTrue( true );
104
	}
105
106
	/**
107
	 * Tests get.
108
	 *
109
	 * @covers ::get
110
	 *
111
	 * @uses ::get_key
112
	 */
113
	public function test_get() {
114
		$raw_key = 'foo';
115
		$key     = $this->invokeMethod( $this->cache, 'get_key', [ $raw_key ] );
116
117
		Functions\expect( 'wp_cache_get' )->once()->with( $key, self::GROUP, false, null )->andReturn( 'bar' );
118
119
		$this->assertSame( 'bar', $this->cache->get( $raw_key ) );
120
	}
121
122
	/**
123
	 * Tests set.
124
	 *
125
	 * @covers ::set
126
	 *
127
	 * @uses ::get_key
128
	 */
129
	public function test_set() {
130
		$value    = 'bar';
131
		$raw_key  = 'foo';
132
		$duration = 99;
133
		$key      = $this->invokeMethod( $this->cache, 'get_key', [ $raw_key ] );
134
135
		Functions\expect( 'wp_cache_set' )->once()->with( $key, $value, self::GROUP, $duration )->andReturn( true );
136
137
		$this->assertTrue( $this->cache->set( $raw_key, $value, $duration ) );
138
	}
139
140
	/**
141
	 * Tests delete.
142
	 *
143
	 * @covers ::delete
144
	 *
145
	 * @uses ::get_key
146
	 */
147
	public function test_delete() {
148
		$raw_key = 'foo';
149
		$key     = $this->invokeMethod( $this->cache, 'get_key', [ $raw_key ] );
150
151
		Functions\expect( 'wp_cache_delete' )->once()->with( $key, self::GROUP )->andReturn( true );
152
153
		$this->assertTrue( $this->cache->delete( $raw_key ) );
154
	}
155
}
156