Skip to content

Commit beeb28e

Browse files
committed
prof: add more unit tests for optional, assert
1 parent 78d8185 commit beeb28e

File tree

3 files changed

+108
-16
lines changed

3 files changed

+108
-16
lines changed

src/Helper/BasePhpTestCase.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,19 @@ protected static function getMethod(string|object $class, string $method): Refle
4848
* @param callable $cb
4949
*
5050
* @return Throwable
51+
* @deprecated please use tryCatchRun()
5152
*/
5253
protected function runAndGetException(callable $cb): Throwable
54+
{
55+
return $this->tryCatchRun($cb);
56+
}
57+
58+
/**
59+
* @param callable $cb
60+
*
61+
* @return Throwable
62+
*/
63+
protected function tryCatchRun(callable $cb): Throwable
5364
{
5465
try {
5566
$cb();
@@ -59,4 +70,20 @@ protected function runAndGetException(callable $cb): Throwable
5970

6071
return new RuntimeException('NO ERROR', -1);
6172
}
73+
74+
/**
75+
* @param Throwable $e
76+
* @param string $msg
77+
* @param int|null $code
78+
*
79+
* @return void
80+
*/
81+
public function assertException(Throwable $e, string $msg, int $code = null): void
82+
{
83+
$this->assertEquals($msg, $e->getMessage());
84+
85+
if ($code !== null) {
86+
$this->assertEquals($code, $e->getCode());
87+
}
88+
}
6289
}

test/Helper/AssertTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\StdlibTest\Str;
4+
5+
use Toolkit\Stdlib\Helper\Assert;
6+
use Toolkit\StdlibTest\BaseLibTestCase;
7+
8+
/**
9+
* class AssertTest
10+
*
11+
* @author inhere
12+
*/
13+
class AssertTest extends BaseLibTestCase
14+
{
15+
public function testAssert_notEmpty(): void
16+
{
17+
$tests = [
18+
false,
19+
null,
20+
0,
21+
0.0,
22+
'',
23+
'0',
24+
[],
25+
];
26+
foreach ($tests as $val) {
27+
$e = $this->tryCatchRun(fn() => Assert::notEmpty($val));
28+
$this->assertException($e, 'Expected a non-empty value');
29+
}
30+
31+
$tests = [
32+
true,
33+
];
34+
foreach ($tests as $val) {
35+
$e = $this->tryCatchRun(fn() => Assert::notEmpty($val));
36+
$this->assertException($e, 'NO ERROR', -1);
37+
}
38+
}
39+
40+
public function testAssert_bool(): void
41+
{
42+
$e = $this->tryCatchRun(fn() => Assert::isFalse(false));
43+
$this->assertException($e, 'NO ERROR', -1);
44+
45+
$e = $this->tryCatchRun(fn() => Assert::isFalse(true));
46+
$this->assertException($e, 'Expected a false value');
47+
$e = $this->tryCatchRun(fn() => Assert::isFalse(true, 'custom error'));
48+
$this->assertException($e, 'custom error');
49+
50+
$e = $this->tryCatchRun(fn() => Assert::isTrue(true));
51+
$this->assertException($e, 'NO ERROR', -1);
52+
53+
$e = $this->tryCatchRun(fn() => Assert::isTrue(false));
54+
$this->assertException($e, 'Expected a true value');
55+
$e = $this->tryCatchRun(fn() => Assert::isTrue(false, 'custom error'));
56+
$this->assertException($e, 'custom error');
57+
}
58+
}

test/Util/OptionalTest.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Toolkit\StdlibTest\Util;
44

55
use Toolkit\Stdlib\Util\Optional;
6+
use Toolkit\Stdlib\Util\Stream\IntStream;
67
use Toolkit\StdlibTest\BaseLibTestCase;
78

89
/**
@@ -18,32 +19,38 @@ public function testOptionalBasic(): void
1819
$this->assertEquals(23, $o->get());
1920
$this->assertEquals('23', $o->map('strval')->get());
2021

21-
$val = $o->filter(static function ($val) {
22-
return $val > 25;
23-
})->orElse(25);
24-
25-
$this->assertEquals(25, $val);
26-
2722
// use arrow syntax:
2823
$val = $o->filter(fn($val) => $val > 25)->orElse(25);
2924
$this->assertEquals(25, $val);
25+
26+
$o = Optional::nullable(null);
27+
$this->assertEquals(23, $o->orElse(23));
28+
$this->assertEquals(25, $o->orElseGet(fn() => 25));
3029
}
3130

32-
public function testOptional_or(): void
31+
public function testOptional_stream(): void
3332
{
34-
$o = Optional::ofNullable(null);
33+
$o = Optional::nullable([23, '56', '78']);
3534

36-
$this->runAndGetException(function () use($o) {
37-
$o->get();
38-
});
35+
$this->assertEquals([23, '56', '78'], $o->get());
36+
$list = $o->stream()->map(fn($val) => (int)$val)->toArray();
37+
$this->assertEquals([23, 56, 78], $list);
3938

40-
$val = $o->or(function () {
41-
return Optional::of(23);
42-
})->get();
43-
$this->assertEquals(23, $val);
39+
$this->assertEquals([23, '56', '78'], $o->get());
40+
$list = $o->stream(IntStream::class)->toArray();
41+
$this->assertEquals([23, 56, 78], $list);
42+
}
43+
44+
public function testOptional_or(): void
45+
{
46+
$o = Optional::ofNullable(null);
47+
$e = $this->tryCatchRun(fn() => $o->get());
48+
$this->assertException($e, 'No value present');
4449

45-
// use arrow syntax:
4650
$val = $o->or(fn() => Optional::of(23))->get();
4751
$this->assertEquals(23, $val);
52+
53+
$val = $o->or(fn() => Optional::of('abc'))->get();
54+
$this->assertEquals('abc', $val);
4855
}
4956
}

0 commit comments

Comments
 (0)