Skip to content

Commit

Permalink
Convert embedded metadata document to stdClass
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Sep 8, 2023
1 parent 3a665f1 commit a326ea4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
12 changes: 9 additions & 3 deletions tests/Fixtures/Codec/TestFileCodec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use MongoDB\Exception\UnsupportedValueException;
use MongoDB\Tests\Fixtures\Document\TestFile;

use function assert;

final class TestFileCodec implements DocumentCodec
{
use DecodeIfSupported;
Expand All @@ -31,13 +33,17 @@ public function decode($value): TestFile
$fileObject->id = $value->get('_id');
$fileObject->length = (int) $value->get('length');
$fileObject->chunkSize = (int) $value->get('chunkSize');
$fileObject->filename = $value->get('filename');
$fileObject->filename = (string) $value->get('filename');

$uploadDate = $value->get('uploadDate');
$fileObject->uploadDate = $uploadDate ? DateTimeImmutable::createFromMutable($uploadDate->toDateTime()) : null;
if ($uploadDate instanceof UTCDateTime) {
$fileObject->uploadDate = DateTimeImmutable::createFromMutable($uploadDate->toDateTime());
}

if ($value->has('metadata')) {
$fileObject->metadata = $value->get('metadata');
$metadata = $value->get('metadata');
assert($metadata instanceof Document);
$fileObject->metadata = $metadata->toPHP();
}

return $fileObject;
Expand Down
3 changes: 2 additions & 1 deletion tests/Fixtures/Document/TestFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace MongoDB\Tests\Fixtures\Document;

use DateTimeImmutable;
use stdClass;

final class TestFile
{
Expand All @@ -26,5 +27,5 @@ final class TestFile
public int $chunkSize;
public ?DateTimeImmutable $uploadDate = null;
public string $filename;
public $metadata;
public ?stdClass $metadata = null;
}
6 changes: 3 additions & 3 deletions tests/GridFS/BucketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace MongoDB\Tests\GridFS;

use MongoDB\BSON\Binary;
use MongoDB\BSON\Document;
use MongoDB\Collection;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
Expand All @@ -19,6 +18,7 @@
use MongoDB\Tests\Fixtures\Codec\TestDocumentCodec;
use MongoDB\Tests\Fixtures\Codec\TestFileCodec;
use MongoDB\Tests\Fixtures\Document\TestFile;
use stdClass;

use function array_merge;
use function call_user_func;
Expand Down Expand Up @@ -509,8 +509,8 @@ public function testGetFileDocumentForStreamUsesCodec(): void
$this->assertInstanceOf(TestFile::class, $fileDocument);

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

public function testGetFileDocumentForStreamWithReadableStream(): void
Expand Down

0 comments on commit a326ea4

Please sign in to comment.