diff --git a/src/TenantManager.php b/src/TenantManager.php index 739faac1..b952499e 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -5,6 +5,7 @@ namespace Stancl\Tenancy; use Illuminate\Support\Facades\Redis; use Stancl\Tenancy\Interfaces\StorageDriver; use Stancl\Tenancy\Traits\BootstrapsTenancy; +use Illuminate\Contracts\Foundation\Application; class TenantManager { @@ -13,7 +14,7 @@ class TenantManager /** * The application instance. * - * @var \Illuminate\Contracts\Foundation\Application|\Illuminate\Foundation\Application + * @var Application */ private $app; @@ -38,7 +39,7 @@ class TenantManager */ public $tenant; - public function __construct($app, StorageDriver $storage, DatabaseManager $database) + public function __construct(Application $app, StorageDriver $storage, DatabaseManager $database) { $this->app = $app; $this->storage = $storage; @@ -190,7 +191,7 @@ class TenantManager public function actAsId(string $uuid): array { $this->setTenant($this->storage->getTenantById($uuid)); - $this->bootstrap(); // todo this could break storage_path() for example? + $this->bootstrap(); return $this->tenant; } diff --git a/src/Traits/BootstrapsTenancy.php b/src/Traits/BootstrapsTenancy.php index a791854a..3ce41bd3 100644 --- a/src/Traits/BootstrapsTenancy.php +++ b/src/Traits/BootstrapsTenancy.php @@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Redis; trait BootstrapsTenancy { - public $oldStoragePaths; + public $oldStoragePaths = []; public function bootstrap() { @@ -40,7 +40,7 @@ trait BootstrapsTenancy public function suffixFilesystemRootPaths() { - $old = [ + $old = $this->oldStoragePaths ?: [ "storage_disks" => [], "storage_path" => $this->app->storagePath(), ]; @@ -59,7 +59,7 @@ trait BootstrapsTenancy } // storage_path() - $this->app->useStoragePath($this->app->storagePath() . "/{$suffix}"); + $this->app->useStoragePath($old['storage_path'] . "/{$suffix}"); $this->oldStoragePaths = $old; } diff --git a/tests/BootstrapsTenancyTest.php b/tests/BootstrapsTenancyTest.php index 8950a5ab..e407858c 100644 --- a/tests/BootstrapsTenancyTest.php +++ b/tests/BootstrapsTenancyTest.php @@ -34,10 +34,21 @@ class BootstrapsTenancyTest extends TestCase public function filesystem_is_suffixed() { $old_storage_path = storage_path(); - $this->initTenancy(); - $new_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"); + } + $this->initTenancy(); + + $new_storage_path = 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 */ diff --git a/tests/ReidentificationTest.php b/tests/ReidentificationTest.php new file mode 100644 index 00000000..616d04f3 --- /dev/null +++ b/tests/ReidentificationTest.php @@ -0,0 +1,45 @@ +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()); + } +}