Skip to content

Commit b212194

Browse files
committed
support deserialization of objects with promoted properties
1 parent d30f53c commit b212194

File tree

6 files changed

+28
-27
lines changed

6 files changed

+28
-27
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"elasticsearch/elasticsearch": "^8.0",
3131
"friendsofphp/php-cs-fixer": "^3.75",
3232
"html2text/html2text": "^4.3",
33-
"illuminate/database": "^10.0",
33+
"illuminate/database": "^10.0|^11.0|^12.0",
3434
"phpstan/phpstan": "^2.1",
3535
"phpunit/phpunit": "^9.0",
3636
"rector/rector": "^2.0",

src/Chat/History/EloquentChatHistory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ protected function load(): void
2626
/** @var Model $model */
2727
$model = new $this->modelClass();
2828

29+
/** @var array<string, mixed> $messages */
2930
$messages = $model->newQuery()
3031
->where('thread_id', $this->threadId)
3132
->orderBy('id')
3233
->get()
34+
// @phpstan-ignore-next-line
3335
->map($this->recordToArray(...))
3436
->all();
3537

tests/ChatHistory/EloquentChatHistoryTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ public function test_serializes_message_meta_correctly(): void
248248

249249
/**
250250
* Mock Eloquent Model for testing
251+
*
252+
* @property string $thread_id
253+
* @property string $role
254+
* @property string $content
255+
* @property array $meta
251256
*/
252257
class ChatMessage extends Model
253258
{

tests/DeserializerTest.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use NeuronAI\StructuredOutput\SchemaProperty;
1010
use NeuronAI\Tests\Stubs\DummyEnum;
1111
use NeuronAI\Tests\Stubs\IntEnum;
12+
use NeuronAI\Tests\Stubs\StructuredOutput\ColorWithDefaults;
1213
use NeuronAI\Tests\Stubs\StructuredOutput\Person;
1314
use NeuronAI\Tests\Stubs\StructuredOutput\Tag;
1415
use NeuronAI\Tests\Stubs\StringEnum;
@@ -38,17 +39,6 @@ public function test_person_with_address(): void
3839
$this->assertEquals('Rome', $obj->address->city);
3940
}
4041

41-
public function test_constructor_as_new_instance(): void
42-
{
43-
// Check we can manually instantiate the class with values
44-
$greenObj = new ColorWithDefaults(0, 255, 0);
45-
46-
$this->assertInstanceOf(ColorWithDefaults::class, $greenObj);
47-
$this->assertEquals(0, $greenObj->r);
48-
$this->assertEquals(255, $greenObj->g);
49-
$this->assertEquals(0, $greenObj->b);
50-
}
51-
5242
public function test_constructor_deserialize_with_default_values(): void
5343
{
5444
// Create a new instance from json deserialization, where all properties are optional and have default values (will be black)
@@ -57,22 +47,23 @@ public function test_constructor_deserialize_with_default_values(): void
5747
$obj = Deserializer::make()->fromJson($json, ColorWithDefaults::class);
5848

5949
$this->assertInstanceOf(ColorWithDefaults::class, $obj);
60-
$this->assertEquals(0, $obj->r);
61-
$this->assertEquals(0, $obj->g);
62-
$this->assertEquals(0, $obj->b);
50+
$this->assertEquals(100, $obj->r);
51+
$this->assertEquals(100, $obj->g);
52+
$this->assertEquals(100, $obj->b);
6353
}
6454

6555
public function test_constructor_deserialize_with_provided_values(): void
6656
{
6757
// Create a new instance, where properties are being provided for a "green" color
68-
$json = '{"r": 0, "g": 255, "b": 0}';
58+
$json = '{"r": 255, "g": 0, "b": 0, "transparency": 100}';
6959

7060
$obj = Deserializer::make()->fromJson($json, ColorWithDefaults::class);
7161

7262
$this->assertInstanceOf(ColorWithDefaults::class, $obj);
73-
$this->assertEquals(0, $obj->r);
74-
$this->assertEquals(255, $obj->g);
63+
$this->assertEquals(255, $obj->r);
64+
$this->assertEquals(0, $obj->g);
7565
$this->assertEquals(0, $obj->b);
66+
$this->assertEquals(100, $obj->transparency);
7667
}
7768

7869
public function test_deserialize_array(): void
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
<?php
22

3-
declare( strict_types=1 );
3+
declare(strict_types=1);
44

55
namespace NeuronAI\Tests\Stubs\StructuredOutput;
66

77
use NeuronAI\StructuredOutput\SchemaProperty;
88

99
class ColorWithDefaults
1010
{
11+
#[SchemaProperty(description: "The alpha value", required: false)]
12+
public int $transparency;
1113

1214
public function __construct(
13-
#[SchemaProperty( description: "The RED", required: false )]
14-
public int $r = 0,
15-
#[SchemaProperty( description: "The GREEN", required: false )]
16-
public int $g = 0,
17-
#[SchemaProperty( description: "The BLUE", required: false )]
18-
public int $b = 0,
19-
) {}
15+
#[SchemaProperty(description: "The RED", required: false)]
16+
public int $r = 100,
17+
#[SchemaProperty(description: "The GREEN", required: false)]
18+
public int $g = 100,
19+
#[SchemaProperty(description: "The BLUE", required: false)]
20+
public int $b = 100,
21+
) {
22+
}
2023

2124
}

tests/Traits/CheckOpenPort.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
trait CheckOpenPort
88
{
9-
private function isPortOpen(string $host, int $port, int $timeout = 1): bool
9+
private function isPortOpen(string $host, int $port, float $timeout = 0.5): bool
1010
{
1111
$connection = @\fsockopen($host, $port, $errno, $errstr, $timeout);
1212
if (\is_resource($connection)) {

0 commit comments

Comments
 (0)