›
›
›
  1. docs
  2. ›
  3. byrcsc/laravel-whitelabel
1.x
Browse documentationOpenClose

Getting started

  • Introduction
  • Installation and setup
  • Quick start

Core concepts

  • Brand definitions
  • Fallback and clearing
  • Brand resolution
  • Brand repositories

Using brands

  • Blade components
  • Mail and notifications
  • Queues
  • Spatie Multitenancy

Operations

  • Manage database brands
  • Custom drivers and resolvers
  • Events and listeners
  • Cache management

Reference

  • Configuration
  • Public API
  • Console commands
  • Testing
  • Troubleshooting

Getting started

  • Introduction
  • Installation and setup
  • Quick start

Core concepts

  • Brand definitions
  • Fallback and clearing
  • Brand resolution
  • Brand repositories

Using brands

  • Blade components
  • Mail and notifications
  • Queues
  • Spatie Multitenancy

Operations

  • Manage database brands
  • Custom drivers and resolvers
  • Events and listeners
  • Cache management

Reference

  • Configuration
  • Public API
  • Console commands
  • Testing
  • Troubleshooting

byrcsc/laravel-whitelabel · 1.x

Testing.

Activate temporary brands and build database records without leaking state between tests.

Use InteractsWithBrands when a test needs an active brand without changing configuration or writing a database row. The trait clears active and temporary brands when Laravel tears down the test application.

Activate a temporary brand

Add the trait to your Laravel test case:

namespace Tests;

use Byrcsc\Whitelabel\Testing\InteractsWithBrands;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use InteractsWithBrands;
}

Then activate an inline definition:

it('renders the active brand', function (): void {
    $this->actingWithBrand([
        'name' => 'Acme',
        'colors' => ['primary' => '#7c3aed'],
    ]);

    $this->get('/')->assertSee('Acme');
});

Inline definitions use testing as their identifier. Pass another identifier as the second argument when the value matters:

$brand = $this->actingWithBrand(['name' => 'Acme'], 'acme-test');

The runtime brand still inherits omitted values from the configured default.

Activate an existing brand

Pass an identifier to use the configured repository:

$brand = $this->actingWithBrand('acme');

An unknown identifier throws UnknownBrand, matching production activation.

Define without activating

$this->defineBrand('spare', ['name' => 'Spare']);

expect(Whitelabel::find('spare')?->name())->toBe('Spare');
expect(Whitelabel::current()?->id())->toBe('default');

The trait flushes active and runtime-defined brands before Laravel destroys the test application. It does not delete brands persisted through a repository.

Test database-backed brands

The internal BrandRecord model exposes a public factory for database setup:

use Byrcsc\Whitelabel\Models\BrandRecord;

BrandRecord::factory()
    ->identifiedBy('acme')
    ->create(['name' => 'Acme']);

Create a record that defines no optional values:

BrandRecord::factory()
    ->identifiedBy('acme')
    ->bare()
    ->create();

Read the result through BrandRepository or Whitelabel, not through model methods. The model itself is not part of the public application API.

Test queued brand context

Use a real synchronous or test queue worker when asserting restoration. Queue fakes can prove dispatch but do not run the JobProcessing listener:

config()->set('queue.default', 'database');

$this->actingWithBrand('acme');
GenerateInvoice::dispatch();

Whitelabel::flush();

$this->artisan('queue:work', [
    '--once' => true,
    '--stop-when-empty' => true,
])->assertSuccessful();

What to read next

  • Queues for the restoration behavior these tests exercise.
  • Events and listeners to fake and assert package events.
  • Troubleshooting for state or cache failures in tests.
PreviousConsole commandsNextTroubleshooting
View source

On this page

  1. Activate a temporary brand
  2. Activate an existing brand
  3. Define without activating
  4. Test database-backed brands
  5. Test queued brand context
  6. What to read next