From 25f19c2c399c22a5644200ffd546651b62fabb45 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Tue, 9 Aug 2022 10:18:07 +0500 Subject: [PATCH] wip --- src/helpers.php | 15 ++++++++++++++- tests/PathIdentificationTest.php | 13 ++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index 9c3b75cd..bc4ac1d1 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Stancl\Tenancy\Contracts\Tenant; +use Stancl\Tenancy\Resolvers\PathTenantResolver; use Stancl\Tenancy\Tenancy; if (! function_exists('tenancy')) { @@ -59,7 +60,7 @@ if (! function_exists('global_cache')) { if (! function_exists('tenant_route')) { function tenant_route(string $domain, $route, $parameters = [], $absolute = true) { - // replace first occurance of hostname fragment with $domain + // replace first occurrence of hostname fragment with $domain $url = route($route, $parameters, $absolute); $hostname = parse_url($url, PHP_URL_HOST); $position = strpos($url, $hostname); @@ -67,3 +68,15 @@ if (! function_exists('tenant_route')) { return substr_replace($url, $domain, $position, strlen($hostname)); } } + +if (! function_exists('tenant_path_route')) { + function tenant_path_route($route, $parameters = []) + { + if (! array_key_exists(PathTenantResolver::$tenantParameterName, $parameters)) { + $parameters[PathTenantResolver::$tenantParameterName] = optional(tenant())->getTenantKey(); + } + dd($parameters); + + return route($route, $parameters); + } +} diff --git a/tests/PathIdentificationTest.php b/tests/PathIdentificationTest.php index bda0cfcb..0d0c8096 100644 --- a/tests/PathIdentificationTest.php +++ b/tests/PathIdentificationTest.php @@ -18,7 +18,7 @@ beforeEach(function () { ], function () { Route::get('/foo/{a}/{b}', function ($a, $b) { return "$a + $b"; - }); + })->name('foo'); }); }); @@ -123,3 +123,14 @@ test('tenant parameter name can be customized', function () { ->withoutExceptionHandling() ->get('/acme/foo/abc/xyz'); }); + +test('tenant path route helper function test', function () { + Tenant::create([ + 'id' => 'acme', + ]); + + pest()->get( tenant_path_route('foo', ['a' => 'a', 'b' => 'b'])); + + expect(tenancy()->initialized)->toBeTrue(); + expect(tenant('id'))->toBe('acme'); +});