/
StreamLoaderTest.php
163 lines (138 loc) · 4.13 KB
/
StreamLoaderTest.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
namespace Flow\ETL\Tests\Unit\Loader;
use function Flow\ETL\DSL\{int_entry, ref, row, rows, str_entry, to_output, to_stream};
use Flow\ETL\Exception\RuntimeException;
use Flow\ETL\Filesystem\Stream\Mode;
use Flow\ETL\Loader\StreamLoader;
use Flow\ETL\{Config, FlowContext};
use PHPUnit\Framework\TestCase;
final class StreamLoaderTest extends TestCase
{
public function test_loading_data_into_invalid_stream() : void
{
$this->expectExceptionMessage("Can't open stream for url: php://qweqweqw in mode: w");
$this->expectException(RuntimeException::class);
$loader = to_stream('php://qweqweqw', 0);
$loader->load(
rows(
row(int_entry('id', 1), str_entry('name', 'id_1')),
row(int_entry('id', 2), str_entry('name', 'id_2')),
row(int_entry('id', 3), str_entry('name', 'id_3'))
),
new FlowContext(Config::default())
);
}
public function test_loading_partitioned_rows_into_php_memory_stream() : void
{
$loader = new StreamLoader('php://output', Mode::WRITE, 0);
\ob_start();
$loader->load(
rows(
row(int_entry('id', 1), str_entry('name', 'id_1'), str_entry('group', 'a')),
row(int_entry('id', 2), str_entry('name', 'id_2'), str_entry('group', 'a')),
row(int_entry('id', 3), str_entry('name', 'id_3'), str_entry('group', 'a'))
)->partitionBy(ref('group'))[0],
new FlowContext(Config::default())
);
$output = \ob_get_contents();
\ob_end_clean();
self::assertStringContainsString(
<<<'TABLE'
+----+------+-------+
| id | name | group |
+----+------+-------+
| 1 | id_1 | a |
| 2 | id_2 | a |
| 3 | id_3 | a |
+----+------+-------+
Partitions:
- group=a
3 rows
TABLE,
$output
);
}
public function test_loading_rows_and_schema_into_php_memory_stream() : void
{
$loader = to_output(false, StreamLoader\Output::rows_and_schema);
\ob_start();
$loader->load(
rows(
row(int_entry('id', 1), str_entry('name', 'id_1')),
row(int_entry('id', 2), str_entry('name', 'id_2')),
row(int_entry('id', 3), str_entry('name', 'id_3'))
),
new FlowContext(Config::default())
);
$output = \ob_get_contents();
\ob_end_clean();
self::assertSame(
<<<'ASCII'
+----+------+
| id | name |
+----+------+
| 1 | id_1 |
| 2 | id_2 |
| 3 | id_3 |
+----+------+
3 rows
schema
|-- id: integer
|-- name: string
ASCII,
$output
);
}
public function test_loading_rows_into_php_memory_stream() : void
{
$loader = new StreamLoader('php://output', Mode::WRITE, 0);
\ob_start();
$loader->load(
rows(
row(int_entry('id', 1), str_entry('name', 'id_1')),
row(int_entry('id', 2), str_entry('name', 'id_2')),
row(int_entry('id', 3), str_entry('name', 'id_3'))
),
new FlowContext(Config::default())
);
$output = \ob_get_contents();
\ob_end_clean();
self::assertStringContainsString(
<<<'TABLE'
+----+------+
| id | name |
+----+------+
| 1 | id_1 |
| 2 | id_2 |
| 3 | id_3 |
+----+------+
3 rows
TABLE,
$output
);
}
public function test_loading_schema_into_php_memory_stream() : void
{
$loader = new StreamLoader('php://output', Mode::WRITE, 0, StreamLoader\Output::schema);
\ob_start();
$loader->load(
rows(
row(int_entry('id', 1), str_entry('name', 'id_1')),
row(int_entry('id', 2), str_entry('name', 'id_2')),
row(int_entry('id', 3), str_entry('name', 'id_3'))
),
new FlowContext(Config::default())
);
$output = \ob_get_contents();
\ob_end_clean();
self::assertSame(
<<<'ASCII'
schema
|-- id: integer
|-- name: string
ASCII,
$output
);
}
}