-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBaseServerConfigTest.php
More file actions
265 lines (217 loc) · 8.05 KB
/
BaseServerConfigTest.php
File metadata and controls
265 lines (217 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace OCA\Solid;
use OCA\Solid\AppInfo\Application;
use OCP\IConfig;
use PHPUnit\Framework\TestCase;
use TypeError;
function random_bytes()
{
return BaseServerConfigTest::MOCK_RANDOM_BYTES;
}
/**
* @coversDefaultClass \OCA\Solid\BaseServerConfig
* @uses \OCA\Solid\BaseServerConfig
* @covers ::__construct
*/
class BaseServerConfigTest extends TestCase
{
////////////////////////////////// FIXTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public const MOCK_RANDOM_BYTES = 'mock random bytes';
private const MOCK_REDIRECT_URI = 'mock redirect uri';
private const MOCK_CLIENT_ID = 'mock-client-id';
private const MOCK_ORIGIN = 'mock origin';
/////////////////////////////////// TESTS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
/**
* @testdox BaseServerConfig should complain when called before given a Configuration
* @covers ::__construct
*/
public function testConstructorWithoutConfig()
{
$this->expectException(TypeError::class);
$this->expectExceptionMessage('Too few arguments to function');
new BaseServerConfig();
}
/**
* @testdox BaseServerConfig should be instantiated when given a valid Configuration
* @covers ::__construct
*/
public function testConstructorWithValidConfig()
{
$configMock = $this->createMock(IConfig::class);
$baseServerConfig = new BaseServerConfig($configMock);
$this->assertInstanceOf(BaseServerConfig::class, $baseServerConfig);
}
/**
* @testdox BaseServerConfig should return a boolean when asked whether UserSubDomains are Enabled
* @covers ::getUserSubDomainsEnabled
* @covers ::castToBool
* @dataProvider provideBooleans
*/
public function testGetUserSubDomainsEnabled($value, $expected)
{
$configMock = $this->createMock(IConfig::class);
$configMock->method('getAppValue')->willReturn($value);
$baseServerConfig = new BaseServerConfig($configMock);
$actual = $baseServerConfig->getUserSubDomainsEnabled();
$this->assertEquals($expected, $actual);
}
/**
* @testdox BaseServerConfig should get value from AppConfig when asked whether UserSubDomains are Enabled
* @covers ::getUserSubDomainsEnabled
*/
public function testGetUserSubDomainsEnabledFromAppConfig()
{
$configMock = $this->createMock(IConfig::class);
$configMock->expects($this->atLeast(1))
->method('getAppValue')
->with(Application::APP_ID, 'userSubDomainsEnabled', false)
->willReturn(true);
$baseServerConfig = new BaseServerConfig($configMock);
$actual = $baseServerConfig->getUserSubDomainsEnabled();
$this->assertTrue($actual);
}
/**
* @testdox BaseServerConfig should set value in AppConfig when asked to set UserSubDomainsEnabled
* @covers ::setUserSubDomainsEnabled
* @covers ::castToBool
*
* @dataProvider provideBooleans
*/
public function testSetUserSubDomainsEnabled($value, $expected)
{
$configMock = $this->createMock(IConfig::class);
$configMock->expects($this->atLeast(1))
->method('setAppValue')
->with(Application::APP_ID, 'userSubDomainsEnabled', $expected)
;
$baseServerConfig = new BaseServerConfig($configMock);
$baseServerConfig->setUserSubDomainsEnabled($value);
}
/**
* @testdox BaseServerConfig should retrieve client ID AppValue when asked to GetClientRegistration for existing client
* @covers ::getClientRegistration
*/
public function testGetClientRegistrationForExistingClient()
{
$configMock = $this->createMock(IConfig::class);
$baseServerConfig = new BaseServerConfig($configMock);
$expected = ['mock' => 'client'];
$configMock->expects($this->once())
->method('getAppValue')
->with(Application::APP_ID, 'client-' . self::MOCK_CLIENT_ID)
->willReturn(json_encode($expected));
$actual = $baseServerConfig->getClientRegistration(self::MOCK_CLIENT_ID);
$this->assertEquals($expected, $actual);
}
/**
* @testdox BaseServerConfig should return empty array when asked to GetClientRegistration for non-existing client
* @covers ::getClientRegistration
*/
public function testGetClientRegistrationForNonExistingClient()
{
$configMock = $this->createMock(IConfig::class);
$baseServerConfig = new BaseServerConfig($configMock);
$expected = [];
$configMock->expects($this->once())
->method('getAppValue')
->with(Application::APP_ID, 'client-' . self::MOCK_CLIENT_ID)
->willReturnArgument(2);
$actual = $baseServerConfig->getClientRegistration(self::MOCK_CLIENT_ID);
$this->assertEquals($expected, $actual);
}
/**
* @testdox BaseServerConfig should complain when asked to save ClientRegistration without client data
* @covers ::saveClientRegistration
*/
public function testSaveClientRegistrationWithoutClientData()
{
$this->expectException(TypeError::class);
$this->expectExceptionMessage('Too few arguments to function');
$configMock = $this->createMock(IConfig::class);
$baseServerConfig = new BaseServerConfig($configMock);
$baseServerConfig->saveClientRegistration();
}
/**
* @testdox BaseServerConfig should save ClientRegistration when asked to save ClientRegistration for new client
* @covers ::saveClientRegistration
*/
public function testSaveClientRegistrationForNewClient()
{
$configMock = $this->createMock(IConfig::class);
$configMock->expects($this->once())
->method('getAppValue')
->willReturnArgument(2);
$configMock->expects($this->exactly(1))
->method('setAppValue')
->willReturnMap([
// Using willReturnMap as withConsecutive is removed since PHPUnit 10
[Application::APP_ID, 'client-' . self::MOCK_ORIGIN, json_encode('{}')]
]);
$baseServerConfig = new BaseServerConfig($configMock);
$actual = $baseServerConfig->saveClientRegistration([]);
$this->assertArrayHasKey('client_id', $actual);
$this->assertArrayHasKey('client_secret', $actual);
$this->assertArrayHasKey('client_name', $actual);
}
/**
* @testdox BaseServerConfig should remove ClientRegistration when asked to remove ClientRegistration
* @covers ::removeClientRegistration
*/
public function testRemoveClientRegistration()
{
$configMock = $this->createMock(IConfig::class);
$baseServerConfig = new BaseServerConfig($configMock);
$configMock->expects($this->once())
->method('deleteAppValue')
->with(Application::APP_ID, 'client-' . self::MOCK_CLIENT_ID);
$baseServerConfig->removeClientRegistration(self::MOCK_CLIENT_ID);
}
/**
* @testdox BaseServerConfig should return decoded trusted apps when asked to GetTrustedApps
* @covers ::getTrustedApps
*/
public function testGetTrustedApps()
{
$configMock = $this->createMock(IConfig::class);
$baseServerConfig = new BaseServerConfig($configMock);
$expected = [self::MOCK_ORIGIN];
$configMock->expects($this->once())
->method('getAppValue')
->with(Application::APP_ID, 'trustedApps', '[]')
->willReturn(json_encode($expected));
$actual = $baseServerConfig->getTrustedApps();
$this->assertEquals($expected, $actual);
}
/////////////////////////////// DATAPROVIDERS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public function provideBooleans()
{
return [
// Only 'boolean', 'NULL', 'integer', 'string' are allowed
// @TODO: Add test for type that trigger a TypeError:
// - array
// - callable
// - float
// - object
// - resource
// @TODO: Add test for values that trigger a TypeError
// 'integer:-1' => ['value'=> -1],
// 'integer:2' => ['value'=> 2],
// 'string:-1' => ['value'=> '-1'],
// 'string:2' => ['value'=> '2'],
// 'string:foo' => ['value'=> 'foo'],
// 'string:NULL' => ['value'=> 'NULL'],
'boolean:false' => ['value'=> false, 'expected' => false],
'boolean:true' => ['value'=> true, 'expected' => true],
'integer:0' => ['value'=> 0, 'expected' => false],
'integer:1' => ['value'=> 1, 'expected' => true],
'NULL' => ['value'=> null, 'expected' => false],
'string:0' => ['value'=> '0', 'expected' => false],
'string:1' => ['value'=> '1', 'expected' => true],
'string:empty' => ['value'=> '', 'expected' => false],
'string:false' => ['value'=> 'false', 'expected' => false],
'string:FALSE' => ['value'=> 'FALSE', 'expected' => false],
'string:true' => ['value'=> 'true', 'expected' => true],
'string:TRUE' => ['value'=> 'TRUE', 'expected' => true],
];
}
}