| title | Testing Rules Summary | ||||
|---|---|---|---|---|---|
| module | Lang | ||||
| type | concept | ||||
| tags |
|
||||
| created | 2026-07-14 | ||||
| updated | 2026-07-14 | ||||
| qmd | lang service helper text fix | ||||
| related |
|
- MAI usare PHPUnit class-based (
class Test extends TestCase) - SEMPRE usare Pest functional syntax (
test(),it()) - I test devono essere scritti in formato Pest, non PHPUnit
- MAI usare
use RefreshDatabasenei test - MAI usare
RefreshDatabasetrait - Utilizzare
.env.testingcon SQLite in-memory - Usare
DatabaseTransactionsse necessario (raro)
- Tutti i test devono leggere
.env.testing - PHPStan usa configurazione da
phpstan.neon(non passare livello come parametro) - Script di conversione vanno in
bashscripts/, non nella root di Laravel
- MAI estendere classi Filament direttamente
- SEMPRE estendere
XotBase*oLangBase*a seconda del modulo - Controllare se il modulo è multilingue prima di scegliere
- MAI usare
property_exists()con modelli Eloquent - Usare
isset()per proprietà magiche
.env.testing- configurazione ottimizzata per test velociphpunit.xml- configurazione principalephpstan.neon- configurazione PHPStan (livello già impostato)
laravel/tests/ # Test principali
laravel/Modules/*/tests/ # Test dei moduli
laravel/Themes/*/tests/ # Test dei temi
Ogni modulo può avere il proprio Pest.php con:
- Estensioni personalizzate
- Helper functions
- Custom expectations
# ❌ ERRATO - Non passare il livello
./vendor/bin/phpstan analyse --level=8 Modules
# ✅ CORRETTO - Usa configurazione da phpstan.neon
./vendor/bin/phpstan analyse Modules --memory-limit=-1# Run all tests
composer test
# Run Pest tests
./vendor/bin/pest
# Test specific module
cd Modules/ModuleName && ./vendor/bin/pest# Script di conversione (in bashscripts/)
php bashscripts/test_conversion/convert_phpunit_to_pest.php- Usare
RefreshDatabasein qualsiasi test - Scrivere test PHPUnit class-based invece di Pest
- Passare livello a PHPStan come parametro
- Mettere script nella root di Laravel invece che in
bashscripts/ - Estendere classi Filament direttamente invece di XotBase/LangBase
- Usare
.env.testingcon SQLite in-memory - Scrivere test in formato Pest functional
- Usare
DatabaseTransactionsinvece diRefreshDatabase - Seguire la struttura esistente dei test
- Documentare le regole nei file
docs/dei moduli
Ogni modulo e tema deve documentare:
- Regole specifiche del modulo
- Configurazione testing
- Esempi di test corretti
- Errori comuni da evitare
I file di documentazione vanno nelle cartelle docs/ dentro ogni modulo/tema.
This document provides testing guidelines and examples for the Lang module in Laraxot.
Modules/Lang/tests/
├── Feature/
│ ├── (feature tests)
├── Unit/
│ └── (unit tests)
├── TestCase.php
└── Pest.php
- TestCase.php - Base test case with database configuration
- Pest.php - Pest configuration and extensions
- Feature/ - Feature tests for Lang functionality
- Unit/ - Unit tests for Lang components
The Lang TestCase extends the base testing configuration and provides:
- Database connection setup
- Module-specific configuration
- Test environment setup
Lang module uses the following database connections:
lang- Main Lang module connectionmysql- Default connection- All connections configured to use test database
Use database transactions for test isolation:
use Illuminate\Foundation\Testing\DatabaseTransactions;Each test should be independent:
protected function tearDown(): void
{
parent::tearDown();
// Clean up test data
}Configure Lang-specific settings:
protected function setUp(): void
{
parent::setUp();
// Configure Lang module
config(['lang.default_locale' => 'it']);
config(['lang.fallback_locale' => 'en']);
}test('translation can be created', function () {
$translation = \Modules\Lang\Models\Translation::create([
'key' => 'test.key',
'value' => 'Test translation',
'locale' => 'it',
'group' => 'messages',
]);
expect($translation)->toBeInstanceOf(\Modules\Lang\Models\Translation::class);
expect($translation->key)->toBe('test.key');
});test('lang configuration is loaded', function () {
$langConfig = config('lang');
expect($langConfig['default_locale'])->toBe('it');
expect($langConfig['fallback_locale'])->toBe('en');
});test('lang service provider is registered', function () {
$app = app();
expect($app->bound(\Modules\Lang\Providers\LangServiceProvider::class))->toBeTrue());
});# Run all Lang module tests
./vendor/bin/pest Modules/Lang/tests
# Run tests with coverage
./vendor/bin/pest Modules/Lang/tests --coverage
# Run tests with verbose output
./vendor/bin/pest Modules/Lang/tests --verbose# Run PHPStan on Lang module
./vendor/bin/phpstan analyze Modules/Lang
# Run PHPMD on Lang module
./vendor/bin/phpmd Modules/Lang/src
# Run PHPInsights on Lang module
./vendor/bin/phpinsights analyse Modules/LangProblem: Lang configuration not loaded
Solution: Ensure proper configuration in TestCase:
protected function setUp(): void
{
parent::setUp();
config(['lang.default_locale' => 'it']);
config(['lang.fallback_locale' => 'en']);
}Problem: Database connection issues
Solution: Configure database connections properly:
protected function createApplication()
{
$app = parent::createApplication();
$app['config']->set([
'database.connections.lang.database' => 'modulo questionari_data_test',
]);
return $app;
}- Aim for 100% code coverage
- Test all public methods
- Test all edge cases
- Test all error scenarios
- Tests should run in <200ms each
- Use database transactions for isolation
- Optimize database queries
- Minimize test data
- All tests must pass PHPStan level 9+
- All tests must follow DRY, KISS, SOLID principles
- All tests must be maintainable
- All tests must be robust
- Configure testing environment
- Set up database connections
- Install testing dependencies
- Verify configuration
- Write tests for new features
- Update existing tests
- Add regression tests
- Maintain test coverage
- Run tests
- Run quality checks
- Fix any issues
- Update documentation
- Ensure all tests pass
- Verify coverage requirements
- Update documentation
- Commit changes
- Update this file when adding new tests
- Document any special testing requirements
- Add examples for new test types
- Keep documentation current
- Update root documentation when module testing changes
- Add backlinks to this file
- Keep documentation consistent
- Update troubleshooting guides
test('translation can be created', function () {
$translation = \Modules\Lang\Models\Translation::create([
'key' => 'test.key',
'value' => 'Test translation',
'locale' => 'it',
'group' => 'messages',
'namespace' => 'app',
]);
expect($translation)->toBeInstanceOf(\Modules\Lang\Models\Translation::class);
expect($translation->key)->toBe('test.key');
expect($translation->value)->toBe('Test translation');
expect($translation->locale)->toBe('it');
expect($translation->group)->toBe('messages');
expect($translation->namespace)->toBe('app');
});test('lang service can translate text', function () {
$service = new \Modules\Lang\Services\LangService();
$translation = $service->translate('test.key', 'it');
expect($translation)->toBe('Test translation');
});test('lang api can get translations', function () {
\Modules\Lang\Models\Translation::create([
'key' => 'test.key',
'value' => 'Test translation',
'locale' => 'it',
'group' => 'messages',
]);
$response = $this->get('/api/lang/translations/it/messages');
$response->assertStatus(200);
$response->assertJson([
'test.key' => 'Test translation',
]);
});- Understand the feature to test
- Review existing tests
- Plan test scenarios
- Prepare test data
- Use descriptive test names
- Use proper assertions
- Clean up test data
- Document tests
- Run tests
- Check coverage
- Run quality checks
- Update documentation
- All tests pass
- Coverage requirements met
- Quality checks pass
- Documentation updated
Following these guidelines will ensure your Lang module tests are:
- Fast and reliable
- Maintainable and scalable
- Comprehensive and thorough
- Consistent and robust
Remember: Good tests are the foundation of reliable software development.
Last updated: January 2025 * * * * Last updated: January 2025