|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace KaririCode\Tests\ProcessorPipeline; |
| 6 | + |
| 7 | +use KaririCode\Contract\Processor\ConfigurableProcessor; |
| 8 | +use KaririCode\Contract\Processor\Pipeline; |
| 9 | +use KaririCode\Contract\Processor\Processor; |
| 10 | +use KaririCode\ProcessorPipeline\ProcessorBuilder; |
| 11 | +use KaririCode\ProcessorPipeline\ProcessorPipeline; |
| 12 | +use KaririCode\ProcessorPipeline\ProcessorRegistry; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +final class ProcessorBuilderTest extends TestCase |
| 17 | +{ |
| 18 | + private ProcessorRegistry|MockObject $registry; |
| 19 | + private ProcessorBuilder $builder; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + $this->registry = $this->createMock(ProcessorRegistry::class); |
| 24 | + $this->builder = new ProcessorBuilder($this->registry); |
| 25 | + } |
| 26 | + |
| 27 | + public function testBuildNonConfigurableProcessor(): void |
| 28 | + { |
| 29 | + $processor = $this->createMock(Processor::class); |
| 30 | + $this->registry->expects($this->once()) |
| 31 | + ->method('get') |
| 32 | + ->with('context', 'name') |
| 33 | + ->willReturn($processor); |
| 34 | + |
| 35 | + $result = $this->builder->build('context', 'name'); |
| 36 | + $this->assertSame($processor, $result); |
| 37 | + } |
| 38 | + |
| 39 | + public function testBuildConfigurableProcessor(): void |
| 40 | + { |
| 41 | + $processor = $this->createMock(ConfigurableProcessor::class); |
| 42 | + $this->registry->expects($this->once()) |
| 43 | + ->method('get') |
| 44 | + ->with('context', 'name') |
| 45 | + ->willReturn($processor); |
| 46 | + |
| 47 | + $processor->expects($this->once()) |
| 48 | + ->method('configure') |
| 49 | + ->with(['option' => 'value']); |
| 50 | + |
| 51 | + $result = $this->builder->build('context', 'name', ['option' => 'value']); |
| 52 | + $this->assertSame($processor, $result); |
| 53 | + } |
| 54 | + |
| 55 | + public function testBuildPipeline(): void |
| 56 | + { |
| 57 | + $processor1 = $this->createMock(Processor::class); |
| 58 | + $processor2 = $this->createMock(ConfigurableProcessor::class); |
| 59 | + |
| 60 | + $this->registry->expects($this->exactly(2)) |
| 61 | + ->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 | + }); |
| 71 | + |
| 72 | + $processor2->expects($this->once()) |
| 73 | + ->method('configure') |
| 74 | + ->with(['option' => 'value']); |
| 75 | + |
| 76 | + $result = $this->builder->buildPipeline('context', [ |
| 77 | + 'processor1', |
| 78 | + 'processor2' => ['option' => 'value'], |
| 79 | + ]); |
| 80 | + |
| 81 | + $this->assertInstanceOf(Pipeline::class, $result); |
| 82 | + $this->assertInstanceOf(ProcessorPipeline::class, $result); |
| 83 | + } |
| 84 | +} |
0 commit comments