Skip to content

Commit

Permalink
Merge pull request #7 from KaririCode-Framework/develop
Browse files Browse the repository at this point in the history
feat(tests): Enhance ProcessorBuilderTest for 100% coverage
  • Loading branch information
walmir-silva authored Oct 18, 2024
2 parents 07c645a + 1d79011 commit 6bc3e74
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 49 deletions.
44 changes: 22 additions & 22 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 16 additions & 15 deletions src/ProcessorBuilder.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace KaririCode\ProcessorPipeline;

use KaririCode\Contract\Processor\ConfigurableProcessor;
Expand All @@ -27,33 +25,36 @@ public function build(string $context, string $name, array $processorConfig = []
}

/**
* @param array<int|string, string|array<string, mixed>> $processorSpecs
* @param array<string, mixed> $processorSpecs
*/
public function buildPipeline(string $context, array $processorSpecs): Pipeline
{
$pipeline = new ProcessorPipeline();
foreach ($processorSpecs as $key => $spec) {
$processorName = $this->resolveProcessorName($key, $spec);
$processorConfig = $this->resolveProcessorConfig($key, $spec);
$processor = $this->build($context, $processorName, $processorConfig);

foreach ($processorSpecs as $name => $config) {
if (!$this->isValidProcessorSpec($config)) {
continue;
}

$processorConfig = $this->normalizeProcessorConfig($config);
$processor = $this->build($context, $name, $processorConfig);
$pipeline->addProcessor($processor);
}

return $pipeline;
}

private function isUnnamedProcessor(int|string $key): bool
private function isValidProcessorSpec(mixed $spec): bool
{
return is_int($key);
return is_array($spec) || true === $spec;
}

private function resolveProcessorName(int|string $key, string|array $spec): string
private function normalizeProcessorConfig(mixed $config): array
{
return $this->isUnnamedProcessor($key) ? (string) $spec : (string) $key;
}
if (is_array($config)) {
return $config;
}

private function resolveProcessorConfig(int|string $key, string|array $spec): array
{
return $this->isUnnamedProcessor($key) ? [] : (array) $spec;
return [];
}
}
63 changes: 51 additions & 12 deletions tests/ProcessorBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,71 @@ public function testBuildConfigurableProcessor(): void
$this->assertSame($processor, $result);
}

public function testBuildPipeline(): void
public function testBuildConfigurableProcessorWithEmptyConfig(): void
{
$processor = $this->createMock(ConfigurableProcessor::class);
$this->registry->expects($this->once())
->method('get')
->with('context', 'name')
->willReturn($processor);

$processor->expects($this->never())
->method('configure');

$result = $this->builder->build('context', 'name', []);
$this->assertSame($processor, $result);
}

public function testBuildPipelineWithVariousProcessorTypes(): void
{
$processor1 = $this->createMock(Processor::class);
$processor2 = $this->createMock(ConfigurableProcessor::class);
$processor3 = $this->createMock(Processor::class);

$this->registry->expects($this->exactly(2))
$this->registry->expects($this->exactly(3))
->method('get')
->willReturnCallback(function ($context, $name) use ($processor1, $processor2) {
if ('context' === $context && 'processor1' === $name) {
return $processor1;
}
if ('context' === $context && 'processor2' === $name) {
return $processor2;
}
$this->fail('Unexpected get() call');
});
->willReturnMap([
['context', 'processor1', $processor1],
['context', 'processor2', $processor2],
['context', 'processor3', $processor3],
]);

$processor2->expects($this->once())
->method('configure')
->with(['option' => 'value']);

$result = $this->builder->buildPipeline('context', [
'processor1',
'processor1' => true,
'processor2' => ['option' => 'value'],
'processor3' => [],
]);

$this->assertInstanceOf(Pipeline::class, $result);
$this->assertInstanceOf(ProcessorPipeline::class, $result);
}

public function testBuildPipelineWithInvalidProcessorSpec(): void
{
$processor = $this->createMock(Processor::class);

$this->registry->expects($this->once())
->method('get')
->with('context', 'validProcessor')
->willReturn($processor);

$result = $this->builder->buildPipeline('context', [
'validProcessor' => true,
'invalidProcessor' => false,
'anotherInvalidProcessor' => null,
]);

$this->assertInstanceOf(Pipeline::class, $result);
}

public function testBuildPipelineWithEmptySpecs(): void
{
$result = $this->builder->buildPipeline('context', []);

$this->assertInstanceOf(Pipeline::class, $result);
$this->assertInstanceOf(ProcessorPipeline::class, $result);
}
Expand Down

0 comments on commit 6bc3e74

Please sign in to comment.