# Patent Module Testing Guide

## Overview

The Patent module includes a comprehensive test suite covering unit tests, feature tests, and integration tests for all components.

## Test Structure

```
Tests/
├── TestCase.php                          # Base test case
├── Unit/                                 # Unit tests
│   ├── PatentTicketTest.php
│   ├── PatentTransactionTest.php
│   ├── AnnualFeeDeadlineTest.php
│   └── AnnualFeeServiceTest.php
├── Feature/                              # Feature tests
│   ├── PatentControllerTest.php
│   ├── PatentTransactionControllerTest.php
│   ├── AnnualFeeControllerTest.php
│   └── PatentWorkflowIntegrationTest.php
└── Database/factories/                   # Model factories
    ├── PatentTicketFactory.php
    ├── PatentTransactionFactory.php
    ├── AnnualFeeDeadlineFactory.php
    └── ApplicationChangeFactory.php
```

## Running Tests

### Quick Start
```bash
cd Modules/Patent
./run-tests.sh                    # Run all tests
./run-tests.sh -t unit           # Run unit tests only
./run-tests.sh -c                # Run with coverage
./run-tests.sh -f PatentTransactionTest  # Run specific test
```

### Manual Execution
```bash
php ../../../vendor/bin/phpunit --configuration phpunit.xml
```

## Test Categories

### Unit Tests
- **Model Tests**: Test individual model functionality
- **Service Tests**: Test business logic services
- **Factory Tests**: Test data generation

### Feature Tests
- **Controller Tests**: Test HTTP endpoints and responses
- **API Tests**: Test API functionality
- **Form Tests**: Test form validation and submission

### Integration Tests
- **Workflow Tests**: Test complete user workflows
- **Database Tests**: Test database interactions
- **External Service Tests**: Test third-party integrations

## Test Data Management

### Model Factories
```php
// Create test data
$patent = PatentTicketFactory::new()->active()->create();
$transaction = PatentTransactionFactory::new()->pending()->create();
$deadline = AnnualFeeDeadlineFactory::new()->overdue()->create();
```

### Helper Methods
```php
// Create test entities
$user = $this->createUserWithRole(1);
$patent = $this->createTestPatent();
$transaction = $this->createTestTransaction($patent->id);

// Assert states
$this->assertPatentStatus($patent->id, 'Active');
$this->assertTransactionStatus($transaction->id, 'pending');
```

## Best Practices

1. **Use factories** for test data generation
2. **One assertion per test** for clarity
3. **Descriptive test names** that explain the behavior
4. **Clean database state** for each test
5. **Test edge cases** and error conditions

## Troubleshooting

### Common Issues
- **Database errors**: Clear test database and re-run migrations
- **Permission errors**: Ensure test runner is executable
- **Memory issues**: Increase PHP memory limit

### Debug Mode
```bash
./run-tests.sh -v  # Verbose output
```

## Coverage

The test suite aims for:
- **Models**: 95%+ coverage
- **Services**: 90%+ coverage
- **Controllers**: 85%+ coverage
- **Overall**: 90%+ coverage

## Continuous Integration

Tests can be integrated into CI/CD pipelines using the provided configuration and test runner script.