diff --git a/composer.lock b/composer.lock index cba5c93..c2a9191 100644 --- a/composer.lock +++ b/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "kariricode/contract", - "version": "v2.7.2", + "version": "v2.7.7", "source": { "type": "git", "url": "https://github.com/KaririCode-Framework/kariricode-contract.git", - "reference": "f26a062a39657243a14ffa112e11bee51ad9b69b" + "reference": "bd78b746e4704ef36402478f7e6460a001fc579f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/KaririCode-Framework/kariricode-contract/zipball/f26a062a39657243a14ffa112e11bee51ad9b69b", - "reference": "f26a062a39657243a14ffa112e11bee51ad9b69b", + "url": "https://api.github.com/repos/KaririCode-Framework/kariricode-contract/zipball/bd78b746e4704ef36402478f7e6460a001fc579f", + "reference": "bd78b746e4704ef36402478f7e6460a001fc579f", "shasum": "" }, "require": { @@ -66,7 +66,7 @@ "issues": "https://github.com/KaririCode-Framework/kariricode-contract/issues", "source": "https://github.com/KaririCode-Framework/kariricode-contract" }, - "time": "2024-10-14T20:34:44+00:00" + "time": "2024-10-18T13:01:57+00:00" }, { "name": "kariricode/data-structure", @@ -839,16 +839,16 @@ }, { "name": "guzzlehttp/promises", - "version": "2.0.3", + "version": "2.0.4", "source": { "type": "git", "url": "https://github.com/guzzle/promises.git", - "reference": "6ea8dd08867a2a42619d65c3deb2c0fcbf81c8f8" + "reference": "f9c436286ab2892c7db7be8c8da4ef61ccf7b455" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/promises/zipball/6ea8dd08867a2a42619d65c3deb2c0fcbf81c8f8", - "reference": "6ea8dd08867a2a42619d65c3deb2c0fcbf81c8f8", + "url": "https://api.github.com/repos/guzzle/promises/zipball/f9c436286ab2892c7db7be8c8da4ef61ccf7b455", + "reference": "f9c436286ab2892c7db7be8c8da4ef61ccf7b455", "shasum": "" }, "require": { @@ -902,7 +902,7 @@ ], "support": { "issues": "https://github.com/guzzle/promises/issues", - "source": "https://github.com/guzzle/promises/tree/2.0.3" + "source": "https://github.com/guzzle/promises/tree/2.0.4" }, "funding": [ { @@ -918,7 +918,7 @@ "type": "tidelift" } ], - "time": "2024-07-18T10:29:17+00:00" + "time": "2024-10-17T10:06:22+00:00" }, { "name": "guzzlehttp/psr7", @@ -1274,16 +1274,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.12.6", + "version": "1.12.7", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "dc4d2f145a88ea7141ae698effd64d9df46527ae" + "reference": "dc2b9976bd8b0f84ec9b0e50cc35378551de7af0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/dc4d2f145a88ea7141ae698effd64d9df46527ae", - "reference": "dc4d2f145a88ea7141ae698effd64d9df46527ae", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/dc2b9976bd8b0f84ec9b0e50cc35378551de7af0", + "reference": "dc2b9976bd8b0f84ec9b0e50cc35378551de7af0", "shasum": "" }, "require": { @@ -1328,7 +1328,7 @@ "type": "github" } ], - "time": "2024-10-06T15:03:59+00:00" + "time": "2024-10-18T11:12:07+00:00" }, { "name": "phpunit/php-code-coverage", @@ -2812,16 +2812,16 @@ }, { "name": "sebastian/comparator", - "version": "6.1.0", + "version": "6.1.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "fa37b9e2ca618cb051d71b60120952ee8ca8b03d" + "reference": "5ef523a49ae7a302b87b2102b72b1eda8918d686" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/fa37b9e2ca618cb051d71b60120952ee8ca8b03d", - "reference": "fa37b9e2ca618cb051d71b60120952ee8ca8b03d", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/5ef523a49ae7a302b87b2102b72b1eda8918d686", + "reference": "5ef523a49ae7a302b87b2102b72b1eda8918d686", "shasum": "" }, "require": { @@ -2877,7 +2877,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/comparator/issues", "security": "https://github.com/sebastianbergmann/comparator/security/policy", - "source": "https://github.com/sebastianbergmann/comparator/tree/6.1.0" + "source": "https://github.com/sebastianbergmann/comparator/tree/6.1.1" }, "funding": [ { @@ -2885,7 +2885,7 @@ "type": "github" } ], - "time": "2024-09-11T15:42:56+00:00" + "time": "2024-10-18T15:00:48+00:00" }, { "name": "sebastian/complexity", diff --git a/src/ProcessorBuilder.php b/src/ProcessorBuilder.php index 3821b89..ecab3da 100644 --- a/src/ProcessorBuilder.php +++ b/src/ProcessorBuilder.php @@ -1,7 +1,5 @@ > $processorSpecs + * @param array $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 []; } } diff --git a/tests/ProcessorBuilderTest.php b/tests/ProcessorBuilderTest.php index b5628f2..22dd95f 100644 --- a/tests/ProcessorBuilderTest.php +++ b/tests/ProcessorBuilderTest.php @@ -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); }