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

Commit 8402c59

Browse files
committed
Remove tests for arrays and objects as identifiers when keyType is string
1 parent 831485c commit 8402c59

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

tests/ModelTest.php

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public function testSoftDelete(): void
332332
/**
333333
* @dataProvider provideId
334334
*/
335-
public function testPrimaryKey(string $model, $id, $expected): void
335+
public function testPrimaryKey(string $model, $id, $expected, bool $expectedFound): void
336336
{
337337
$model::truncate();
338338
$expectedType = get_debug_type($expected);
@@ -349,78 +349,78 @@ public function testPrimaryKey(string $model, $id, $expected): void
349349

350350
$check = $model::find($id);
351351

352-
$this->assertNotNull($check, 'Not found');
353-
$this->assertSame($expectedType, get_debug_type($check->_id));
354-
$this->assertEquals($id, $check->_id);
355-
$this->assertSame($expectedType, get_debug_type($check->getKey()));
356-
$this->assertEquals($id, $check->getKey());
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+
}
357361
}
358362

359363
public static function provideId(): iterable
360364
{
361365
yield 'int' => [
362-
User::class,
363-
10,
364-
10,
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,
365371
];
366372

367373
yield 'cast as int' => [
368-
IdIsInt::class,
369-
10,
370-
10,
374+
'model' => IdIsInt::class,
375+
'id' => 10,
376+
'expected' => 10,
377+
'expectedFound' => true,
371378
];
372379

373380
yield 'string' => [
374-
User::class,
375-
'user-10',
376-
'user-10',
381+
'model' => User::class,
382+
'id' => 'user-10',
383+
'expected' => 'user-10',
384+
'expectedFound' => true,
377385
];
378386

379387
yield 'cast as string' => [
380-
IdIsString::class,
381-
'user-10',
382-
'user-10',
388+
'model' => IdIsString::class,
389+
'id' => 'user-10',
390+
'expected' => 'user-10',
391+
'expectedFound' => true,
383392
];
384393

385394
$objectId = new ObjectID();
386395
yield 'ObjectID' => [
387-
User::class,
388-
$objectId,
389-
(string) $objectId,
396+
'model' => User::class,
397+
'id' => $objectId,
398+
'expected' => (string) $objectId,
399+
'expectedFound' => true,
390400
];
391401

392402
$binaryUuid = new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID);
393403
yield 'BinaryUuid' => [
394-
User::class,
395-
$binaryUuid,
396-
(string) $binaryUuid,
404+
'model' => User::class,
405+
'id' => $binaryUuid,
406+
'expected' => (string) $binaryUuid,
407+
'expectedFound' => true,
397408
];
398409

399410
yield 'cast as BinaryUuid' => [
400-
IdIsBinaryUuid::class,
401-
$binaryUuid,
402-
(string) $binaryUuid,
411+
'model' => IdIsBinaryUuid::class,
412+
'id' => $binaryUuid,
413+
'expected' => (string) $binaryUuid,
414+
'expectedFound' => true,
403415
];
404416

405417
$date = new UTCDateTime();
406418
yield 'UTCDateTime' => [
407-
User::class,
408-
$date,
409-
$date,
410-
];
411-
412-
$array = ['foo' => 'bar'];
413-
yield 'array' => [
414-
User::class,
415-
$array,
416-
$array,
417-
];
418-
419-
$object = (object) ['foo' => 'bar'];
420-
yield 'object' => [
421-
User::class,
422-
$object,
423-
$object,
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,
424424
];
425425
}
426426

tests/Models/IdIsInt.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
class IdIsInt extends Eloquent
1010
{
11+
protected $keyType = 'int';
1112
protected $connection = 'mongodb';
1213
protected static $unguarded = true;
1314
protected $casts = [

0 commit comments

Comments
 (0)