# Testing Guide

## Running Tests

```bash
composer test
```

Or directly:

```bash
php artisan test
php artisan test --filter=InstallerTest
php artisan test tests/Feature/AttachmentTest.php
```

## Configuration

`phpunit.xml` configures:

- SQLite in-memory database (`:memory:`)
- Array cache and session drivers
- `APP_ENV=testing`

Tests use `RefreshDatabase` to migrate before each test class.

## Test Suites

### Unit (`tests/Unit/`)

- `PricingServiceTest` — charge basis calculations
- `DocumentNumberServiceTest` — sequential document numbering

### Feature (`tests/Feature/`)

- `BookingConfirmationTest` — booking lifecycle
- `AvailabilityServiceTest` — space conflict rules
- `QuotationInvoicePaymentTest` — quote → invoice → payment flow
- `AttachmentTest` — file upload validation and storage
- `InstallerTest` — installer validation, lock file, env escaping

## Writing Tests

Pattern:

```php
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed([SettingsSeeder::class, RolesAndPermissionsSeeder::class]);
    }

    public function test_something(): void
    {
        // arrange, act, assert
    }
}
```

Seed `SettingsSeeder` and `RolesAndPermissionsSeeder` when tests touch permissions or document numbering.

## Installer Tests

`InstallerTest` exercises `App\Install\Installer` without requiring MySQL:

- Lock file prevents reinstall detection
- Partial install detection (.env without lock)
- Form validation
- Env value escaping
- APP_KEY generation format
- SQL statement splitting

Full end-to-end installer tests against MySQL are optional in CI; run manually on staging before release.

## Attachment Tests

`AttachmentTest` uses `Storage::fake('local')` and verifies:

- Allowed files are stored under `storage/app/attachments`
- Blocked extensions (e.g. `.php`) are rejected

## Continuous Integration

Recommended CI steps:

```bash
composer install --prefer-dist --no-progress
cp .env.example .env
php artisan key:generate
composer test
```

## Coverage Goals

Prioritize tests for:

- Pricing and availability rules
- Document state transitions (issue, void, reverse)
- Permission enforcement on critical actions
- Installer validation helpers
- Attachment security rules
