From f6dd3c0078032aa4ae0de495ed54de78bdd42a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 26 Jul 2023 18:51:02 +0200 Subject: [PATCH 1/4] PHPORM-35 Add various tests on Model _id --- tests/ModelTest.php | 103 +++++++++++++++++++++++++++++++- tests/Models/IdIsBinaryUuid.php | 17 ++++++ tests/Models/IdIsInt.php | 16 +++++ tests/Models/IdIsString.php | 16 +++++ 4 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 tests/Models/IdIsBinaryUuid.php create mode 100644 tests/Models/IdIsInt.php create mode 100644 tests/Models/IdIsString.php diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 21523c7..11da643 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -16,10 +16,14 @@ use Jenssegers\Mongodb\Eloquent\Model; use Jenssegers\Mongodb\Tests\Models\Book; use Jenssegers\Mongodb\Tests\Models\Guarded; +use Jenssegers\Mongodb\Tests\Models\IdIsBinaryUuid; +use Jenssegers\Mongodb\Tests\Models\IdIsInt; +use Jenssegers\Mongodb\Tests\Models\IdIsString; use Jenssegers\Mongodb\Tests\Models\Item; use Jenssegers\Mongodb\Tests\Models\MemberStatus; use Jenssegers\Mongodb\Tests\Models\Soft; use Jenssegers\Mongodb\Tests\Models\User; +use MongoDB\BSON\Binary; use MongoDB\BSON\ObjectID; use MongoDB\BSON\UTCDateTime; @@ -325,11 +329,106 @@ public function testSoftDelete(): void $this->assertEquals(2, Soft::count()); } - public function testPrimaryKey(): void + /** + * @dataProvider provideId + */ + public function testPrimaryKey(string $model, $id, string $type): void { - $user = new User; + $model::truncate(); + + $document = new $model; + $this->assertEquals('_id', $document->getKeyName()); + + $document->_id = $id; + $document->save(); + $this->assertSame($type, get_debug_type($document->_id)); + $this->assertEquals($id, $document->_id); + $this->assertSame($type, get_debug_type($document->getKey())); + $this->assertEquals($id, $document->getKey()); + + $check = $model::find($id); + + $this->assertNotNull($check, 'Not found'); + $this->assertSame($type, get_debug_type($check->_id)); + $this->assertEquals($id, $check->_id); + $this->assertSame($type, get_debug_type($check->getKey())); + $this->assertEquals($id, $check->getKey()); + } + + public static function provideId(): iterable + { + yield 'int' => [ + User::class, + 10, + 'int', + ]; + + yield 'cast as int' => [ + IdIsInt::class, + 10, + 'int', + ]; + + yield 'string' => [ + User::class, + 'user-10', + 'string', + ]; + + yield 'cast as string' => [ + IdIsString::class, + 'user-10', + 'string', + ]; + + yield 'ObjectID' => [ + User::class, + new ObjectID(), + 'string', + ]; + + yield 'BinaryUuid' => [ + User::class, + new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID), + 'string', + ]; + + yield 'cast as BinaryUuid' => [ + IdIsBinaryUuid::class, + new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID), + 'string', + ]; + + yield 'UTCDateTime' => [ + User::class, + new UTCDateTime(), + UTCDateTime::class, + ]; + } + + public function testPrimaryKeyBinaryUuid(): void + { + $user = new IdIsBinaryUuid; $this->assertEquals('_id', $user->getKeyName()); + $uuid = new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID); + $idAsString = (string) $uuid; + $user->_id = $uuid; + $user->name = 'John Doe'; + $user->save(); + $this->assertIsString($user->getKey()); + $this->assertSame($idAsString, $user->getKey()); + + $check = IdIsBinaryUuid::find($uuid); + $this->assertIsString($check->_id); + $this->assertSame($idAsString, $check->_id); + $this->assertIsString($check->getKey()); + $this->assertSame($idAsString, $check->getKey()); + $this->assertSame('John Doe', $check->name); + } + + public function testCustomPrimaryKey(): void + { $book = new Book; $this->assertEquals('title', $book->getKeyName()); diff --git a/tests/Models/IdIsBinaryUuid.php b/tests/Models/IdIsBinaryUuid.php new file mode 100644 index 0000000..1d8c592 --- /dev/null +++ b/tests/Models/IdIsBinaryUuid.php @@ -0,0 +1,17 @@ + BinaryUuid::class, + ]; +} diff --git a/tests/Models/IdIsInt.php b/tests/Models/IdIsInt.php new file mode 100644 index 0000000..33a5ab3 --- /dev/null +++ b/tests/Models/IdIsInt.php @@ -0,0 +1,16 @@ + 'int', + ]; +} diff --git a/tests/Models/IdIsString.php b/tests/Models/IdIsString.php new file mode 100644 index 0000000..48a2845 --- /dev/null +++ b/tests/Models/IdIsString.php @@ -0,0 +1,16 @@ + 'string', + ]; +} From c6e3f337f6a2849b800670f25f14551fa95d0011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 27 Jul 2023 14:48:25 +0200 Subject: [PATCH 2/4] Add assertion on expected value --- tests/ModelTest.php | 63 +++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 11da643..727e130 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -332,26 +332,27 @@ public function testSoftDelete(): void /** * @dataProvider provideId */ - public function testPrimaryKey(string $model, $id, string $type): void + public function testPrimaryKey(string $model, $id, $expected): void { $model::truncate(); + $expectedType = get_debug_type($expected); $document = new $model; $this->assertEquals('_id', $document->getKeyName()); $document->_id = $id; $document->save(); - $this->assertSame($type, get_debug_type($document->_id)); - $this->assertEquals($id, $document->_id); - $this->assertSame($type, get_debug_type($document->getKey())); - $this->assertEquals($id, $document->getKey()); + $this->assertSame($expectedType, get_debug_type($document->_id)); + $this->assertEquals($expected, $document->_id); + $this->assertSame($expectedType, get_debug_type($document->getKey())); + $this->assertEquals($expected, $document->getKey()); $check = $model::find($id); $this->assertNotNull($check, 'Not found'); - $this->assertSame($type, get_debug_type($check->_id)); + $this->assertSame($expectedType, get_debug_type($check->_id)); $this->assertEquals($id, $check->_id); - $this->assertSame($type, get_debug_type($check->getKey())); + $this->assertSame($expectedType, get_debug_type($check->getKey())); $this->assertEquals($id, $check->getKey()); } @@ -360,73 +361,55 @@ public static function provideId(): iterable yield 'int' => [ User::class, 10, - 'int', + 10, ]; yield 'cast as int' => [ IdIsInt::class, 10, - 'int', + 10, ]; yield 'string' => [ User::class, 'user-10', - 'string', + 'user-10', ]; yield 'cast as string' => [ IdIsString::class, 'user-10', - 'string', + 'user-10', ]; + $objectId = new ObjectID(); yield 'ObjectID' => [ User::class, - new ObjectID(), - 'string', + $objectId, + (string) $objectId, ]; + $binaryUuid = new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID); yield 'BinaryUuid' => [ User::class, - new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID), - 'string', + $binaryUuid, + (string) $binaryUuid, ]; yield 'cast as BinaryUuid' => [ IdIsBinaryUuid::class, - new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID), - 'string', + $binaryUuid, + (string) $binaryUuid, ]; + $date = new UTCDateTime(); yield 'UTCDateTime' => [ User::class, - new UTCDateTime(), - UTCDateTime::class, + $date, + $date, ]; } - public function testPrimaryKeyBinaryUuid(): void - { - $user = new IdIsBinaryUuid; - $this->assertEquals('_id', $user->getKeyName()); - - $uuid = new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID); - $idAsString = (string) $uuid; - $user->_id = $uuid; - $user->name = 'John Doe'; - $user->save(); - $this->assertIsString($user->getKey()); - $this->assertSame($idAsString, $user->getKey()); - - $check = IdIsBinaryUuid::find($uuid); - $this->assertIsString($check->_id); - $this->assertSame($idAsString, $check->_id); - $this->assertIsString($check->getKey()); - $this->assertSame($idAsString, $check->getKey()); - $this->assertSame('John Doe', $check->name); - } - public function testCustomPrimaryKey(): void { $book = new Book; From 831485c2c4804b3e949014bcc791af802796a7f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 27 Jul 2023 15:23:05 +0200 Subject: [PATCH 3/4] Test _id as array and object --- tests/ModelTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 727e130..d915d34 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -408,6 +408,20 @@ public static function provideId(): iterable $date, $date, ]; + + $array = ['foo' => 'bar']; + yield 'array' => [ + User::class, + $array, + $array, + ]; + + $object = (object) ['foo' => 'bar']; + yield 'object' => [ + User::class, + $object, + $object, + ]; } public function testCustomPrimaryKey(): void From 8402c59fb7c3c6d1a65b5aededc3b14a071d02ce Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 2 Aug 2023 15:25:50 +0200 Subject: [PATCH 4/4] Remove tests for arrays and objects as identifiers when keyType is string --- tests/ModelTest.php | 88 ++++++++++++++++++++-------------------- tests/Models/IdIsInt.php | 1 + 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/tests/ModelTest.php b/tests/ModelTest.php index d915d34..1042a07 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -332,7 +332,7 @@ public function testSoftDelete(): void /** * @dataProvider provideId */ - public function testPrimaryKey(string $model, $id, $expected): void + public function testPrimaryKey(string $model, $id, $expected, bool $expectedFound): void { $model::truncate(); $expectedType = get_debug_type($expected); @@ -349,78 +349,78 @@ public function testPrimaryKey(string $model, $id, $expected): void $check = $model::find($id); - $this->assertNotNull($check, 'Not found'); - $this->assertSame($expectedType, get_debug_type($check->_id)); - $this->assertEquals($id, $check->_id); - $this->assertSame($expectedType, get_debug_type($check->getKey())); - $this->assertEquals($id, $check->getKey()); + if ($expectedFound) { + $this->assertNotNull($check, 'Not found'); + $this->assertSame($expectedType, get_debug_type($check->_id)); + $this->assertEquals($id, $check->_id); + $this->assertSame($expectedType, get_debug_type($check->getKey())); + $this->assertEquals($id, $check->getKey()); + } else { + $this->assertNull($check, 'Found'); + } } public static function provideId(): iterable { yield 'int' => [ - User::class, - 10, - 10, + 'model' => User::class, + 'id' => 10, + 'expected' => 10, + // Don't expect this to be found, as the int is cast to string for the query + 'expectedFound' => false, ]; yield 'cast as int' => [ - IdIsInt::class, - 10, - 10, + 'model' => IdIsInt::class, + 'id' => 10, + 'expected' => 10, + 'expectedFound' => true, ]; yield 'string' => [ - User::class, - 'user-10', - 'user-10', + 'model' => User::class, + 'id' => 'user-10', + 'expected' => 'user-10', + 'expectedFound' => true, ]; yield 'cast as string' => [ - IdIsString::class, - 'user-10', - 'user-10', + 'model' => IdIsString::class, + 'id' => 'user-10', + 'expected' => 'user-10', + 'expectedFound' => true, ]; $objectId = new ObjectID(); yield 'ObjectID' => [ - User::class, - $objectId, - (string) $objectId, + 'model' => User::class, + 'id' => $objectId, + 'expected' => (string) $objectId, + 'expectedFound' => true, ]; $binaryUuid = new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID); yield 'BinaryUuid' => [ - User::class, - $binaryUuid, - (string) $binaryUuid, + 'model' => User::class, + 'id' => $binaryUuid, + 'expected' => (string) $binaryUuid, + 'expectedFound' => true, ]; yield 'cast as BinaryUuid' => [ - IdIsBinaryUuid::class, - $binaryUuid, - (string) $binaryUuid, + 'model' => IdIsBinaryUuid::class, + 'id' => $binaryUuid, + 'expected' => (string) $binaryUuid, + 'expectedFound' => true, ]; $date = new UTCDateTime(); yield 'UTCDateTime' => [ - User::class, - $date, - $date, - ]; - - $array = ['foo' => 'bar']; - yield 'array' => [ - User::class, - $array, - $array, - ]; - - $object = (object) ['foo' => 'bar']; - yield 'object' => [ - User::class, - $object, - $object, + 'model' => User::class, + 'id' => $date, + 'expected' => $date, + // Don't expect this to be found, as the original value is stored as UTCDateTime but then cast to string + 'expectedFound' => false, ]; } diff --git a/tests/Models/IdIsInt.php b/tests/Models/IdIsInt.php index 33a5ab3..d721320 100644 --- a/tests/Models/IdIsInt.php +++ b/tests/Models/IdIsInt.php @@ -8,6 +8,7 @@ class IdIsInt extends Eloquent { + protected $keyType = 'int'; protected $connection = 'mongodb'; protected static $unguarded = true; protected $casts = [