# Testing Guide - Xeek Marketplace

This guide explains how to run automated tests to verify your code changes don't break existing functionality.

## Quick Start

### Run All Tests
```bash
./run-tests.sh
```

### Run Specific Test Suite
```bash
php -d memory_limit=512M ./vendor/bin/phpunit tests/Feature/RouteHealthCheckTest.php --testdox
```

## What's Being Tested

The **RouteHealthCheckTest** suite validates 65 critical routes across the application:

### Route Categories
- **Public Routes** (8 tests) - Home, items, categories, deals, marketplace feed
- **Authentication** (7 tests) - Dashboard access, profile, auth requirements
- **Item Management** (12 tests) - Listings, drafts, purchases, sales, inventory, watchlist
- **Admin Routes** (15 tests) - Dashboard, items, users, reports, settings, analytics
- **Utility Routes** (23 tests) - Messages, transactions, reporting, availability checks, announcements

## Understanding Test Results

### All Green (✔)
```
OK (65 tests, 96 assertions)
```
Your code changes didn't break any routes. Safe to deploy!

### Failures (✘)
```
FAILURES!
Tests: 65, Assertions: 96, Failures: 2
```
Some routes are returning unexpected status codes. Check the error details to identify which routes failed.

## Running Tests After Code Changes

**Recommended workflow:**
1. Make your code changes
2. Run the test suite: `./run-tests.sh`
3. If tests pass → commit and push
4. If tests fail → debug and fix the issue

## Test Database

Tests use an in-memory SQLite database that's automatically created and cleaned up. Each test:
- Creates fresh database
- Runs migrations
- Creates test data (users, items, categories, deals, announcements)
- Tests the route
- Cleans up automatically

No production database is affected.

## Creating New Tests

To add tests for new routes:

1. Open `tests/Feature/RouteHealthCheckTest.php`
2. Add a test method following the pattern:

```php
public function test_new_route_name(): void
{
    // Create test data if needed
    $user = User::factory()->create();

    // Make request
    $response = $this->actingAs($user)->get(route('my.route'));

    // Assert response
    $response->assertStatus(200);
    $response->assertViewIs('view.name');
}
```

3. Run `./run-tests.sh` to verify

## Troubleshooting

### "PHPUnit not found" Error
The script will automatically install dependencies. If it fails:
```bash
COMPOSER_ALLOW_SUPERUSER=1 composer install --dev
```

### Memory Limit Issues
Tests use 512MB memory limit by default. If you hit limits:
```bash
php -d memory_limit=1G ./vendor/bin/phpunit tests/Feature/RouteHealthCheckTest.php
```

### Tests Timeout
Increase the timeout in phpunit.xml or run individual tests:
```bash
php -d memory_limit=512M ./vendor/bin/phpunit tests/Feature/RouteHealthCheckTest.php --filter "test_my_specific_test"
```

## CI/CD Integration

To run tests in your deployment pipeline:

```bash
#!/bin/bash
set -e
cd /var/www/html
./run-tests.sh || exit 1
# Continue with deployment...
```

## Questions?

See `CLAUDE.md` for project documentation or check the test file comments for specific test details.
