Skip to content

Commit b5c080f

Browse files
authored
Feat: Improve context builder (#48)
1 parent 3d39938 commit b5c080f

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

src/Internal/EtlBuilderTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use BenTools\ETL\Transformer\ChainTransformer;
1919
use BenTools\ETL\Transformer\TransformerInterface;
2020

21+
use function array_intersect_key;
2122
use function count;
2223

2324
/**
@@ -112,8 +113,12 @@ public function withProcessor(ProcessorInterface $processor): self
112113
/**
113114
* @param array<string, mixed> $context
114115
*/
115-
public function withContext(array $context): self
116+
public function withContext(array $context, bool $clear = false, bool $overwrite = true): self
116117
{
117-
return $this->cloneWith(['context' => $context]);
118+
return $this->cloneWith(['context' => [
119+
...($clear ? [] : $this->context),
120+
...$context,
121+
...($overwrite ? [] : array_intersect_key($this->context, $context)),
122+
]]);
118123
}
119124
}

tests/Unit/ContextTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BenTools\ETL\Tests\Unit;
6+
7+
use BenTools\ETL\EtlExecutor;
8+
9+
use function expect;
10+
use function it;
11+
12+
it('provides a default context', function () {
13+
// Given
14+
$executor = (new EtlExecutor(context: ['color' => 'green', 'shape' => 'square', 'lights' => 'on']));
15+
16+
// When
17+
$report = $executor->process([], context: ['shape' => 'round', 'size' => 'small']);
18+
19+
// Then
20+
expect($report->context)->toBe(['color' => 'green', 'shape' => 'round', 'lights' => 'on', 'size' => 'small']);
21+
});
22+
23+
it('adds some more context', function () {
24+
// Given
25+
$executor = (new EtlExecutor(context: ['color' => 'green', 'shape' => 'square', 'lights' => 'on']))
26+
->withContext(['color' => 'blue', 'flavor' => 'vanilla']);
27+
28+
// When
29+
$report = $executor->process([], context: ['shape' => 'round', 'size' => 'small']);
30+
31+
// Then
32+
expect($report->context)->toBe(['color' => 'blue', 'shape' => 'round', 'lights' => 'on', 'flavor' => 'vanilla', 'size' => 'small']);
33+
});
34+
35+
it('replaces the whole context', function () {
36+
// Given
37+
$executor = (new EtlExecutor(context: ['color' => 'green', 'shape' => 'square', 'lights' => 'on']))
38+
->withContext(['color' => 'blue', 'flavor' => 'vanilla'], clear: true);
39+
40+
// When
41+
$report = $executor->process([], context: ['shape' => 'round', 'size' => 'small']);
42+
43+
// Then
44+
expect($report->context)->toBe(['color' => 'blue', 'flavor' => 'vanilla', 'shape' => 'round', 'size' => 'small']);
45+
});
46+
47+
it('does not override existing values', function () {
48+
// Given
49+
$executor = (new EtlExecutor(context: ['color' => 'green', 'shape' => 'square', 'lights' => 'on']))
50+
->withContext(['color' => 'blue', 'flavor' => 'vanilla'], overwrite: false);
51+
52+
// When
53+
$report = $executor->process([], context: ['shape' => 'round', 'size' => 'small']);
54+
55+
// Then
56+
expect($report->context)->toBe(['color' => 'green', 'shape' => 'round', 'lights' => 'on', 'flavor' => 'vanilla', 'size' => 'small']);
57+
});

tests/Unit/EtlExecutorTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,3 @@ public function process(EtlExecutor $executor, EtlState $state, mixed $extracted
114114
// When
115115
$executor->process([]);
116116
})->throws(ExtractException::class);
117-
118-
it('accepts a default context', function () {
119-
// Given
120-
$executor = (new EtlExecutor())->withContext(['foo' => 'bar']);
121-
122-
// When
123-
$report = $executor->process([], context: ['bar' => 'baz']);
124-
125-
// Then
126-
expect($report->context)->toBe(['foo' => 'bar', 'bar' => 'baz']);
127-
});

0 commit comments

Comments
 (0)