Skip to content

Commit 0862508

Browse files
committed
[Store] Add InMemoryLoader for testing purposes
1 parent e1bf21e commit 0862508

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Store\Document\Loader;
13+
14+
use Symfony\AI\Store\Document\LoaderInterface;
15+
use Symfony\AI\Store\Document\TextDocument;
16+
17+
/**
18+
* @author Oskar Stark <[email protected]>
19+
*/
20+
final readonly class InMemoryLoader implements LoaderInterface
21+
{
22+
/**
23+
* @param array<TextDocument> $documents
24+
*/
25+
public function __construct(
26+
private array $documents = [],
27+
) {
28+
}
29+
30+
public function load(string $source, array $options = []): iterable
31+
{
32+
yield from $this->documents;
33+
}
34+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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\Store\Tests\Document\Loader;
13+
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\AI\Store\Document\Loader\InMemoryLoader;
17+
use Symfony\AI\Store\Document\Metadata;
18+
use Symfony\AI\Store\Document\TextDocument;
19+
use Symfony\Component\Uid\Uuid;
20+
21+
/**
22+
* @author Oskar Stark <[email protected]>
23+
*/
24+
#[CoversClass(InMemoryLoader::class)]
25+
final class InMemoryLoaderTest extends TestCase
26+
{
27+
public function testLoadWithEmptyDocuments()
28+
{
29+
$loader = new InMemoryLoader();
30+
$documents = iterator_to_array($loader->load('ignored-source'));
31+
32+
$this->assertEmpty($documents);
33+
}
34+
35+
public function testLoadWithSingleDocument()
36+
{
37+
$document = new TextDocument(Uuid::v4(), 'This is test content');
38+
$loader = new InMemoryLoader([$document]);
39+
40+
$documents = iterator_to_array($loader->load('ignored-source'));
41+
42+
$this->assertCount(1, $documents);
43+
$this->assertSame($document, $documents[0]);
44+
$this->assertSame('This is test content', $documents[0]->content);
45+
}
46+
47+
public function testLoadWithMultipleDocuments()
48+
{
49+
$document1 = new TextDocument(Uuid::v4(), 'First document');
50+
$document2 = new TextDocument(Uuid::v4(), 'Second document', new Metadata(['type' => 'test']));
51+
$loader = new InMemoryLoader([$document1, $document2]);
52+
53+
$documents = iterator_to_array($loader->load('ignored-source'));
54+
55+
$this->assertCount(2, $documents);
56+
$this->assertSame($document1, $documents[0]);
57+
$this->assertSame($document2, $documents[1]);
58+
$this->assertSame('First document', $documents[0]->content);
59+
$this->assertSame('Second document', $documents[1]->content);
60+
$this->assertSame('test', $documents[1]->metadata['type']);
61+
}
62+
63+
public function testLoadIgnoresSourceParameter()
64+
{
65+
$document = new TextDocument(Uuid::v4(), 'test content');
66+
$loader = new InMemoryLoader([$document]);
67+
68+
$documents1 = iterator_to_array($loader->load('source1'));
69+
$documents2 = iterator_to_array($loader->load('source2'));
70+
$documents3 = iterator_to_array($loader->load('any-source'));
71+
72+
$this->assertCount(1, $documents1);
73+
$this->assertCount(1, $documents2);
74+
$this->assertCount(1, $documents3);
75+
$this->assertSame($document, $documents1[0]);
76+
$this->assertSame($document, $documents2[0]);
77+
$this->assertSame($document, $documents3[0]);
78+
}
79+
}

0 commit comments

Comments
 (0)