UtilTest   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 319
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 88
dl 0
loc 319
rs 10
c 3
b 0
f 0
wmc 29

28 Methods

Rating   Name   Duplication   Size   Complexity  
A throwIfNotTypeNullFailure() 0 4 1
A throwIfNotTypeIntFailure() 0 4 1
A throwIfNotTypeNotStringTypeArg() 0 4 1
A throwIfNotTypeBadFunctionName() 0 4 1
A ensureSuccess() 0 3 1
A throwIfNotTypeBasicSuccess() 0 5 1
A getExceptionInfo() 0 16 1
A ensureNotSuccess() 0 3 1
A throwIfNotTypeAllowNullsSuccess() 0 5 1
A raiseExceptionSetsExceptionPropertiesCorrectly() 0 14 2
A raiseExceptionReturnsFalseIfErrorReportingDisabled() 0 5 1
A setExceptionAliasesGetSet() 0 5 1
A ensureNotDynamicExceptionWithAlias() 0 6 1
A throwIfNotTypeStringFailure() 0 4 1
A throwIfNotTypeWhitespaceFailure() 0 4 1
A ensureNotException() 0 6 1
A ensureDynamicException() 0 5 1
A ensureNotBaseException() 0 4 1
A ensureNotDynamicException() 0 5 1
A ensureNotBadArg() 0 4 1
A ensureNotUserMessage() 0 5 1
A ensureDynamicExceptionWithAlias() 0 6 1
A ensureBadArg() 0 4 1
A ensureBaseException() 0 4 1
A ensureUserMessage() 0 5 1
A throwIfNotTypeBoolFailure() 0 4 1
A raiseExceptionThrowsErrorException() 0 6 1
A ensureException() 0 6 1
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
     */
44
    public function raiseExceptionThrowsErrorException()
45
    {
46
        $this->expectException(ErrorException::class);
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());
0 ignored issues
show
Bug introduced by
The method getSeverity() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as ErrorException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
            $this->assertSame(E_USER_NOTICE, $e->/** @scrutinizer ignore-call */ getSeverity());
Loading history...
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
     */
98
    public function throwIfNotTypeStringFailure()
99
    {
100
        $this->expectException(InvalidArgumentException::class);
101
        Utility::throwIfNotType(['string' => 2]);
102
    }
103
104
    /**
105
     * @test
106
     * @covers ::throwIfNotType
107
     */
108
    public function throwIfNotTypeBoolFailure()
109
    {
110
        $this->expectException(InvalidArgumentException::class);
111
        Utility::throwIfNotType(['bool' => 2]);
112
    }
113
114
    /**
115
     * @test
116
     * @covers ::throwIfNotType
117
     */
118
    public function throwIfNotTypeNullFailure()
119
    {
120
        $this->expectException(InvalidArgumentException::class);
121
        Utility::throwIfNotType(['null' => 2]);
122
    }
123
124
    /**
125
     * @test
126
     * @covers ::throwIfNotType
127
     */
128
    public function throwIfNotTypeIntFailure()
129
    {
130
        $this->expectException(InvalidArgumentException::class);
131
        Utility::throwIfNotType(['int' => [1, 'not an int']]);
132
    }
133
134
    /**
135
     * @test
136
     * @covers ::throwIfNotType
137
     */
138
    public function throwIfNotTypeNotStringTypeArg()
139
    {
140
        $this->expectException(InvalidArgumentException::class);
141
        Utility::throwIfNotType([1]);
142
    }
143
144
    /**
145
     * @test
146
     * @covers ::throwIfNotType
147
     */
148
    public function throwIfNotTypeBadFunctionName()
149
    {
150
        $this->expectException(InvalidArgumentException::class);
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
     */
169
    public function throwIfNotTypeWhitespaceFailure()
170
    {
171
        $this->expectException(InvalidArgumentException::class);
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
     */
188
    public function ensureNotBadArg()
189
    {
190
        $this->expectException(InvalidArgumentException::class);
191
        Utility::ensureNot(false, false, 1);
192
    }
193
194
    /**
195
     * @test
196
     * @covers ::ensureNot
197
     */
198
    public function ensureNotBaseException()
199
    {
200
        $this->expectException(Exception::class);
201
        Utility::ensureNot(false, is_string(1));
202
    }
203
204
    /**
205
     * @test
206
     * @covers ::ensureNot
207
     */
208
    public function ensureNotUserMessage()
209
    {
210
        $this->expectException(Exception::class);
211
        $this->expectExceptionMessage('bah');
212
        Utility::ensureNot(false, is_string(1), 'bah');
213
    }
214
215
    /**
216
     * @test
217
     * @covers ::ensureNot
218
     */
219
    public function ensureNotDynamicException()
220
    {
221
        $this->expectException(Exception::class);
222
        $this->expectExceptionMessage('bah');
223
        Utility::ensureNot(false, is_string(1), 'Exception', ['bah']);
224
    }
225
226
    /**
227
     * @test
228
     * @covers ::ensureNot
229
     */
230
    public function ensureNotDynamicExceptionWithAlias()
231
    {
232
        $this->expectException(\TraderInteractive\HttpException::class);
233
        $this->expectExceptionMessage('bah');
234
        $this->expectExceptionCode('404');
235
        Utility::ensureNot(false, is_string(1), 'http', ['bah', 404, 404]);
236
    }
237
238
    /**
239
     * @test
240
     * @covers ::ensureNot
241
     */
242
    public function ensureNotException()
243
    {
244
        $this->expectException(Exception::class);
245
        $this->expectExceptionMessage('foo');
246
        $this->expectExceptionCode('2');
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
     */
263
    public function ensureBadArg()
264
    {
265
        $this->expectException(InvalidArgumentException::class);
266
        Utility::ensure(false, true, 1);
267
    }
268
269
    /**
270
     * @test
271
     * @covers ::ensure
272
     */
273
    public function ensureBaseException()
274
    {
275
        $this->expectException(Exception::class);
276
        Utility::ensure(true, is_string(1));
277
    }
278
279
    /**
280
     * @test
281
     * @covers ::ensure
282
     */
283
    public function ensureUserMessage()
284
    {
285
        $this->expectException(Exception::class);
286
        $this->expectExceptionMessage('bah');
287
        Utility::ensure(true, is_string(1), 'bah');
288
    }
289
290
    /**
291
     * @test
292
     * @covers ::ensure
293
     */
294
    public function ensureDynamicException()
295
    {
296
        $this->expectException(Exception::class);
297
        $this->expectExceptionMessage('bah');
298
        Utility::ensure(true, is_string(1), 'Exception', ['bah']);
299
    }
300
301
    /**
302
     * @test
303
     * @covers ::ensure
304
     */
305
    public function ensureDynamicExceptionWithAlias()
306
    {
307
        $this->expectException(Exception::class);
308
        $this->expectExceptionMessage('bah');
309
        $this->expectExceptionCode('404');
310
        Utility::ensure(true, is_string(1), 'http', ['bah', 404, 404]);
311
    }
312
313
    /**
314
     * @test
315
     * @covers ::ensure
316
     */
317
    public function ensureException()
318
    {
319
        $this->expectException(Exception::class);
320
        $this->expectExceptionMessage('foo');
321
        $this->expectExceptionCode('2');
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