1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 09:34:04 +00:00

[2.3.0] tenant_route() helper (#229)

* Add tenant_route helper

* Add tests

* Remove redundant setUp()

* Fix test namespaces

* Apply fixes from StyleCI
This commit is contained in:
Samuel Štancl 2019-11-29 00:12:07 +01:00 committed by GitHub
parent 4e477b472f
commit fd00be646e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 10 deletions

View file

@ -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));
}
}

View file

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Stancl\Tenancy\Tests\Feature;
namespace Stancl\Tenancy\Tests\Features;
use Stancl\Tenancy\Tests\TestCase;

View file

@ -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;

View file

@ -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']));
}
}