Skip to content

Commit

Permalink
Added tests for Hidden attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellAdriel committed May 6, 2024
1 parent fd69ce1 commit 86aba7e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tests/Datasets/ProductFillable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#[Cast(field: 'random_number', type: 'int')]
#[Cast(field: 'expires_at', type: 'immutable_datetime')]
#[Database(table: 'products')]
class ProductFillable extends Model
final class ProductFillable extends Model
{
use Virtue;
}
33 changes: 33 additions & 0 deletions tests/Datasets/ProductHidden.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\Attributes\Hidden;
use WendellAdriel\Virtue\Models\Concerns\Virtue;

#[Fillable([
'name',
'price',
'random_number',
'another_random_number',
'expires_at',
])]
#[Hidden([
'random_number',
'another_random_number',
])]
#[Cast(field: 'price', type: 'float')]
#[Cast(field: 'random_number', type: 'int')]
#[Cast(field: 'another_random_number', type: 'int')]
#[Cast(field: 'expires_at', type: 'immutable_datetime')]
#[Database(table: 'products')]
final class ProductHidden extends Model
{
use Virtue;
}
2 changes: 1 addition & 1 deletion tests/Datasets/UserGuarded.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'email',
])]
#[Database(table: 'users_guarded')]
class UserGuarded extends Model
final class UserGuarded extends Model
{
use Virtue;
}
23 changes: 23 additions & 0 deletions tests/Feature/HiddenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Tests\Datasets\ProductHidden;

it('set properties to hidden', function () {
$product = ProductHidden::create([
'name' => 'Product 1',
'price' => '10.99',
'random_number' => '123',
'another_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')
->and($product->toArray())->not->toHaveKey('random_number')
->and($product->toArray())->not->toHaveKey('another_random_number');
});

0 comments on commit 86aba7e

Please sign in to comment.