| Install | |
|---|---|
composer require bberkaysari/laravel-test-generator |
|
| Latest Version: | v1.6.0 |
| PHP: | ^8.2|^8.3|^8.4 |
| License: | MIT |
| Last Updated: | Jan 19, 2026 |
| Links: | GitHub · Packagist |
Production-ready Laravel test generator with 85-90% automation without AI
Generate comprehensive PHPUnit tests for your Laravel application through static code analysis - no AI, no guessing, just reliable test generation from your actual code.
$request->validate() callscomposer require --dev bberkaysari/laravel-test-generator
Generate all tests:
php vendor/bin/generate-tests
Generate only model tests:
php vendor/bin/generate-tests --type=model
Generate only controller tests:
php vendor/bin/generate-tests --type=controller
Generate service/repository tests:
php vendor/bin/generate-tests --type=service
Custom options:
php vendor/bin/generate-tests \
--path=/path/to/laravel \
--output=tests \
--force \
--no-cache
Run all tests:
vendor/bin/phpunit
Run only service tests:
vendor/bin/phpunit --group service
Run only repository tests:
vendor/bin/phpunit --group repository
Run edge case tests:
vendor/bin/phpunit --group edge-case
Exclude incomplete tests:
vendor/bin/phpunit --exclude-group error-handling
Run parametrized tests:
vendor/bin/phpunit --group parametrized
use Bberkaysari\LaravelTestGenerator\Core\ProjectAnalyzer;
use Bberkaysari\LaravelTestGenerator\Generator\Generators\ModelTestGenerator;
use Bberkaysari\LaravelTestGenerator\Generator\Generators\ControllerTestGenerator;
// Analyze entire project
$analyzer = new ProjectAnalyzer('/path/to/laravel');
$results = $analyzer->analyze();
// Results contain:
// - models: Array of analyzed models
// - controllers: Array of analyzed controllers
// - migrations: Array of parsed migrations
// - statistics: Test estimates and metrics
// - performance: Execution time and memory
// Generate model tests
$modelGenerator = new ModelTestGenerator();
foreach ($results['models'] as $model) {
$testCode = $modelGenerator->generate($model);
file_put_contents("tests/Unit/{$model['name']}Test.php", $testCode);
}
// Generate controller tests
$controllerGenerator = new ControllerTestGenerator();
foreach ($results['controllers'] as $controller) {
$testCode = $controllerGenerator->generate($controller);
file_put_contents("tests/Feature/{$controller['name']}Test.php", $testCode);
}
✓ test_model_can_be_instantiated
✓ test_fillable_attributes_work_correctly
✓ test_casts_are_applied_correctly
✓ test_belongs_to_relationships_work
✓ test_has_many_relationships_work
✓ test_belongs_to_many_relationships_work
✓ test_query_scopes_work_correctly
✓ test_database_schema_matches_expectations
✓ test_index // GET /users returns 200
✓ test_store // POST /users creates user
✓ test_store_validation // POST /users validation fails
✓ test_show // GET /users/{id} returns user
✓ test_update // PUT /users/{id} updates user
✓ test_update_validation // PUT /users/{id} validation fails
✓ test_destroy // DELETE /users/{id} removes user
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Models\User;
/**
* @covers \App\Http\Controllers\UserController
*/
class UserControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index(): void
{
$response = $this->get('users');
$response->assertStatus(200);
}
public function test_store(): void
{
$data = [
'name' => 'Test Name',
'email' => 'test@example.com',
];
$response = $this->post('users', $data);
$response->assertStatus(201);
$this->assertDatabaseHas('users', [
'email' => 'test@example.com',
]);
}
public function test_store_validation(): void
{
$response = $this->post('users', []);
$response->assertStatus(422);
$response->assertJsonValidationErrors(['name', 'email']);
}
}
| Project Size | First Run | Cached Run | Speedup | Memory |
|---|---|---|---|---|
| Small (10 models, 5 controllers) | 100ms | 5ms | 20x | 12 MB |
| Medium (100 models, 50 controllers) | 1s | 50ms | 20x | 128 MB |
| Large (1000 models, 200 controllers) | 10s | 500ms | 20x | 512 MB |
Cache Performance (Demo Output):
First run: Time: 27.41 ms
Second run: Time: 2.09 ms (13x faster!) ⚡
Third run: Time: 1.90 ms (14x faster!) ⚡
| Option | Short | Description | Default |
|---|---|---|---|
--path=PATH |
-p |
Laravel project path | Current directory |
--type=TYPE |
-t |
Test type (model, controller, all) | all |
--output=DIR |
-o |
Output directory | tests |
--force |
-f |
Overwrite existing tests | false |
--no-cache |
Disable caching | false |
php vendor/bin/generate-tests --path=/var/www/my-laravel-app
php vendor/bin/generate-tests --force
php vendor/bin/generate-tests --no-cache
php vendor/bin/generate-tests --output=my-custom-tests
php vendor/bin/generate-tests --type=model
Run the interactive demo to see all features:
php bin/demo.php
Demo Output:
╔═══════════════════════════════════════╗
║ Laravel Test Generator Demo ║
║ Enterprise-Grade Tool ║
╚═══════════════════════════════════════╝
📊 ANALYSIS COMPLETE
• Models: 2
• Controllers: 1
• Migrations: 2
• Estimated Tests: 35
⚡ PERFORMANCE:
• Time: 27ms → 2ms (cache)
• Memory: 4 MB
🔷 MODELS (2):
📦 User
Fillable: 3 fields
Casts: 2 fields
Relations: 3
• posts (hasMany → Post)
• profile (hasOne → Profile)
• roles (belongsToMany → Role)
🔷 CONTROLLERS (1):
🎮 UserController
Type: Resource
Methods: 5
• GET index()
• POST store() [validated]
• GET show() {user}
• PUT update() {user} [validated]
• DELETE destroy() {user}
src/
├── Core/
│ ├── ProjectAnalyzer.php # Main orchestration
│ ├── Cache/CacheManager.php # Intelligent caching
│ ├── Performance/ # Performance tracking
│ └── Progress/ # Progress bars
├── Scanner/
│ ├── ProjectScanner.php # Laravel detection
│ └── Scanners/
│ ├── ModelScanner.php # Model analysis
│ ├── ControllerScanner.php# Controller analysis
│ └── MigrationScanner.php # Migration parsing
├── Generator/
│ └── Generators/
│ ├── ModelTestGenerator.php # Model tests
│ └── ControllerTestGenerator.php # Controller tests
└── Commands/
└── GenerateTestsCommand.php # CLI interface
# Run all tests
composer test
# Run specific test suite
vendor/bin/phpunit tests/Unit
vendor/bin/phpunit tests/Integration
# Check code quality
composer phpstan
composer cs-fix
Current Status: ✅ 69 tests, 193 assertions, 100% passing
# Run all tests
composer test
# Run tests with coverage (requires Xdebug or PCOV)
composer test:coverage
# Run mutation testing (requires Xdebug or PCOV)
composer infection
# PHPStan static analysis (level 5)
composer analyse
# Run full CI suite locally
composer ci
Current Status: ✅ 145 tests, 394 assertions, 100% passing
For Xdebug:
pecl install xdebug
# Add to php.ini: zend_extension=xdebug.so
# For PHP 8.0+: xdebug.mode=coverage
For PCOV (faster):
pecl install pcov
# Add to php.ini: extension=pcov.so
This project includes a complete GitHub Actions workflow:
Contributions are welcome! Please:
git checkout -b feature/amazing)composer test)git commit -m 'Add amazing feature')git push origin feature/amazing)This project is licensed under the MIT License - see the LICENSE file for details.
Created by Berkay Sari
Built with:
If you find this project useful, please consider giving it a star! ⭐
Made with ❤️ for the Laravel community