›
byrcsc/laravel-whitelabel · 1.x
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.
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.
Pass an identifier to use the configured repository:
$brand = $this->actingWithBrand('acme');An unknown identifier throws UnknownBrand, matching production activation.
$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.
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.
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();