parvej-mahmud / laravel-tdd-generator
Automatically generate comprehensive TDD files for Laravel modules including models, controllers, migrations, and routes
Requires
- php: ^8.1
- illuminate/console: ^10.0|^11.0
- illuminate/filesystem: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-08-04 04:10:17 UTC
README
A powerful Laravel package that automatically generates comprehensive Test-Driven Development (TDD) files for any given module or feature. This package creates well-structured test files for models, controllers, migrations, and routes with a single command.
Features
- ๐ One Command, Complete TDD Suite: Generate all test files for a module with one command
- ๐งช Comprehensive Test Coverage: Creates tests for models, controllers, migrations, and routes
- ๐ฏ Smart Test Generation: Automatically detects common patterns and generates appropriate tests
- ๐ง Highly Customizable: Customizable stubs and configurations
- ๐ Best Practices: Follows Laravel testing best practices and conventions
- ๐๏ธ Multiple Test Types: Supports unit, feature, and integration tests
- ๐ Security Aware: Includes authentication and authorization tests
Installation
Install the package via Composer:
composer require parvej-mahmud/laravel-tdd-generator --dev
Publish the configuration file:
php artisan vendor:publish --provider="ParvejMahmud\LaravelTddGenerator\TddGeneratorServiceProvider" --tag="config"
Optionally, publish the stub files for customization:
php artisan vendor:publish --provider="ParvejMahmud\LaravelTddGenerator\TddGeneratorServiceProvider" --tag="stubs"
Usage
Generate Complete TDD Suite
Generate all test files for a module:
php artisan make:tdd Post
This creates:
tests/Unit/PostTest.php
- Model teststests/Feature/PostControllerTest.php
- Controller teststests/Unit/PostsMigrationTest.php
- Migration teststests/Feature/PostRouteTest.php
- Route tests
Generate Specific Test Types
Generate only specific test types:
# Generate only model tests php artisan make:tdd Post --model # Generate only controller tests php artisan make:tdd Post --controller # Generate multiple specific types php artisan make:tdd Post --model --controller
Individual Commands
You can also use individual commands for more control:
# Generate model test only php artisan make:model-test Post # Generate controller test only php artisan make:controller-test PostController # Generate migration test only php artisan make:migration-test posts # Generate route test only php artisan make:route-test Post
Command Options
--model
: Generate model tests--controller
: Generate controller tests--migration
: Generate migration tests--routes
: Generate route tests--all
: Generate all test types (default behavior)--force
: Overwrite existing files
Generated Tests
Model Tests
The generated model tests include:
- โ CRUD operations (Create, Read, Update, Delete)
- โ Fillable attributes validation
- โ Required fields validation
- โ Relationships testing
- โ Timestamps verification
- โ Factory integration
Controller Tests
The generated controller tests include:
- โ All CRUD endpoints (index, show, store, update, destroy)
- โ Request validation testing
- โ Authentication and authorization
- โ Error handling (404, 422, etc.)
- โ Pagination testing
- โ Search functionality
Migration Tests
The generated migration tests include:
- โ Table creation verification
- โ Column existence and types
- โ Primary key validation
- โ Timestamps verification
- โ Constraints testing
- โ Data insertion capabilities
Route Tests
The generated route tests include:
- โ Route existence verification
- โ HTTP method validation
- โ URL generation testing
- โ Middleware verification
- โ Authentication requirements
- โ Error handling
Configuration
The package comes with a comprehensive configuration file. Here are the key options:
// config/tdd-generator.php return [ // Default test types to generate 'default_types' => [ 'model' => true, 'controller' => true, 'migration' => true, 'routes' => true, ], // Test namespaces 'namespaces' => [ 'unit' => 'Tests\\Unit', 'feature' => 'Tests\\Feature', ], // Authentication requirements 'auth_required' => [ 'store' => true, 'update' => true, 'destroy' => true, ], ];
Customizing Stubs
You can customize the generated test files by publishing and modifying the stub files:
php artisan vendor:publish --provider="ParvejMahmud\LaravelTddGenerator\TddGeneratorServiceProvider" --tag="stubs"
The stub files will be published to resources/stubs/tdd-generator/
where you can modify them to match your project's coding standards.
Examples
Basic Usage
# Generate complete TDD suite for a User module php artisan make:tdd User # Generate tests for an API resource php artisan make:tdd Product --controller --routes # Force overwrite existing files php artisan make:tdd Order --force
Advanced Usage
# Generate tests for a complex module with custom options php artisan make:tdd BlogPost --all # Generate only unit tests php artisan make:tdd Category --model --migration # Generate only feature tests php artisan make:tdd Comment --controller --routes
Best Practices
- Run Tests First: Always run the generated tests to ensure they pass with your current setup
- Customize as Needed: Modify the generated tests to match your specific business logic
- Use Factories: Ensure you have model factories set up for better test data generation
- Follow TDD: Use the generated tests as a starting point for your TDD workflow
- Keep Tests Updated: Update tests when you modify your models, controllers, or routes
Requirements
- PHP ^8.1
- Laravel ^10.0|^11.0
- PHPUnit ^10.0
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This package is open-sourced software licensed under the MIT license.
Support
If you encounter any issues or have questions, please open an issue on GitHub.