-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
265 additions
and
11 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,14 @@ | ||
{ | ||
"$schema": "vendor/infection/infection/resources/schema.json", | ||
"source": { | ||
"directories": [ | ||
"src" | ||
] | ||
}, | ||
"logs": { | ||
"text": "build/infection.log" | ||
}, | ||
"mutators": { | ||
"@default": true | ||
} | ||
} |
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
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,111 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Sprout\Tests\Core; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use Orchestra\Testbench\Concerns\WithWorkbench; | ||
use Orchestra\Testbench\TestCase; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Sprout\Managers\IdentityResolverManager; | ||
use Sprout\Managers\ProviderManager; | ||
use Sprout\Managers\TenancyManager; | ||
use Sprout\Sprout; | ||
use Sprout\SproutServiceProvider; | ||
|
||
class ServiceProviderTest extends TestCase | ||
{ | ||
use WithWorkbench; | ||
|
||
protected $enablesPackageDiscoveries = true; | ||
|
||
#[Test] | ||
public function serviceProviderIsRegistered(): void | ||
{ | ||
$this->assertTrue(app()->providerIsLoaded(SproutServiceProvider::class)); | ||
} | ||
|
||
#[Test] | ||
public function sproutIsRegistered(): void | ||
{ | ||
$this->assertTrue(app()->has(Sprout::class)); | ||
$this->assertTrue(app()->has('sprout')); | ||
$this->assertTrue(app()->isShared(Sprout::class)); | ||
$this->assertFalse(app()->isShared('sprout')); | ||
|
||
$this->assertSame(app()->make(Sprout::class), app()->make(Sprout::class)); | ||
$this->assertSame(app()->make('sprout'), app()->make('sprout')); | ||
$this->assertSame(app()->make(Sprout::class), app()->make('sprout')); | ||
$this->assertSame(app()->make('sprout'), app()->make(Sprout::class)); | ||
} | ||
|
||
#[Test] | ||
public function coreSproutConfigExists(): void | ||
{ | ||
$this->assertTrue(app()['config']->has('sprout')); | ||
$this->assertIsArray(app()['config']->get('sprout')); | ||
$this->assertTrue(app()['config']->has('sprout.listen_for_routing')); | ||
$this->assertTrue(app()['config']->has('sprout.context')); | ||
$this->assertTrue(app()['config']->has('sprout.context.key')); | ||
$this->assertTrue(app()['config']->has('sprout.context.use')); | ||
} | ||
|
||
#[Test] | ||
public function providerManagerIsRegistered(): void | ||
{ | ||
$this->assertTrue(app()->has(ProviderManager::class)); | ||
$this->assertTrue(app()->has('sprout.providers')); | ||
$this->assertTrue(app()->isShared(ProviderManager::class)); | ||
$this->assertFalse(app()->isShared('sprout.providers')); | ||
|
||
$this->assertSame(app()->make(ProviderManager::class), app()->make(ProviderManager::class)); | ||
$this->assertSame(app()->make('sprout.providers'), app()->make('sprout.providers')); | ||
$this->assertSame(app()->make(ProviderManager::class), app()->make('sprout.providers')); | ||
$this->assertSame(app()->make('sprout.providers'), app()->make(ProviderManager::class)); | ||
$this->assertSame(app()->make(Sprout::class)->providers(), app()->make('sprout.providers')); | ||
$this->assertSame(app()->make(Sprout::class)->providers(), app()->make(ProviderManager::class)); | ||
} | ||
|
||
#[Test] | ||
public function identityResolverManagerIsRegistered(): void | ||
{ | ||
$this->assertTrue(app()->has(IdentityResolverManager::class)); | ||
$this->assertTrue(app()->has('sprout.resolvers')); | ||
$this->assertTrue(app()->isShared(IdentityResolverManager::class)); | ||
$this->assertFalse(app()->isShared('sprout.resolvers')); | ||
|
||
$this->assertSame(app()->make(IdentityResolverManager::class), app()->make(IdentityResolverManager::class)); | ||
$this->assertSame(app()->make('sprout.resolvers'), app()->make('sprout.resolvers')); | ||
$this->assertSame(app()->make(IdentityResolverManager::class), app()->make('sprout.resolvers')); | ||
$this->assertSame(app()->make('sprout.resolvers'), app()->make(IdentityResolverManager::class)); | ||
$this->assertSame(app()->make(Sprout::class)->resolvers(), app()->make('sprout.resolvers')); | ||
$this->assertSame(app()->make(Sprout::class)->resolvers(), app()->make(IdentityResolverManager::class)); | ||
} | ||
|
||
#[Test] | ||
public function tenancyManagerIsRegistered(): void | ||
{ | ||
$this->assertTrue(app()->has(TenancyManager::class)); | ||
$this->assertTrue(app()->has('sprout.tenancies')); | ||
$this->assertTrue(app()->isShared(TenancyManager::class)); | ||
$this->assertFalse(app()->isShared('sprout.tenancies')); | ||
|
||
$this->assertSame(app()->make(TenancyManager::class), app()->make(TenancyManager::class)); | ||
$this->assertSame(app()->make('sprout.tenancies'), app()->make('sprout.tenancies')); | ||
$this->assertSame(app()->make(TenancyManager::class), app()->make('sprout.tenancies')); | ||
$this->assertSame(app()->make('sprout.tenancies'), app()->make(TenancyManager::class)); | ||
$this->assertSame(app()->make(Sprout::class)->tenancies(), app()->make('sprout.tenancies')); | ||
$this->assertSame(app()->make(Sprout::class)->tenancies(), app()->make(TenancyManager::class)); | ||
} | ||
|
||
#[Test] | ||
public function publishesConfig(): void | ||
{ | ||
$paths = ServiceProvider::pathsToPublish(SproutServiceProvider::class, 'config'); | ||
|
||
$key = realpath(__DIR__ . '/../../src'); | ||
|
||
$this->assertArrayHasKey($key . '/../resources/config/multitenancy.php', $paths); | ||
$this->assertContains(config_path('multitenancy.php'), $paths); | ||
} | ||
} |
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,113 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Sprout\Tests\Core; | ||
|
||
use Illuminate\Config\Repository; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Orchestra\Testbench\Concerns\WithWorkbench; | ||
use Orchestra\Testbench\TestCase; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Sprout\Sprout; | ||
use Workbench\App\Models\TenantModel; | ||
|
||
class SproutTest extends TestCase | ||
{ | ||
use WithWorkbench, RefreshDatabase; | ||
|
||
protected $enablesPackageDiscoveries = true; | ||
|
||
protected function defineEnvironment($app): void | ||
{ | ||
tap($app['config'], static function (Repository $config) { | ||
$config->set('multitenancy.providers.tenants.model', TenantModel::class); | ||
}); | ||
} | ||
|
||
#[Test] | ||
public function makesCoreConfigAccessible(): void | ||
{ | ||
$sprout = app()->make(Sprout::class); | ||
|
||
$this->assertTrue($sprout->config('listen_for_routing')); | ||
$this->assertTrue(config('sprout.listen_for_routing')); | ||
$this->assertNotNull($sprout->config('context')); | ||
$this->assertNotNull(config('sprout.context')); | ||
|
||
app()['config']->set('sprout.listen_for_routing', false); | ||
|
||
$this->assertFalse($sprout->config('listen_for_routing')); | ||
$this->assertFalse(config('sprout.listen_for_routing')); | ||
} | ||
|
||
#[Test] | ||
public function hasHelperForListeningToRoutingEvents(): void | ||
{ | ||
$sprout = app()->make(Sprout::class); | ||
|
||
app()['config']->set('sprout.listen_for_routing', false); | ||
|
||
$this->assertFalse($sprout->config('listen_for_routing')); | ||
$this->assertFalse(config('sprout.listen_for_routing')); | ||
$this->assertFalse($sprout->shouldListenForRouting()); | ||
|
||
app()['config']->set('sprout.listen_for_routing', true); | ||
|
||
$this->assertTrue($sprout->config('listen_for_routing')); | ||
$this->assertTrue(config('sprout.listen_for_routing')); | ||
$this->assertTrue($sprout->shouldListenForRouting()); | ||
} | ||
|
||
#[Test] | ||
public function canProvideContextKeyForTenancy(): void | ||
{ | ||
$sprout = app()->make(Sprout::class); | ||
$tenancy = $sprout->tenancies()->get('tenants'); | ||
|
||
app()['config']->set('sprout.context.key', '{tenancy}_key'); | ||
|
||
$this->assertSame('tenants_key', $sprout->contextKey($tenancy)); | ||
|
||
app()['config']->set('sprout.context.key', 'the_key_for_the_{tenancy}'); | ||
|
||
$this->assertSame('the_key_for_the_tenants', $sprout->contextKey($tenancy)); | ||
} | ||
|
||
#[Test] | ||
public function canProvideContextValueForTenant(): void | ||
{ | ||
$sprout = app()->make(Sprout::class); | ||
$tenant = TenantModel::first(); | ||
|
||
app()['config']->set('sprout.context.use', 'key'); | ||
|
||
$this->assertSame($tenant->getTenantKey(), $sprout->contextValue($tenant)); | ||
|
||
app()['config']->set('sprout.context.use', 'identifier'); | ||
|
||
$this->assertSame($tenant->getTenantIdentifier(), $sprout->contextValue($tenant)); | ||
} | ||
|
||
#[Test] | ||
public function keepsTrackOfCurrentTenancies(): void | ||
{ | ||
$sprout = app()->make(Sprout::class); | ||
|
||
$this->assertFalse($sprout->hasCurrentTenancy()); | ||
$this->assertNull($sprout->getCurrentTenancy()); | ||
$this->assertEmpty($sprout->getAllCurrentTenancies()); | ||
|
||
$tenancy = $sprout->tenancies()->get('tenants'); | ||
$sprout->setCurrentTenancy($tenancy); | ||
|
||
$this->assertTrue($sprout->hasCurrentTenancy()); | ||
$this->assertNotNull($sprout->getCurrentTenancy()); | ||
$this->assertSame($tenancy, $sprout->getCurrentTenancy()); | ||
$this->assertNotEmpty($sprout->getAllCurrentTenancies()); | ||
$this->assertCount(1, $sprout->getAllCurrentTenancies()); | ||
|
||
$sprout->setCurrentTenancy($tenancy); | ||
|
||
$this->assertCount(1, $sprout->getAllCurrentTenancies()); | ||
} | ||
} |