From 8a2e688ab5129a90e8acc0f3672ff08be199f742 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Sun, 22 Sep 2024 21:31:24 +0200 Subject: [PATCH] - --- rector.php | 14 ++++++----- src/ToolBox/Tool/Clock.php | 1 + src/ToolBox/Tool/SerpApi.php | 2 ++ src/ToolBox/Tool/Wikipedia.php | 3 +++ tests/Message/MessageBagTest.php | 19 ++++++++++----- tests/Message/MessageTest.php | 19 ++++++++++----- tests/Response/ChoiceTest.php | 13 ++++++---- tests/Response/ResponseTest.php | 25 +++++++++++++------- tests/Response/ToolCallTest.php | 7 ++++-- tests/StructuredOutput/SchemaFactoryTest.php | 10 +++++--- tests/ToolBox/ParameterAnalyzerTest.php | 10 +++++--- tests/ToolBox/RegistryTest.php | 7 ++++-- tests/ToolBox/ToolAnalyzerTest.php | 10 +++++--- 13 files changed, 97 insertions(+), 43 deletions(-) diff --git a/rector.php b/rector.php index 3c6542ae..a2c158ea 100644 --- a/rector.php +++ b/rector.php @@ -3,20 +3,22 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\PHPUnit\Set\PHPUnitSetList; use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector; +use Rector\PHPUnit\Set\PHPUnitSetList; return RectorConfig::configure() ->withPaths([ - __DIR__ . '/examples', - __DIR__ . '/src', - __DIR__ . '/tests', + __DIR__.'/examples', + __DIR__.'/src', + __DIR__.'/tests', ]) ->withPhpSets(php82: true) ->withSets([ - PHPUnitSetList::PHPUNIT_90, + PHPUnitSetList::PHPUNIT_110, + PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES, ]) + ->withImportNames(importNames: true, importShortClasses: false) ->withSkip([ - ClosureToArrowFunctionRector::class + ClosureToArrowFunctionRector::class, ]) ->withTypeCoverageLevel(0); diff --git a/src/ToolBox/Tool/Clock.php b/src/ToolBox/Tool/Clock.php index f800ffb5..8703e83c 100644 --- a/src/ToolBox/Tool/Clock.php +++ b/src/ToolBox/Tool/Clock.php @@ -14,6 +14,7 @@ public function __construct( private ClockInterface $clock, ) { } + public function __invoke(): string { return sprintf( diff --git a/src/ToolBox/Tool/SerpApi.php b/src/ToolBox/Tool/SerpApi.php index d65b7c93..b0033ce8 100644 --- a/src/ToolBox/Tool/SerpApi.php +++ b/src/ToolBox/Tool/SerpApi.php @@ -15,6 +15,7 @@ public function __construct( private string $apiKey, ) { } + /** * @param string $query The search query to use */ @@ -29,6 +30,7 @@ public function __invoke(string $query): string return sprintf('Results for "%s" are "%s".', $query, $this->extractBestResponse($response->toArray())); } + /** * @param array $results */ diff --git a/src/ToolBox/Tool/Wikipedia.php b/src/ToolBox/Tool/Wikipedia.php index fa6499a3..3c971364 100644 --- a/src/ToolBox/Tool/Wikipedia.php +++ b/src/ToolBox/Tool/Wikipedia.php @@ -16,6 +16,7 @@ public function __construct( private string $locale = 'en', ) { } + /** * @param string $query The query to search for on Wikipedia */ @@ -32,6 +33,7 @@ public function search(string $query): string return 'On Wikipedia, I found the following articles: '.implode(', ', $titles).'.'; } + /** * @param string $title The title of the article to retrieve from Wikipedia */ @@ -47,6 +49,7 @@ public function getArticle(string $title): string return current($result['query']['pages'])['extract']; } + /** * @param array $query * diff --git a/tests/Message/MessageBagTest.php b/tests/Message/MessageBagTest.php index 9e4e175b..f574e29c 100644 --- a/tests/Message/MessageBagTest.php +++ b/tests/Message/MessageBagTest.php @@ -8,6 +8,7 @@ use PhpLlm\LlmChain\Message\MessageBag; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Small; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\UsesClass; use PHPUnit\Framework\TestCase; @@ -16,7 +17,8 @@ #[Small] final class MessageBagTest extends TestCase { - public function testGetSystemMessage(): void + #[Test] + public function getSystemMessage(): void { $messageBag = new MessageBag( Message::forSystem('My amazing system prompt.'), @@ -29,7 +31,8 @@ public function testGetSystemMessage(): void self::assertSame('My amazing system prompt.', $systemMessage->content); } - public function testGetSystemMessageWithoutSystemMessage(): void + #[Test] + public function getSystemMessageWithoutSystemMessage(): void { $messageBag = new MessageBag( Message::ofAssistant('It is time to sleep.'), @@ -39,7 +42,8 @@ public function testGetSystemMessageWithoutSystemMessage(): void self::assertNull($messageBag->getSystemMessage()); } - public function testWith(): void + #[Test] + public function with(): void { $messageBag = new MessageBag( Message::forSystem('My amazing system prompt.'), @@ -55,7 +59,8 @@ public function testWith(): void self::assertSame('It is time to wake up.', $newMessageBag[3]->content); } - public function testWithoutSystemMessage(): void + #[Test] + public function withoutSystemMessage(): void { $messageBag = new MessageBag( Message::forSystem('My amazing system prompt.'), @@ -70,7 +75,8 @@ public function testWithoutSystemMessage(): void self::assertSame('It is time to sleep.', $newMessageBag[0]->content); } - public function testPrepend(): void + #[Test] + public function prepend(): void { $messageBag = new MessageBag( Message::ofAssistant('It is time to sleep.'), @@ -85,7 +91,8 @@ public function testPrepend(): void self::assertSame('My amazing system prompt.', $newMessageBag[0]->content); } - public function testJsonSerialize(): void + #[Test] + public function jsonSerialize(): void { $messageBag = new MessageBag( Message::forSystem('My amazing system prompt.'), diff --git a/tests/Message/MessageTest.php b/tests/Message/MessageTest.php index 667a2a5a..51e9f845 100644 --- a/tests/Message/MessageTest.php +++ b/tests/Message/MessageTest.php @@ -9,6 +9,7 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Small; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\UsesClass; use PHPUnit\Framework\TestCase; @@ -17,7 +18,8 @@ #[Small] final class MessageTest extends TestCase { - public function testCreateSystemMessage(): void + #[Test] + public function createSystemMessage(): void { $message = Message::forSystem('My amazing system prompt.'); @@ -29,7 +31,8 @@ public function testCreateSystemMessage(): void self::assertFalse($message->hasToolCalls()); } - public function testCreateAssistantMessage(): void + #[Test] + public function createAssistantMessage(): void { $message = Message::ofAssistant('It is time to sleep.'); @@ -41,7 +44,8 @@ public function testCreateAssistantMessage(): void self::assertFalse($message->hasToolCalls()); } - public function testCreateAssistantMessageWithToolCalls(): void + #[Test] + public function createAssistantMessageWithToolCalls(): void { $toolCalls = [ new ToolCall('call_123456', 'my_tool', ['foo' => 'bar']), @@ -57,7 +61,8 @@ public function testCreateAssistantMessageWithToolCalls(): void self::assertTrue($message->hasToolCalls()); } - public function testCreateUserMessage(): void + #[Test] + public function createUserMessage(): void { $message = Message::ofUser('Hi, my name is John.'); @@ -69,7 +74,8 @@ public function testCreateUserMessage(): void self::assertFalse($message->hasToolCalls()); } - public function testCreateToolCallMessage(): void + #[Test] + public function createToolCallMessage(): void { $toolCall = new ToolCall('call_123456', 'my_tool', ['foo' => 'bar']); $message = Message::ofToolCall($toolCall, 'Foo bar.'); @@ -84,7 +90,8 @@ public function testCreateToolCallMessage(): void } #[DataProvider('provideJsonScenarios')] - public function testJsonSerialize(Message $message, array $expected): void + #[Test] + public function jsonSerialize(Message $message, array $expected): void { self::assertSame($expected, $message->jsonSerialize()); } diff --git a/tests/Response/ChoiceTest.php b/tests/Response/ChoiceTest.php index 2e9a249c..1cb20181 100644 --- a/tests/Response/ChoiceTest.php +++ b/tests/Response/ChoiceTest.php @@ -8,6 +8,7 @@ use PhpLlm\LlmChain\Response\ToolCall; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Small; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\UsesClass; use PHPUnit\Framework\TestCase; @@ -16,7 +17,8 @@ #[Small] final class ChoiceTest extends TestCase { - public function testChoiceEmpty(): void + #[Test] + public function choiceEmpty(): void { $choice = new Choice(); self::assertFalse($choice->hasContent()); @@ -25,7 +27,8 @@ public function testChoiceEmpty(): void self::assertCount(0, $choice->getToolCalls()); } - public function testChoiceWithContent(): void + #[Test] + public function choiceWithContent(): void { $choice = new Choice('content'); self::assertTrue($choice->hasContent()); @@ -34,7 +37,8 @@ public function testChoiceWithContent(): void self::assertCount(0, $choice->getToolCalls()); } - public function testChoiceWithToolCall(): void + #[Test] + public function choiceWithToolCall(): void { $choice = new Choice(null, [new ToolCall('name', 'arguments')]); self::assertFalse($choice->hasContent()); @@ -43,7 +47,8 @@ public function testChoiceWithToolCall(): void self::assertCount(1, $choice->getToolCalls()); } - public function testChoiceWithContentAndToolCall(): void + #[Test] + public function choiceWithContentAndToolCall(): void { $choice = new Choice('content', [new ToolCall('name', 'arguments')]); self::assertTrue($choice->hasContent()); diff --git a/tests/Response/ResponseTest.php b/tests/Response/ResponseTest.php index 23d27759..e5eaf50f 100644 --- a/tests/Response/ResponseTest.php +++ b/tests/Response/ResponseTest.php @@ -9,6 +9,7 @@ use PhpLlm\LlmChain\Response\ToolCall; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Small; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\UsesClass; use PHPUnit\Framework\TestCase; @@ -18,7 +19,8 @@ #[Small] final class ResponseTest extends TestCase { - public function testGetChoices(): void + #[Test] + public function getChoices(): void { $response = new Response( new Choice('content', [new ToolCall('call_123456', 'name', ['foo' => 'bar'])]), @@ -28,7 +30,8 @@ public function testGetChoices(): void self::assertCount(2, $response->getChoices()); } - public function testConstructorThrowsException(): void + #[Test] + public function constructorThrowsException(): void { $this->expectException(\LogicException::class); $this->expectExceptionMessage('Response must have at least one choice'); @@ -36,7 +39,8 @@ public function testConstructorThrowsException(): void $response = new Response(); } - public function testGetContent(): void + #[Test] + public function getContent(): void { $response = new Response( new Choice('content', [new ToolCall('call_123456', 'name', ['foo' => 'bar'])]), @@ -45,7 +49,8 @@ public function testGetContent(): void self::assertSame('content', $response->getContent()); } - public function testGetContentThrowsException(): void + #[Test] + public function getContentThrowsException(): void { $response = new Response( new Choice('content', [new ToolCall('call_123456', 'name', ['foo' => 'bar'])]), @@ -58,7 +63,8 @@ public function testGetContentThrowsException(): void $response->getContent(); } - public function testGetToolCalls(): void + #[Test] + public function getToolCalls(): void { $response = new Response( new Choice('content', [new ToolCall('call_123456', 'name', ['foo' => 'bar'])]), @@ -67,7 +73,8 @@ public function testGetToolCalls(): void self::assertCount(1, $response->getToolCalls()); } - public function testGetToolCallsThrowsException(): void + #[Test] + public function getToolCallsThrowsException(): void { $response = new Response( new Choice('content', [new ToolCall('call_123456', 'name', ['foo' => 'bar'])]), @@ -80,7 +87,8 @@ public function testGetToolCallsThrowsException(): void $response->getToolCalls(); } - public function testHasToolCalls(): void + #[Test] + public function hasToolCalls(): void { $response = new Response( new Choice('content', [new ToolCall('call_123456', 'name', ['foo' => 'bar'])]), @@ -89,7 +97,8 @@ public function testHasToolCalls(): void self::assertTrue($response->hasToolCalls()); } - public function testHasToolCallsReturnsFalse(): void + #[Test] + public function hasToolCallsReturnsFalse(): void { $response = new Response(new Choice('content')); diff --git a/tests/Response/ToolCallTest.php b/tests/Response/ToolCallTest.php index a64fb7d5..7254b0d7 100644 --- a/tests/Response/ToolCallTest.php +++ b/tests/Response/ToolCallTest.php @@ -7,13 +7,15 @@ use PhpLlm\LlmChain\Response\ToolCall; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Small; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; #[CoversClass(ToolCall::class)] #[Small] final class ToolCallTest extends TestCase { - public function testToolCall(): void + #[Test] + public function toolCall(): void { $toolCall = new ToolCall('id', 'name', ['foo' => 'bar']); self::assertSame('id', $toolCall->id); @@ -21,7 +23,8 @@ public function testToolCall(): void self::assertSame(['foo' => 'bar'], $toolCall->arguments); } - public function testToolCallJsonSerialize(): void + #[Test] + public function toolCallJsonSerialize(): void { $toolCall = new ToolCall('id', 'name', ['foo' => 'bar']); self::assertSame( diff --git a/tests/StructuredOutput/SchemaFactoryTest.php b/tests/StructuredOutput/SchemaFactoryTest.php index 19864264..78f65788 100644 --- a/tests/StructuredOutput/SchemaFactoryTest.php +++ b/tests/StructuredOutput/SchemaFactoryTest.php @@ -9,6 +9,7 @@ use PhpLlm\LlmChain\Tests\StructuredOutput\Data\Step; use PhpLlm\LlmChain\Tests\StructuredOutput\Data\User; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; #[CoversClass(SchemaFactory::class)] @@ -21,7 +22,8 @@ protected function setUp(): void $this->schemaFactory = SchemaFactory::create(); } - public function testBuildSchemaForUserClass(): void + #[Test] + public function buildSchemaForUserClass(): void { $expected = [ 'title' => 'User', @@ -44,7 +46,8 @@ public function testBuildSchemaForUserClass(): void self::assertSame($expected, $actual); } - public function testBuildSchemaForMathReasoningClass(): void + #[Test] + public function buildSchemaForMathReasoningClass(): void { $expected = [ 'title' => 'MathReasoning', @@ -74,7 +77,8 @@ public function testBuildSchemaForMathReasoningClass(): void self::assertSame($expected, $actual); } - public function testBuildSchemaForStepClass(): void + #[Test] + public function buildSchemaForStepClass(): void { $expected = [ 'title' => 'Step', diff --git a/tests/ToolBox/ParameterAnalyzerTest.php b/tests/ToolBox/ParameterAnalyzerTest.php index f49215f2..7d5f1766 100644 --- a/tests/ToolBox/ParameterAnalyzerTest.php +++ b/tests/ToolBox/ParameterAnalyzerTest.php @@ -11,6 +11,7 @@ use PhpLlm\LlmChain\ToolBox\Metadata; use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\UsesClass; use PHPUnit\Framework\TestCase; @@ -27,7 +28,8 @@ protected function setUp(): void $this->analyzer = new ParameterAnalyzer(); } - public function testDetectParameterDefinitionRequired(): void + #[Test] + public function detectParameterDefinitionRequired(): void { $actual = $this->analyzer->getDefinition(ToolRequiredParams::class, 'bar'); $expected = [ @@ -51,7 +53,8 @@ public function testDetectParameterDefinitionRequired(): void self::assertSame($expected, $actual); } - public function testDetectParameterDefinitionOptional(): void + #[Test] + public function detectParameterDefinitionOptional(): void { $actual = $this->analyzer->getDefinition(ToolOptionalParam::class, 'bar'); $expected = [ @@ -74,7 +77,8 @@ public function testDetectParameterDefinitionOptional(): void self::assertSame($expected, $actual); } - public function testDetectParameterDefinitionNone(): void + #[Test] + public function detectParameterDefinitionNone(): void { $actual = $this->analyzer->getDefinition(ToolNoParams::class, '__invoke'); diff --git a/tests/ToolBox/RegistryTest.php b/tests/ToolBox/RegistryTest.php index a3231649..5206eac8 100644 --- a/tests/ToolBox/RegistryTest.php +++ b/tests/ToolBox/RegistryTest.php @@ -14,6 +14,7 @@ use PhpLlm\LlmChain\ToolBox\Registry; use PhpLlm\LlmChain\ToolBox\ToolAnalyzer; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\UsesClass; use PHPUnit\Framework\TestCase; @@ -37,7 +38,8 @@ protected function setUp(): void ]); } - public function testFunctionsMap(): void + #[Test] + public function functionsMap(): void { $actual = $this->registry->getMap(); $expected = [ @@ -100,7 +102,8 @@ public function testFunctionsMap(): void self::assertSame(json_encode($expected), json_encode($actual)); } - public function testExecute(): void + #[Test] + public function execute(): void { $actual = $this->registry->execute(new ToolCall('call_1234', 'tool_required_params', ['text' => 'Hello', 'number' => 3])); $expected = 'Hello says "3".'; diff --git a/tests/ToolBox/ToolAnalyzerTest.php b/tests/ToolBox/ToolAnalyzerTest.php index eba9fb02..914fc45a 100644 --- a/tests/ToolBox/ToolAnalyzerTest.php +++ b/tests/ToolBox/ToolAnalyzerTest.php @@ -13,6 +13,7 @@ use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer; use PhpLlm\LlmChain\ToolBox\ToolAnalyzer; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\UsesClass; use PHPUnit\Framework\TestCase; @@ -30,13 +31,15 @@ protected function setUp(): void $this->toolAnalyzer = new ToolAnalyzer(new ParameterAnalyzer()); } - public function testWithoutAttribute(): void + #[Test] + public function withoutAttribute(): void { $this->expectException(InvalidToolImplementation::class); iterator_to_array($this->toolAnalyzer->getMetadata(ToolWrong::class)); } - public function testGetDefinition(): void + #[Test] + public function getDefinition(): void { /** @var Metadata[] $actual */ $actual = iterator_to_array($this->toolAnalyzer->getMetadata(ToolRequiredParams::class)); @@ -49,7 +52,8 @@ public function testGetDefinition(): void self::assertIsArray($actual[0]->parameters); } - public function testGetDefinitionWithMultiple(): void + #[Test] + public function getDefinitionWithMultiple(): void { $actual = iterator_to_array($this->toolAnalyzer->getMetadata(ToolMultiple::class));