-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathContextTest.php
More file actions
286 lines (232 loc) · 9.6 KB
/
ContextTest.php
File metadata and controls
286 lines (232 loc) · 9.6 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
declare(strict_types=1);
namespace ShopifyTest;
use Psr\Log\LogLevel;
use ReflectionClass;
use Shopify\ApiVersion;
use Shopify\Auth\Scopes;
use Shopify\Context;
use Shopify\Exception\FeatureDeprecatedException;
use Shopify\Utils;
use ShopifyTest\Auth\MockSessionStorage;
final class ContextTest extends BaseTestCase
{
public function testCanCreateContext()
{
Context::initialize(
apiKey: 'ash',
apiSecretKey: 'steffi',
scopes: ['sleepy', 'kitty'],
hostName: 'my-friends-cats',
sessionStorage: new MockSessionStorage(),
);
$this->assertEquals('ash', Context::$API_KEY);
$this->assertEquals('steffi', Context::$API_SECRET_KEY);
$this->assertEquals(new Scopes(['sleepy', 'kitty']), Context::$SCOPES);
$this->assertEquals('my-friends-cats', Context::$HOST_NAME);
$this->assertEquals('https', Context::$HOST_SCHEME);
// This should not trigger the exception
Context::throwIfUninitialized();
// This should not trigger the exception
Context::throwIfPrivateApp('Not supposed to happen!');
}
// Context with different values has been set up in BaseTestCase
public function testCanUpdateContext()
{
Context::initialize(
apiKey: 'tuck',
apiSecretKey: 'rocky',
scopes: ['silly', 'doggo'],
hostName: 'yay-for-doggos',
sessionStorage: new MockSessionStorage(),
);
$this->assertEquals('tuck', Context::$API_KEY);
$this->assertEquals('rocky', Context::$API_SECRET_KEY);
$this->assertEquals(new Scopes(['silly', 'doggo']), Context::$SCOPES);
$this->assertEquals('yay-for-doggos', Context::$HOST_NAME);
}
public function testThrowsIfMissingArguments()
{
$this->expectException(\Shopify\Exception\MissingArgumentException::class);
$this->expectExceptionMessage(
'Cannot initialize Shopify API Library. Missing values for: apiKey, apiSecretKey, scopes, hostName'
);
Context::initialize(
apiKey: '',
apiSecretKey: '',
scopes: [],
hostName: '',
sessionStorage: new MockSessionStorage(),
);
}
public function testThrowsIfUninitialized()
{
// ReflectionClass is used in this test as IS_INITIALIZED is a private static variable,
// which would have been set as true due to previous tests
$reflectedContext = new ReflectionClass('Shopify\Context');
$reflectedIsInitialized = $reflectedContext->getProperty('IS_INITIALIZED');
$reflectedIsInitialized->setAccessible(true);
$reflectedIsInitialized->setValue(false);
$this->expectException(\Shopify\Exception\UninitializedContextException::class);
Context::throwIfUninitialized();
}
public function testThrowsIfPrivateApp()
{
Context::initialize(
apiKey: 'ash',
apiSecretKey: 'steffi',
scopes: ['sleepy', 'kitty'],
hostName: 'my-friends-cats',
sessionStorage: new MockSessionStorage(),
apiVersion: 'unstable',
isPrivateApp: true,
);
$this->expectException(\Shopify\Exception\PrivateAppException::class);
$this->expectExceptionMessage('BOOOOOO');
Context::throwIfPrivateApp('BOOOOOO');
}
public function testCanAddOverrideLogger()
{
$testLogger = new LogMock();
Context::log('Logging something!', LogLevel::DEBUG);
$this->assertEmpty($testLogger->records);
Context::$LOGGER = $testLogger;
Context::log('Defaults to info');
$this->assertTrue($testLogger->hasInfo('Defaults to info'));
Context::log('Debug log', LogLevel::DEBUG);
$this->assertTrue($testLogger->hasDebug('Debug log'));
Context::log('Info log', LogLevel::INFO);
$this->assertTrue($testLogger->hasInfo('Info log'));
Context::log('Notice log', LogLevel::NOTICE);
$this->assertTrue($testLogger->hasNotice('Notice log'));
Context::log('Warning log', LogLevel::WARNING);
$this->assertTrue($testLogger->hasWarning('Warning log'));
Context::log('Err log', LogLevel::ERROR);
$this->assertTrue($testLogger->hasError('Err log'));
Context::log('Crit log', LogLevel::CRITICAL);
$this->assertTrue($testLogger->hasCritical('Crit log'));
Context::log('Alert log', LogLevel::ALERT);
$this->assertTrue($testLogger->hasAlert('Alert log'));
Context::log('Emerg log', LogLevel::EMERGENCY);
$this->assertTrue($testLogger->hasEmergency('Emerg log'));
}
public function testLogHelper()
{
$testLogger = new LogMock();
Context::$LOGGER = $testLogger;
Context::logDebug('Debug log');
$this->assertTrue($testLogger->hasDebug('Debug log'));
Context::logInfo('Info log');
$this->assertTrue($testLogger->hasInfo('Info log'));
Context::logNotice('Notice log');
$this->assertTrue($testLogger->hasNotice('Notice log'));
Context::logWarning('Warning log');
$this->assertTrue($testLogger->hasWarning('Warning log'));
Context::logError('Err log');
$this->assertTrue($testLogger->hasError('Err log'));
Context::logCritical('Crit log');
$this->assertTrue($testLogger->hasCritical('Crit log'));
Context::logAlert('Alert log');
$this->assertTrue($testLogger->hasAlert('Alert log'));
Context::logEmergency('Emerg log');
$this->assertTrue($testLogger->hasEmergency('Emerg log'));
}
public function testLogDeprecation()
{
$testLogger = new LogMock();
Context::$LOGGER = $testLogger;
Context::logDeprecation('9999.8888.7777', 'This message should be logged.');
$this->assertTrue($testLogger->hasWarning('This message should be logged.'));
$record = $testLogger->recordsByLevel[LogLevel::WARNING][0];
$this->assertArrayHasKey('context', $record);
$this->assertArrayHasKey('current_version', $record['context']);
$this->assertArrayHasKey('deprecated_from', $record['context']);
$this->assertEquals(Utils::getVersion(), $record['context']['current_version']);
$this->assertEquals('9999.8888.7777', $record['context']['deprecated_from']);
}
public function testLogDeprecationFeatureDeprecatedExceptionText()
{
$this->expectException(FeatureDeprecatedException::class);
$this->expectExceptionMessage('Feature was deprecated in version 1.0.0');
Context::logDeprecation('1.0.0', 'This message should not be logged because we trigger an exception first.');
}
public function testLogDeprecationVersionExceptionText()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Encountered an invalid version: "abc"');
Context::logDeprecation('abc', 'This message should not be logged.');
}
public function testLogDeprecationExceptionTooComplexVersion()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Encountered an invalid version: "1.2.3-RC"');
Context::logDeprecation('1.2.3-RC', 'This message should not be logged.');
}
/**
* @dataProvider canSetHostSchemeProvider
*/
public function testCanSetHostScheme($host, $expectedScheme, $expectedHost)
{
Context::initialize(
apiKey: 'ash',
apiSecretKey: 'steffi',
scopes: ['sleepy', 'kitty'],
hostName: $host,
sessionStorage: new MockSessionStorage(),
);
$this->assertEquals($expectedHost, Context::$HOST_NAME);
$this->assertEquals($expectedScheme, Context::$HOST_SCHEME);
}
public function canSetHostSchemeProvider()
{
return [
['my-friends-cats.io', 'https', 'my-friends-cats.io'],
['https://my-friends-cats.io', 'https', 'my-friends-cats.io'],
['http://my-friends-cats.io', 'http', 'my-friends-cats.io'],
['http://localhost', 'http', 'localhost'],
['http://localhost:1234', 'http', 'localhost:1234'],
];
}
public function testFailsOnInvalidHost()
{
$this->expectException(\Shopify\Exception\InvalidArgumentException::class);
Context::initialize(
apiKey: 'ash',
apiSecretKey: 'steffi',
scopes: ['sleepy', 'kitty'],
hostName: 'not-a-host-!@#$%^&*()',
sessionStorage: new MockSessionStorage(),
);
}
public function testCanSetCustomShopDomains()
{
$domains = ['*.special-domain-1.io', '*.special-domain-2.io'];
Context::initialize(
apiKey: 'ash',
apiSecretKey: 'steffi',
scopes: ['sleepy', 'kitty'],
hostName: 'my-friends-cats',
sessionStorage: new MockSessionStorage(),
apiVersion: ApiVersion::LATEST,
isPrivateApp: false,
customShopDomains: $domains,
);
$this->assertEquals($domains, Context::$CUSTOM_SHOP_DOMAINS);
}
public function testCanSetMaxTries()
{
$maxTries = 5;
Context::initialize(
apiKey: 'ash',
apiSecretKey: 'steffi',
scopes: ['sleepy', 'kitty'],
hostName: 'my-friends-cats',
sessionStorage: new MockSessionStorage(),
apiVersion: ApiVersion::LATEST,
isPrivateApp: false,
customShopDomains: [],
maxTries: $maxTries,
);
$this->assertEquals($maxTries, Context::$MAX_TRIES);
}
}