1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 17:24:03 +00:00

Pending tenants: Add getPendingAttributes()

This method lets the user specify default values for custom
non-nullable columns. The primary use case is when the tenants table
has a column like 'slug' and createPending() is called with no
value for 'slug'. This would produce an exception due to the column
having no default value.

Here, getPendingAttributes() can set an initial dummy slug (like a
randomly generated string) before it's overwritten during a pull.

getPendingAttributes() accepts an $attributes array which corresponds
to the attributes passed to createPending(). The array returned from
getPendingAttributes() is ultimately merged with $attributes, so
the user doesn't need to use the $attributes value in
getPendingAttributes(), however it serves to provide more context when
the pending attributes might be dependent on $attributes and therefore
derived from the $attributes actually being used.

Also fixed the `finally` branch in createPending() as it was
potentially referencing the $tenant variable before it was initialized.
This commit is contained in:
Samuel Štancl 2025-10-27 17:54:39 +01:00
parent aba7a50619
commit 6523f24a60
3 changed files with 55 additions and 2 deletions

View file

@ -2,8 +2,12 @@
declare(strict_types=1);
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Stancl\Tenancy\Commands\ClearPendingTenants;
use Stancl\Tenancy\Commands\CreatePendingTenants;
use Stancl\Tenancy\Events\CreatingPendingTenant;
@ -13,6 +17,13 @@ use Stancl\Tenancy\Events\PullingPendingTenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use function Stancl\Tenancy\Tests\pest;
beforeEach($cleanup = function () {
Tenant::$extraCustomColumns = [];
Tenant::$getPendingAttributesUsing = null;
});
afterEach($cleanup);
test('tenants are correctly identified as pending', function (){
Tenant::createPending();
@ -191,3 +202,25 @@ test('commands run for pending tenants too if the with pending option is passed'
$artisan->assertExitCode(0);
});
test('pending tenants can have default attributes for non-nullable columns', function (bool $withPendingAttributes) {
Schema::table('tenants', function (Blueprint $table) {
$table->string('slug')->unique();
});
Tenant::$extraCustomColumns = ['slug'];
if ($withPendingAttributes) Tenant::$getPendingAttributesUsing = fn () => [
'slug' => Str::random(8),
];
$fn = fn () => Tenant::createPending();
// If there are non-nullable custom columns, and createPending() is called
// on its own without any values passed for those columns (as it would be called
// by the tenants:pending-create artisan command), we expect it to fail, unless
// getPendingAttributes() provides default values for those custom columns.
if ($withPendingAttributes)
expect($fn)->not()->toThrow(QueryException::class);
else
expect($fn)->toThrow(QueryException::class);
})->with([true, false]);