mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:44:04 +00:00
Add more tests, fix bugs
This commit is contained in:
parent
d14aa0b642
commit
3f650d887d
4 changed files with 65 additions and 8 deletions
|
|
@ -5,6 +5,7 @@ namespace Stancl\Tenancy;
|
||||||
use Illuminate\Support\Facades\Redis;
|
use Illuminate\Support\Facades\Redis;
|
||||||
use Stancl\Tenancy\Interfaces\StorageDriver;
|
use Stancl\Tenancy\Interfaces\StorageDriver;
|
||||||
use Stancl\Tenancy\Traits\BootstrapsTenancy;
|
use Stancl\Tenancy\Traits\BootstrapsTenancy;
|
||||||
|
use Illuminate\Contracts\Foundation\Application;
|
||||||
|
|
||||||
class TenantManager
|
class TenantManager
|
||||||
{
|
{
|
||||||
|
|
@ -13,7 +14,7 @@ class TenantManager
|
||||||
/**
|
/**
|
||||||
* The application instance.
|
* The application instance.
|
||||||
*
|
*
|
||||||
* @var \Illuminate\Contracts\Foundation\Application|\Illuminate\Foundation\Application
|
* @var Application
|
||||||
*/
|
*/
|
||||||
private $app;
|
private $app;
|
||||||
|
|
||||||
|
|
@ -38,7 +39,7 @@ class TenantManager
|
||||||
*/
|
*/
|
||||||
public $tenant;
|
public $tenant;
|
||||||
|
|
||||||
public function __construct($app, StorageDriver $storage, DatabaseManager $database)
|
public function __construct(Application $app, StorageDriver $storage, DatabaseManager $database)
|
||||||
{
|
{
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
$this->storage = $storage;
|
$this->storage = $storage;
|
||||||
|
|
@ -190,7 +191,7 @@ class TenantManager
|
||||||
public function actAsId(string $uuid): array
|
public function actAsId(string $uuid): array
|
||||||
{
|
{
|
||||||
$this->setTenant($this->storage->getTenantById($uuid));
|
$this->setTenant($this->storage->getTenantById($uuid));
|
||||||
$this->bootstrap(); // todo this could break storage_path() for example?
|
$this->bootstrap();
|
||||||
return $this->tenant;
|
return $this->tenant;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Redis;
|
||||||
|
|
||||||
trait BootstrapsTenancy
|
trait BootstrapsTenancy
|
||||||
{
|
{
|
||||||
public $oldStoragePaths;
|
public $oldStoragePaths = [];
|
||||||
|
|
||||||
public function bootstrap()
|
public function bootstrap()
|
||||||
{
|
{
|
||||||
|
|
@ -40,7 +40,7 @@ trait BootstrapsTenancy
|
||||||
|
|
||||||
public function suffixFilesystemRootPaths()
|
public function suffixFilesystemRootPaths()
|
||||||
{
|
{
|
||||||
$old = [
|
$old = $this->oldStoragePaths ?: [
|
||||||
"storage_disks" => [],
|
"storage_disks" => [],
|
||||||
"storage_path" => $this->app->storagePath(),
|
"storage_path" => $this->app->storagePath(),
|
||||||
];
|
];
|
||||||
|
|
@ -59,7 +59,7 @@ trait BootstrapsTenancy
|
||||||
}
|
}
|
||||||
|
|
||||||
// storage_path()
|
// storage_path()
|
||||||
$this->app->useStoragePath($this->app->storagePath() . "/{$suffix}");
|
$this->app->useStoragePath($old['storage_path'] . "/{$suffix}");
|
||||||
|
|
||||||
$this->oldStoragePaths = $old;
|
$this->oldStoragePaths = $old;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,10 +34,21 @@ class BootstrapsTenancyTest extends TestCase
|
||||||
public function filesystem_is_suffixed()
|
public function filesystem_is_suffixed()
|
||||||
{
|
{
|
||||||
$old_storage_path = storage_path();
|
$old_storage_path = storage_path();
|
||||||
$this->initTenancy();
|
$old_storage_facade_roots = [];
|
||||||
$new_storage_path = storage_path();
|
foreach (config('tenancy.filesystem.disks') as $disk) {
|
||||||
|
$old_storage_facade_roots[$disk] = config("filesystems.disks.{$disk}.root");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->initTenancy();
|
||||||
|
|
||||||
|
$new_storage_path = storage_path();
|
||||||
$this->assertEquals($old_storage_path . "/" . config('tenancy.filesystem.suffix_base') . tenant('uuid'), $new_storage_path);
|
$this->assertEquals($old_storage_path . "/" . config('tenancy.filesystem.suffix_base') . tenant('uuid'), $new_storage_path);
|
||||||
|
|
||||||
|
foreach (config('tenancy.filesystem.disks') as $disk) {
|
||||||
|
$suffix = config('tenancy.filesystem.suffix_base') . tenant('uuid');
|
||||||
|
$current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix();
|
||||||
|
$this->assertSame($old_storage_facade_roots[$disk] . "/$suffix/", $current_path_prefix);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
|
||||||
45
tests/ReidentificationTest.php
Normal file
45
tests/ReidentificationTest.php
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
|
class ReidentificationTest extends TestCase
|
||||||
|
{
|
||||||
|
public $autoInitTenancy = false;
|
||||||
|
/**
|
||||||
|
* These tests are run when a tenant is identified after another tenant has already been identified.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function storage_facade_roots_are_correct()
|
||||||
|
{
|
||||||
|
$originals = [];
|
||||||
|
|
||||||
|
foreach (config('tenancy.filesystem.disks') as $disk) {
|
||||||
|
$originals[$disk] = config("filesystems.disks.{$disk}.root");
|
||||||
|
}
|
||||||
|
|
||||||
|
tenancy()->init('localhost');
|
||||||
|
tenant()->create('second.localhost');
|
||||||
|
tenancy()->init('second.localhost');
|
||||||
|
|
||||||
|
foreach (config('tenancy.filesystem.disks') as $disk) {
|
||||||
|
$suffix = config('tenancy.filesystem.suffix_base') . tenant('uuid');
|
||||||
|
$current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix();
|
||||||
|
$this->assertSame($originals[$disk] . "/$suffix/", $current_path_prefix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function storage_path_is_correct()
|
||||||
|
{
|
||||||
|
$original = storage_path();
|
||||||
|
|
||||||
|
tenancy()->init('localhost');
|
||||||
|
tenant()->create('second.localhost');
|
||||||
|
tenancy()->init('second.localhost');
|
||||||
|
|
||||||
|
|
||||||
|
$suffix = config('tenancy.filesystem.suffix_base') . tenant('uuid');
|
||||||
|
$this->assertSame($original . "/$suffix", storage_path());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue