Skip to content

Commit 6bc3e74

Browse files
authored
Merge pull request #7 from KaririCode-Framework/develop
feat(tests): Enhance ProcessorBuilderTest for 100% coverage
2 parents 07c645a + 1d79011 commit 6bc3e74

File tree

3 files changed

+89
-49
lines changed

3 files changed

+89
-49
lines changed

composer.lock

+22-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ProcessorBuilder.php

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace KaririCode\ProcessorPipeline;
64

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

2927
/**
30-
* @param array<int|string, string|array<string, mixed>> $processorSpecs
28+
* @param array<string, mixed> $processorSpecs
3129
*/
3230
public function buildPipeline(string $context, array $processorSpecs): Pipeline
3331
{
3432
$pipeline = new ProcessorPipeline();
35-
foreach ($processorSpecs as $key => $spec) {
36-
$processorName = $this->resolveProcessorName($key, $spec);
37-
$processorConfig = $this->resolveProcessorConfig($key, $spec);
38-
$processor = $this->build($context, $processorName, $processorConfig);
33+
34+
foreach ($processorSpecs as $name => $config) {
35+
if (!$this->isValidProcessorSpec($config)) {
36+
continue;
37+
}
38+
39+
$processorConfig = $this->normalizeProcessorConfig($config);
40+
$processor = $this->build($context, $name, $processorConfig);
3941
$pipeline->addProcessor($processor);
4042
}
4143

4244
return $pipeline;
4345
}
4446

45-
private function isUnnamedProcessor(int|string $key): bool
47+
private function isValidProcessorSpec(mixed $spec): bool
4648
{
47-
return is_int($key);
49+
return is_array($spec) || true === $spec;
4850
}
4951

50-
private function resolveProcessorName(int|string $key, string|array $spec): string
52+
private function normalizeProcessorConfig(mixed $config): array
5153
{
52-
return $this->isUnnamedProcessor($key) ? (string) $spec : (string) $key;
53-
}
54+
if (is_array($config)) {
55+
return $config;
56+
}
5457

55-
private function resolveProcessorConfig(int|string $key, string|array $spec): array
56-
{
57-
return $this->isUnnamedProcessor($key) ? [] : (array) $spec;
58+
return [];
5859
}
5960
}

tests/ProcessorBuilderTest.php

+51-12
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,71 @@ public function testBuildConfigurableProcessor(): void
5252
$this->assertSame($processor, $result);
5353
}
5454

55-
public function testBuildPipeline(): void
55+
public function testBuildConfigurableProcessorWithEmptyConfig(): void
56+
{
57+
$processor = $this->createMock(ConfigurableProcessor::class);
58+
$this->registry->expects($this->once())
59+
->method('get')
60+
->with('context', 'name')
61+
->willReturn($processor);
62+
63+
$processor->expects($this->never())
64+
->method('configure');
65+
66+
$result = $this->builder->build('context', 'name', []);
67+
$this->assertSame($processor, $result);
68+
}
69+
70+
public function testBuildPipelineWithVariousProcessorTypes(): void
5671
{
5772
$processor1 = $this->createMock(Processor::class);
5873
$processor2 = $this->createMock(ConfigurableProcessor::class);
74+
$processor3 = $this->createMock(Processor::class);
5975

60-
$this->registry->expects($this->exactly(2))
76+
$this->registry->expects($this->exactly(3))
6177
->method('get')
62-
->willReturnCallback(function ($context, $name) use ($processor1, $processor2) {
63-
if ('context' === $context && 'processor1' === $name) {
64-
return $processor1;
65-
}
66-
if ('context' === $context && 'processor2' === $name) {
67-
return $processor2;
68-
}
69-
$this->fail('Unexpected get() call');
70-
});
78+
->willReturnMap([
79+
['context', 'processor1', $processor1],
80+
['context', 'processor2', $processor2],
81+
['context', 'processor3', $processor3],
82+
]);
7183

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

7688
$result = $this->builder->buildPipeline('context', [
77-
'processor1',
89+
'processor1' => true,
7890
'processor2' => ['option' => 'value'],
91+
'processor3' => [],
92+
]);
93+
94+
$this->assertInstanceOf(Pipeline::class, $result);
95+
$this->assertInstanceOf(ProcessorPipeline::class, $result);
96+
}
97+
98+
public function testBuildPipelineWithInvalidProcessorSpec(): void
99+
{
100+
$processor = $this->createMock(Processor::class);
101+
102+
$this->registry->expects($this->once())
103+
->method('get')
104+
->with('context', 'validProcessor')
105+
->willReturn($processor);
106+
107+
$result = $this->builder->buildPipeline('context', [
108+
'validProcessor' => true,
109+
'invalidProcessor' => false,
110+
'anotherInvalidProcessor' => null,
79111
]);
80112

113+
$this->assertInstanceOf(Pipeline::class, $result);
114+
}
115+
116+
public function testBuildPipelineWithEmptySpecs(): void
117+
{
118+
$result = $this->builder->buildPipeline('context', []);
119+
81120
$this->assertInstanceOf(Pipeline::class, $result);
82121
$this->assertInstanceOf(ProcessorPipeline::class, $result);
83122
}

0 commit comments

Comments
 (0)