Skip to content

Commit

Permalink
Added tests for Fillable, Cast and Database attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellAdriel committed May 6, 2024
1 parent 7c9c9c9 commit dd9a2bc
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Models/Concerns/Virtue.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function initializeVirtue(): void
private function applyDatabaseCustomizations(): void
{
/** @var ReflectionAttribute|null $attribute */
$attribute = $this->resolveSingleAttribute(Fillable::class);
$attribute = $this->resolveSingleAttribute(Database::class);
if (is_null($attribute)) {
return;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Datasets/ProductFillable.php
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;
}
20 changes: 20 additions & 0 deletions tests/Datasets/UserGuarded.php
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;
}
38 changes: 38 additions & 0 deletions tests/Feature/FillableCastAndDatabaseTest.php
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@!!!',
]);
});

0 comments on commit dd9a2bc

Please sign in to comment.