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\Options; |
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\Options unit test for the singleton methods. |
37
|
|
|
* |
38
|
|
|
* @coversDefaultClass \Mundschenk\Data_Storage\Options |
39
|
|
|
* @usesDefaultClass \Mundschenk\Data_Storage\Options |
40
|
|
|
* |
41
|
|
|
* @uses ::__construct |
42
|
|
|
* @uses \Mundschenk\Data_Storage\Abstract_Cache::__construct |
43
|
|
|
*/ |
44
|
|
|
class Options_Test extends TestCase { |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Test fixture. |
48
|
|
|
* |
49
|
|
|
* @var \Mundschenk\Data_Storage\Cache |
50
|
|
|
*/ |
51
|
|
|
protected $options; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sets up the fixture, for example, opens a network connection. |
55
|
|
|
* This method is called before a test is executed. |
56
|
|
|
*/ |
57
|
|
|
protected function setUp() { // @codingStandardsIgnoreLine |
58
|
|
|
$this->options = m::mock( Options::class )->makePartial(); |
59
|
|
|
|
60
|
|
|
parent::setUp(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Necesssary clean-up work. |
65
|
|
|
*/ |
66
|
|
|
protected function tearDown() { // @codingStandardsIgnoreLine |
67
|
|
|
parent::tearDown(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Tests constructor. |
72
|
|
|
* |
73
|
|
|
* @covers ::__construct |
74
|
|
|
* |
75
|
|
|
* @uses \Mundschenk\Data_Storage\Abstract_Cache::__construct |
76
|
|
|
*/ |
77
|
|
|
public function test___construct() { |
78
|
|
|
$cache = m::mock( Options::class, [] )->makePartial(); |
79
|
|
|
|
80
|
|
|
$this->assertInstanceOf( Options::class, $cache ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Tests get. |
85
|
|
|
* |
86
|
|
|
* @covers ::get |
87
|
|
|
* |
88
|
|
|
* @uses ::get_name |
89
|
|
|
*/ |
90
|
|
|
public function test_get() { |
91
|
|
|
$raw_key = 'foo'; |
92
|
|
|
$key = $this->options->get_name( $raw_key ); |
93
|
|
|
$default = 'something'; |
94
|
|
|
|
95
|
|
|
Functions\expect( 'get_option' )->once()->with( $key, $default )->andReturn( 'bar' ); |
96
|
|
|
|
97
|
|
|
$this->assertSame( 'bar', $this->options->get( $raw_key, $default ) ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Tests get. |
102
|
|
|
* |
103
|
|
|
* @covers ::get |
104
|
|
|
* |
105
|
|
|
* @uses ::get_name |
106
|
|
|
*/ |
107
|
|
|
public function test_get_missing_array() { |
108
|
|
|
$raw_key = 'foo'; |
109
|
|
|
$key = $this->options->get_name( $raw_key ); |
110
|
|
|
$default = []; |
111
|
|
|
|
112
|
|
|
Functions\expect( 'get_option' )->once()->with( $key, $default )->andReturn( '' ); |
113
|
|
|
|
114
|
|
|
$this->assertSame( [], $this->options->get( $raw_key, $default ) ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Tests get. |
119
|
|
|
* |
120
|
|
|
* @covers ::get |
121
|
|
|
*/ |
122
|
|
|
public function test_get_raw() { |
123
|
|
|
$raw_key = 'foo'; |
124
|
|
|
$default = 'something'; |
125
|
|
|
|
126
|
|
|
Functions\expect( 'get_option' )->once()->with( $raw_key, $default )->andReturn( 'bar' ); |
127
|
|
|
$this->options->shouldReceive( 'get_name' )->never(); |
128
|
|
|
|
129
|
|
|
$this->assertSame( 'bar', $this->options->get( $raw_key, $default, true ) ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Tests delete. |
134
|
|
|
* |
135
|
|
|
* @covers ::delete |
136
|
|
|
* |
137
|
|
|
* @uses ::get_name |
138
|
|
|
*/ |
139
|
|
|
public function test_delete() { |
140
|
|
|
$raw_key = 'foo'; |
141
|
|
|
$key = $this->options->get_name( $raw_key ); |
142
|
|
|
|
143
|
|
|
Functions\expect( 'delete_option' )->once()->with( $key )->andReturn( true ); |
144
|
|
|
|
145
|
|
|
$this->assertTrue( $this->options->delete( $raw_key ) ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Tests delete. |
150
|
|
|
* |
151
|
|
|
* @covers ::delete |
152
|
|
|
*/ |
153
|
|
|
public function test_delete_raw() { |
154
|
|
|
$raw_key = 'foo'; |
155
|
|
|
|
156
|
|
|
Functions\expect( 'delete_option' )->once()->with( $raw_key )->andReturn( true ); |
157
|
|
|
$this->options->shouldReceive( 'get_name' )->never(); |
158
|
|
|
|
159
|
|
|
$this->assertTrue( $this->options->delete( $raw_key, true ) ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Tests set. |
164
|
|
|
* |
165
|
|
|
* @covers ::set |
166
|
|
|
* |
167
|
|
|
* @uses ::get_name |
168
|
|
|
*/ |
169
|
|
|
public function test_set() { |
170
|
|
|
$value = 'bar'; |
171
|
|
|
$raw_key = 'foo'; |
172
|
|
|
$key = $this->options->get_name( $raw_key ); |
173
|
|
|
|
174
|
|
|
Functions\expect( 'update_option' )->once()->with( $key, $value, true )->andReturn( true ); |
175
|
|
|
|
176
|
|
|
$this->assertTrue( $this->options->set( $raw_key, $value ) ); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Tests set. |
181
|
|
|
* |
182
|
|
|
* @covers ::set |
183
|
|
|
*/ |
184
|
|
|
public function test_set_raw() { |
185
|
|
|
$value = 'bar'; |
186
|
|
|
$raw_key = 'foo'; |
187
|
|
|
|
188
|
|
|
Functions\expect( 'update_option' )->once()->with( $raw_key, $value, true )->andReturn( true ); |
189
|
|
|
$this->options->shouldReceive( 'get_name' )->never(); |
190
|
|
|
|
191
|
|
|
$this->assertTrue( $this->options->set( $raw_key, $value, true, true ) ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Tests get_name. |
196
|
|
|
* |
197
|
|
|
* @covers ::get_name |
198
|
|
|
*/ |
199
|
|
|
public function test_get_name() { |
200
|
|
|
$raw_key = 'foo'; |
201
|
|
|
|
202
|
|
|
$this->assertSame( Options::PREFIX . "_{$raw_key}", $this->options->get_name( $raw_key ) ); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|