From cc2d555e3edf87e8b42e94eef56594aa269575b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 19 Mar 2024 20:36:01 +0100 Subject: [PATCH] Write test for specifying headers in TenantAssetController, fix error introduced in previous commit --- src/Controllers/TenantAssetController.php | 3 +-- tests/TenantAssetTest.php | 26 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Controllers/TenantAssetController.php b/src/Controllers/TenantAssetController.php index 66ef55b0..ece0d9ef 100644 --- a/src/Controllers/TenantAssetController.php +++ b/src/Controllers/TenantAssetController.php @@ -16,7 +16,6 @@ class TenantAssetController implements HasMiddleware // todo@docs this was renam { /** * Used for adding custom headers to the response. - * todo@tests add a test for this * * @var (Closure(Request): array)|null */ @@ -33,7 +32,7 @@ class TenantAssetController implements HasMiddleware // todo@docs this was renam { return [ new Middleware(array_merge( - tenancy()->defaultMiddleware(), + [tenancy()->defaultMiddleware()], static::$middleware, )), ]; diff --git a/tests/TenantAssetTest.php b/tests/TenantAssetTest.php index 70ffebb4..0299af78 100644 --- a/tests/TenantAssetTest.php +++ b/tests/TenantAssetTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Contracts\Http\Kernel; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Storage; @@ -14,6 +15,7 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByPath; use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper; +use Stancl\Tenancy\Controllers\TenantAssetController; use Stancl\Tenancy\Overrides\TenancyUrlGenerator; beforeEach(function () { @@ -114,6 +116,30 @@ test('asset helper works correctly with path identification', function (bool $ke 'route-level identification' => false, ]); +test('TenantAssetController headers are configurable', function () { + TenantAssetController::$headers = function (Request $request) { + return ['X-Foo' => 'Bar']; + }; + + $tenant = Tenant::create(); + tenancy()->initialize($tenant); + $tenant->createDomain('foo.localhost'); + + $filename = 'testfile' . pest()->randomString(10); + Storage::disk('public')->put($filename, 'bar'); + + $this->withoutExceptionHandling(); + + $response = pest()->get("http://foo.localhost/tenancy/assets/$filename", [ + 'X-Tenant' => $tenant->id, + ]); + + $response->assertSuccessful(); + $response->assertHeader('X-Foo', 'Bar'); + + TenantAssetController::$headers = null; // reset static property +}); + test('global asset helper returns the same url regardless of tenancy initialization', function () { $original = global_asset('foobar'); expect(global_asset('foobar'))->toBe(asset('foobar'));