From 91334758c529335d2ae5323da75871ec0fab3d52 Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Mon, 6 May 2024 14:02:09 +0100 Subject: [PATCH] Added tests for PrimaryKey attribute --- tests/Datasets/Crew.php | 21 ++++ tests/Datasets/Game.php | 21 ++++ tests/Datasets/Movie.php | 19 +++ tests/Datasets/UserCustom.php | 24 ++++ tests/Datasets/UserUuid.php | 24 ++++ tests/Feature/PrimaryKeyTest.php | 199 +++++++++++++++++++++++++++++++ 6 files changed, 308 insertions(+) create mode 100644 tests/Datasets/Crew.php create mode 100644 tests/Datasets/Game.php create mode 100644 tests/Datasets/Movie.php create mode 100644 tests/Datasets/UserCustom.php create mode 100644 tests/Datasets/UserUuid.php create mode 100644 tests/Feature/PrimaryKeyTest.php diff --git a/tests/Datasets/Crew.php b/tests/Datasets/Crew.php new file mode 100644 index 0000000..e32842c --- /dev/null +++ b/tests/Datasets/Crew.php @@ -0,0 +1,21 @@ +getKeyName())->toBe('id') + ->and($user->getKeyType())->toBe('int') + ->and($user->getIncrementing())->toBe(true); +}); + +describe('custom primary key with custom incrementing value', function () { + it('returns custom primary key values', function () { + $user = new UserCustom(); + + expect($user->getKeyName())->toBe('id') + ->and($user->getKeyType())->toBe('int') + ->and($user->getIncrementing())->toBe(false); + }); + + it('creates model with custom primary key', function () { + $user = UserCustom::create([ + 'id' => 10, + 'name' => fake()->name, + 'email' => fake()->unique()->safeEmail, + 'password' => 's3Cr3t@!!!', + ]); + + $this->assertDatabaseCount(UserCustom::class, 1); + $this->assertDatabaseHas(UserCustom::class, [ + 'id' => 10, + 'name' => $user->name, + 'email' => $user->email, + ]); + }); + + it('updates model with custom primary key', function () { + $user = UserCustom::create([ + 'id' => 10, + 'name' => fake()->name, + 'email' => fake()->unique()->safeEmail, + 'password' => 's3Cr3t@!!!', + ]); + + $user->update([ + 'name' => 'John Doe', + 'email' => 'john.doe@example.com', + ]); + + $this->assertDatabaseCount(UserCustom::class, 1); + $this->assertDatabaseHas(UserCustom::class, [ + 'id' => 10, + 'name' => 'John Doe', + 'email' => 'john.doe@example.com', + ]); + }); + + it('retrieves user with custom primary key', function () { + UserCustom::create([ + 'id' => 5, + 'name' => 'John Doe', + 'email' => 'john.doe@example.com', + 'password' => 's3Cr3t@!!!', + ]); + $user = UserCustom::query()->find(5); + + expect($user->id)->toBe(5) + ->and($user->name)->toBe('John Doe') + ->and($user->email)->toBe('john.doe@example.com'); + }); +}); + +describe('custom primary key with custom type and incrementing value', function () { + it('returns custom primary key values', function () { + $user = new UserUuid(); + + expect($user->getKeyName())->toBe('uuid') + ->and($user->getKeyType())->toBe('string') + ->and($user->getIncrementing())->toBe(false); + }); + + it('creates model with custom primary key', function () { + $user = UserUuid::create([ + 'uuid' => '123e4567-e89b-12d3-a456-426614174000', + 'name' => fake()->name, + 'email' => fake()->unique()->safeEmail, + 'password' => 's3Cr3t@!!!', + ]); + + $this->assertDatabaseCount(UserUuid::class, 1); + $this->assertDatabaseHas(UserUuid::class, [ + 'uuid' => '123e4567-e89b-12d3-a456-426614174000', + 'name' => $user->name, + 'email' => $user->email, + ]); + }); + + it('updates model with custom primary key', function () { + $user = UserUuid::create([ + 'uuid' => '123e4567-e89b-12d3-a456-426614174000', + 'name' => fake()->name, + 'email' => fake()->unique()->safeEmail, + 'password' => 's3Cr3t@!!!', + ]); + + $user->update([ + 'name' => 'John Doe', + 'email' => 'john.doe@example.com', + ]); + + $this->assertDatabaseCount(UserUuid::class, 1); + $this->assertDatabaseHas(UserUuid::class, [ + 'uuid' => '123e4567-e89b-12d3-a456-426614174000', + 'name' => 'John Doe', + 'email' => 'john.doe@example.com', + ]); + }); + + it('retrieves user with custom primary key', function () { + UserUuid::create([ + 'uuid' => '123e4567-e89b-12d3-a456-426614174000', + 'name' => 'John Doe', + 'email' => 'john.doe@example.com', + 'password' => 's3Cr3t@!!!', + ]); + $user = UserUuid::query()->find('123e4567-e89b-12d3-a456-426614174000'); + + expect($user->uuid)->toBe('123e4567-e89b-12d3-a456-426614174000') + ->and($user->name)->toBe('John Doe') + ->and($user->email)->toBe('john.doe@example.com'); + }); +}); + +describe('custom primary key with custom column name', function () { + it('returns custom primary key values', function () { + $movie = new Movie(); + + expect($movie->getKeyName())->toBe('movie_id') + ->and($movie->getKeyType())->toBe('string') + ->and($movie->getIncrementing())->toBe(false); + }); + + it('creates model with custom primary key', function () { + $movie = Movie::create([ + 'movie_id' => '123e4567-e89b-12d3-a456-426614174000', + ]); + + $this->assertDatabaseCount(Movie::class, 1); + $this->assertDatabaseHas(Movie::class, [ + 'movie_id' => '123e4567-e89b-12d3-a456-426614174000', + ]); + }); + + it('updates model with custom primary key', function () { + $movie = Movie::create([ + 'movie_id' => '123e4567-e89b-12d3-a456-426614174000', + ]); + + $movie->update([ + 'movie_id' => '123e4567-e89b-12d3-a456-426614174001', + ]); + + $this->assertDatabaseCount(Movie::class, 1); + $this->assertDatabaseHas(Movie::class, [ + 'movie_id' => '123e4567-e89b-12d3-a456-426614174001', + ]); + }); + + it('retrieves user with custom primary key', function () { + Movie::create([ + 'movie_id' => '123e4567-e89b-12d3-a456-426614174000', + ]); + $movie = Movie::query()->find('123e4567-e89b-12d3-a456-426614174000'); + + expect($movie->movie_id)->toBe('123e4567-e89b-12d3-a456-426614174000'); + }); +}); + +it('sets default ULID primary key column', function () { + $crew = new Crew(); + $crew->save(); + + expect($crew->id)->not->toBeNull(); +}); + +it('sets default UUID primary key column', function () { + $game = new Game(); + $game->save(); + + expect($game->id)->not->toBeNull(); +});