Skip to content

Commit bb3d8ac

Browse files
committed
Test hydratation with every typeMap
1 parent 51ff7a4 commit bb3d8ac

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

src/Query/Builder.php

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use InvalidArgumentException;
2121
use LogicException;
2222
use MongoDB\BSON\Binary;
23-
use MongoDB\BSON\Document;
2423
use MongoDB\BSON\ObjectID;
2524
use MongoDB\BSON\Regex;
2625
use MongoDB\BSON\UTCDateTime;

tests/ModelTest.php

+46-20
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
use MongoDB\Laravel\Tests\Models\Soft;
2929
use MongoDB\Laravel\Tests\Models\SqlUser;
3030
use MongoDB\Laravel\Tests\Models\User;
31+
use MongoDB\Model\BSONArray;
32+
use MongoDB\Model\BSONDocument;
3133
use PHPUnit\Framework\Attributes\DataProvider;
3234
use PHPUnit\Framework\Attributes\TestWith;
3335

@@ -907,35 +909,48 @@ public function testRaw(): void
907909
$this->assertInstanceOf(EloquentCollection::class, $users);
908910
$this->assertInstanceOf(User::class, $users[0]);
909911

910-
$user = User::raw(function (Collection $collection) {
911-
return $collection->findOne(
912-
['age' => 35],
913-
['projection' => ['_id' => 1, 'name' => 1, 'age' => 1, 'now' => '$$NOW']],
914-
);
915-
});
916-
917-
$this->assertInstanceOf(User::class, $user);
918-
$this->assertArrayNotHasKey('_id', $user->getAttributes());
919-
$this->assertArrayHasKey('id', $user->getAttributes());
920-
$this->assertNotEmpty($user->id);
921-
$this->assertInstanceOf(Carbon::class, $user->now);
922-
923912
$count = User::raw(function (Collection $collection) {
924-
return $collection->count();
913+
return $collection->estimatedDocumentCount();
925914
});
926915
$this->assertEquals(3, $count);
927916

928917
$result = User::raw(function (Collection $collection) {
929918
return $collection->insertOne(['name' => 'Yvonne Yoe', 'age' => 35]);
930919
});
931920
$this->assertNotNull($result);
921+
}
932922

933-
$result = User::raw(function (Collection $collection) {
934-
return $collection->aggregate([
935-
['$set' => ['now' => '$$NOW']],
936-
['$limit' => 2],
937-
]);
938-
});
923+
#[DataProvider('provideTypeMap')]
924+
public function testRawHyradeModel(array $typeMap): void
925+
{
926+
User::insert([
927+
['name' => 'John Doe', 'age' => 35, 'embed' => ['foo' => 'bar'], 'list' => [1, 2, 3]],
928+
['name' => 'Jane Doe', 'age' => 35, 'embed' => ['foo' => 'bar'], 'list' => [1, 2, 3]],
929+
['name' => 'Harry Hoe', 'age' => 15, 'embed' => ['foo' => 'bar'], 'list' => [1, 2, 3]],
930+
]);
931+
932+
// Single document result
933+
$user = User::raw(fn (Collection $collection) => $collection->findOne(
934+
['age' => 35],
935+
[
936+
'projection' => ['_id' => 1, 'name' => 1, 'age' => 1, 'now' => '$$NOW', 'embed' => 1, 'list' => 1],
937+
'typeMap' => $typeMap,
938+
],
939+
));
940+
941+
$this->assertInstanceOf(User::class, $user);
942+
$this->assertArrayNotHasKey('_id', $user->getAttributes());
943+
$this->assertArrayHasKey('id', $user->getAttributes());
944+
$this->assertNotEmpty($user->id);
945+
$this->assertInstanceOf(Carbon::class, $user->now);
946+
$this->assertEquals(['foo' => 'bar'], (array) $user->embed);
947+
$this->assertEquals([1, 2, 3], (array) $user->list);
948+
949+
// Cursor result
950+
$result = User::raw(fn (Collection $collection) => $collection->aggregate([
951+
['$set' => ['now' => '$$NOW']],
952+
['$limit' => 2],
953+
], ['typeMap' => $typeMap]));
939954

940955
$this->assertInstanceOf(EloquentCollection::class, $result);
941956
$this->assertCount(2, $result);
@@ -945,6 +960,17 @@ public function testRaw(): void
945960
$this->assertArrayHasKey('id', $user->getAttributes());
946961
$this->assertNotEmpty($user->id);
947962
$this->assertInstanceOf(Carbon::class, $user->now);
963+
$this->assertEquals(['foo' => 'bar'], $user->embed);
964+
$this->assertEquals([1, 2, 3], $user->list);
965+
}
966+
967+
public static function provideTypeMap(): Generator
968+
{
969+
yield 'default' => [[]];
970+
yield 'array' => [['root' => 'array', 'document' => 'array', 'array' => 'array']];
971+
yield 'object' => [['root' => 'object', 'document' => 'object', 'array' => 'array']];
972+
yield 'Library BSON' => [['root' => BSONDocument::class, 'document' => BSONDocument::class, 'array' => BSONArray::class]];
973+
yield 'Driver BSON' => [['root' => 'bson', 'document' => 'bson', 'array' => 'bson']];
948974
}
949975

950976
public function testDotNotation(): void

0 commit comments

Comments
 (0)