|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlow\Test\Functional; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use SlayerBirden\DataFlow\DataBagInterface; |
|
8
|
|
|
use SlayerBirden\DataFlow\Emitter\BlackHole; |
|
9
|
|
|
use SlayerBirden\DataFlow\Pipe\FilterCallbackInterface; |
|
10
|
|
|
use SlayerBirden\DataFlow\Pipe\MapperCallbackInterface; |
|
11
|
|
|
use SlayerBirden\DataFlow\PipelineBuilder; |
|
12
|
|
|
use SlayerBirden\DataFlow\Plumber; |
|
13
|
|
|
use SlayerBirden\DataFlow\Provider\ArrayProvider; |
|
14
|
|
|
use SlayerBirden\DataFlow\Writer\ArrayWrite; |
|
15
|
|
|
|
|
16
|
|
|
class SimplePipeTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
private $pipeline; |
|
19
|
|
|
private $storage = []; |
|
20
|
|
|
private $emitter; |
|
21
|
|
|
|
|
22
|
|
|
protected function setUp() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->emitter = new BlackHole(); |
|
25
|
|
|
$this->pipeline = (new PipelineBuilder($this->emitter)) |
|
26
|
|
|
->filter(new class implements FilterCallbackInterface |
|
27
|
|
|
{ |
|
28
|
|
|
public function __invoke(DataBagInterface $dataBag): bool |
|
29
|
|
|
{ |
|
30
|
|
|
return stripos($dataBag['firstname'], 'cl') !== false; |
|
31
|
|
|
} |
|
32
|
|
|
}) |
|
33
|
|
|
->map('name', new class implements MapperCallbackInterface |
|
34
|
|
|
{ |
|
35
|
|
|
public function __invoke($value, ?DataBagInterface $dataBag = null) |
|
36
|
|
|
{ |
|
37
|
|
|
return $dataBag['firstname'] . ' ' . $dataBag['lastname']; |
|
38
|
|
|
} |
|
39
|
|
|
}) |
|
40
|
|
|
->addSection(new ArrayWrite('testWrite', $this->storage)) |
|
41
|
|
|
->build(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testSimplePipeFlow() |
|
45
|
|
|
{ |
|
46
|
|
|
$provider = new ArrayProvider('heroes', [ |
|
47
|
|
|
[ |
|
48
|
|
|
'id' => 1, |
|
49
|
|
|
'firstname' => 'Clark', |
|
50
|
|
|
'lastname' => 'Kent', |
|
51
|
|
|
], |
|
52
|
|
|
[ |
|
53
|
|
|
'id' => 2, |
|
54
|
|
|
'firstname' => 'Peter', |
|
55
|
|
|
'lastname' => 'Parker', |
|
56
|
|
|
], |
|
57
|
|
|
[ |
|
58
|
|
|
'id' => 3, |
|
59
|
|
|
'firstname' => 'Clock', |
|
60
|
|
|
'lastname' => 'Werk', |
|
61
|
|
|
], |
|
62
|
|
|
]); |
|
63
|
|
|
|
|
64
|
|
|
(new Plumber($provider, $this->pipeline, $this->emitter))->pour(); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertEquals([ |
|
67
|
|
|
[ |
|
68
|
|
|
'id' => 1, |
|
69
|
|
|
'firstname' => 'Clark', |
|
70
|
|
|
'lastname' => 'Kent', |
|
71
|
|
|
'name' => 'Clark Kent', |
|
72
|
|
|
], |
|
73
|
|
|
[ |
|
74
|
|
|
'id' => 3, |
|
75
|
|
|
'firstname' => 'Clock', |
|
76
|
|
|
'lastname' => 'Werk', |
|
77
|
|
|
'name' => 'Clock Werk', |
|
78
|
|
|
], |
|
79
|
|
|
], $this->storage); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|