From 4d4132ea0337efe53b84384684b249c09595494b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 4 Dec 2024 12:06:40 +0100 Subject: [PATCH 1/2] PHPLIB-1600 Accept object in BSONDocument constructor --- src/Model/BSONDocument.php | 4 ++-- tests/Model/BSONDocumentTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Model/BSONDocument.php b/src/Model/BSONDocument.php index b9a9c0d80..57b1fc35e 100644 --- a/src/Model/BSONDocument.php +++ b/src/Model/BSONDocument.php @@ -52,10 +52,10 @@ public function __clone() * by default. * * @see https://php.net/arrayobject.construct - * @param array $input + * @param object|array $input * @psalm-param class-string>|class-string> $iteratorClass */ - public function __construct(array $input = [], int $flags = ArrayObject::ARRAY_AS_PROPS, string $iteratorClass = ArrayIterator::class) + public function __construct(array|object $input = [], int $flags = ArrayObject::ARRAY_AS_PROPS, string $iteratorClass = ArrayIterator::class) { parent::__construct($input, $flags, $iteratorClass); } diff --git a/tests/Model/BSONDocumentTest.php b/tests/Model/BSONDocumentTest.php index 4d832d591..f16ccfdad 100644 --- a/tests/Model/BSONDocumentTest.php +++ b/tests/Model/BSONDocumentTest.php @@ -23,6 +23,22 @@ public function testConstructorDefaultsToPropertyAccess(): void $this->assertSame('bar', $document->foo); } + public function testConstructorWithStandardObject(): void + { + $document = new BSONDocument((object) ['foo' => 'bar']); + $this->assertSame('bar', $document->foo); + } + + public function testConstructorWithClassObject(): void + { + $document = new BSONDocument(new class () { + public string $foo = 'bar'; + protected string $baz = 'qux'; + }); + $this->assertSame('bar', $document->foo); + $this->assertObjectNotHasProperty('baz', $document); + } + public function testBsonSerializeCastsToObject(): void { $data = [0 => 'foo', 2 => 'bar']; From 8abcce5f82bc4ebae9a5359782a92f1580201616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 10 Dec 2024 13:45:30 -0500 Subject: [PATCH 2/2] BSONDocument should only accept stdClass --- src/Model/BSONDocument.php | 4 ++-- tests/Model/BSONDocumentTest.php | 15 +++------------ 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/Model/BSONDocument.php b/src/Model/BSONDocument.php index 57b1fc35e..1ff8bc1e6 100644 --- a/src/Model/BSONDocument.php +++ b/src/Model/BSONDocument.php @@ -52,10 +52,10 @@ public function __clone() * by default. * * @see https://php.net/arrayobject.construct - * @param object|array $input + * @param array|stdClass $input * @psalm-param class-string>|class-string> $iteratorClass */ - public function __construct(array|object $input = [], int $flags = ArrayObject::ARRAY_AS_PROPS, string $iteratorClass = ArrayIterator::class) + public function __construct(array|stdClass $input = [], int $flags = ArrayObject::ARRAY_AS_PROPS, string $iteratorClass = ArrayIterator::class) { parent::__construct($input, $flags, $iteratorClass); } diff --git a/tests/Model/BSONDocumentTest.php b/tests/Model/BSONDocumentTest.php index f16ccfdad..2000aef63 100644 --- a/tests/Model/BSONDocumentTest.php +++ b/tests/Model/BSONDocumentTest.php @@ -25,18 +25,9 @@ public function testConstructorDefaultsToPropertyAccess(): void public function testConstructorWithStandardObject(): void { - $document = new BSONDocument((object) ['foo' => 'bar']); - $this->assertSame('bar', $document->foo); - } - - public function testConstructorWithClassObject(): void - { - $document = new BSONDocument(new class () { - public string $foo = 'bar'; - protected string $baz = 'qux'; - }); - $this->assertSame('bar', $document->foo); - $this->assertObjectNotHasProperty('baz', $document); + $object = (object) ['foo' => 'bar']; + $document = new BSONDocument($object); + $this->assertEquals($object, $document->bsonSerialize()); } public function testBsonSerializeCastsToObject(): void