Skip to content

Testing

The companion app uses Pest v4 for testing within the Laravel framework.

Running Tests

bash
php artisan test --compact                          # Full suite
php artisan test --compact --filter=WishlistTest    # Filter by name
php artisan test --compact tests/Feature/           # Run a directory

Test Structure

tests/
  Feature/
    BundlePricingTest.php       ← Bundle pricing calculations
    BundleProxyApiTest.php      ← Bundle proxy endpoint tests
    BundleServiceTest.php       ← Bundle service logic
    ...
  Unit/
    ...

Most tests are feature tests that test through HTTP or service classes. Unit tests are used for isolated logic.

Writing Tests

Creating Tests

bash
php artisan make:test --pest MyFeatureTest           # Feature test
php artisan make:test --pest --unit MyUnitTest        # Unit test

Pest Syntax

php
it('returns inventory for a product', function () {
    // Arrange
    $product = Product::factory()->create();

    // Act
    $response = $this->get("/shopify/proxy/inventory/{$product->handle}");

    // Assert
    $response->assertOk();
    expect($response->json('data'))->toHaveCount(2);
});

Factories

Always use factories to create test models:

php
$bundle = Bundle::factory()->create();
$slot = BundleSlot::factory()->for($bundle)->create();
$item = BundleSlotItem::factory()->for($slot)->create();

Check if a factory has custom states before manually setting up models:

php
$bundle = Bundle::factory()
    ->active()
    ->withSlots(3)
    ->create();

Available Factories

FactoryModel
BundleFactoryBundle
BundleSlotFactoryBundleSlot
BundleSlotItemFactoryBundleSlotItem
BundleAnchorFactoryBundleAnchor

Conventions

  • Use it() syntax with descriptive test names
  • Use expect() for assertions (Pest style over PHPUnit assertions)
  • Use fake() or $this->faker for fake data (follow existing convention in the file)
  • Feature tests should use RefreshDatabase
  • Mock external services (Shopify API, Klaviyo) — never make real API calls in tests
  • Run vendor/bin/pint --dirty after writing tests to ensure formatting

Code Coverage

Run tests with coverage:

bash
php artisan test --coverage

Internal developer documentation