Skip to content

Commit

Permalink
PHPORM-35 Add various tests on Model _id types (#22)
Browse files Browse the repository at this point in the history
* PHPORM-35 Add various tests on Model _id

* Add assertion on expected value

* Test _id as array and object

* Remove tests for arrays and objects as identifiers when keyType is string

---------

Co-authored-by: Andreas Braun <git@alcaeus.org>
  • Loading branch information
GromNaN and alcaeus committed Aug 22, 2023
1 parent ea89e86 commit 49ec43c
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 4 deletions.
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' => [
'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',
];
}

0 comments on commit 49ec43c

Please sign in to comment.