diff --git a/src/Internal/EtlBuilderTrait.php b/src/Internal/EtlBuilderTrait.php index 22f4bb5..079964a 100644 --- a/src/Internal/EtlBuilderTrait.php +++ b/src/Internal/EtlBuilderTrait.php @@ -18,6 +18,7 @@ use BenTools\ETL\Transformer\ChainTransformer; use BenTools\ETL\Transformer\TransformerInterface; +use function array_intersect_key; use function count; /** @@ -112,8 +113,12 @@ public function withProcessor(ProcessorInterface $processor): self /** * @param array $context */ - public function withContext(array $context): self + public function withContext(array $context, bool $clear = false, bool $overwrite = true): self { - return $this->cloneWith(['context' => $context]); + return $this->cloneWith(['context' => [ + ...($clear ? [] : $this->context), + ...$context, + ...($overwrite ? [] : array_intersect_key($this->context, $context)), + ]]); } } diff --git a/tests/Unit/ContextTest.php b/tests/Unit/ContextTest.php new file mode 100644 index 0000000..ae5115f --- /dev/null +++ b/tests/Unit/ContextTest.php @@ -0,0 +1,57 @@ + 'green', 'shape' => 'square', 'lights' => 'on'])); + + // When + $report = $executor->process([], context: ['shape' => 'round', 'size' => 'small']); + + // Then + expect($report->context)->toBe(['color' => 'green', 'shape' => 'round', 'lights' => 'on', 'size' => 'small']); +}); + +it('adds some more context', function () { + // Given + $executor = (new EtlExecutor(context: ['color' => 'green', 'shape' => 'square', 'lights' => 'on'])) + ->withContext(['color' => 'blue', 'flavor' => 'vanilla']); + + // When + $report = $executor->process([], context: ['shape' => 'round', 'size' => 'small']); + + // Then + expect($report->context)->toBe(['color' => 'blue', 'shape' => 'round', 'lights' => 'on', 'flavor' => 'vanilla', 'size' => 'small']); +}); + +it('replaces the whole context', function () { + // Given + $executor = (new EtlExecutor(context: ['color' => 'green', 'shape' => 'square', 'lights' => 'on'])) + ->withContext(['color' => 'blue', 'flavor' => 'vanilla'], clear: true); + + // When + $report = $executor->process([], context: ['shape' => 'round', 'size' => 'small']); + + // Then + expect($report->context)->toBe(['color' => 'blue', 'flavor' => 'vanilla', 'shape' => 'round', 'size' => 'small']); +}); + +it('does not override existing values', function () { + // Given + $executor = (new EtlExecutor(context: ['color' => 'green', 'shape' => 'square', 'lights' => 'on'])) + ->withContext(['color' => 'blue', 'flavor' => 'vanilla'], overwrite: false); + + // When + $report = $executor->process([], context: ['shape' => 'round', 'size' => 'small']); + + // Then + expect($report->context)->toBe(['color' => 'green', 'shape' => 'round', 'lights' => 'on', 'flavor' => 'vanilla', 'size' => 'small']); +}); diff --git a/tests/Unit/EtlExecutorTest.php b/tests/Unit/EtlExecutorTest.php index 7ff99ef..3dbe0c9 100644 --- a/tests/Unit/EtlExecutorTest.php +++ b/tests/Unit/EtlExecutorTest.php @@ -114,14 +114,3 @@ public function process(EtlExecutor $executor, EtlState $state, mixed $extracted // When $executor->process([]); })->throws(ExtractException::class); - -it('accepts a default context', function () { - // Given - $executor = (new EtlExecutor())->withContext(['foo' => 'bar']); - - // When - $report = $executor->process([], context: ['bar' => 'baz']); - - // Then - expect($report->context)->toBe(['foo' => 'bar', 'bar' => 'baz']); -});