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