> ## Documentation Index
> Fetch the complete documentation index at: https://docs.unleash-commerce.eu/llms.txt
> Use this file to discover all available pages before exploring further.

# Testing

> Write tests for your Unleash Commerce Core functionality

## Overview

Unleash Commerce Core ships with factories for all models, making it easy to write tests for your custom implementations and extensions.

## Model Factories

All core models include Laravel factories that you can use in your tests. Create test data quickly:

```php theme={null}
use Esign\UnleashCommerce\Core\Models\Product;
use Esign\UnleashCommerce\Core\Models\Order;
use Esign\UnleashCommerce\Core\Models\Customer;

$product = Product::factory()->create();
$customer = Customer::factory()->create();
$order = Order::factory()->for($customer)->create();
```

## Using Factories with Relationships

Use the `for` method to associate models:

```php theme={null}
$order = Order::factory()
    ->for($customer)
    ->create();

$cartLine = CartLine::factory()
    ->for($product)
    ->for($cart)
    ->create();
```

### Attaching Customers to Users

The User factory includes a `hasCustomer` method to easily attach customers with proper pivot attributes.

<Note>
  By default, `hasCustomer` marks the customer as **active** (`is_active` = true, `invitation_accepted_at` = now).
  The active customer is used throughout the application for authorization and scoping data in B2B contexts.
</Note>

```php theme={null}
// Automatically creates a customer and attaches it as active
$user = User::factory()
    ->hasCustomer()
    ->create();

// With a specific customer (also marked as active)
$customer = Customer::factory()->create();
$user = User::factory()
    ->hasCustomer($customer)
    ->create();

// Override pivot attributes to make customer inactive
$user = User::factory()
    ->hasCustomer($customer, ['is_active' => false])
    ->create();
```

### Assigning Roles to Users

The User factory includes a `role` method to assign roles to users in the context of their active customer.

<Note>
  Roles in Unleash Commerce are scoped to customers (B2B multi-tenancy). The `role` method automatically
  uses the user's **active customer** as the authorization context. If no customer is explicitly provided,
  the role is assigned using `$user->activeCustomer`.
</Note>

```php theme={null}
// Assigns role in the context of the user's active customer
$user = User::factory()
    ->hasCustomer()
    ->role('admin')
    ->create();

// Explicitly specify a customer context
$user = User::factory()
    ->hasCustomer($customerA)
    ->role('manager', $customerB) // Role is scoped to $customerB
    ->create();

// With a Role instance
$role = Role::factory()->create(['name' => 'editor']);
$user = User::factory()
    ->hasCustomer()
    ->role($role) // Role is scoped to the active customer
    ->create();
```

## Database Assertions

Use model classes instead of table names:

```php theme={null}
$this->assertDatabaseHas(Product::class, ['name' => 'Test']);
$this->assertDatabaseMissing(Product::class, ['deleted' => true]);
```
