-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added 'currentTenant' function (#19)
- Added Tests for Multitenancy 'currentTenant' function - Updated README
- Loading branch information
1 parent
ee3d2e2
commit 8d5b94d
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace RomegaDigital\Multitenancy\Tests\Feature; | ||
|
||
use Illuminate\Http\Response; | ||
use RomegaDigital\Multitenancy\Exceptions\TenantDoesNotExist; | ||
use RomegaDigital\Multitenancy\Middleware\TenantMiddleware; | ||
use RomegaDigital\Multitenancy\Tests\TestCase; | ||
|
||
class MultitenancyTest extends TestCase | ||
{ | ||
protected $tenantMiddleware; | ||
|
||
/** | ||
* Define environment setup. | ||
* | ||
* @param Illuminate\Foundation\Application $app | ||
* | ||
* @return void | ||
*/ | ||
protected function getEnvironmentSetUp($app) | ||
{ | ||
parent::getEnvironmentSetUp($app); | ||
|
||
$app['router']->get('/login', function () { | ||
return 'login'; | ||
})->name(config('multitenancy.redirect_route')); | ||
} | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->tenantMiddleware = new TenantMiddleware(app('auth'), app('multitenancy')); | ||
} | ||
|
||
protected function buildRequest($domain) | ||
{ | ||
app('request')->headers->set('HOST', $domain.'.example.com'); | ||
|
||
return $this->tenantMiddleware->handle(app('request'), function () { | ||
return (new Response())->setContent('<html></html>'); | ||
}); | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_the_current_tenant_when_set_by_middleware() | ||
{ | ||
$this->actingAs($this->testUser); | ||
|
||
$this->testTenant->users()->sync($this->testUser); | ||
|
||
$this->buildRequest($this->testTenant->domain); | ||
|
||
$this->assertEquals($this->testTenant->domain, app('multitenancy')->currentTenant()->domain); | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_exception_when_tenant_not_set() | ||
{ | ||
$this->actingAs($this->testUser); | ||
|
||
$this->testTenant->users()->sync($this->testUser); | ||
|
||
try { | ||
$this->buildRequest('testdomain'); | ||
app('multitenancy')->currentTenant(); | ||
$this->fail('Expected exception not thrown'); | ||
} catch (TenantDoesNotExist $e) { | ||
$this->assertEquals('There is no tenant at domain `testdomain`.', $e->getMessage()); | ||
} | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_exception_when_tenant_not_set_never_touched_middleware() | ||
{ | ||
$this->actingAs($this->testUser); | ||
|
||
$this->testTenant->users()->sync($this->testUser); | ||
|
||
try { | ||
app('multitenancy')->currentTenant(); | ||
$this->fail('Expected exception not thrown'); | ||
} catch (TenantDoesNotExist $e) { | ||
$this->assertEquals('There is no tenant at domain ``.', $e->getMessage()); | ||
} | ||
} | ||
} |