|
| 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 App\Tests\Blog\Command; |
| 13 | + |
| 14 | +use App\Blog\Command\StreamCommand; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\AI\Agent\AgentInterface; |
| 17 | +use Symfony\AI\Platform\Message\MessageBag; |
| 18 | +use Symfony\AI\Platform\Metadata\Metadata; |
| 19 | +use Symfony\AI\Platform\Result\RawResultInterface; |
| 20 | +use Symfony\AI\Platform\Result\ResultInterface; |
| 21 | +use Symfony\Component\Console\Input\ArrayInput; |
| 22 | +use Symfony\Component\Console\Output\BufferedOutput; |
| 23 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 24 | + |
| 25 | +class StreamCommandTest extends TestCase |
| 26 | +{ |
| 27 | + public function testStreamCommandOutputsStreamedContentAndSuccess() |
| 28 | + { |
| 29 | + $mockAgent = $this->createMock(AgentInterface::class); |
| 30 | + $mockAgent |
| 31 | + ->method('call') |
| 32 | + ->with($this->isInstanceOf(MessageBag::class), ['stream' => true]) |
| 33 | + ->willReturn(new class implements ResultInterface { |
| 34 | + public function getContent(): iterable |
| 35 | + { |
| 36 | + yield 'Hello'; |
| 37 | + yield ' '; |
| 38 | + yield 'world'; |
| 39 | + yield '!'; |
| 40 | + } |
| 41 | + |
| 42 | + public function getMetadata(): Metadata |
| 43 | + { |
| 44 | + } |
| 45 | + |
| 46 | + public function getRawResult(): ?RawResultInterface |
| 47 | + { |
| 48 | + } |
| 49 | + |
| 50 | + public function setRawResult(RawResultInterface $rawResult): void |
| 51 | + { |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + $input = new ArrayInput([]); |
| 56 | + $input->setInteractive(false); |
| 57 | + $io = new SymfonyStyle($input, $buffer = new BufferedOutput()); |
| 58 | + $command = new StreamCommand($mockAgent); |
| 59 | + $command->__invoke($io); |
| 60 | + |
| 61 | + $output = $buffer->fetch(); |
| 62 | + |
| 63 | + $this->assertStringContainsString('Stream Example Command', $output); |
| 64 | + $this->assertStringContainsString('This command demonstrates streaming output', $output); |
| 65 | + $this->assertStringContainsString('Agent Response:', $output); |
| 66 | + $this->assertStringContainsString('Hello world!', $output); |
| 67 | + $this->assertStringContainsString('The command has completed successfully.', $output); |
| 68 | + } |
| 69 | +} |
0 commit comments