Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

PHPORM-35 Add various tests on Model _id types #22

Merged
merged 4 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 100 additions & 4 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -325,11 +329,103 @@ public function testSoftDelete(): void
$this->assertEquals(2, Soft::count());
}

public function testPrimaryKey(): void
/**
* @dataProvider provideId
*/
public function testPrimaryKey(string $model, $id, $expected, bool $expectedFound): void
{
$model::truncate();
$expectedType = get_debug_type($expected);

$document = new $model;
$this->assertEquals('_id', $document->getKeyName());

$document->_id = $id;
$document->save();
$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);

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' => [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would also make sense to check the expected value of the identifier, not only the type. This is relevant as the default behaviour for binary UUIDs is to cast it to a string, which is completely different from what the cast does.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the tests to validate the expected value. Using lose assertEquals because the ObjectId instance is recreated when the "model" is hydrated from the database.

'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' => [
'model' => IdIsInt::class,
'id' => 10,
'expected' => 10,
'expectedFound' => true,
];

yield 'string' => [
'model' => User::class,
'id' => 'user-10',
'expected' => 'user-10',
'expectedFound' => true,
];

yield 'cast as string' => [
'model' => IdIsString::class,
'id' => 'user-10',
'expected' => 'user-10',
'expectedFound' => true,
];

$objectId = new ObjectID();
yield 'ObjectID' => [
'model' => User::class,
'id' => $objectId,
'expected' => (string) $objectId,
'expectedFound' => true,
];

$binaryUuid = new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID);
yield 'BinaryUuid' => [
'model' => User::class,
'id' => $binaryUuid,
'expected' => (string) $binaryUuid,
'expectedFound' => true,
];

yield 'cast as BinaryUuid' => [
'model' => IdIsBinaryUuid::class,
'id' => $binaryUuid,
'expected' => (string) $binaryUuid,
'expectedFound' => true,
];

$date = new UTCDateTime();
yield 'UTCDateTime' => [
'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,
];
}

public function testCustomPrimaryKey(): void
{
$user = new User;
$this->assertEquals('_id', $user->getKeyName());

$book = new Book;
$this->assertEquals('title', $book->getKeyName());

Expand Down
17 changes: 17 additions & 0 deletions tests/Models/IdIsBinaryUuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Jenssegers\Mongodb\Tests\Models;

use Jenssegers\Mongodb\Eloquent\Casts\BinaryUuid;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class IdIsBinaryUuid extends Eloquent
{
protected $connection = 'mongodb';
protected static $unguarded = true;
protected $casts = [
'_id' => BinaryUuid::class,
];
}
17 changes: 17 additions & 0 deletions tests/Models/IdIsInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Jenssegers\Mongodb\Tests\Models;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class IdIsInt extends Eloquent
{
protected $keyType = 'int';
protected $connection = 'mongodb';
protected static $unguarded = true;
protected $casts = [
'_id' => 'int',
];
}
16 changes: 16 additions & 0 deletions tests/Models/IdIsString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Jenssegers\Mongodb\Tests\Models;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class IdIsString extends Eloquent
{
protected $connection = 'mongodb';
protected static $unguarded = true;
protected $casts = [
'_id' => 'string',
];
}