hidayetov/auto-testify
Laravel package that generates CRUD and uniqueness tests for Eloquent models via an Artisan command. Detects unique fields from the DB schema with doctrine/dbal or from a $unique model property, and skips DB-dependent checks when no connection is available.
AutoTestify is a powerful Laravel package designed to automatically generate comprehensive test files for your Eloquent models. It simplifies the testing process by creating CRUD (Create, Read, Update, Delete) and uniqueness tests, saving you time and ensuring your models are thoroughly tested.
doctrine/dbal is installed, detects unique fields from the database.$unique property defined in your model.doctrine/dbal (for database schema detection)Add the package to your Laravel project:
composer require hidayetov/auto-testify
If you want AutoTestify to detect unique fields from your database schema, install doctrine/dbal:
composer require doctrine/dbal
Without this, the package will rely on the $unique property in your models.
Run the following Artisan command to generate a test file for a specific model:
php artisan make:test-model ModelName
For example, to generate tests for a User model:
php artisan make:test-model User
This will create tests/Feature/UserTest.php with CRUD and uniqueness tests.
Define $fillable and (optionally) $unique properties in your model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $fillable = ['name', 'email', 'password'];
protected $unique = ['email']; // Optional if not using database detection
}
For the User model above, the generated UserTest.php might look like this:
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
class UserTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_be_created_with_fillable_fields()
{
$attributes = [
'name' => 'Test Name',
'email' => '[email protected]',
'password' => 'password',
];
$user = \App\Models\User::create($attributes);
$this->assertEquals($attributes['name'], $user->name);
$this->assertEquals($attributes['email'], $user->email);
$this->assertTrue(Hash::check('password', $user->password));
}
public function test_user_can_be_retrieved()
{
$attributes = [
'name' => 'Test Name',
'email' => '[email protected]',
'password' => 'password',
];
$user = \App\Models\User::create($attributes);
$retrieved = \App\Models\User::find($user->id);
$this->assertNotNull($retrieved);
$this->assertEquals($attributes['name'], $retrieved->name);
$this->assertEquals($attributes['email'], $retrieved->email);
$this->assertTrue(Hash::check('password', $retrieved->password));
}
public function test_user_can_be_updated()
{
$attributes = [
'name' => 'Test Name',
'email' => '[email protected]',
'password' => 'password',
];
$user = \App\Models\User::create($attributes);
$updatedAttributes = [
'name' => 'Updated Test Name',
'email' => '[email protected]',
'password' => 'newpassword',
];
$user->update($updatedAttributes);
$this->assertEquals($updatedAttributes['name'], $user->name);
$this->assertEquals($updatedAttributes['email'], $user->email);
$this->assertTrue(Hash::check('newpassword', $user->password));
}
public function test_user_can_be_deleted()
{
$attributes = [
'name' => 'Test Name',
'email' => '[email protected]',
'password' => 'password',
];
$user = \App\Models\User::create($attributes);
$user->delete();
$this->assertDatabaseMissing('users', ['id' => $user->id]);
}
public function test_user_email_must_be_unique()
{
$attributes = [
'name' => 'Test Name',
'email' => '[email protected]',
'password' => 'password',
];
\App\Models\User::create($attributes);
$this->assertDatabaseCount('users', 1);
$this->expectException(\Illuminate\Database\QueryException::class);
\App\Models\User::create($attributes);
}
}
$fillable: Required to specify fields that can be mass-assigned.$unique: Optional; define unique fields if not relying on database schema detection.Ensure your database is migrated (php artisan migrate) and doctrine/dbal is installed for automatic unique field detection.
After generating the test files, run your tests with:
php artisan test
PASS Tests\Feature\UserTest
✓ user can be created with fillable fields
✓ user can be retrieved
✓ user can be updated
✓ user can be deleted
✓ user email must be unique
.env file is configured correctly and your database server is running.
.env for MySQL:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION=sqlite
DB_DATABASE=/path/to/database.sqlite
$unique in your model or install doctrine/dbal and migrate your database.This package is open-sourced under the MIT License.
Happy testing with AutoTestify! 🚀
How can I help you explore Laravel packages today?