Skip to content

Commit 9f1f8f3

Browse files
GromNaNalcaeus
andauthored
PHPORM-35 Add various tests on Model _id types (mongodb#22)
* 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 <[email protected]>
1 parent 2319d53 commit 9f1f8f3

File tree

4 files changed

+150
-4
lines changed

4 files changed

+150
-4
lines changed

Diff for: tests/ModelTest.php

+100-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
use Jenssegers\Mongodb\Eloquent\Model;
1717
use Jenssegers\Mongodb\Tests\Models\Book;
1818
use Jenssegers\Mongodb\Tests\Models\Guarded;
19+
use Jenssegers\Mongodb\Tests\Models\IdIsBinaryUuid;
20+
use Jenssegers\Mongodb\Tests\Models\IdIsInt;
21+
use Jenssegers\Mongodb\Tests\Models\IdIsString;
1922
use Jenssegers\Mongodb\Tests\Models\Item;
2023
use Jenssegers\Mongodb\Tests\Models\MemberStatus;
2124
use Jenssegers\Mongodb\Tests\Models\Soft;
2225
use Jenssegers\Mongodb\Tests\Models\User;
26+
use MongoDB\BSON\Binary;
2327
use MongoDB\BSON\ObjectID;
2428
use MongoDB\BSON\UTCDateTime;
2529

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

328-
public function testPrimaryKey(): void
332+
/**
333+
* @dataProvider provideId
334+
*/
335+
public function testPrimaryKey(string $model, $id, $expected, bool $expectedFound): void
336+
{
337+
$model::truncate();
338+
$expectedType = get_debug_type($expected);
339+
340+
$document = new $model;
341+
$this->assertEquals('_id', $document->getKeyName());
342+
343+
$document->_id = $id;
344+
$document->save();
345+
$this->assertSame($expectedType, get_debug_type($document->_id));
346+
$this->assertEquals($expected, $document->_id);
347+
$this->assertSame($expectedType, get_debug_type($document->getKey()));
348+
$this->assertEquals($expected, $document->getKey());
349+
350+
$check = $model::find($id);
351+
352+
if ($expectedFound) {
353+
$this->assertNotNull($check, 'Not found');
354+
$this->assertSame($expectedType, get_debug_type($check->_id));
355+
$this->assertEquals($id, $check->_id);
356+
$this->assertSame($expectedType, get_debug_type($check->getKey()));
357+
$this->assertEquals($id, $check->getKey());
358+
} else {
359+
$this->assertNull($check, 'Found');
360+
}
361+
}
362+
363+
public static function provideId(): iterable
364+
{
365+
yield 'int' => [
366+
'model' => User::class,
367+
'id' => 10,
368+
'expected' => 10,
369+
// Don't expect this to be found, as the int is cast to string for the query
370+
'expectedFound' => false,
371+
];
372+
373+
yield 'cast as int' => [
374+
'model' => IdIsInt::class,
375+
'id' => 10,
376+
'expected' => 10,
377+
'expectedFound' => true,
378+
];
379+
380+
yield 'string' => [
381+
'model' => User::class,
382+
'id' => 'user-10',
383+
'expected' => 'user-10',
384+
'expectedFound' => true,
385+
];
386+
387+
yield 'cast as string' => [
388+
'model' => IdIsString::class,
389+
'id' => 'user-10',
390+
'expected' => 'user-10',
391+
'expectedFound' => true,
392+
];
393+
394+
$objectId = new ObjectID();
395+
yield 'ObjectID' => [
396+
'model' => User::class,
397+
'id' => $objectId,
398+
'expected' => (string) $objectId,
399+
'expectedFound' => true,
400+
];
401+
402+
$binaryUuid = new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID);
403+
yield 'BinaryUuid' => [
404+
'model' => User::class,
405+
'id' => $binaryUuid,
406+
'expected' => (string) $binaryUuid,
407+
'expectedFound' => true,
408+
];
409+
410+
yield 'cast as BinaryUuid' => [
411+
'model' => IdIsBinaryUuid::class,
412+
'id' => $binaryUuid,
413+
'expected' => (string) $binaryUuid,
414+
'expectedFound' => true,
415+
];
416+
417+
$date = new UTCDateTime();
418+
yield 'UTCDateTime' => [
419+
'model' => User::class,
420+
'id' => $date,
421+
'expected' => $date,
422+
// Don't expect this to be found, as the original value is stored as UTCDateTime but then cast to string
423+
'expectedFound' => false,
424+
];
425+
}
426+
427+
public function testCustomPrimaryKey(): void
329428
{
330-
$user = new User;
331-
$this->assertEquals('_id', $user->getKeyName());
332-
333429
$book = new Book;
334430
$this->assertEquals('title', $book->getKeyName());
335431

Diff for: tests/Models/IdIsBinaryUuid.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jenssegers\Mongodb\Tests\Models;
6+
7+
use Jenssegers\Mongodb\Eloquent\Casts\BinaryUuid;
8+
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
9+
10+
class IdIsBinaryUuid extends Eloquent
11+
{
12+
protected $connection = 'mongodb';
13+
protected static $unguarded = true;
14+
protected $casts = [
15+
'_id' => BinaryUuid::class,
16+
];
17+
}

Diff for: tests/Models/IdIsInt.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jenssegers\Mongodb\Tests\Models;
6+
7+
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
8+
9+
class IdIsInt extends Eloquent
10+
{
11+
protected $keyType = 'int';
12+
protected $connection = 'mongodb';
13+
protected static $unguarded = true;
14+
protected $casts = [
15+
'_id' => 'int',
16+
];
17+
}

Diff for: tests/Models/IdIsString.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jenssegers\Mongodb\Tests\Models;
6+
7+
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
8+
9+
class IdIsString extends Eloquent
10+
{
11+
protected $connection = 'mongodb';
12+
protected static $unguarded = true;
13+
protected $casts = [
14+
'_id' => 'string',
15+
];
16+
}

0 commit comments

Comments
 (0)