Skip to content

Commit 839c284

Browse files
test pipe with
1 parent 51d3649 commit 839c284

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/Pipe.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,17 @@ function pipe($payload, callable ...$stages)
1616

1717
return $payload;
1818
}
19+
20+
/**
21+
* @param $value
22+
* @param callable ...$jobs
23+
* @return mixed returns the value
24+
*/
25+
function with(&$value, callable ...$jobs)
26+
{
27+
foreach ($jobs as $job) {
28+
$job($value);
29+
}
30+
31+
return $value;
32+
}

tests/PipeTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt\Tests;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use function BrenoRoosevelt\pipe;
8+
use function BrenoRoosevelt\with;
9+
10+
class PipeTest extends TestCase
11+
{
12+
public function testPipe()
13+
{
14+
$payload = new \stdClass();
15+
$payload->value = 1;
16+
$result = pipe(
17+
$payload,
18+
function ($p) {
19+
$p->value += 1;
20+
return $p;
21+
},
22+
function ($p) {
23+
$p->value *= 4;
24+
return $p;
25+
},
26+
function ($p) {
27+
$p->value -= 3;
28+
return $p;
29+
}
30+
);
31+
32+
$this->assertSame($payload, $result);
33+
$this->assertEquals(5, $result->value);
34+
}
35+
36+
public function testWith()
37+
{
38+
$payload = new \stdClass();
39+
$payload->value = 1;
40+
$result = with(
41+
$payload,
42+
fn($p) => $p->value += 1,
43+
fn($p) => $p->value *= 4,
44+
fn($p) => $p->value -= 3
45+
);
46+
47+
$this->assertSame($payload, $result);
48+
$this->assertEquals(5, $result->value);
49+
}
50+
}

0 commit comments

Comments
 (0)