Skip to content

Commit

Permalink
tests: Add Response Tests (#120)
Browse files Browse the repository at this point in the history
* Add tests

* -

* -

* -

* -

* -

* -
  • Loading branch information
OskarStark authored Oct 5, 2024
1 parent ff1aa9f commit f3b464f
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/Response/ChoiceResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Response;

use PhpLlm\LlmChain\Exception\InvalidArgumentException;
use PhpLlm\LlmChain\Response\Choice;
use PhpLlm\LlmChain\Response\ChoiceResponse;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(ChoiceResponse::class)]
#[UsesClass(Choice::class)]
#[Small]
final class ChoiceResponseTest extends TestCase
{
#[Test]
public function choiceResponseCreation(): void
{
$choice1 = new Choice('choice1');
$choice2 = new Choice(null);
$choice3 = new Choice('choice3');
$response = new ChoiceResponse($choice1, $choice2, $choice3);

self::assertCount(3, $response->getContent());
self::assertSame('choice1', $response->getContent()[0]->getContent());
self::assertNull($response->getContent()[1]->getContent());
self::assertSame('choice3', $response->getContent()[2]->getContent());
}

#[Test]
public function choiceResponseWithNoChoices(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Response must have at least one choice.');

new ChoiceResponse();
}
}
34 changes: 34 additions & 0 deletions tests/Response/StreamResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Response;

use PhpLlm\LlmChain\Response\StreamResponse;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(StreamResponse::class)]
#[Small]
final class StreamResponseTest extends TestCase
{
#[Test]
public function getContent(): void
{
$generator = (function () {
yield 'data1';
yield 'data2';
})();

$response = new StreamResponse($generator);
self::assertInstanceOf(\Generator::class, $response->getContent());

$content = iterator_to_array($response->getContent());

self::assertCount(2, $content);
self::assertSame('data1', $content[0]);
self::assertSame('data2', $content[1]);
}
}
30 changes: 30 additions & 0 deletions tests/Response/StructuredResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Response;

use PhpLlm\LlmChain\Response\StructuredResponse;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(StructuredResponse::class)]
#[Small]
final class StructuredResponseTest extends TestCase
{
#[Test]
public function getContentWithArray(): void
{
$response = new StructuredResponse($expected = ['foo' => 'bar', 'baz' => ['qux']]);
self::assertSame($expected, $response->getContent());
}

#[Test]
public function getContentWithObject(): void
{
$response = new StructuredResponse($expected = (object) ['foo' => 'bar', 'baz' => ['qux']]);
self::assertSame($expected, $response->getContent());
}
}
23 changes: 23 additions & 0 deletions tests/Response/TextResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Response;

use PhpLlm\LlmChain\Response\TextResponse;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(TextResponse::class)]
#[Small]
final class TextResponseTest extends TestCase
{
#[Test]
public function getContent(): void
{
$response = new TextResponse($expected = 'foo');
self::assertSame($expected, $response->getContent());
}
}
36 changes: 36 additions & 0 deletions tests/Response/TollCallResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Response;

use PhpLlm\LlmChain\Exception\InvalidArgumentException;
use PhpLlm\LlmChain\Response\ToolCall;
use PhpLlm\LlmChain\Response\ToolCallResponse;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(ToolCallResponse::class)]
#[UsesClass(ToolCall::class)]
#[Small]
final class TollCallResponseTest extends TestCase
{
#[Test]
public function throwsIfNoToolCall(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Response must have at least one tool call.');

new ToolCallResponse();
}

#[Test]
public function getContent(): void
{
$response = new ToolCallResponse($toolCall = new ToolCall('ID', 'name', ['foo' => 'bar']));
self::assertSame([$toolCall], $response->getContent());
}
}

0 comments on commit f3b464f

Please sign in to comment.