18
18
use MongoDB \BSON \UTCDateTime ;
19
19
use PHPUnit \Framework \Attributes \DataProvider ;
20
20
21
+ use function get_debug_type ;
21
22
use function md5 ;
22
23
use function str_pad ;
23
24
use function str_repeat ;
@@ -33,14 +34,12 @@ public function testConversion(string $typeName, mixed $phpValue, mixed $bsonVal
33
34
$ bsonValue ??= $ phpValue ;
34
35
$ type = Type::getType ($ typeName );
35
36
36
- self ::assertEquals ($ phpValue , $ type ->convertToPHPValue ($ bsonValue ));
37
- self ::assertEquals ($ bsonValue , $ type ->convertToDatabaseValue ($ phpValue ));
37
+ self ::assertSameTypeAndValue ($ phpValue , $ type ->convertToPHPValue ($ bsonValue ));
38
+ self ::assertSameTypeAndValue ($ bsonValue , $ type ->convertToDatabaseValue ($ phpValue ));
38
39
}
39
40
40
41
public static function provideTypes (): array
41
42
{
42
- $ array = ['foo ' => 'bar ' ];
43
-
44
43
return [
45
44
'id ' => [Type::ID , '507f1f77bcf86cd799439011 ' , new ObjectId ('507f1f77bcf86cd799439011 ' )],
46
45
'intId ' => [Type::INTID , 1 ],
@@ -49,32 +48,32 @@ public static function provideTypes(): array
49
48
'boolean ' => [Type::BOOLEAN , false ],
50
49
'int ' => [Type::INT , 69 ],
51
50
'integer ' => [Type::INTEGER , 42 ],
52
- 'int64 ' => [Type::INT64 , 9223372036854775807 , new Int64 (9223372036854775807 )],
51
+ 'int64 ' => [Type::INT64 , 100 , new Int64 (100 )],
53
52
'float ' => [Type::FLOAT , 3.14 ],
54
53
'string ' => [Type::STRING , 'ohai ' ],
55
54
'minKey ' => [Type::KEY , 0 , new MinKey ()],
56
55
'maxKey ' => [Type::KEY , 1 , new MaxKey ()],
57
56
'timestamp ' => [Type::TIMESTAMP , $ t = time (), new Timestamp (0 , $ t )],
58
- 'binData ' => [Type::BINDATA , 'foobarbaz ' ],
59
- 'binDataFunc ' => [Type::BINDATAFUNC , 'foobarbaz ' ],
60
- 'binDataByteArray ' => [Type::BINDATABYTEARRAY , 'foobarbaz ' ],
61
- 'binDataUuid ' => [Type::BINDATAUUID , 'testtesttesttest ' ],
62
- 'binDataUuidRFC4122 ' => [Type::BINDATAUUIDRFC4122 , str_repeat ('a ' , 16 )],
63
- 'binDataMD5 ' => [Type::BINDATAMD5 , md5 ('ODM ' )],
64
- 'binDataCustom ' => [Type::BINDATACUSTOM , 'foobarbaz ' ],
57
+ 'binData ' => [Type::BINDATA , 'foobarbaz ' , new Binary ( ' foobarbaz ' , Binary:: TYPE_GENERIC ) ],
58
+ 'binDataFunc ' => [Type::BINDATAFUNC , 'foobarbaz ' , new Binary ( ' foobarbaz ' , Binary:: TYPE_FUNCTION ) ],
59
+ 'binDataByteArray ' => [Type::BINDATABYTEARRAY , 'foobarbaz ' , new Binary ( ' foobarbaz ' , Binary:: TYPE_OLD_BINARY ) ],
60
+ 'binDataUuid ' => [Type::BINDATAUUID , 'testtesttesttest ' , new Binary ( ' testtesttesttest ' , Binary:: TYPE_OLD_UUID ) ],
61
+ 'binDataUuidRFC4122 ' => [Type::BINDATAUUIDRFC4122 , str_repeat ('a ' , 16 ), new Binary ( str_repeat ( ' a ' , 16 ), Binary:: TYPE_UUID ) ],
62
+ 'binDataMD5 ' => [Type::BINDATAMD5 , md5 ('ODM ' ), new Binary ( md5 ( ' ODM ' ), Binary:: TYPE_MD5 ) ],
63
+ 'binDataCustom ' => [Type::BINDATACUSTOM , 'foobarbaz ' , new Binary ( ' foobarbaz ' , Binary:: TYPE_USER_DEFINED ) ],
65
64
'hash ' => [Type::HASH , ['foo ' => 'bar ' ], (object ) ['foo ' => 'bar ' ]],
66
65
'collection ' => [Type::COLLECTION , ['foo ' , 'bar ' ]],
67
- 'objectId ' => [Type::OBJECTID , '507f1f77bcf86cd799439011 ' ],
66
+ 'objectId ' => [Type::OBJECTID , '507f1f77bcf86cd799439011 ' , new ObjectId ( ' 507f1f77bcf86cd799439011 ' ) ],
68
67
'raw ' => [Type::RAW , (object ) ['foo ' => 'bar ' ]],
69
- 'decimal128 ' => [Type::DECIMAL128 , '4.20 ' ],
68
+ 'decimal128 ' => [Type::DECIMAL128 , '4.20 ' , new Decimal128 ( ' 4.20 ' ) ],
70
69
];
71
70
}
72
71
73
72
/** @param mixed $test */
74
73
#[DataProvider('provideTypesForIdempotent ' )]
75
74
public function testConversionIsIdempotent (Type $ type , $ test ): void
76
75
{
77
- self ::assertEquals ($ test , $ type ->convertToDatabaseValue ($ test ));
76
+ self ::assertSameTypeAndValue ($ test , $ type ->convertToDatabaseValue ($ test ));
78
77
}
79
78
80
79
public static function provideTypesForIdempotent (): array
@@ -83,6 +82,7 @@ public static function provideTypesForIdempotent(): array
83
82
'id ' => [Type::getType (Type::ID ), new ObjectId ()],
84
83
'date ' => [Type::getType (Type::DATE ), new UTCDateTime ()],
85
84
'dateImmutable ' => [Type::getType (Type::DATE_IMMUTABLE ), new UTCDateTime ()],
85
+ 'int64 ' => [Type::getType (Type::INT64 ), new Int64 (100 )],
86
86
'timestamp ' => [Type::getType (Type::TIMESTAMP ), new Timestamp (0 , time ())],
87
87
'binData ' => [Type::getType (Type::BINDATA ), new Binary ('foobarbaz ' , Binary::TYPE_GENERIC )],
88
88
'binDataFunc ' => [Type::getType (Type::BINDATAFUNC ), new Binary ('foobarbaz ' , Binary::TYPE_FUNCTION )],
@@ -125,4 +125,10 @@ public function testConvertImmutableDate(): void
125
125
126
126
self ::assertInstanceOf (UTCDateTime::class, Type::convertPHPToDatabaseValue ($ date ));
127
127
}
128
+
129
+ private static function assertSameTypeAndValue (mixed $ expected , mixed $ actual ): void
130
+ {
131
+ self ::assertSame (get_debug_type ($ expected ), get_debug_type ($ actual ));
132
+ self ::assertEquals ($ expected , $ actual );
133
+ }
128
134
}
0 commit comments