Testes unitários para models em aplicações Laravel 10 com TestCase customizado
I will show a way to make testing your models a little bit easier using a custom TestCase.
// Path: Tests/ModelTestCase.php | |
<?php | |
namespace Tests; | |
use Tests\TestCase; | |
use Tests\TestsHelpers\DataTransferObjects\ModelConfigurationAssertionParamsDto; | |
abstract class ModelTestCase extends TestCase | |
{ | |
protected function runConfigurationAssertions(ModelConfigurationAssertionParamsDto $dto): void | |
{ | |
$this->assertEquals($dto->fillable, $dto->model->getFillable()); | |
$this->assertEquals($dto->guarded, $dto->model->getGuarded()); | |
$this->assertEquals($dto->hidden, $dto->model->getHidden()); | |
$this->assertEquals($dto->visible, $dto->model->getVisible()); | |
$this->assertEquals($dto->casts, $dto->model->getCasts()); | |
$this->assertEquals($dto->dates, $dto->model->getDates()); | |
$this->assertEquals($dto->primaryKey, $dto->model->getKeyName()); | |
$collection = $dto->model->newCollection(); | |
$this->assertEquals($dto->collectionClass, get_class($collection)); | |
if ($dto->table !== null) { | |
$this->assertEquals($dto->table, $dto->model->getTable()); | |
} | |
} | |
} |
// Path: Tests/TestsHelpers/DataTransferObjects/ModelConfigurationAssertionParamsDto.php | |
/* | |
* Obs: This DTO was built using the package "laravel-data". | |
* You can learn more about the package here: https://spatie.be/docs/laravel-data/v4/introduction | |
*/ | |
<?php | |
namespace Tests\TestsHelpers\DataTransferObjects; | |
use Spatie\LaravelData\Data; | |
use Spatie\LaravelData\Attributes\MapInputName; | |
use Spatie\LaravelData\Mappers\SnakeCaseMapper; | |
use Illuminate\Database\Eloquent\Collection; | |
use Illuminate\Database\Eloquent\Model; | |
#[MapInputName(SnakeCaseMapper::class)] | |
class ModelConfigurationAssertionParamsDto extends Data | |
{ | |
public function __construct( | |
public Model $model, | |
public array $fillable = [], | |
public array $hidden = [], | |
public array $guarded = ['*'], | |
public array $visible = [], | |
public array $casts = ['id' => 'int'], | |
public array $dates = ['created_at', 'updated_at'], | |
public string $collectionClass = Collection::class, | |
public ?string $table = null, | |
public string $primaryKey = 'id' | |
) { | |
} | |
} |
// Path: Tests/Unit/App/Domain/User/Models/UserTest.php | |
// Obs: The model User was used as an example. You can do this with any model. | |
<?php | |
namespace Tests\Unit\App\Domain\User\Models; | |
use Tests\ModelTestCase; | |
use Tests\TestsHelpers\DataTransferObjects\ModelConfigurationAssertionParamsDto; | |
use App\Domain\User\Models\User; | |
class UserTest extends ModelTestCase | |
{ | |
/** | |
* @group user | |
*/ | |
public function test_has_valid_configuration(): void | |
{ | |
$dto = ModelConfigurationAssertionParamsDto::from([ | |
'model'=> new User(), | |
'fillable' => ['name', 'email', 'password'], | |
'hidden' => ['password', 'remember_token'], | |
'casts' => [ | |
'id' => 'int', | |
'email_verified_at' => 'datetime', | |
'password' => 'hashed' | |
], | |
'table' => 'users' | |
]); | |
$this->runConfigurationAssertions($dto); | |
} | |
} |
// Path: App/Domain/User/Models/User.php | |
// Obs: This model is used as an example. You can do this with any model. | |
<?php | |
namespace App\Domain\User\Models; | |
// use Illuminate\Contracts\Auth\MustVerifyEmail; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use Illuminate\Notifications\Notifiable; | |
use Laravel\Sanctum\HasApiTokens; | |
class User extends Authenticatable | |
{ | |
use HasApiTokens; | |
use HasFactory; | |
use Notifiable; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array<int, string> | |
*/ | |
protected $fillable = [ | |
'name', | |
'email', | |
'password', | |
]; | |
/** | |
* The attributes that should be hidden for serialization. | |
* | |
* @var array<int, string> | |
*/ | |
protected $hidden = [ | |
'password', | |
'remember_token', | |
]; | |
/** | |
* The attributes that should be cast. | |
* | |
* @var array<string, string> | |
*/ | |
protected $casts = [ | |
'email_verified_at' => 'datetime', | |
'password' => 'hashed', | |
]; | |
} |