|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Agent\Tests\Toolbox; |
| 13 | + |
| 14 | +use PHPUnit\Framework\MockObject\Exception; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\AI\Agent\Toolbox\Event\ToolCallArgumentsResolved; |
| 17 | +use Symfony\AI\Agent\Toolbox\Event\ToolCallFailed; |
| 18 | +use Symfony\AI\Agent\Toolbox\Event\ToolCallSucceeded; |
| 19 | +use Symfony\AI\Agent\Toolbox\Toolbox; |
| 20 | +use Symfony\AI\Fixtures\Tool\ToolCustomException; |
| 21 | +use Symfony\AI\Fixtures\Tool\ToolException; |
| 22 | +use Symfony\AI\Fixtures\Tool\ToolNoParams; |
| 23 | +use Symfony\AI\Platform\Result\ToolCall; |
| 24 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 25 | + |
| 26 | +final class ToolboxEventDispatcherTest extends TestCase |
| 27 | +{ |
| 28 | + private Toolbox $toolbox; |
| 29 | + private array $dispatchedEvents = []; |
| 30 | + |
| 31 | + /** |
| 32 | + * @throws Exception |
| 33 | + */ |
| 34 | + protected function setUp(): void |
| 35 | + { |
| 36 | + $dispatcher = $this->createMock(EventDispatcherInterface::class); |
| 37 | + $dispatcher |
| 38 | + ->method('dispatch') |
| 39 | + ->willReturnCallback(function (object $event, ?string $eventName = null) { |
| 40 | + $this->dispatchedEvents[] = $eventName ?? $event::class; |
| 41 | + |
| 42 | + return $event; |
| 43 | + }); |
| 44 | + $this->toolbox = new Toolbox([ |
| 45 | + new ToolNoParams(), |
| 46 | + new ToolException(), |
| 47 | + new ToolCustomException(), |
| 48 | + ], eventDispatcher: $dispatcher); |
| 49 | + } |
| 50 | + |
| 51 | + public function testExecuteWithUnknownTool() |
| 52 | + { |
| 53 | + try { |
| 54 | + $this->toolbox->execute(new ToolCall('call_1234', 'foo_bar_baz')); |
| 55 | + } catch (\Throwable) { |
| 56 | + } |
| 57 | + $this->assertEmpty($this->dispatchedEvents); |
| 58 | + } |
| 59 | + |
| 60 | + public function testExecuteWithToolExecutionException() |
| 61 | + { |
| 62 | + try { |
| 63 | + $this->toolbox->execute(new ToolCall('call_1234', 'tool_exception')); |
| 64 | + } catch (\Throwable) { |
| 65 | + } |
| 66 | + $this->assertEquals([ |
| 67 | + ToolCallArgumentsResolved::class, |
| 68 | + ToolCallFailed::class, |
| 69 | + ], $this->dispatchedEvents); |
| 70 | + } |
| 71 | + |
| 72 | + public function testExecuteWithCustomExecutionException() |
| 73 | + { |
| 74 | + try { |
| 75 | + $this->toolbox->execute(new ToolCall('call_1234', 'tool_custom_exception')); |
| 76 | + } catch (\Throwable) { |
| 77 | + } |
| 78 | + $this->assertEquals([ |
| 79 | + ToolCallArgumentsResolved::class, |
| 80 | + ToolCallFailed::class, |
| 81 | + ], $this->dispatchedEvents); |
| 82 | + } |
| 83 | + |
| 84 | + public function testExecuteSuccess() |
| 85 | + { |
| 86 | + try { |
| 87 | + $this->toolbox->execute(new ToolCall('call_1234', 'tool_no_params')); |
| 88 | + } catch (\Throwable) { |
| 89 | + } |
| 90 | + $this->assertEquals([ |
| 91 | + ToolCallArgumentsResolved::class, |
| 92 | + ToolCallSucceeded::class, |
| 93 | + ], $this->dispatchedEvents); |
| 94 | + } |
| 95 | +} |
0 commit comments