28
28
use MongoDB \Laravel \Tests \Models \Soft ;
29
29
use MongoDB \Laravel \Tests \Models \SqlUser ;
30
30
use MongoDB \Laravel \Tests \Models \User ;
31
+ use MongoDB \Model \BSONArray ;
32
+ use MongoDB \Model \BSONDocument ;
31
33
use PHPUnit \Framework \Attributes \DataProvider ;
32
34
use PHPUnit \Framework \Attributes \TestWith ;
33
35
@@ -907,35 +909,48 @@ public function testRaw(): void
907
909
$ this ->assertInstanceOf (EloquentCollection::class, $ users );
908
910
$ this ->assertInstanceOf (User::class, $ users [0 ]);
909
911
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
-
923
912
$ count = User::raw (function (Collection $ collection ) {
924
- return $ collection ->count ();
913
+ return $ collection ->estimatedDocumentCount ();
925
914
});
926
915
$ this ->assertEquals (3 , $ count );
927
916
928
917
$ result = User::raw (function (Collection $ collection ) {
929
918
return $ collection ->insertOne (['name ' => 'Yvonne Yoe ' , 'age ' => 35 ]);
930
919
});
931
920
$ this ->assertNotNull ($ result );
921
+ }
932
922
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 ]));
939
954
940
955
$ this ->assertInstanceOf (EloquentCollection::class, $ result );
941
956
$ this ->assertCount (2 , $ result );
@@ -945,6 +960,17 @@ public function testRaw(): void
945
960
$ this ->assertArrayHasKey ('id ' , $ user ->getAttributes ());
946
961
$ this ->assertNotEmpty ($ user ->id );
947
962
$ 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 ' ]];
948
974
}
949
975
950
976
public function testDotNotation (): void
0 commit comments