1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive; |
4
|
|
|
|
5
|
|
|
use TraderInteractive\Util as Utility; |
6
|
|
|
use ErrorException; |
7
|
|
|
use Exception; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @coversDefaultClass \TraderInteractive\Util |
13
|
|
|
* @covers ::<private> |
14
|
|
|
*/ |
15
|
|
|
final class UtilTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @test |
19
|
|
|
* @covers ::getExceptionInfo |
20
|
|
|
*/ |
21
|
|
|
public function getExceptionInfo() |
22
|
|
|
{ |
23
|
|
|
$expectedLine = __LINE__ + 1; |
24
|
|
|
$result = Utility::getExceptionInfo(new Exception('a message', 42)); |
25
|
|
|
|
26
|
|
|
$this->assertTrue(strpos($result['trace'], 'getExceptionInfo') !== false); |
27
|
|
|
|
28
|
|
|
$expected = [ |
29
|
|
|
'type' => 'Exception', |
30
|
|
|
'message' => 'a message', |
31
|
|
|
'code' => 42, |
32
|
|
|
'file' => __FILE__, |
33
|
|
|
'line' => $expectedLine, |
34
|
|
|
'trace' => $result['trace'], |
35
|
|
|
]; |
36
|
|
|
$this->assertSame($expected, $result); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @test |
41
|
|
|
* @covers ::raiseException |
42
|
|
|
* @expectedException ErrorException |
43
|
|
|
*/ |
44
|
|
|
public function raiseExceptionThrowsErrorException() |
45
|
|
|
{ |
46
|
|
|
set_error_handler('\TraderInteractive\Util::raiseException'); |
47
|
|
|
trigger_error('test'); |
48
|
|
|
restore_error_handler(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
* @covers ::raiseException |
54
|
|
|
*/ |
55
|
|
|
public function raiseExceptionSetsExceptionPropertiesCorrectly() |
56
|
|
|
{ |
57
|
|
|
set_error_handler('\TraderInteractive\Util::raiseException'); |
58
|
|
|
try { |
59
|
|
|
trigger_error('test', E_USER_NOTICE); |
60
|
|
|
} catch (ErrorException $e) { |
|
|
|
|
61
|
|
|
$this->assertSame('test', $e->getMessage()); |
62
|
|
|
$this->assertSame(0, $e->getCode()); |
63
|
|
|
$this->assertSame(E_USER_NOTICE, $e->getSeverity()); |
64
|
|
|
$this->assertSame((__LINE__) - 5, $e->getLine()); |
65
|
|
|
$this->assertSame(__FILE__, $e->getFile()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
restore_error_handler(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
* @covers ::raiseException |
74
|
|
|
*/ |
75
|
|
|
public function raiseExceptionReturnsFalseIfErrorReportingDisabled() |
76
|
|
|
{ |
77
|
|
|
$restoreLevel = error_reporting(0); |
78
|
|
|
$this->assertFalse(Utility::raiseException(E_USER_NOTICE, 'test', __FILE__, __LINE__)); |
79
|
|
|
error_reporting($restoreLevel); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @test |
84
|
|
|
* @covers ::throwIfNotType |
85
|
|
|
*/ |
86
|
|
|
public function throwIfNotTypeBasicSuccess() |
87
|
|
|
{ |
88
|
|
|
Utility::throwIfNotType(['string' => ['string1', 'string2'], 'integer' => [1, 2], 'int' => 3, 'null' => null]); |
89
|
|
|
//Added for strict tests. throwIfNotType() throws on failure |
90
|
|
|
$this->assertTrue(true); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @test |
95
|
|
|
* @covers ::throwIfNotType |
96
|
|
|
* @expectedException InvalidArgumentException |
97
|
|
|
*/ |
98
|
|
|
public function throwIfNotTypeStringFailure() |
99
|
|
|
{ |
100
|
|
|
Utility::throwIfNotType(['string' => 2]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @test |
105
|
|
|
* @covers ::throwIfNotType |
106
|
|
|
* @expectedException InvalidArgumentException |
107
|
|
|
*/ |
108
|
|
|
public function throwIfNotTypeBoolFailure() |
109
|
|
|
{ |
110
|
|
|
Utility::throwIfNotType(['bool' => 2]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @test |
115
|
|
|
* @covers ::throwIfNotType |
116
|
|
|
* @expectedException InvalidArgumentException |
117
|
|
|
*/ |
118
|
|
|
public function throwIfNotTypeNullFailure() |
119
|
|
|
{ |
120
|
|
|
Utility::throwIfNotType(['null' => 2]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @test |
125
|
|
|
* @covers ::throwIfNotType |
126
|
|
|
* @expectedException InvalidArgumentException |
127
|
|
|
*/ |
128
|
|
|
public function throwIfNotTypeIntFailure() |
129
|
|
|
{ |
130
|
|
|
Utility::throwIfNotType(['int' => [1, 'not an int']]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @test |
135
|
|
|
* @covers ::throwIfNotType |
136
|
|
|
* @expectedException InvalidArgumentException |
137
|
|
|
*/ |
138
|
|
|
public function throwIfNotTypeNotStringTypeArg() |
139
|
|
|
{ |
140
|
|
|
Utility::throwIfNotType([1]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @test |
145
|
|
|
* @covers ::throwIfNotType |
146
|
|
|
* @expectedException InvalidArgumentException |
147
|
|
|
*/ |
148
|
|
|
public function throwIfNotTypeBadFunctionName() |
149
|
|
|
{ |
150
|
|
|
Utility::throwIfNotType(['FUNCTHATDOESNTEXIST' => 2]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @test |
155
|
|
|
* @covers ::throwIfNotType |
156
|
|
|
*/ |
157
|
|
|
public function throwIfNotTypeAllowNullsSuccess() |
158
|
|
|
{ |
159
|
|
|
Utility::throwIfNotType(['int' => [1, null], 'string' => null, 'bool' => null], false, true); |
160
|
|
|
//Added for strict tests. throwIfNotType() throws on failure |
161
|
|
|
$this->assertTrue(true); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @test |
166
|
|
|
* @covers ::throwIfNotType |
167
|
|
|
* @expectedException InvalidArgumentException |
168
|
|
|
*/ |
169
|
|
|
public function throwIfNotTypeWhitespaceFailure() |
170
|
|
|
{ |
171
|
|
|
Utility::throwIfNotType(['int' => 1, 'string' => ' '], true); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @test |
176
|
|
|
* @covers ::ensureNot |
177
|
|
|
*/ |
178
|
|
|
public function ensureNotSuccess() |
179
|
|
|
{ |
180
|
|
|
$this->assertTrue(Utility::ensureNot(false, is_string('boo'))); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @test |
185
|
|
|
* @covers ::ensureNot |
186
|
|
|
* @expectedException InvalidArgumentException |
187
|
|
|
*/ |
188
|
|
|
public function ensureNotBadArg() |
189
|
|
|
{ |
190
|
|
|
Utility::ensureNot(false, false, 1); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @test |
195
|
|
|
* @covers ::ensureNot |
196
|
|
|
* @expectedException Exception |
197
|
|
|
*/ |
198
|
|
|
public function ensureNotBaseException() |
199
|
|
|
{ |
200
|
|
|
Utility::ensureNot(false, is_string(1)); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @test |
205
|
|
|
* @covers ::ensureNot |
206
|
|
|
* @expectedException Exception |
207
|
|
|
* @expectedExceptionMessage bah |
208
|
|
|
*/ |
209
|
|
|
public function ensureNotUserMessage() |
210
|
|
|
{ |
211
|
|
|
Utility::ensureNot(false, is_string(1), 'bah'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @test |
216
|
|
|
* @covers ::ensureNot |
217
|
|
|
* @expectedException Exception |
218
|
|
|
* @expectedExceptionMessage bah |
219
|
|
|
*/ |
220
|
|
|
public function ensureNotDynamicException() |
221
|
|
|
{ |
222
|
|
|
Utility::ensureNot(false, is_string(1), 'Exception', ['bah']); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @test |
227
|
|
|
* @covers ::ensureNot |
228
|
|
|
* @expectedException \DominionEnterprises\HttpException |
229
|
|
|
* @expectedExceptionMessage bah |
230
|
|
|
* @expectedExceptionCode 404 |
231
|
|
|
*/ |
232
|
|
|
public function ensureNotDynamicExceptionWithAlias() |
233
|
|
|
{ |
234
|
|
|
Utility::ensureNot(false, is_string(1), 'http', ['bah', 404, 404]); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @test |
239
|
|
|
* @covers ::ensureNot |
240
|
|
|
* @expectedException Exception |
241
|
|
|
* @expectedExceptionMessage foo |
242
|
|
|
* @expectedExceptionCode 2 |
243
|
|
|
*/ |
244
|
|
|
public function ensureNotException() |
245
|
|
|
{ |
246
|
|
|
Utility::ensureNot(false, false, new Exception('foo', 2)); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @test |
251
|
|
|
* @covers ::ensure |
252
|
|
|
*/ |
253
|
|
|
public function ensureSuccess() |
254
|
|
|
{ |
255
|
|
|
$this->assertTrue(Utility::ensure(true, is_string('boo'))); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @test |
260
|
|
|
* @covers ::ensure |
261
|
|
|
* @expectedException InvalidArgumentException |
262
|
|
|
*/ |
263
|
|
|
public function ensureBadArg() |
264
|
|
|
{ |
265
|
|
|
Utility::ensure(false, true, 1); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @test |
270
|
|
|
* @covers ::ensure |
271
|
|
|
* @expectedException Exception |
272
|
|
|
*/ |
273
|
|
|
public function ensureBaseException() |
274
|
|
|
{ |
275
|
|
|
Utility::ensure(true, is_string(1)); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @test |
280
|
|
|
* @covers ::ensure |
281
|
|
|
* @expectedException Exception |
282
|
|
|
* @expectedExceptionMessage bah |
283
|
|
|
*/ |
284
|
|
|
public function ensureUserMessage() |
285
|
|
|
{ |
286
|
|
|
Utility::ensure(true, is_string(1), 'bah'); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @test |
291
|
|
|
* @covers ::ensure |
292
|
|
|
* @expectedException Exception |
293
|
|
|
* @expectedExceptionMessage bah |
294
|
|
|
*/ |
295
|
|
|
public function ensureDynamicException() |
296
|
|
|
{ |
297
|
|
|
Utility::ensure(true, is_string(1), 'Exception', ['bah']); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @test |
302
|
|
|
* @covers ::ensure |
303
|
|
|
* @expectedException \DominionEnterprises\HttpException |
304
|
|
|
* @expectedExceptionMessage bah |
305
|
|
|
* @expectedExceptionCode 404 |
306
|
|
|
*/ |
307
|
|
|
public function ensureDynamicExceptionWithAlias() |
308
|
|
|
{ |
309
|
|
|
Utility::ensure(true, is_string(1), 'http', ['bah', 404, 404]); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @test |
314
|
|
|
* @covers ::ensure |
315
|
|
|
* @expectedException Exception |
316
|
|
|
* @expectedExceptionMessage foo |
317
|
|
|
* @expectedExceptionCode 2 |
318
|
|
|
*/ |
319
|
|
|
public function ensureException() |
320
|
|
|
{ |
321
|
|
|
Utility::ensure(true, false, new Exception('foo', 2)); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @test |
326
|
|
|
* @covers ::setExceptionAliases |
327
|
|
|
* @covers ::getExceptionAliases |
328
|
|
|
*/ |
329
|
|
|
public function setExceptionAliasesGetSet() |
330
|
|
|
{ |
331
|
|
|
$exceptionAliases = ['shortNameOne' => 'fullNameOne', 'shortNameTwo' => 'fullNameTwo']; |
332
|
|
|
Utility::setExceptionAliases($exceptionAliases); |
333
|
|
|
$this->assertSame($exceptionAliases, Utility::getExceptionAliases()); |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|