|
16 | 16 | use Jenssegers\Mongodb\Eloquent\Model;
|
17 | 17 | use Jenssegers\Mongodb\Tests\Models\Book;
|
18 | 18 | 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; |
19 | 22 | use Jenssegers\Mongodb\Tests\Models\Item;
|
20 | 23 | use Jenssegers\Mongodb\Tests\Models\MemberStatus;
|
21 | 24 | use Jenssegers\Mongodb\Tests\Models\Soft;
|
22 | 25 | use Jenssegers\Mongodb\Tests\Models\User;
|
| 26 | +use MongoDB\BSON\Binary; |
23 | 27 | use MongoDB\BSON\ObjectID;
|
24 | 28 | use MongoDB\BSON\UTCDateTime;
|
25 | 29 |
|
@@ -325,11 +329,103 @@ public function testSoftDelete(): void
|
325 | 329 | $this->assertEquals(2, Soft::count());
|
326 | 330 | }
|
327 | 331 |
|
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 |
329 | 428 | {
|
330 |
| - $user = new User; |
331 |
| - $this->assertEquals('_id', $user->getKeyName()); |
332 |
| - |
333 | 429 | $book = new Book;
|
334 | 430 | $this->assertEquals('title', $book->getKeyName());
|
335 | 431 |
|
|
0 commit comments