-
-
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 Fillable, Cast and Database attributes
- Loading branch information
1 parent
7c9c9c9
commit dd9a2bc
Showing
4 changed files
with
85 additions
and
1 deletion.
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
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Datasets; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use WendellAdriel\Virtue\Models\Attributes\Cast; | ||
use WendellAdriel\Virtue\Models\Attributes\Database; | ||
use WendellAdriel\Virtue\Models\Attributes\Fillable; | ||
use WendellAdriel\Virtue\Models\Concerns\Virtue; | ||
|
||
#[Fillable([ | ||
'name', | ||
'price', | ||
'random_number', | ||
'expires_at', | ||
])] | ||
#[Cast(field: 'price', type: 'float')] | ||
#[Cast(field: 'random_number', type: 'int')] | ||
#[Cast(field: 'expires_at', type: 'immutable_datetime')] | ||
#[Database(table: 'products')] | ||
class ProductFillable 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,20 @@ | ||
<?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\Concerns\Virtue; | ||
|
||
#[Fillable([ | ||
'name', | ||
'email', | ||
])] | ||
#[Database(table: 'users_guarded')] | ||
class UserGuarded 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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Tests\Datasets\ProductFillable; | ||
use Tests\Datasets\UserGuarded; | ||
|
||
it('set fillable properties', function () { | ||
$product = ProductFillable::create([ | ||
'name' => 'Product 1', | ||
'price' => '10.99', | ||
'random_number' => '123', | ||
'expires_at' => '2023-12-31 23:59:59', | ||
]); | ||
|
||
expect($product->name)->toBe('Product 1') | ||
->and($product->price)->toBe(10.99) | ||
->and($product->random_number)->toBe(123) | ||
->and($product->expires_at)->toBeInstanceOf(Carbon\CarbonImmutable::class) | ||
->and($product->expires_at->format('Y-m-d H:i:s'))->toBe('2023-12-31 23:59:59'); | ||
}); | ||
|
||
it('can update not fillable properties when setting them individually', function () { | ||
$user = UserGuarded::make([ | ||
'name' => fake()->name, | ||
'email' => fake()->unique()->safeEmail, | ||
]); | ||
|
||
$user->password = 's3Cr3T@!!!'; | ||
$user->save(); | ||
|
||
$this->assertDatabaseCount(UserGuarded::class, 1); | ||
$this->assertDatabaseHas(UserGuarded::class, [ | ||
'name' => $user->name, | ||
'email' => $user->email, | ||
'password' => 's3Cr3T@!!!', | ||
]); | ||
}); |