-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathPipelineTest.php
109 lines (79 loc) · 3.61 KB
/
PipelineTest.php
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
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace Tests;
use Marquine\Etl\Pipeline;
class PipelineTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->row1 = $this->createMock('Marquine\Etl\Row');
$this->row1->expects($this->any())->method('toArray')->willReturn('row1');
$this->row2 = $this->createMock('Marquine\Etl\Row');
$this->row2->expects($this->any())->method('toArray')->willReturn('row2');
$this->row3 = $this->createMock('Marquine\Etl\Row');
$this->row3->expects($this->any())->method('toArray')->willReturn('row3');
$generator = function () {
yield $this->row1;
yield $this->row2;
yield $this->row3;
};
$this->extractor = $this->createMock('Marquine\Etl\Extractors\Extractor');
$this->extractor->expects($this->any())->method('extract')->willReturn($generator());
$this->transformer = $this->createMock('Marquine\Etl\Transformers\Transformer');
$this->transformer->expects($this->any())->method('transform')->withConsecutive([$this->row1], [$this->row2], [$this->row3]);
$this->loader = $this->createMock('Marquine\Etl\Loaders\Loader');
$this->loader->expects($this->any())->method('load')->withConsecutive([$this->row1], [$this->row2], [$this->row3]);
$this->pipeline = new Pipeline;
$this->pipeline->extractor($this->extractor);
}
/** @test */
public function pipeline_flow()
{
$this->row1->expects($this->once())->method('toArray');
$this->row2->expects($this->once())->method('toArray');
$this->row3->expects($this->once())->method('toArray');
$this->extractor->expects($this->once())->method('extract');
$this->extractor->expects($this->once())->method('initialize');
$this->extractor->expects($this->once())->method('finalize');
$this->transformer->expects($this->exactly(3))->method('transform');
$this->transformer->expects($this->once())->method('initialize');
$this->transformer->expects($this->once())->method('finalize');
$this->loader->expects($this->exactly(3))->method('load');
$this->loader->expects($this->once())->method('initialize');
$this->loader->expects($this->once())->method('finalize');
$this->pipeline->pipe($this->transformer);
$this->pipeline->pipe($this->loader);
$this->assertEquals(['row1', 'row2', 'row3'], iterator_to_array($this->pipeline));
}
/** @test */
public function limit_the_number_of_rows()
{
$this->pipeline->limit(1);
$this->assertEquals(['row1'], iterator_to_array($this->pipeline));
}
/** @test */
public function skip_initial_rows()
{
$this->pipeline->skip(2);
$this->assertEquals(['row3'], iterator_to_array($this->pipeline));
$this->pipeline->skip(3);
$this->assertEquals([], iterator_to_array($this->pipeline));
}
/** @test */
public function limit_after_skipping_rows()
{
$this->pipeline->skip(1);
$this->pipeline->limit(1);
$this->assertEquals(['row2'], iterator_to_array($this->pipeline));
}
/** @test */
public function discard_rows()
{
$this->row2->expects($this->once())->method('discarded')->willReturn(true);
$this->pipeline->pipe($this->transformer);
$this->pipeline->pipe($this->loader);
$this->transformer->expects($this->exactly(2))->method('transform');
$this->loader->expects($this->exactly(2))->method('load');
$this->assertEquals(['row1', 'row3'], iterator_to_array($this->pipeline));
}
}