1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 15:14:03 +00:00

Write test for specifying headers in TenantAssetController, fix error introduced in previous commit

This commit is contained in:
Samuel Štancl 2024-03-19 20:36:01 +01:00
parent a39da042af
commit cc2d555e3e
2 changed files with 27 additions and 2 deletions

View file

@ -16,7 +16,6 @@ class TenantAssetController implements HasMiddleware // todo@docs this was renam
{ {
/** /**
* Used for adding custom headers to the response. * Used for adding custom headers to the response.
* todo@tests add a test for this
* *
* @var (Closure(Request): array)|null * @var (Closure(Request): array)|null
*/ */
@ -33,7 +32,7 @@ class TenantAssetController implements HasMiddleware // todo@docs this was renam
{ {
return [ return [
new Middleware(array_merge( new Middleware(array_merge(
tenancy()->defaultMiddleware(), [tenancy()->defaultMiddleware()],
static::$middleware, static::$middleware,
)), )),
]; ];

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Contracts\Http\Kernel; use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
@ -14,6 +15,7 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData; use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper; use Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper;
use Stancl\Tenancy\Controllers\TenantAssetController;
use Stancl\Tenancy\Overrides\TenancyUrlGenerator; use Stancl\Tenancy\Overrides\TenancyUrlGenerator;
beforeEach(function () { beforeEach(function () {
@ -114,6 +116,30 @@ test('asset helper works correctly with path identification', function (bool $ke
'route-level identification' => false, '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 () { test('global asset helper returns the same url regardless of tenancy initialization', function () {
$original = global_asset('foobar'); $original = global_asset('foobar');
expect(global_asset('foobar'))->toBe(asset('foobar')); expect(global_asset('foobar'))->toBe(asset('foobar'));