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

Fix event listener tests, improve domain logic

This commit is contained in:
Samuel Štancl 2020-05-09 02:49:08 +02:00
parent 0bbc66f451
commit a602cec940
13 changed files with 148 additions and 14 deletions

View file

@ -19,6 +19,7 @@ class CreateDomainsTable extends Migration
$table->increments('id'); $table->increments('id');
$table->string('domain', 255)->unique(); $table->string('domain', 255)->unique();
$table->string('tenant_id', 36); $table->string('tenant_id', 36);
$table->unsignedTinyInteger('is_primary')->default(false);
$table->timestamps(); $table->timestamps();
$table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade'); $table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade');

View file

@ -1,7 +1,9 @@
<?php <?php
// todo move namespace one dir above
namespace Stancl\Tenancy\Database\Models\Concerns; namespace Stancl\Tenancy\Database\Models\Concerns;
// todo rename
trait HasADataColumn trait HasADataColumn
{ {
public static function bootHasADataColumn() public static function bootHasADataColumn()

View file

@ -0,0 +1,11 @@
<?php
namespace Stancl\Tenancy\Database\Models\Concerns;
trait HasDomains
{
public function domains()
{
return $this->hasMany(config('tenancy.domain_model'));
}
}

View file

@ -0,0 +1,17 @@
<?php
namespace Stancl\Tenancy\Database\Models\Concerns;
/**
* @property-read string $primary_domain_hostname
*/
trait PrimaryDomain
{
// This string should usually come from a relationship implemented by you.
abstract public function getPrimaryDomainHostnameAttribute(): string;
public function route($route, $parameters = [], $absolute = true)
{
return tenant_route($this->primary_domain_hostname, $route, $parameters, $absolute);
}
}

View file

@ -0,0 +1,11 @@
<?php
namespace Stancl\Tenancy\Database\Models\Concerns;
trait TenantConnection
{
public function getConnectionName()
{
return 'tenant';
}
}

View file

@ -19,6 +19,9 @@ use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
class Domain extends Model class Domain extends Model
{ {
public $guarded = []; public $guarded = [];
public $casts = [
'is_primary' => 'bool',
];
public static function booted() public static function booted()
{ {

View file

@ -8,8 +8,18 @@ abstract class QueueableListener implements ShouldQueue
{ {
public static $shouldQueue = false; public static $shouldQueue = false;
public function shouldQueue() abstract public function handle();
public function shouldQueue($event)
{ {
return static::$shouldQueue; if (static::$shouldQueue) {
return true;
} else {
// The listener is not queued so we manually
// pass the event to the handle() method.
$this->handle($event);
return false;
}
} }
} }

View file

@ -53,12 +53,10 @@ if (! function_exists('global_cache')) {
} }
if (! function_exists('tenant_route')) { if (! function_exists('tenant_route')) {
function tenant_route($route, $parameters = [], string $domain = null) function tenant_route(string $domain, $route, $parameters = [], $absolute = true)
{ {
$domain = $domain ?? request()->getHost();
// replace first occurance of hostname fragment with $domain // replace first occurance of hostname fragment with $domain
$url = route($route, $parameters); $url = route($route, $parameters, $absolute);
$hostname = parse_url($url, PHP_URL_HOST); $hostname = parse_url($url, PHP_URL_HOST);
$position = strpos($url, $hostname); $position = strpos($url, $hostname);

View file

@ -0,0 +1,32 @@
<?php
namespace Stancl\Tenancy\Tests\v3;
use Stancl\Tenancy\Tests\TestCase;
class AutomaticModeTest extends TestCase
{
/** @test */
public function custom_bootstrappers_can_be_registered()
{
}
/** @test */
public function context_is_switched_when_tenancy_is_initialized()
{
}
/** @test */
public function context_is_reverted_when_tenancy_is_ended()
{
}
/** @test */
public function context_is_switched_when_tenancy_is_reinitialized()
{
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Stancl\Tenancy\Tests\v3;
use Stancl\Tenancy\Tests\TestCase;
class BootstrapperTest extends TestCase
{
/** @test */
public function database_data_is_separated()
{
}
/** @test */
public function cache_data_is_separated()
{
}
/** @test */
public function redis_data_is_separated()
{
}
/** @test */
public function filesystem_data_is_separated()
{
}
/** @test */
public function queue_data_is_separated()
{
}
}

View file

@ -3,7 +3,7 @@
namespace Stancl\Tenancy\Tests\v3; namespace Stancl\Tenancy\Tests\v3;
use Stancl\Tenancy\Database\Models; use Stancl\Tenancy\Database\Models;
use Stancl\Tenancy\Database\Models\Domain; use Stancl\Tenancy\Database\Models\Concerns\HasDomains;
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException; use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
use Stancl\Tenancy\Resolvers\DomainTenantResolver; use Stancl\Tenancy\Resolvers\DomainTenantResolver;
@ -63,8 +63,5 @@ class DomainTest extends TestCase
class Tenant extends Models\Tenant class Tenant extends Models\Tenant
{ {
public function domains() use HasDomains;
{
return $this->hasMany(Domain::class);
}
} }

View file

@ -2,6 +2,7 @@
namespace Stancl\Tenancy\Tests\v3; namespace Stancl\Tenancy\Tests\v3;
use Illuminate\Events\CallQueuedListener;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Queue;
use Stancl\Tenancy\Database\Models\Tenant; use Stancl\Tenancy\Database\Models\Tenant;
@ -9,7 +10,6 @@ use Stancl\Tenancy\Events\Listeners\QueueableListener;
use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Tests\TestCase; use Stancl\Tenancy\Tests\TestCase;
// todo these tests do not pass: https://github.com/laravel/framework/issues/32722
class EventListenerTest extends TestCase class EventListenerTest extends TestCase
{ {
/** @test */ /** @test */
@ -30,12 +30,14 @@ class EventListenerTest extends TestCase
{ {
Queue::fake(); Queue::fake();
FooListener::$shouldQueue = true;
Event::listen(TenantCreated::class, FooListener::class); Event::listen(TenantCreated::class, FooListener::class);
FooListener::$shouldQueue = true;
Tenant::create(); Tenant::create();
Queue::assertPushed(FooListener::class); Queue::assertPushed(CallQueuedListener::class, function (CallQueuedListener $job) {
return $job->class === FooListener::class;
});
$this->assertFalse(app()->bound('foo')); $this->assertFalse(app()->bound('foo'));
} }

View file

@ -106,4 +106,16 @@ class TenantModelTest extends TestCase
$this->assertSame(1, $tenant1->id); $this->assertSame(1, $tenant1->id);
$this->assertSame(2, $tenant2->id); $this->assertSame(2, $tenant2->id);
} }
/** @test */
public function custom_tenant_model_can_be_used()
{
}
/** @test */
public function custom_tenant_model_that_doesnt_extend_vendor_Tenant_model_can_be_used()
{
}
} }