# Testing

Turista uses Pest PHP 4 for testing. Tests run against an in-memory SQLite database.

## Running tests

```bash
php artisan test
```

To run a specific test file:

```bash
php artisan test tests/Feature/Auth/LoginTest.php
```

To run with verbose output:

```bash
php artisan test --verbose
```

## Test database

`phpunit.xml` configures SQLite in-memory mode:

```xml
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
```

The `RefreshDatabase` trait is applied to all feature and unit tests via `tests/Pest.php`.

## Test structure

| Directory | Purpose |
|-----------|---------|
| `tests/Feature/` | End-to-end HTTP and feature tests. |
| `tests/Unit/` | Isolated tests for services, policies, and models. |

## Base test case

`tests/TestCase.php` provides shared helpers and sets up:

- Cache and permission cache flushing.
- CoreVerde HTTP faking.
- Default CoreVerde config for tests.

## Writing feature tests

A typical feature test:

```php
it('allows a verified customer to create a reservation', function () {
    $customer = User::factory()
        ->has(Customer::factory())
        ->create()
        ->assignRole('customer');

    $unit = Unit::factory()->create();

    actingAs($customer, 'api')
        ->postJson('/api/v1/customer/reservations', [
            'unit_id' => $unit->id,
            'check_in_date' => now()->addDay()->toDateString(),
            'check_out_date' => now()->addDays(3)->toDateString(),
            'adults_count' => 2,
        ])
        ->assertCreated();
});
```

## Writing unit tests

Unit tests focus on a single class or method without HTTP:

```php
it('blocks dates in the availability ledger', function () {
    $unit = Unit::factory()->create();

    UnitAvailabilityService::blockDates($unit, ['2026-07-01', '2026-07-02']);

    expect($unit->availabilities()->where('status', 'blocked')->count())->toBe(2);
});
```

## Coverage

As of the latest project progress report, the suite has 297 tests and 1111 assertions. Aim to maintain or improve this coverage when adding features.
