-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for PrimaryKey attribute
- Loading branch information
1 parent
b4abd37
commit 9133475
Showing
6 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Datasets; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUlids; | ||
use Illuminate\Database\Eloquent\Model; | ||
use WendellAdriel\Virtue\Models\Attributes\Fillable; | ||
use WendellAdriel\Virtue\Models\Attributes\PrimaryKey; | ||
use WendellAdriel\Virtue\Models\Concerns\Virtue; | ||
|
||
#[Fillable([ | ||
'id', | ||
])] | ||
#[PrimaryKey(type: 'string', incrementing: false)] | ||
final class Crew extends Model | ||
{ | ||
use HasUlids; | ||
use Virtue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Datasets; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Model; | ||
use WendellAdriel\Virtue\Models\Attributes\Fillable; | ||
use WendellAdriel\Virtue\Models\Attributes\PrimaryKey; | ||
use WendellAdriel\Virtue\Models\Concerns\Virtue; | ||
|
||
#[Fillable([ | ||
'id', | ||
])] | ||
#[PrimaryKey(type: 'string', incrementing: false)] | ||
final class Game extends Model | ||
{ | ||
use HasUuids; | ||
use Virtue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Datasets; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use WendellAdriel\Virtue\Models\Attributes\Fillable; | ||
use WendellAdriel\Virtue\Models\Attributes\PrimaryKey; | ||
use WendellAdriel\Virtue\Models\Concerns\Virtue; | ||
|
||
#[Fillable([ | ||
'movie_id', | ||
])] | ||
#[PrimaryKey(name: 'movie_id', type: 'string', incrementing: false)] | ||
final class Movie extends Model | ||
{ | ||
use Virtue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Datasets; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use WendellAdriel\Virtue\Models\Attributes\Database; | ||
use WendellAdriel\Virtue\Models\Attributes\Fillable; | ||
use WendellAdriel\Virtue\Models\Attributes\PrimaryKey; | ||
use WendellAdriel\Virtue\Models\Concerns\Virtue; | ||
|
||
#[Fillable([ | ||
'id', | ||
'name', | ||
'email', | ||
'password', | ||
])] | ||
#[Database(table: 'users')] | ||
#[PrimaryKey(incrementing: false)] | ||
final class UserCustom extends Model | ||
{ | ||
use Virtue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Datasets; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use WendellAdriel\Virtue\Models\Attributes\Database; | ||
use WendellAdriel\Virtue\Models\Attributes\Fillable; | ||
use WendellAdriel\Virtue\Models\Attributes\PrimaryKey; | ||
use WendellAdriel\Virtue\Models\Concerns\Virtue; | ||
|
||
#[Fillable([ | ||
'uuid', | ||
'name', | ||
'email', | ||
'password', | ||
])] | ||
#[Database(table: 'users_uuid')] | ||
#[PrimaryKey(name: 'uuid', type: 'string', incrementing: false)] | ||
class UserUuid extends Model | ||
{ | ||
use Virtue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Tests\Datasets\Crew; | ||
use Tests\Datasets\Game; | ||
use Tests\Datasets\Movie; | ||
use Tests\Datasets\User; | ||
use Tests\Datasets\UserCustom; | ||
use Tests\Datasets\UserUuid; | ||
|
||
it('returns the default values for primary key when not set', function () { | ||
$user = new User(); | ||
|
||
expect($user->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' => '[email protected]', | ||
]); | ||
|
||
$this->assertDatabaseCount(UserCustom::class, 1); | ||
$this->assertDatabaseHas(UserCustom::class, [ | ||
'id' => 10, | ||
'name' => 'John Doe', | ||
'email' => '[email protected]', | ||
]); | ||
}); | ||
|
||
it('retrieves user with custom primary key', function () { | ||
UserCustom::create([ | ||
'id' => 5, | ||
'name' => 'John Doe', | ||
'email' => '[email protected]', | ||
'password' => 's3Cr3t@!!!', | ||
]); | ||
$user = UserCustom::query()->find(5); | ||
|
||
expect($user->id)->toBe(5) | ||
->and($user->name)->toBe('John Doe') | ||
->and($user->email)->toBe('[email protected]'); | ||
}); | ||
}); | ||
|
||
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' => '[email protected]', | ||
]); | ||
|
||
$this->assertDatabaseCount(UserUuid::class, 1); | ||
$this->assertDatabaseHas(UserUuid::class, [ | ||
'uuid' => '123e4567-e89b-12d3-a456-426614174000', | ||
'name' => 'John Doe', | ||
'email' => '[email protected]', | ||
]); | ||
}); | ||
|
||
it('retrieves user with custom primary key', function () { | ||
UserUuid::create([ | ||
'uuid' => '123e4567-e89b-12d3-a456-426614174000', | ||
'name' => 'John Doe', | ||
'email' => '[email protected]', | ||
'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('[email protected]'); | ||
}); | ||
}); | ||
|
||
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(); | ||
}); |