Skip to content

Commit 6ca6861

Browse files
authored
Merge pull request #67 from chadicus/master
Allow Error objects to be used with Util::ensure
2 parents c72024d + 8634cbe commit 6ca6861

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Util.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static function ensureNot($valueToThrowOn, $valueToCheck, $exception = nu
126126
*/
127127
private static function buildException($exception, array $exceptionArgs = null) : Throwable
128128
{
129-
if ($exception instanceof Exception) {
129+
if ($exception instanceof Throwable) {
130130
return $exception;
131131
}
132132

tests/UtilTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace TraderInteractive;
44

5+
use Error;
56
use Throwable;
67
use TraderInteractive\Util as Utility;
78
use ErrorException;
89
use Exception;
910
use InvalidArgumentException;
1011
use PHPUnit\Framework\TestCase;
12+
use TypeError;
1113

1214
/**
1315
* @coversDefaultClass \TraderInteractive\Util
@@ -256,6 +258,16 @@ public function ensureSuccess()
256258
$this->assertTrue(Utility::ensure(true, is_string('boo')));
257259
}
258260

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+
259271
/**
260272
* @test
261273
* @covers ::ensure
@@ -322,6 +334,18 @@ public function ensureException()
322334
Utility::ensure(true, false, new Exception('foo', 2));
323335
}
324336

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+
325349
/**
326350
* @test
327351
* @covers ::setExceptionAliases

0 commit comments

Comments
 (0)