Skip to content

Commit 539d2e4

Browse files
committed
up: add new assert isResource check
1 parent 13025d1 commit 539d2e4

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Helper/Assert.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use LogicException;
77
use function in_array;
88
use function is_dir;
9+
use function is_file;
10+
use function is_resource;
911

1012
/**
1113
* class Assert
@@ -285,6 +287,19 @@ public static function isFile(string $path, string $errMsg = ''): void
285287
}
286288
}
287289

290+
/**
291+
* @param mixed|resource $res
292+
* @param string $errMsg
293+
*
294+
* @return void
295+
*/
296+
public static function isResource($res, string $errMsg = ''): void
297+
{
298+
if (!is_resource($res)) {
299+
throw static::createEx($errMsg ?: "Excepted an resource");
300+
}
301+
}
302+
288303
// ------------- helper methods -------------
289304

290305
/**

test/Helper/AssertTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Toolkit\Stdlib\Helper\Assert;
66
use Toolkit\StdlibTest\BaseLibTestCase;
7+
use const STDOUT;
78

89
/**
910
* class AssertTest
@@ -69,5 +70,11 @@ public function testAssert_FS(): void
6970

7071
$e = $this->tryCatchRun(fn() => Assert::isDir('./not-exists'));
7172
$this->assertException($e, 'No such dir: ./not-exists');
73+
74+
$e = $this->tryCatchRun(fn() => Assert::isResource('invalid'));
75+
$this->assertException($e, 'Excepted an resource');
76+
77+
$e = $this->tryCatchRun(fn() => Assert::isResource(STDOUT));
78+
$this->assertException($e, 'NO ERROR', -1);
7279
}
7380
}

0 commit comments

Comments
 (0)