-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipes.phpt
96 lines (81 loc) · 2.32 KB
/
pipes.phpt
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare(strict_types=1);
/**
* This file is a part of dakujem/cumulus package.
* @author Andrej Rypak (dakujem) <xrypak@gmail.com>
*/
namespace Dakujem\Cumulus\Test;
require_once __DIR__ . '/bootstrap.php';
use ArrayIterator;
use Dakujem\Cumulus\Pipeline;
use IteratorAggregate;
use Tester\Assert;
use Tester\TestCase;
use Traversable;
abstract class _PipelineHelper implements IteratorAggregate
{
// protected array $stages;
protected $stages;
public function __construct(iterable $initialPipeline = [])
{
$this->stages = is_array($initialPipeline) ? $initialPipeline : iterator_to_array($initialPipeline);
}
public function pipe(callable $pipe)
{
$this->stages[] = $pipe;
return $this;
}
abstract public function execute($passable = null);
public function getIterator(): Traversable
{
return new ArrayIterator($this->stages);
}
}
final class _OnionPipelineHelper extends _PipelineHelper
{
public function execute($passable = null)
{
return Pipeline::onion($this)($passable);
}
}
final class _TubePipelineHelper extends _PipelineHelper
{
public function execute($passable = null)
{
return Pipeline::tube($this)($passable);
}
}
/**
* @author Andrej Rypak (dakujem) <xrypak@gmail.com>
*/
class _PipesHelperTest extends TestCase
{
public function testOnion()
{
$pipeline =
(new _OnionPipelineHelper())
->pipe(function (string $passable, callable $next) {
return $next($passable . '.');
})
->pipe(function (array $passable, callable $next) {
return $next(implode(' ', $passable));
})
->pipe(function (array $passable, callable $next) {
return $next(array_merge($passable, ['Hello', 'world']));
});
Assert::same('Hello world.', $pipeline->execute([]));
}
public function testTube()
{
$pipeline = (new _TubePipelineHelper())
->pipe(function (string $a) {
return $a . 'foo';
})
->pipe(function (string $a) {
return $a . 'bar';
});
Assert::same('ifoobar', $pipeline->execute('i'));
}
}
// run the test
(new _PipesHelperTest)->run();