|
17 | 17 | use Symfony\AI\Platform\Message\Content\Text; |
18 | 18 | use Symfony\AI\Platform\Message\Message; |
19 | 19 | use Symfony\AI\Platform\Result\ToolCall; |
| 20 | +use Symfony\AI\Platform\Tests\Fixtures\StructuredOutput\City; |
20 | 21 |
|
21 | 22 | final class MessageTest extends TestCase |
22 | 23 | { |
@@ -122,4 +123,117 @@ public function testCreateToolCallMessage() |
122 | 123 | $this->assertSame('Foo bar.', $message->getContent()); |
123 | 124 | $this->assertSame($toolCall, $message->getToolCall()); |
124 | 125 | } |
| 126 | + |
| 127 | + public function testCreateUserMessageWithObjectSerializesItAsContext() |
| 128 | + { |
| 129 | + $city = new City(name: 'Berlin', population: 3500000); |
| 130 | + $message = Message::ofUser('Please research missing data', $city); |
| 131 | + |
| 132 | + $content = $message->getContent(); |
| 133 | + $this->assertCount(2, $content); |
| 134 | + |
| 135 | + $this->assertInstanceOf(Text::class, $content[0]); |
| 136 | + $this->assertSame('Please research missing data', $content[0]->getText()); |
| 137 | + |
| 138 | + $this->assertInstanceOf(Text::class, $content[1]); |
| 139 | + $serializedContent = $content[1]->getText(); |
| 140 | + $this->assertStringContainsString('Berlin', $serializedContent); |
| 141 | + $this->assertStringContainsString('3500000', $serializedContent); |
| 142 | + $this->assertStringContainsString('"name"', $serializedContent); |
| 143 | + $this->assertStringContainsString('"population"', $serializedContent); |
| 144 | + } |
| 145 | + |
| 146 | + public function testCreateUserMessageWithObjectStoresItInMetadata() |
| 147 | + { |
| 148 | + $city = new City(name: 'Berlin'); |
| 149 | + $message = Message::ofUser('Research data', $city); |
| 150 | + |
| 151 | + $this->assertTrue($message->getMetadata()->has('structured_output_object')); |
| 152 | + $this->assertSame($city, $message->getMetadata()->get('structured_output_object')); |
| 153 | + } |
| 154 | + |
| 155 | + public function testCreateUserMessageWithMultipleContentAndObject() |
| 156 | + { |
| 157 | + $city = new City(name: 'Paris'); |
| 158 | + $message = Message::ofUser( |
| 159 | + 'Check this city', |
| 160 | + new ImageUrl('http://example.com/paris.jpg'), |
| 161 | + $city |
| 162 | + ); |
| 163 | + |
| 164 | + $content = $message->getContent(); |
| 165 | + $this->assertCount(3, $content); |
| 166 | + |
| 167 | + $this->assertInstanceOf(Text::class, $content[0]); |
| 168 | + $this->assertInstanceOf(ImageUrl::class, $content[1]); |
| 169 | + |
| 170 | + $this->assertInstanceOf(Text::class, $content[2]); |
| 171 | + $this->assertStringContainsString('Paris', $content[2]->getText()); |
| 172 | + } |
| 173 | + |
| 174 | + public function testCreateUserMessageWithOnlyObject() |
| 175 | + { |
| 176 | + $city = new City(name: 'Tokyo'); |
| 177 | + $message = Message::ofUser($city); |
| 178 | + |
| 179 | + $content = $message->getContent(); |
| 180 | + $this->assertCount(1, $content); |
| 181 | + |
| 182 | + $this->assertInstanceOf(Text::class, $content[0]); |
| 183 | + $serializedContent = $content[0]->getText(); |
| 184 | + $this->assertStringContainsString('Tokyo', $serializedContent); |
| 185 | + $this->assertStringContainsString('"name"', $serializedContent); |
| 186 | + } |
| 187 | + |
| 188 | + public function testCreateUserMessageWithStringableIsNotTreatedAsObject() |
| 189 | + { |
| 190 | + $stringable = new class implements \Stringable { |
| 191 | + public function __toString(): string |
| 192 | + { |
| 193 | + return 'I am stringable'; |
| 194 | + } |
| 195 | + }; |
| 196 | + |
| 197 | + $message = Message::ofUser('Hello', $stringable); |
| 198 | + |
| 199 | + $content = $message->getContent(); |
| 200 | + $this->assertCount(2, $content); |
| 201 | + |
| 202 | + $this->assertInstanceOf(Text::class, $content[0]); |
| 203 | + $this->assertInstanceOf(Text::class, $content[1]); |
| 204 | + $this->assertSame('I am stringable', $content[1]->getText()); |
| 205 | + |
| 206 | + $this->assertFalse($message->getMetadata()->has('structured_output_object')); |
| 207 | + } |
| 208 | + |
| 209 | + public function testCreateUserMessageWithContentInterfaceIsNotTreatedAsObject() |
| 210 | + { |
| 211 | + $imageUrl = new ImageUrl('http://example.com/image.jpg'); |
| 212 | + $message = Message::ofUser('Look at this', $imageUrl); |
| 213 | + |
| 214 | + $content = $message->getContent(); |
| 215 | + $this->assertCount(2, $content); |
| 216 | + |
| 217 | + $this->assertInstanceOf(Text::class, $content[0]); |
| 218 | + $this->assertInstanceOf(ImageUrl::class, $content[1]); |
| 219 | + |
| 220 | + $this->assertFalse($message->getMetadata()->has('structured_output_object')); |
| 221 | + } |
| 222 | + |
| 223 | + public function testCreateUserMessageThrowsExceptionForInvalidContentType() |
| 224 | + { |
| 225 | + $this->expectException(\InvalidArgumentException::class); |
| 226 | + $this->expectExceptionMessage('Content must be string, Stringable, or ContentInterface'); |
| 227 | + |
| 228 | + Message::ofUser('Hello', ['invalid' => 'array']); |
| 229 | + } |
| 230 | + |
| 231 | + public function testCreateUserMessageWithObjectAndInvalidTypeThrowsException() |
| 232 | + { |
| 233 | + $this->expectException(\InvalidArgumentException::class); |
| 234 | + $this->expectExceptionMessage('Content must be string, Stringable, or ContentInterface'); |
| 235 | + |
| 236 | + $city = new City(name: 'London'); |
| 237 | + Message::ofUser(123, 'text', $city); |
| 238 | + } |
125 | 239 | } |
0 commit comments