|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Tool; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\AI\Agent\Toolbox\Tool\Scraper; |
| 16 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 17 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 18 | + |
| 19 | +final class ScraperTest extends TestCase |
| 20 | +{ |
| 21 | + public function testInvoke() |
| 22 | + { |
| 23 | + $htmlContent = file_get_contents(__DIR__.'/../../Fixtures/Tool/scraper-page.html'); |
| 24 | + $response = new MockResponse($htmlContent); |
| 25 | + $httpClient = new MockHttpClient($response); |
| 26 | + |
| 27 | + $scraper = new Scraper($httpClient); |
| 28 | + |
| 29 | + $result = $scraper('https://example.com'); |
| 30 | + |
| 31 | + $this->assertArrayHasKey('title', $result); |
| 32 | + $this->assertArrayHasKey('content', $result); |
| 33 | + $this->assertSame('Example Page Title', $result['title']); |
| 34 | + $this->assertStringContainsString('Welcome to Example Page', $result['content']); |
| 35 | + $this->assertStringContainsString('This is some visible text content.', $result['content']); |
| 36 | + } |
| 37 | + |
| 38 | + public function testSourceIsAdded() |
| 39 | + { |
| 40 | + $htmlContent = file_get_contents(__DIR__.'/../../Fixtures/Tool/scraper-page.html'); |
| 41 | + $response = new MockResponse($htmlContent); |
| 42 | + $httpClient = new MockHttpClient($response); |
| 43 | + |
| 44 | + $scraper = new Scraper($httpClient); |
| 45 | + |
| 46 | + $scraper('https://example.com'); |
| 47 | + |
| 48 | + $sources = $scraper->getSourceMap()->getSources(); |
| 49 | + $this->assertCount(1, $sources); |
| 50 | + $this->assertSame('Example Page Title', $sources[0]->getName()); |
| 51 | + $this->assertSame('https://example.com', $sources[0]->getReference()); |
| 52 | + $this->assertStringContainsString('Welcome to Example Page', $sources[0]->getContent()); |
| 53 | + } |
| 54 | +} |
0 commit comments