Skip to content

Commit f3a3be3

Browse files
committed
Convert embedded metadata document to stdClass
1 parent ed114f1 commit f3a3be3

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

tests/Fixtures/Codec/TestFileCodec.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use MongoDB\Exception\UnsupportedValueException;
1212
use MongoDB\Tests\Fixtures\Document\TestFile;
1313

14+
use function assert;
15+
1416
final class TestFileCodec implements DocumentCodec
1517
{
1618
use DecodeIfSupported;
@@ -31,13 +33,17 @@ public function decode($value): TestFile
3133
$fileObject->id = $value->get('_id');
3234
$fileObject->length = (int) $value->get('length');
3335
$fileObject->chunkSize = (int) $value->get('chunkSize');
34-
$fileObject->filename = $value->get('filename');
36+
$fileObject->filename = (string) $value->get('filename');
3537

3638
$uploadDate = $value->get('uploadDate');
37-
$fileObject->uploadDate = $uploadDate ? DateTimeImmutable::createFromMutable($uploadDate->toDateTime()) : null;
39+
if ($uploadDate instanceof UTCDateTime) {
40+
$fileObject->uploadDate = DateTimeImmutable::createFromMutable($uploadDate->toDateTime());
41+
}
3842

3943
if ($value->has('metadata')) {
40-
$fileObject->metadata = $value->get('metadata');
44+
$metadata = $value->get('metadata');
45+
assert($metadata instanceof Document);
46+
$fileObject->metadata = $metadata->toPHP();
4147
}
4248

4349
return $fileObject;

tests/Fixtures/Document/TestFile.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace MongoDB\Tests\Fixtures\Document;
1919

2020
use DateTimeImmutable;
21+
use stdClass;
2122

2223
final class TestFile
2324
{
@@ -26,5 +27,5 @@ final class TestFile
2627
public int $chunkSize;
2728
public ?DateTimeImmutable $uploadDate = null;
2829
public string $filename;
29-
public $metadata;
30+
public ?stdClass $metadata = null;
3031
}

tests/GridFS/BucketFunctionalTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace MongoDB\Tests\GridFS;
44

55
use MongoDB\BSON\Binary;
6-
use MongoDB\BSON\Document;
76
use MongoDB\Collection;
87
use MongoDB\Driver\ReadConcern;
98
use MongoDB\Driver\ReadPreference;
@@ -19,6 +18,7 @@
1918
use MongoDB\Tests\Fixtures\Codec\TestDocumentCodec;
2019
use MongoDB\Tests\Fixtures\Codec\TestFileCodec;
2120
use MongoDB\Tests\Fixtures\Document\TestFile;
21+
use stdClass;
2222

2323
use function array_merge;
2424
use function call_user_func;
@@ -509,8 +509,8 @@ public function testGetFileDocumentForStreamUsesCodec(): void
509509
$this->assertInstanceOf(TestFile::class, $fileDocument);
510510

511511
$this->assertSame('filename', $fileDocument->filename);
512-
$this->assertInstanceOf(Document::class, $fileDocument->metadata);
513-
$this->assertSame($metadata, $fileDocument->metadata->toPHP(['root' => 'array']));
512+
$this->assertInstanceOf(stdClass::class, $fileDocument->metadata);
513+
$this->assertSame($metadata, (array) $fileDocument->metadata);
514514
}
515515

516516
public function testGetFileDocumentForStreamWithReadableStream(): void

0 commit comments

Comments
 (0)