|
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,106 @@ 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, string $type): void |
329 | 336 | {
|
330 |
| - $user = new User; |
| 337 | + $model::truncate(); |
| 338 | + |
| 339 | + $document = new $model; |
| 340 | + $this->assertEquals('_id', $document->getKeyName()); |
| 341 | + |
| 342 | + $document->_id = $id; |
| 343 | + $document->save(); |
| 344 | + $this->assertSame($type, get_debug_type($document->_id)); |
| 345 | + $this->assertEquals($id, $document->_id); |
| 346 | + $this->assertSame($type, get_debug_type($document->getKey())); |
| 347 | + $this->assertEquals($id, $document->getKey()); |
| 348 | + |
| 349 | + $check = $model::find($id); |
| 350 | + |
| 351 | + $this->assertNotNull($check, 'Not found'); |
| 352 | + $this->assertSame($type, get_debug_type($check->_id)); |
| 353 | + $this->assertEquals($id, $check->_id); |
| 354 | + $this->assertSame($type, get_debug_type($check->getKey())); |
| 355 | + $this->assertEquals($id, $check->getKey()); |
| 356 | + } |
| 357 | + |
| 358 | + public static function provideId(): iterable |
| 359 | + { |
| 360 | + yield 'int' => [ |
| 361 | + User::class, |
| 362 | + 10, |
| 363 | + 'int', |
| 364 | + ]; |
| 365 | + |
| 366 | + yield 'cast as int' => [ |
| 367 | + IdIsInt::class, |
| 368 | + 10, |
| 369 | + 'int', |
| 370 | + ]; |
| 371 | + |
| 372 | + yield 'string' => [ |
| 373 | + User::class, |
| 374 | + 'user-10', |
| 375 | + 'string', |
| 376 | + ]; |
| 377 | + |
| 378 | + yield 'cast as string' => [ |
| 379 | + IdIsString::class, |
| 380 | + 'user-10', |
| 381 | + 'string', |
| 382 | + ]; |
| 383 | + |
| 384 | + yield 'ObjectID' => [ |
| 385 | + User::class, |
| 386 | + new ObjectID(), |
| 387 | + 'string', |
| 388 | + ]; |
| 389 | + |
| 390 | + yield 'BinaryUuid' => [ |
| 391 | + User::class, |
| 392 | + new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID), |
| 393 | + 'string', |
| 394 | + ]; |
| 395 | + |
| 396 | + yield 'cast as BinaryUuid' => [ |
| 397 | + IdIsBinaryUuid::class, |
| 398 | + new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID), |
| 399 | + 'string', |
| 400 | + ]; |
| 401 | + |
| 402 | + yield 'UTCDateTime' => [ |
| 403 | + User::class, |
| 404 | + new UTCDateTime(), |
| 405 | + UTCDateTime::class, |
| 406 | + ]; |
| 407 | + } |
| 408 | + |
| 409 | + public function testPrimaryKeyBinaryUuid(): void |
| 410 | + { |
| 411 | + $user = new IdIsBinaryUuid; |
331 | 412 | $this->assertEquals('_id', $user->getKeyName());
|
332 | 413 |
|
| 414 | + $uuid = new Binary(hex2bin('0c103357380648c9a84b867dcb625cfb'), Binary::TYPE_UUID); |
| 415 | + $idAsString = (string) $uuid; |
| 416 | + $user->_id = $uuid; |
| 417 | + $user->name = 'John Doe'; |
| 418 | + $user->save(); |
| 419 | + $this->assertIsString($user->getKey()); |
| 420 | + $this->assertSame($idAsString, $user->getKey()); |
| 421 | + |
| 422 | + $check = IdIsBinaryUuid::find($uuid); |
| 423 | + $this->assertIsString($check->_id); |
| 424 | + $this->assertSame($idAsString, $check->_id); |
| 425 | + $this->assertIsString($check->getKey()); |
| 426 | + $this->assertSame($idAsString, $check->getKey()); |
| 427 | + $this->assertSame('John Doe', $check->name); |
| 428 | + } |
| 429 | + |
| 430 | + public function testCustomPrimaryKey(): void |
| 431 | + { |
333 | 432 | $book = new Book;
|
334 | 433 | $this->assertEquals('title', $book->getKeyName());
|
335 | 434 |
|
|
0 commit comments