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.
Install the package:
composer require hidayetov/auto-testify
Optionally install doctrine/dbal for database schema detection:
composer require doctrine/dbal
Publish the config (optional):
php artisan vendor:publish --provider="Hidayetov\AutoTestify\AutoTestifyServiceProvider" --tag="config"
(Modify config/autotestify.php if needed for custom paths or behavior.)
Run the generator:
php artisan autotestify:generate ModelName
Example:
php artisan autotestify:generate User
Locate generated tests:
By default, tests are created in tests/Feature/Models/{ModelName}Test.php.
Verify the output matches your expectations (CRUD + uniqueness tests).
Scenario: You have a User model with fields email (unique) and name. Generate tests to:
email fails).Command:
php artisan autotestify:generate User
Expected Output:
test_can_create_user()test_can_retrieve_user()test_can_update_user()test_can_delete_user()test_email_must_be_unique()Integrate into CI/CD:
autotestify:generate in a pre-test hook to auto-generate tests before running phpunit..github/workflows/test.yml snippet:
- name: Generate Tests
run: php artisan autotestify:generate --all
- name: Run Tests
run: phpunit
Model-Specific Customization:
TestBase with helper methods).Partial Generation:
php artisan autotestify:generate User Post
--skip:
php artisan autotestify:generate --skip=Admin
Database-Agnostic Development:
$unique in your model:
class User extends Model {
protected $unique = ['email', 'username'];
}
Leverage Laravel’s Test Helpers:
create(), factory(), or refreshDatabase() in generated tests for consistency.// Generated test (may need manual tweaks)
public function test_can_create_user() {
$user = User::factory()->create();
$this->assertDatabaseHas('users', ['id' => $user->id]);
}
Extend Test Coverage:
// In UserTest.php
public function test_custom_validation() {
$response = $this->post('/users', ['email' => 'invalid']);
$response->assertSessionHasErrors('email');
}
Seed Test Data:
DatabaseMigrations or DatabaseTransactions traits to ensure clean state:
use DatabaseMigrations;
class UserTest extends TestCase {
use DatabaseMigrations;
// ...
}
Combine with Pest:
php artisan autotestify:generate --pest to adapt syntax:
// Pest-style output
it('creates a user', function () {
$user = User::factory()->create();
expect($user)->toBeValid();
});
Database Connection Issues:
SQLSTATE[HY000] [2002] (no DB connection)..env has valid DB credentials or run tests with --env=testing.--no-database flag to skip DB-dependent tests:
php artisan autotestify:generate --no-database
Schema Mismatch:
doctrine/dbal don’t match $unique in the model.$unique in the model to override schema detection:
protected $unique = ['email']; // Overrides DB schema
Test File Overwrites:
--append to add tests without replacing:
php artisan autotestify:generate --append
// config/autotestify.php
'path' => 'tests/Generated',
Foreign Key Constraints:
Post requires User).// In PostTest.php
public function setUp(): void {
parent::setUp();
$this->user = User::factory()->create();
}
Verbose Output:
php artisan autotestify:generate --verbose
Check Generated Code:
TODO comments in generated tests.Validate Unique Fields:
$unique in the model or DB schema matches expectations:
php artisan schema:dump --printer
Custom Test Templates:
php artisan vendor:publish --provider="Hidayetov\AutoTestify\AutoTestifyServiceProvider" --tag="views"
resources/views/autotestify/test.blade.php.Add Custom Test Methods:
// app/Console/Commands/GenerateCustomTests.php
use Hidayetov\AutoTestify\Generators\TestGenerator;
class GenerateCustomTests extends Command {
protected $signature = 'autotestify:custom {model}';
public function handle() {
$generator = new TestGenerator();
$generator->addCustomMethod('test_custom_logic');
$generator->generate($this->argument('model'));
}
}
Hook into Generation:
autotestify.generated event to post-process tests:
// app/Providers/EventServiceProvider.php
protected $listen = [
'autotestify.generated' => [
'App\Listeners\PostProcessTest',
],
];
Skip Specific Tests:
// config/autotestify.php
'skip_tests' => ['uniqueness'], // Skip uniqueness tests
laravel-shift/database-resetter:
Reset the DB between test runs to avoid state pollution.--dry-run:
Preview changes without writing files:
php artisan autotestify:generate --dry-run
php artisan autotestify:generate --only=crud User
How can I help you explore Laravel packages today?