1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 20:34:03 +00:00

Merge branch 'master' into 3.x

This commit is contained in:
Abrar Ahmad 2022-08-22 12:55:16 +05:00 committed by GitHub
commit 158088faae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
83 changed files with 3744 additions and 3606 deletions

View file

@ -2,8 +2,6 @@
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
@ -15,130 +13,106 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
use Stancl\Tenancy\Tests\Etc\Tenant;
class TenantAssetTest extends TestCase
beforeEach(function () {
config(['tenancy.bootstrappers' => [
FilesystemTenancyBootstrapper::class,
]]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
});
afterEach(function () {
// Cleanup
TenantAssetsController::$tenancyMiddleware = InitializeTenancyByDomain::class;
});
test('asset can be accessed using the url returned by the tenant asset helper', function () {
TenantAssetsController::$tenancyMiddleware = InitializeTenancyByRequestData::class;
$tenant = Tenant::create();
tenancy()->initialize($tenant);
$filename = 'testfile' . pest()->randomString(10);
Storage::disk('public')->put($filename, 'bar');
$path = storage_path("app/public/$filename");
// response()->file() returns BinaryFileResponse whose content is
// inaccessible via getContent, so ->assertSee() can't be used
expect($path)->toBeFile();
$response = pest()->get(tenant_asset($filename), [
'X-Tenant' => $tenant->id,
]);
$response->assertSuccessful();
$f = fopen($path, 'r');
$content = fread($f, filesize($path));
fclose($f);
expect($content)->toBe('bar');
});
test('asset helper returns a link to tenant asset controller when asset url is null', function () {
config(['app.asset_url' => null]);
$tenant = Tenant::create();
tenancy()->initialize($tenant);
expect(asset('foo'))->toBe(route('stancl.tenancy.asset', ['path' => 'foo']));
});
test('asset helper returns a link to an external url when asset url is not null', function () {
config(['app.asset_url' => 'https://an-s3-bucket']);
$tenant = Tenant::create();
tenancy()->initialize($tenant);
expect(asset('foo'))->toBe("https://an-s3-bucket/tenant{$tenant->id}/foo");
});
test('global asset helper returns the same url regardless of tenancy initialization', function () {
$original = global_asset('foobar');
expect(global_asset('foobar'))->toBe(asset('foobar'));
$tenant = Tenant::create();
tenancy()->initialize($tenant);
expect(global_asset('foobar'))->toBe($original);
});
test('asset helper tenancy can be disabled', function () {
$original = asset('foo');
config([
'app.asset_url' => null,
'tenancy.filesystem.asset_helper_tenancy' => false,
]);
$tenant = Tenant::create();
tenancy()->initialize($tenant);
expect(asset('foo'))->toBe($original);
});
test('test asset controller returns a 404 when no path is provided', function () {
TenantAssetsController::$tenancyMiddleware = InitializeTenancyByRequestData::class;
$tenant = Tenant::create();
tenancy()->initialize($tenant);
pest()->get(tenant_asset(null), [
'X-Tenant' => $tenant->id,
])->assertNotFound();
});
function getEnvironmentSetUp($app)
{
public function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);
$app->booted(function () {
if (file_exists(base_path('routes/tenant.php'))) {
Route::middleware(['web'])
->namespace($this->app['config']['tenancy.tenant_route_namespace'] ?? 'App\Http\Controllers')
->group(base_path('routes/tenant.php'));
}
});
}
public function setUp(): void
{
parent::setUp();
config(['tenancy.bootstrappers' => [
FilesystemTenancyBootstrapper::class,
]]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
}
public function tearDown(): void
{
parent::tearDown();
// Cleanup
TenantAssetsController::$tenancyMiddleware = InitializeTenancyByDomain::class;
}
/** @test */
public function asset_can_be_accessed_using_the_url_returned_by_the_tenant_asset_helper()
{
TenantAssetsController::$tenancyMiddleware = InitializeTenancyByRequestData::class;
$tenant = Tenant::create();
tenancy()->initialize($tenant);
$filename = 'testfile' . $this->randomString(10);
Storage::disk('public')->put($filename, 'bar');
$path = storage_path("app/public/$filename");
// response()->file() returns BinaryFileResponse whose content is
// inaccessible via getContent, so ->assertSee() can't be used
$this->assertFileExists($path);
$response = $this->get(tenant_asset($filename), [
'X-Tenant' => $tenant->id,
]);
$response->assertSuccessful();
$f = fopen($path, 'r');
$content = fread($f, filesize($path));
fclose($f);
$this->assertSame('bar', $content);
}
/** @test */
public function asset_helper_returns_a_link_to_TenantAssetController_when_asset_url_is_null()
{
config(['app.asset_url' => null]);
$tenant = Tenant::create();
tenancy()->initialize($tenant);
$this->assertSame(route('stancl.tenancy.asset', ['path' => 'foo']), asset('foo'));
}
/** @test */
public function asset_helper_returns_a_link_to_an_external_url_when_asset_url_is_not_null()
{
config(['app.asset_url' => 'https://an-s3-bucket']);
$tenant = Tenant::create();
tenancy()->initialize($tenant);
$this->assertSame("https://an-s3-bucket/tenant{$tenant->id}/foo", asset('foo'));
}
/** @test */
public function global_asset_helper_returns_the_same_url_regardless_of_tenancy_initialization()
{
$original = global_asset('foobar');
$this->assertSame(asset('foobar'), global_asset('foobar'));
$tenant = Tenant::create();
tenancy()->initialize($tenant);
$this->assertSame($original, global_asset('foobar'));
}
/** @test */
public function asset_helper_tenancy_can_be_disabled()
{
$original = asset('foo');
config([
'app.asset_url' => null,
'tenancy.filesystem.asset_helper_tenancy' => false,
]);
$tenant = Tenant::create();
tenancy()->initialize($tenant);
$this->assertSame($original, asset('foo'));
}
public function test_asset_controller_returns_a_404_when_no_path_is_provided()
{
TenantAssetsController::$tenancyMiddleware = InitializeTenancyByRequestData::class;
$tenant = Tenant::create();
tenancy()->initialize($tenant);
$response = $this->get(tenant_asset(null), [
'X-Tenant' => $tenant->id,
]);
$response->assertNotFound();
}
$app->booted(function () {
if (file_exists(base_path('routes/tenant.php'))) {
Route::middleware(['web'])
->namespace(pest()->app['config']['tenancy.tenant_route_namespace'] ?? 'App\Http\Controllers')
->group(base_path('routes/tenant.php'));
}
});
}