Files
InvoiceShelf/tests/Feature/PilotSpec/SetDomainTest.php
T
Darko Gjorgjijoski 605d700f75 test: add the behavioral spec suites (pilot + ten domains)
98 tests pinning observable behavior ahead of the authorship rewrite.
2026-08-20 17:19:26 +02:00

54 lines
2.0 KiB
PHP

<?php
// Pilot behavioural suite — the domain step and environment-file editing rules.
// Spec: platform-operations-spec.md §3 (domain step, environment-file editing).
// These tests edit the real environment file and restore it afterwards.
use function Pest\Laravel\putJson;
beforeEach(function () {
Artisan::call('db:seed', ['--class' => 'DatabaseSeeder', '--force' => true]);
$this->envBackup = file_get_contents(base_path('.env'));
});
afterEach(function () {
file_put_contents(base_path('.env'), $this->envBackup);
});
it('requires a domain', function () {
putJson('/api/v1/installation/set-domain', [])->assertStatus(422);
});
it('always writes the session domain as the host portion of the submitted value', function () {
config(['app.url' => 'http://pilot.test']);
putJson('/api/v1/installation/set-domain', ['app_domain' => 'http://other.test'])
->assertOk()
->assertJson(['success' => true]);
$env = file_get_contents(base_path('.env'));
expect($env)->toContain('SESSION_DOMAIN=other.test');
});
it('writes the stateful-domains entry when the submitted domain differs from the current one', function () {
config(['app.url' => 'http://pilot.test']);
putJson('/api/v1/installation/set-domain', ['app_domain' => 'elsewhere.test'])->assertOk();
$env = file_get_contents(base_path('.env'));
expect($env)->toContain('SANCTUM_STATEFUL_DOMAINS=elsewhere.test');
expect($env)->toContain('SESSION_DOMAIN=elsewhere.test');
});
it('replaces an existing key in place rather than appending a duplicate', function () {
config(['app.url' => 'http://pilot.test']);
putJson('/api/v1/installation/set-domain', ['app_domain' => 'first.test'])->assertOk();
putJson('/api/v1/installation/set-domain', ['app_domain' => 'second.test'])->assertOk();
$env = file_get_contents(base_path('.env'));
expect(substr_count($env, "\nSESSION_DOMAIN=") + (str_starts_with($env, 'SESSION_DOMAIN=') ? 1 : 0))
->toBe(1);
expect($env)->toContain('SESSION_DOMAIN=second.test');
});