Completed
Pull Request — master (#57)
by
unknown
01:17
created

UtilTest::getExceptionInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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