-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: tests for simple value classes (#10)
- Loading branch information
1 parent
e5a02e4
commit d67c5e7
Showing
14 changed files
with
513 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
vendor | ||
.php-cs-fixer.cache | ||
.phpunit.cache | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Message; | ||
|
||
use PhpLlm\LlmChain\Message\Message; | ||
use PhpLlm\LlmChain\Message\MessageBag; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Small; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(MessageBag::class)] | ||
#[UsesClass(Message::class)] | ||
#[Small] | ||
final class MessageBagTest extends TestCase | ||
{ | ||
public function testGetSystemMessage(): void | ||
{ | ||
$messageBag = new MessageBag( | ||
Message::forSystem('My amazing system prompt.'), | ||
Message::ofAssistant('It is time to sleep.'), | ||
Message::ofUser('Hello, world!'), | ||
); | ||
|
||
$systemMessage = $messageBag->getSystemMessage(); | ||
|
||
self::assertSame('My amazing system prompt.', $systemMessage->content); | ||
} | ||
|
||
public function testGetSystemMessageWithoutSystemMessage(): void | ||
{ | ||
$messageBag = new MessageBag( | ||
Message::ofAssistant('It is time to sleep.'), | ||
Message::ofUser('Hello, world!'), | ||
); | ||
|
||
self::assertNull($messageBag->getSystemMessage()); | ||
} | ||
|
||
public function testWith(): void | ||
{ | ||
$messageBag = new MessageBag( | ||
Message::forSystem('My amazing system prompt.'), | ||
Message::ofAssistant('It is time to sleep.'), | ||
Message::ofUser('Hello, world!'), | ||
); | ||
|
||
$newMessage = Message::ofAssistant('It is time to wake up.'); | ||
$newMessageBag = $messageBag->with($newMessage); | ||
|
||
self::assertCount(3, $messageBag); | ||
self::assertCount(4, $newMessageBag); | ||
self::assertSame('It is time to wake up.', $newMessageBag[3]->content); | ||
} | ||
|
||
public function testWithoutSystemMessage(): void | ||
{ | ||
$messageBag = new MessageBag( | ||
Message::forSystem('My amazing system prompt.'), | ||
Message::ofAssistant('It is time to sleep.'), | ||
Message::ofUser('Hello, world!'), | ||
); | ||
|
||
$newMessageBag = $messageBag->withoutSystemMessage(); | ||
|
||
self::assertCount(3, $messageBag); | ||
self::assertCount(2, $newMessageBag); | ||
self::assertSame('It is time to sleep.', $newMessageBag[0]->content); | ||
} | ||
|
||
public function testPrepend(): void | ||
{ | ||
$messageBag = new MessageBag( | ||
Message::ofAssistant('It is time to sleep.'), | ||
Message::ofUser('Hello, world!'), | ||
); | ||
|
||
$newMessage = Message::forSystem('My amazing system prompt.'); | ||
$newMessageBag = $messageBag->prepend($newMessage); | ||
|
||
self::assertCount(2, $messageBag); | ||
self::assertCount(3, $newMessageBag); | ||
self::assertSame('My amazing system prompt.', $newMessageBag[0]->content); | ||
} | ||
|
||
public function testJsonSerialize(): void | ||
{ | ||
$messageBag = new MessageBag( | ||
Message::forSystem('My amazing system prompt.'), | ||
Message::ofAssistant('It is time to sleep.'), | ||
Message::ofUser('Hello, world!'), | ||
); | ||
|
||
$json = json_encode($messageBag); | ||
|
||
self::assertJson($json); | ||
self::assertJsonStringEqualsJsonString( | ||
json_encode([ | ||
['role' => 'system', 'content' => 'My amazing system prompt.'], | ||
['role' => 'assistant', 'content' => 'It is time to sleep.'], | ||
['role' => 'user', 'content' => 'Hello, world!'], | ||
]), | ||
$json | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Message; | ||
|
||
use PhpLlm\LlmChain\Message\Message; | ||
use PhpLlm\LlmChain\Response\ToolCall; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Small; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(Message::class)] | ||
#[UsesClass(ToolCall::class)] | ||
#[Small] | ||
final class MessageTest extends TestCase | ||
{ | ||
public function testCreateSystemMessage(): void | ||
{ | ||
$message = Message::forSystem('My amazing system prompt.'); | ||
|
||
self::assertSame('My amazing system prompt.', $message->content); | ||
self::assertTrue($message->isSystem()); | ||
self::assertFalse($message->isAssistant()); | ||
self::assertFalse($message->isUser()); | ||
self::assertFalse($message->isToolCall()); | ||
self::assertFalse($message->hasToolCalls()); | ||
} | ||
|
||
public function testCreateAssistantMessage(): void | ||
{ | ||
$message = Message::ofAssistant('It is time to sleep.'); | ||
|
||
self::assertSame('It is time to sleep.', $message->content); | ||
self::assertFalse($message->isSystem()); | ||
self::assertTrue($message->isAssistant()); | ||
self::assertFalse($message->isUser()); | ||
self::assertFalse($message->isToolCall()); | ||
self::assertFalse($message->hasToolCalls()); | ||
} | ||
|
||
public function testCreateAssistantMessageWithToolCalls(): void | ||
{ | ||
$toolCalls = [ | ||
new ToolCall('call_123456', 'my_tool', ['foo' => 'bar']), | ||
new ToolCall('call_456789', 'my_faster_tool'), | ||
]; | ||
$message = Message::ofAssistant(toolCalls: $toolCalls); | ||
|
||
self::assertCount(2, $message->toolCalls); | ||
self::assertFalse($message->isSystem()); | ||
self::assertTrue($message->isAssistant()); | ||
self::assertFalse($message->isUser()); | ||
self::assertFalse($message->isToolCall()); | ||
self::assertTrue($message->hasToolCalls()); | ||
} | ||
|
||
public function testCreateUserMessage(): void | ||
{ | ||
$message = Message::ofUser('Hi, my name is John.'); | ||
|
||
self::assertSame('Hi, my name is John.', $message->content); | ||
self::assertFalse($message->isSystem()); | ||
self::assertFalse($message->isAssistant()); | ||
self::assertTrue($message->isUser()); | ||
self::assertFalse($message->isToolCall()); | ||
self::assertFalse($message->hasToolCalls()); | ||
} | ||
|
||
public function testCreateToolCallMessage(): void | ||
{ | ||
$toolCall = new ToolCall('call_123456', 'my_tool', ['foo' => 'bar']); | ||
$message = Message::ofToolCall($toolCall, 'Foo bar.'); | ||
|
||
self::assertSame('Foo bar.', $message->content); | ||
self::assertCount(1, $message->toolCalls); | ||
self::assertFalse($message->isSystem()); | ||
self::assertFalse($message->isAssistant()); | ||
self::assertFalse($message->isUser()); | ||
self::assertTrue($message->isToolCall()); | ||
self::assertTrue($message->hasToolCalls()); | ||
} | ||
|
||
#[DataProvider('provideJsonScenarios')] | ||
public function testJsonSerialize(Message $message, array $expected): void | ||
{ | ||
self::assertSame($expected, $message->jsonSerialize()); | ||
} | ||
|
||
public static function provideJsonScenarios(): array | ||
{ | ||
$toolCall1 = new ToolCall('call_123456', 'my_tool', ['foo' => 'bar']); | ||
$toolCall2 = new ToolCall('call_456789', 'my_faster_tool'); | ||
|
||
return [ | ||
'system' => [ | ||
Message::forSystem('My amazing system prompt.'), | ||
[ | ||
'role' => 'system', | ||
'content' => 'My amazing system prompt.', | ||
], | ||
], | ||
'assistant' => [ | ||
Message::ofAssistant('It is time to sleep.'), | ||
[ | ||
'role' => 'assistant', | ||
'content' => 'It is time to sleep.', | ||
], | ||
], | ||
'assistant_with_tool_calls' => [ | ||
Message::ofAssistant(toolCalls: [$toolCall1, $toolCall2]), | ||
[ | ||
'role' => 'assistant', | ||
'tool_calls' => [$toolCall1, $toolCall2], | ||
], | ||
], | ||
'user' => [ | ||
Message::ofUser('Hi, my name is John.'), | ||
[ | ||
'role' => 'user', | ||
'content' => 'Hi, my name is John.', | ||
], | ||
], | ||
'tool_call' => [ | ||
Message::ofToolCall($toolCall1, 'Foo bar.'), | ||
[ | ||
'role' => 'tool', | ||
'content' => 'Foo bar.', | ||
'tool_call_id' => 'call_123456', | ||
], | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.