1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 20:54:04 +00:00

Bootstrapper tests

This commit is contained in:
Samuel Štancl 2020-05-11 03:37:47 +02:00
parent 73fc525126
commit 6f4b9f486c
20 changed files with 266 additions and 79 deletions

View file

@ -2,37 +2,185 @@
namespace Stancl\Tenancy\Tests\v3;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Events\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Events\Listeners\JobPipeline;
use Stancl\Tenancy\Events\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\TenancyBootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\TenancyBootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\TenancyBootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\TenancyBootstrappers\RedisTenancyBootstrapper;
use Stancl\Tenancy\Tests\TestCase;
class BootstrapperTest extends TestCase
{
public $mockConsoleOutput = false;
public function setUp(): void
{
parent::setUp();
Event::listen(
TenantCreated::class,
JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener()
);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
}
/** @test */
public function database_data_is_separated()
{
config(['tenancy.bootstrappers' => [
DatabaseTenancyBootstrapper::class
]]);
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
$this->artisan('tenants:migrate');
tenancy()->initialize($tenant1);
// Create Foo user
DB::table('users')->insert(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
$this->assertCount(1, DB::table('users')->get());
tenancy()->initialize($tenant2);
// Assert Foo user is not in this DB
$this->assertCount(0, DB::table('users')->get());
// Create Bar user
DB::table('users')->insert(['name' => 'Bar', 'email' => 'bar@bar.com', 'password' => 'secret']);
$this->assertCount(1, DB::table('users')->get());
tenancy()->initialize($tenant1);
// Assert Bar user is not in this DB
$this->assertCount(1, DB::table('users')->get());
$this->assertSame('Foo', DB::table('users')->first()->name);
}
/** @test */
public function cache_data_is_separated()
{
config([
'tenancy.bootstrappers' => [
CacheTenancyBootstrapper::class
],
'cache.default' => 'redis',
]);
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
cache()->set('foo', 'central');
$this->assertSame('central', Cache::get('foo'));
tenancy()->initialize($tenant1);
// Assert central cache doesn't leak to tenant context
$this->assertFalse(Cache::has('foo'));
cache()->set('foo', 'bar');
$this->assertSame('bar', Cache::get('foo'));
tenancy()->initialize($tenant2);
// Assert one tenant's data doesn't leak to another tenant
$this->assertFalse(Cache::has('foo'));
cache()->set('foo', 'xyz');
$this->assertSame('xyz', Cache::get('foo'));
tenancy()->initialize($tenant1);
// Asset data didn't leak to original tenant
$this->assertSame('bar', Cache::get('foo'));
tenancy()->end();
// Asset central is still the same
$this->assertSame('central', Cache::get('foo'));
}
/** @test */
public function redis_data_is_separated()
{
config(['tenancy.bootstrappers' => [
RedisTenancyBootstrapper::class
]]);
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
tenancy()->initialize($tenant1);
Redis::set('foo', 'bar');
$this->assertSame('bar', Redis::get('foo'));
tenancy()->initialize($tenant2);
$this->assertSame(null, Redis::get('foo'));
Redis::set('foo', 'xyz');
Redis::set('abc', 'def');
$this->assertSame('xyz', Redis::get('foo'));
$this->assertSame('def', Redis::get('abc'));
tenancy()->initialize($tenant1);
$this->assertSame('bar', Redis::get('foo'));
$this->assertSame(null, Redis::get('abc'));
$tenant3 = Tenant::create();
tenancy()->initialize($tenant3);
$this->assertSame(null, Redis::get('foo'));
$this->assertSame(null, Redis::get('abc'));
}
/** @test */
public function filesystem_data_is_separated()
{
config(['tenancy.bootstrappers' => [
FilesystemTenancyBootstrapper::class
]]);
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
tenancy()->initialize($tenant1);
Storage::disk('public')->put('foo', 'bar');
$this->assertSame('bar', Storage::disk('public')->get('foo'));
tenancy()->initialize($tenant2);
$this->assertFalse(Storage::disk('public')->exists('foo'));
Storage::disk('public')->put('foo', 'xyz');
Storage::disk('public')->put('abc', 'def');
$this->assertSame('xyz', Storage::disk('public')->get('foo'));
$this->assertSame('def', Storage::disk('public')->get('abc'));
tenancy()->initialize($tenant1);
$this->assertSame('bar', Storage::disk('public')->get('foo'));
$this->assertFalse(Storage::disk('public')->exists('abc'));
$tenant3 = Tenant::create();
tenancy()->initialize($tenant3);
$this->assertFalse(Storage::disk('public')->exists('foo'));
$this->assertFalse(Storage::disk('public')->exists('abc'));
}
/** @test */
public function queue_data_is_separated()
{
// todo
}
}

View file

@ -9,6 +9,7 @@ use Stancl\Tenancy\Events\Listeners\JobPipeline;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Tests\TestCase;
// todo the shouldQueue() doesnt make sense? test if it really works or if its just because of sync queue driver
class JobPipelineTest extends TestCase
{
/** @test */
@ -19,7 +20,7 @@ class JobPipelineTest extends TestCase
])->toListener());
$this->assertFalse(app()->bound('foo'));
Tenant::create();
$this->assertSame('bar', app('foo'));
@ -60,6 +61,12 @@ class JobPipelineTest extends TestCase
$this->assertSame('first job changed property', app('foo'));
}
/** @test */
public function send_can_return_multiple_arguments()
{
// todo
}
}
class FooJob