Skip to content

feat(tests): Enhance ProcessorBuilderTest for 100% coverage #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading