mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 03:14:03 +00:00
[4.x] Migrate tests to Pest (#884)
* Add Pest dependencies * Add base Pest file * Convert test cases * Remove non-compound imports * Adopt expectation API * Optimize uses * Shift cleanup * phpunit -> pest * Fix tests in PR #884 PHPUnit to Pest Converter (#885) * fixed tests, remove method duplications, restore necessary inner classes * Update CommandsTest.php * temporary checks run on `shift-64622` on branch. * fixed `TestSeeder` class not resolved * fixed messed up names * removed `uses` from individual files and add it in `Pest` * extract tests to helpers * use pest dataset * Update AutomaticModeTest.php * newline * todo convention * resolve reviews * added `// todo@tests` * remove shift branch from CI workflow Co-authored-by: Samuel Štancl <samuel@archte.ch> * check if I have write permission * Convert newly added tests to Pest Co-authored-by: Shift <shift@laravelshift.com> Co-authored-by: Abrar Ahmad <abrar.dev99@gmail.com>
This commit is contained in:
parent
69de181b7d
commit
b47c5549ef
32 changed files with 3010 additions and 3478 deletions
|
|
@ -2,226 +2,204 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Filesystem\FilesystemAdapter;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\JobPipeline\JobPipeline;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use ReflectionObject;
|
||||
use ReflectionProperty;
|
||||
use Stancl\JobPipeline\JobPipeline;
|
||||
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Events\TenantCreated;
|
||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||
use Stancl\Tenancy\Events\TenantCreated;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||
|
||||
class BootstrapperTest extends TestCase
|
||||
beforeEach(function () {
|
||||
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('database data is separated', function () {
|
||||
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']);
|
||||
expect(DB::table('users')->get())->toHaveCount(1);
|
||||
|
||||
tenancy()->initialize($tenant2);
|
||||
|
||||
// Assert Foo user is not in this DB
|
||||
expect(DB::table('users')->get())->toHaveCount(0);
|
||||
// Create Bar user
|
||||
DB::table('users')->insert(['name' => 'Bar', 'email' => 'bar@bar.com', 'password' => 'secret']);
|
||||
expect(DB::table('users')->get())->toHaveCount(1);
|
||||
|
||||
tenancy()->initialize($tenant1);
|
||||
|
||||
// Assert Bar user is not in this DB
|
||||
expect(DB::table('users')->get())->toHaveCount(1);
|
||||
expect(DB::table('users')->first()->name)->toBe('Foo');
|
||||
});
|
||||
|
||||
test('cache data is separated', function () {
|
||||
config([
|
||||
'tenancy.bootstrappers' => [
|
||||
CacheTenancyBootstrapper::class,
|
||||
],
|
||||
'cache.default' => 'redis',
|
||||
]);
|
||||
|
||||
$tenant1 = Tenant::create();
|
||||
$tenant2 = Tenant::create();
|
||||
|
||||
cache()->set('foo', 'central');
|
||||
expect(Cache::get('foo'))->toBe('central');
|
||||
|
||||
tenancy()->initialize($tenant1);
|
||||
|
||||
// Assert central cache doesn't leak to tenant context
|
||||
expect(Cache::has('foo'))->toBeFalse();
|
||||
|
||||
cache()->set('foo', 'bar');
|
||||
expect(Cache::get('foo'))->toBe('bar');
|
||||
|
||||
tenancy()->initialize($tenant2);
|
||||
|
||||
// Assert one tenant's data doesn't leak to another tenant
|
||||
expect(Cache::has('foo'))->toBeFalse();
|
||||
|
||||
cache()->set('foo', 'xyz');
|
||||
expect(Cache::get('foo'))->toBe('xyz');
|
||||
|
||||
tenancy()->initialize($tenant1);
|
||||
|
||||
// Asset data didn't leak to original tenant
|
||||
expect(Cache::get('foo'))->toBe('bar');
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
// Asset central is still the same
|
||||
expect(Cache::get('foo'))->toBe('central');
|
||||
});
|
||||
|
||||
test('redis data is separated', function () {
|
||||
config(['tenancy.bootstrappers' => [
|
||||
RedisTenancyBootstrapper::class,
|
||||
]]);
|
||||
|
||||
$tenant1 = Tenant::create();
|
||||
$tenant2 = Tenant::create();
|
||||
|
||||
tenancy()->initialize($tenant1);
|
||||
Redis::set('foo', 'bar');
|
||||
expect(Redis::get('foo'))->toBe('bar');
|
||||
|
||||
tenancy()->initialize($tenant2);
|
||||
expect(Redis::get('foo'))->toBe(null);
|
||||
Redis::set('foo', 'xyz');
|
||||
Redis::set('abc', 'def');
|
||||
expect(Redis::get('foo'))->toBe('xyz');
|
||||
expect(Redis::get('abc'))->toBe('def');
|
||||
|
||||
tenancy()->initialize($tenant1);
|
||||
expect(Redis::get('foo'))->toBe('bar');
|
||||
expect(Redis::get('abc'))->toBe(null);
|
||||
|
||||
$tenant3 = Tenant::create();
|
||||
tenancy()->initialize($tenant3);
|
||||
expect(Redis::get('foo'))->toBe(null);
|
||||
expect(Redis::get('abc'))->toBe(null);
|
||||
});
|
||||
|
||||
test('filesystem data is separated', function () {
|
||||
config(['tenancy.bootstrappers' => [
|
||||
FilesystemTenancyBootstrapper::class,
|
||||
]]);
|
||||
|
||||
$old_storage_path = storage_path();
|
||||
$old_storage_facade_roots = [];
|
||||
foreach (config('tenancy.filesystem.disks') as $disk) {
|
||||
$old_storage_facade_roots[$disk] = config("filesystems.disks.{$disk}.root");
|
||||
}
|
||||
|
||||
$tenant1 = Tenant::create();
|
||||
$tenant2 = Tenant::create();
|
||||
|
||||
tenancy()->initialize($tenant1);
|
||||
|
||||
Storage::disk('public')->put('foo', 'bar');
|
||||
expect(Storage::disk('public')->get('foo'))->toBe('bar');
|
||||
|
||||
tenancy()->initialize($tenant2);
|
||||
expect(Storage::disk('public')->exists('foo'))->toBeFalse();
|
||||
Storage::disk('public')->put('foo', 'xyz');
|
||||
Storage::disk('public')->put('abc', 'def');
|
||||
expect(Storage::disk('public')->get('foo'))->toBe('xyz');
|
||||
expect(Storage::disk('public')->get('abc'))->toBe('def');
|
||||
|
||||
tenancy()->initialize($tenant1);
|
||||
expect(Storage::disk('public')->get('foo'))->toBe('bar');
|
||||
expect(Storage::disk('public')->exists('abc'))->toBeFalse();
|
||||
|
||||
$tenant3 = Tenant::create();
|
||||
tenancy()->initialize($tenant3);
|
||||
expect(Storage::disk('public')->exists('foo'))->toBeFalse();
|
||||
expect(Storage::disk('public')->exists('abc'))->toBeFalse();
|
||||
|
||||
$expected_storage_path = $old_storage_path . '/tenant' . tenant('id'); // /tenant = suffix base
|
||||
|
||||
// Check that disk prefixes respect the root_override logic
|
||||
expect(getDiskPrefix('local'))->toBe($expected_storage_path . '/app/');
|
||||
expect(getDiskPrefix('public'))->toBe($expected_storage_path . '/app/public/');
|
||||
$this->assertSame('tenant' . tenant('id') . '/', getDiskPrefix('s3'), '/');
|
||||
|
||||
// Check suffixing logic
|
||||
$new_storage_path = storage_path();
|
||||
expect($new_storage_path)->toEqual($expected_storage_path);
|
||||
});
|
||||
|
||||
function getDiskPrefix(string $disk): string
|
||||
{
|
||||
public $mockConsoleOutput = false;
|
||||
/** @var FilesystemAdapter $disk */
|
||||
$disk = Storage::disk($disk);
|
||||
$adapter = $disk->getAdapter();
|
||||
|
||||
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);
|
||||
if (! Str::startsWith(app()->version(), '9.')) {
|
||||
return $adapter->getPathPrefix();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function database_data_is_separated()
|
||||
{
|
||||
config(['tenancy.bootstrappers' => [
|
||||
DatabaseTenancyBootstrapper::class,
|
||||
]]);
|
||||
$prefixer = (new ReflectionObject($adapter))->getProperty('prefixer');
|
||||
$prefixer->setAccessible(true);
|
||||
|
||||
$tenant1 = Tenant::create();
|
||||
$tenant2 = Tenant::create();
|
||||
// reflection -> instance
|
||||
$prefixer = $prefixer->getValue($adapter);
|
||||
|
||||
$this->artisan('tenants:migrate');
|
||||
$prefix = (new ReflectionProperty($prefixer, 'prefix'));
|
||||
$prefix->setAccessible(true);
|
||||
|
||||
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,
|
||||
]]);
|
||||
|
||||
$old_storage_path = storage_path();
|
||||
$old_storage_facade_roots = [];
|
||||
foreach (config('tenancy.filesystem.disks') as $disk) {
|
||||
$old_storage_facade_roots[$disk] = config("filesystems.disks.{$disk}.root");
|
||||
}
|
||||
|
||||
$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'));
|
||||
|
||||
$expected_storage_path = $old_storage_path . '/tenant' . tenant('id'); // /tenant = suffix base
|
||||
|
||||
// Check that disk prefixes respect the root_override logic
|
||||
$this->assertSame($expected_storage_path . '/app/', $this->getDiskPrefix('local'));
|
||||
$this->assertSame($expected_storage_path . '/app/public/', $this->getDiskPrefix('public'));
|
||||
$this->assertSame('tenant' . tenant('id') . '/', $this->getDiskPrefix('s3'), '/');
|
||||
|
||||
// Check suffixing logic
|
||||
$new_storage_path = storage_path();
|
||||
$this->assertEquals($expected_storage_path, $new_storage_path);
|
||||
}
|
||||
|
||||
protected function getDiskPrefix(string $disk): string
|
||||
{
|
||||
/** @var FilesystemAdapter $disk */
|
||||
$disk = Storage::disk($disk);
|
||||
$adapter = $disk->getAdapter();
|
||||
|
||||
if (! Str::startsWith(app()->version(), '9.')) {
|
||||
return $adapter->getPathPrefix();
|
||||
}
|
||||
|
||||
$prefixer = (new ReflectionObject($adapter))->getProperty('prefixer');
|
||||
$prefixer->setAccessible(true);
|
||||
|
||||
// reflection -> instance
|
||||
$prefixer = $prefixer->getValue($adapter);
|
||||
|
||||
$prefix = (new ReflectionProperty($prefixer, 'prefix'));
|
||||
$prefix->setAccessible(true);
|
||||
|
||||
return $prefix->getValue($prefixer);
|
||||
}
|
||||
|
||||
// for queues see QueueTest
|
||||
return $prefix->getValue($prefixer);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue