# QuitSure Test Suite Documentation

## Quick Start

```bash
# Run all tests
php artisan test

# Run by category
php artisan test --testsuite=Unit
php artisan test --testsuite=Feature

# Run specific file
php artisan test tests/Unit/Services/RazorpayServiceTest.php
```

---

## Test Statistics

| Category | Files | Lines |
|----------|-------|-------|
| Unit Tests | 13 | ~3,500 |
| Feature Tests | 48 | ~31,200 |
| **Total** | **61** | **~34,700** |

---

## Directory Structure

```
tests/
├── Helpers/
│   ├── MockHelper.php          # Mock factory for Query/Service classes
│   └── TestDataBuilder.php     # Test data builders
├── Unit/
│   ├── Helpers/
│   │   ├── CommonHelperTest.php
│   │   └── FoundationTest.php
│   └── Services/
│       ├── CalculationServiceTest.php
│       ├── EmailServiceTest.php
│       ├── RazorpayServiceTest.php
│       └── ... (10 service tests)
├── Feature/
│   ├── Controllers/
│   │   ├── V2/
│   │   │   ├── OnBoarding/     # 5 languages × 4 controllers
│   │   │   ├── B2C/            # 5 languages + shared
│   │   │   └── B2B/            # 5 languages + shared
│   │   ├── Payment/            # Stripe, Razorpay, PaymentFailed
│   │   ├── Webhook/            # Stripe, SendGrid, Postmark
│   │   └── Other/              # Article, SubModule, Subscribe, Result
│   └── Integration/
│       ├── OnBoardingFlowTest.php
│       ├── B2CCheckoutFlowTest.php
│       ├── B2BPartnerFlowTest.php
│       └── PaymentFlowTest.php
```

---

## Key Helper Classes

### MockHelper

Creates mocks for Query and Service classes:

```php
use Tests\Helpers\MockHelper;

// Mock Query class
$mock = MockHelper::mockUserQuery();
$mock->shouldReceive('getUserDetail')->andReturn($userData);
$this->app->instance(UserQuery::class, $mock);

// Mock Service class
$mock = MockHelper::mockRazorpayService();

// Setup sessions
MockHelper::setupLoggedInUserSession(['iUserID' => 1]);
MockHelper::setupPartnerSession(['iPartnerId' => 1]);
```

### TestDataBuilder

Creates test data objects:

```php
use Tests\Helpers\TestDataBuilder;

$user = TestDataBuilder::userData(['iUserID' => 1]);
$partner = TestDataBuilder::partnerData(['vName' => 'Test']);
$userWithRelations = TestDataBuilder::userWithAllRelations();
```

---

## Test Categories

### Unit Tests

| File | Coverage |
|------|----------|
| CommonHelperTest | `safe_env()`, `getActiveDayEnv()` |
| FoundationTest | Core framework foundation checks |
| CalculationServiceTest | Addiction score calculations |
| EmailServiceTest | SendGrid email sending |
| PostmarkServiceTest | Postmark email delivery |
| RazorpayServiceTest | Order generation, payment details |
| UserUtilityServiceTest | Encryption, tokens, Branch links |
| UtilityServiceTest | Session helpers, platform detection |
| GeoLocationServiceTest | IP-based location detection |
| GympassServiceTest | Gympass partner integration |
| BajajHealthEventServiceTest | Bajaj Health partner events |
| SubModuleServiceTest | Content module handling |

### Feature Tests - Controllers

| Category | Languages | Description |
|----------|-----------|-------------|
| OnBoarding | 5 | Multi-step wizard flow |
| B2C | 5 | Consumer checkout flow |
| B2B | 5 | Partner integration flow |
| Payment | - | Stripe, Razorpay processing |
| Webhook | - | Email/payment webhooks |

### Integration Tests

| Test | Flow |
|------|------|
| OnBoardingFlowTest | Complete onboarding journey |
| B2CCheckoutFlowTest | Email → OTP → Checkout |
| B2BPartnerFlowTest | Partner → User → Checkout |
| PaymentFlowTest | All payment methods |

---

## Mocking Strategy

- **HTTP**: `Http::fake()` - Auto-enabled in TestCase
- **Mail**: `Mail::fake()` - Auto-enabled in TestCase
- **Storage**: `Storage::fake('shared')` - Use in tests
- **Database**: MockHelper mocks all Query classes

---

## Writing Tests

### Basic Pattern

```php
use Tests\Helpers\MockHelper;
use Tests\Helpers\TestDataBuilder;

beforeEach(function () {
    $this->mockUserQuery = MockHelper::mockUserQuery();
    $this->app->instance(UserQuery::class, $this->mockUserQuery);
});

describe('GET /web/eng/email', function () {
    it('renders email page', function () {
        session(['CustomerData' => ['vSource' => 'test']]);

        $response = $this->get('/web/eng/email');

        $response->assertStatus(200);
        $response->assertViewIs('B2C.english.email');
    });
});
```

---

## Skipped Tests

Some tests are skipped due to SDK limitations:

- Stripe SDK signature verification
- SendGrid signature verification
- FacebookService (uses SDK with direct API calls)

---
