-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathWriterMonadTest.php
More file actions
43 lines (35 loc) · 1020 Bytes
/
WriterMonadTest.php
File metadata and controls
43 lines (35 loc) · 1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
declare(strict_types=1);
namespace example;
use PHPUnit\Framework\TestCase;
use Widmogrod\Functional as f;
use Widmogrod\Monad\Writer as W;
use Widmogrod\Primitive\Stringg as S;
class WriterMonadTest extends TestCase
{
public function test_it_should_filter_with_logs()
{
$data = f\fromIterable([1, 10, 15, 20, 25]);
$filter = function ($i) {
if ($i % 2 == 1) {
return W::of(false, S::of("Reject odd number $i.\n"));
} elseif ($i > 15) {
return W::of(false, S::of("Reject $i because it is bigger than 15\n"));
}
return W::of(true);
};
list($result, $log) = f\filterM($filter, $data)->runWriter();
$this->assertEquals(
f\fromIterable([10]),
$result
);
$this->assertEquals(
'Reject odd number 1.
Reject odd number 15.
Reject 20 because it is bigger than 15
Reject odd number 25.
',
$log->extract()
);
}
}