|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Chain\ToolBox\MetadataFactory; |
| 6 | + |
| 7 | +use PhpLlm\LlmChain\Chain\ToolBox\Exception\ToolConfigurationException; |
| 8 | +use PhpLlm\LlmChain\Chain\ToolBox\Exception\ToolMetadataException; |
| 9 | +use PhpLlm\LlmChain\Chain\ToolBox\MetadataFactory\ChainFactory; |
| 10 | +use PhpLlm\LlmChain\Chain\ToolBox\MetadataFactory\MemoryFactory; |
| 11 | +use PhpLlm\LlmChain\Chain\ToolBox\MetadataFactory\ReflectionFactory; |
| 12 | +use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolMisconfigured; |
| 13 | +use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolMultiple; |
| 14 | +use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolNoAttribute1; |
| 15 | +use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolOptionalParam; |
| 16 | +use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolRequiredParams; |
| 17 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 18 | +use PHPUnit\Framework\Attributes\Medium; |
| 19 | +use PHPUnit\Framework\Attributes\Test; |
| 20 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +#[CoversClass(ChainFactory::class)] |
| 24 | +#[Medium] |
| 25 | +#[UsesClass(MemoryFactory::class)] |
| 26 | +#[UsesClass(ReflectionFactory::class)] |
| 27 | +#[UsesClass(ToolMetadataException::class)] |
| 28 | +final class ChainFactoryTest extends TestCase |
| 29 | +{ |
| 30 | + private ChainFactory $factory; |
| 31 | + |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + $factory1 = (new MemoryFactory()) |
| 35 | + ->addTool(ToolNoAttribute1::class, 'reference', 'A reference tool') |
| 36 | + ->addTool(ToolOptionalParam::class, 'optional_param', 'Tool with optional param', 'bar'); |
| 37 | + $factory2 = new ReflectionFactory(); |
| 38 | + |
| 39 | + $this->factory = new ChainFactory([$factory1, $factory2]); |
| 40 | + } |
| 41 | + |
| 42 | + #[Test] |
| 43 | + public function testGetMetadataNotExistingClass(): void |
| 44 | + { |
| 45 | + $this->expectException(ToolMetadataException::class); |
| 46 | + $this->expectExceptionMessage('The reference "NoClass" is not a valid as tool.'); |
| 47 | + |
| 48 | + iterator_to_array($this->factory->getMetadata('NoClass')); |
| 49 | + } |
| 50 | + |
| 51 | + #[Test] |
| 52 | + public function testGetMetadataNotConfiguredClass(): void |
| 53 | + { |
| 54 | + $this->expectException(ToolConfigurationException::class); |
| 55 | + $this->expectExceptionMessage(sprintf('Method "foo" not found in tool "%s".', ToolMisconfigured::class)); |
| 56 | + |
| 57 | + iterator_to_array($this->factory->getMetadata(ToolMisconfigured::class)); |
| 58 | + } |
| 59 | + |
| 60 | + #[Test] |
| 61 | + public function testGetMetadataWithAttributeSingleHit(): void |
| 62 | + { |
| 63 | + $metadata = iterator_to_array($this->factory->getMetadata(ToolRequiredParams::class)); |
| 64 | + |
| 65 | + self::assertCount(1, $metadata); |
| 66 | + } |
| 67 | + |
| 68 | + #[Test] |
| 69 | + public function testGetMetadataOverwrite(): void |
| 70 | + { |
| 71 | + $metadata = iterator_to_array($this->factory->getMetadata(ToolOptionalParam::class)); |
| 72 | + |
| 73 | + self::assertCount(1, $metadata); |
| 74 | + self::assertSame('optional_param', $metadata[0]->name); |
| 75 | + self::assertSame('Tool with optional param', $metadata[0]->description); |
| 76 | + self::assertSame('bar', $metadata[0]->method); |
| 77 | + } |
| 78 | + |
| 79 | + #[Test] |
| 80 | + public function testGetMetadataWithAttributeDoubleHit(): void |
| 81 | + { |
| 82 | + $metadata = iterator_to_array($this->factory->getMetadata(ToolMultiple::class)); |
| 83 | + |
| 84 | + self::assertCount(2, $metadata); |
| 85 | + } |
| 86 | + |
| 87 | + #[Test] |
| 88 | + public function testGetMetadataWithMemorySingleHit(): void |
| 89 | + { |
| 90 | + $metadata = iterator_to_array($this->factory->getMetadata(ToolNoAttribute1::class)); |
| 91 | + |
| 92 | + self::assertCount(1, $metadata); |
| 93 | + } |
| 94 | +} |
0 commit comments