From fd00be646e82f07cae0a36c7e76c17310c50779a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 29 Nov 2019 00:12:07 +0100 Subject: [PATCH] [2.3.0] tenant_route() helper (#229) * Add tenant_route helper * Add tests * Remove redundant setUp() * Fix test namespaces * Apply fixes from StyleCI --- src/helpers.php | 24 +++++++++++++++---- tests/Features/TenantConfigTest.php | 2 +- tests/Features/TimestampTest.php | 2 +- ...RedirectMacroTest.php => RedirectTest.php} | 18 +++++++++++--- 4 files changed, 36 insertions(+), 10 deletions(-) rename tests/{Features/TenantRedirectMacroTest.php => RedirectTest.php} (54%) diff --git a/src/helpers.php b/src/helpers.php index aacf5fbe..101d43df 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -5,7 +5,7 @@ declare(strict_types=1); use Stancl\Tenancy\Tenant; use Stancl\Tenancy\TenantManager; -if (! \function_exists('tenancy')) { +if (! function_exists('tenancy')) { /** @return TenantManager|mixed */ function tenancy($key = null) { @@ -17,7 +17,7 @@ if (! \function_exists('tenancy')) { } } -if (! \function_exists('tenant')) { +if (! function_exists('tenant')) { /** @return Tenant|mixed */ function tenant($key = null) { @@ -29,7 +29,7 @@ if (! \function_exists('tenant')) { } } -if (! \function_exists('tenant_asset')) { +if (! function_exists('tenant_asset')) { /** @return string */ function tenant_asset($asset) { @@ -37,16 +37,30 @@ if (! \function_exists('tenant_asset')) { } } -if (! \function_exists('global_asset')) { +if (! function_exists('global_asset')) { function global_asset($asset) { return app('globalUrl')->asset($asset); } } -if (! \function_exists('global_cache')) { +if (! function_exists('global_cache')) { function global_cache() { return app('globalCache'); } } + +if (! function_exists('tenant_route')) { + function tenant_route(string $route, array $parameters = [], string $domain = null): string + { + $domain = $domain ?? request()->getHost(); + + // replace first occurance of hostname fragment with $domain + $url = route($route, $parameters); + $hostname = parse_url($url, PHP_URL_HOST); + $position = strpos($url, $hostname); + + return substr_replace($url, $domain, $position, strlen($hostname)); + } +} diff --git a/tests/Features/TenantConfigTest.php b/tests/Features/TenantConfigTest.php index 4e2405be..db8fcaf3 100644 --- a/tests/Features/TenantConfigTest.php +++ b/tests/Features/TenantConfigTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Tests\Feature; +namespace Stancl\Tenancy\Tests\Features; use Stancl\Tenancy\Tests\TestCase; diff --git a/tests/Features/TimestampTest.php b/tests/Features/TimestampTest.php index 1b6c1a81..25cb1017 100644 --- a/tests/Features/TimestampTest.php +++ b/tests/Features/TimestampTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Tests\Feature; +namespace Stancl\Tenancy\Tests\Features; use Stancl\Tenancy\Features\Timestamps; use Stancl\Tenancy\Tenant; diff --git a/tests/Features/TenantRedirectMacroTest.php b/tests/RedirectTest.php similarity index 54% rename from tests/Features/TenantRedirectMacroTest.php rename to tests/RedirectTest.php index 8ae8cbe5..c0bd90a8 100644 --- a/tests/Features/TenantRedirectMacroTest.php +++ b/tests/RedirectTest.php @@ -2,13 +2,12 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Tests\Feature; +namespace Stancl\Tenancy\Tests; use Route; use Stancl\Tenancy\Tenant; -use Stancl\Tenancy\Tests\TestCase; -class TenantRedirectMacroTest extends TestCase +class RedirectTest extends TestCase { public $autoCreateTenant = false; public $autoInitTenancy = false; @@ -34,4 +33,17 @@ class TenantRedirectMacroTest extends TestCase $this->get('/redirect') ->assertRedirect('http://abcd/foobar'); } + + /** @test */ + public function tenant_route_helper_generates_correct_url() + { + Route::get('/abcdef/{a?}/{b?}', function () { + return 'Foo'; + })->name('foo'); + + $this->assertSame('http://foo.localhost/abcdef/as/df', tenant_route('foo', ['a' => 'as', 'b' => 'df'], 'foo.localhost')); + $this->assertSame('http://foo.localhost/abcdef', tenant_route('foo', [], 'foo.localhost')); + + $this->assertSame('http://' . request()->getHost() . '/abcdef/x/y', tenant_route('foo', ['a' => 'x', 'b' => 'y'])); + } }