Testing
Overview
Smart Academic Hub backend includes comprehensive testing with PHPUnit.
Test Types
Unit Tests
Test individual classes and methods in isolation.
Feature Tests
Test API endpoints and application features end-to-end.
Integration Tests
Test interactions between components.
Running Tests
# Run all tests
php artisan test
# Run specific test file
php artisan test tests/Feature/AuthTest.php
# Run with coverage
php artisan test --coverage
Test Structure
tests/
├── Feature/
│ ├── AuthTest.php
│ ├── AcademyTest.php
│ ├── StudentTest.php
│ └── ...
├── Unit/
│ ├── Models/
│ └── Services/
└── TestCase.php
Writing Tests
Feature Test Example
public function test_user_can_login()
{
$user = User::factory()->create();
$response = $this->postJson('/api/v1/auth/login', [
'email' => $user->email,
'password' => 'password',
]);
$response->assertStatus(200)
->assertJsonStructure(['token']);
}
Unit Test Example
public function test_academy_has_students()
{
$academy = Academy::factory()->create();
$students = Student::factory(3)->create([
'academy_id' => $academy->id
]);
$this->assertCount(3, $academy->students);
}
Test Database
- Uses in-memory SQLite for speed
- Automatic migrations before tests
- Database refresh between tests
Factories
Laravel factories for test data generation:
$user = User::factory()->create();
$academy = Academy::factory()->create();
$student = Student::factory()->create();
Best Practices
- Test business logic, not framework features
- Use factories for test data
- Mock external services
- Keep tests isolated and independent
- Write descriptive test names
- Aim for high coverage
- Test edge cases and error scenarios
Continuous Integration
Tests run automatically on:
- Pull requests
- Commits to main branch
- Pre-deployment checks