# Test Suite Documentation

## Overview

This test suite verifies the functionality of the Shamra Academia web application.

## Test Structure

```
tests/
├── bootstrap.php           # Test bootstrap configuration
├── Smoke/                  # Quick health checks
│   └── SmokeTest.php
├── Unit/                   # Unit tests (no external dependencies)
│   ├── Entity/
│   │   └── UserTest.php
│   └── Service/
│       └── AIStudyElasticTest.php
├── Functional/             # HTTP/Controller tests
│   └── Controller/
│       ├── SecurityControllerTest.php
│       ├── HomepageControllerTest.php
│       ├── RegistrationControllerTest.php
│       ├── ProfileControllerTest.php
│       ├── ResearchControllerTest.php
│       ├── ApiControllerTest.php
│       └── CommunityControllerTest.php
├── Integration/            # Database/Service integration tests
│   ├── Repository/
│   │   └── UserRepositoryTest.php
│   └── Service/
│       └── ElasticSearchIntegrationTest.php
└── Traits/
    └── TestHelperTrait.php
```

## Running Tests

### Run All Tests
```bash
./bin/phpunit
# or
composer test
```

### Run Specific Test Suites
```bash
# Smoke tests only (quick health check)
./bin/phpunit --testsuite=Smoke

# Unit tests only
./bin/phpunit --testsuite=Unit

# Functional tests only
./bin/phpunit --testsuite=Functional

# Integration tests only
./bin/phpunit --testsuite=Integration
```

### Run Single Test File
```bash
./bin/phpunit tests/Functional/Controller/SecurityControllerTest.php
```

### Run Single Test Method
```bash
./bin/phpunit --filter=testHomepageLoads
```

### Run with Coverage
```bash
./bin/phpunit --coverage-html var/coverage
```

## Test Environment Setup

### 1. Create Test Database
```bash
php bin/console doctrine:database:create --env=test
php bin/console doctrine:migrations:migrate --env=test --no-interaction
```

### 2. Load Test Fixtures (Optional)
```bash
php bin/console doctrine:fixtures:load --env=test --no-interaction
```

### 3. Configure .env.test
Create a `.env.test` file with test-specific configuration:
```
DATABASE_URL="mysql://user:password@localhost:3306/shamra_test?serverVersion=8.0"
```

## Test Categories

### Smoke Tests
Quick tests to verify critical paths work:
- Homepage loads
- Login page accessible
- Registration page accessible
- Database connection works
- Static assets accessible

### Unit Tests
Test individual components in isolation:
- Entity creation and getters/setters
- Service method logic
- Utility functions

### Functional Tests
Test HTTP request/response:
- Controller actions return expected status codes
- Forms submit correctly
- Authentication flows work
- Authorization rules enforced

### Integration Tests
Test components working together:
- Repository queries work with real database
- Services interact correctly with external systems
- Elasticsearch connectivity

## Writing New Tests

### Example Unit Test
```php
<?php
namespace App\Tests\Unit\Service;

use PHPUnit\Framework\TestCase;

class MyServiceTest extends TestCase
{
    public function testSomething(): void
    {
        $service = new MyService();
        $result = $service->doSomething();
        
        $this->assertEquals('expected', $result);
    }
}
```

### Example Functional Test
```php
<?php
namespace App\Tests\Functional\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyControllerTest extends WebTestCase
{
    public function testPageLoads(): void
    {
        $client = static::createClient();
        $client->request('GET', '/my-page');
        
        $this->assertResponseIsSuccessful();
    }
}
```

## Best Practices

1. **Keep tests fast** - Unit tests should run in milliseconds
2. **Test one thing per test** - Each test method should verify one behavior
3. **Use descriptive names** - `testLoginWithInvalidCredentialsShowsError()`
4. **Isolate tests** - Tests should not depend on each other
5. **Clean up** - Reset state in `tearDown()` if needed
6. **Skip when appropriate** - Use `markTestSkipped()` for missing dependencies

## CI/CD Integration

Add to your CI pipeline:
```yaml
# Example GitHub Actions
- name: Run Tests
  run: |
    php bin/console doctrine:database:create --env=test --if-not-exists
    php bin/console doctrine:migrations:migrate --env=test --no-interaction
    ./bin/phpunit --testsuite=Smoke
    ./bin/phpunit --testsuite=Unit
    ./bin/phpunit --testsuite=Functional
```
