Skip to content

Commit

Permalink
Feat: Improve context builder (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek authored Nov 20, 2023
1 parent 3d39938 commit b5c080f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
9 changes: 7 additions & 2 deletions src/Internal/EtlBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use BenTools\ETL\Transformer\ChainTransformer;
use BenTools\ETL\Transformer\TransformerInterface;

use function array_intersect_key;
use function count;

/**
Expand Down Expand Up @@ -112,8 +113,12 @@ public function withProcessor(ProcessorInterface $processor): self
/**
* @param array<string, mixed> $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)),
]]);
}
}
57 changes: 57 additions & 0 deletions tests/Unit/ContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace BenTools\ETL\Tests\Unit;

use BenTools\ETL\EtlExecutor;

use function expect;
use function it;

it('provides a default context', function () {
// Given
$executor = (new EtlExecutor(context: ['color' => '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']);
});
11 changes: 0 additions & 11 deletions tests/Unit/EtlExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});

0 comments on commit b5c080f

Please sign in to comment.