1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 16:14:02 +00:00

Change default tenant model, write more tests, cleanup

This commit is contained in:
Samuel Štancl 2020-05-13 06:23:41 +02:00
parent c32f229dd5
commit de53b81c0e
33 changed files with 210 additions and 90 deletions

View file

@ -4,7 +4,7 @@ namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Events\TenancyEnded;
@ -74,12 +74,12 @@ class AutomaticModeTest extends TestCase
class MyBootstrapper implements TenancyBootstrapper
{
public function start(\Stancl\Tenancy\Contracts\Tenant $tenant)
public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
{
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
}
public function end()
public function revert()
{
app()->instance('tenancy_ended', true);
}

View file

@ -7,7 +7,7 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\JobPipeline;
use Stancl\Tenancy\Listeners\RevertToCentralContext;

View file

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\TenancyBootstrappers\CacheTenancyBootstrapper;

View file

@ -9,7 +9,7 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\JobPipeline;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
@ -154,6 +154,7 @@ class CommandsTest extends TestCase
$this->artisan('tenancy:install');
$this->assertFileExists(base_path('routes/tenant.php'));
$this->assertFileExists(base_path('config/tenancy.php'));
$this->assertFileExists(app_path('Providers/TenancyServiceProvider.php'));
$this->assertFileExists(database_path('migrations/2019_09_15_000010_create_tenants_table.php'));
$this->assertFileExists(database_path('migrations/2019_09_15_000020_create_domains_table.php'));
$this->assertDirectoryExists(database_path('migrations/tenant'));

View file

@ -7,7 +7,7 @@ use Illuminate\Foundation\Auth\User as Authenticable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Listeners\JobPipeline;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Jobs\CreateDatabase;

View file

@ -11,7 +11,7 @@ use Stancl\Tenancy\Contracts\ManagesDatabaseUsers;
use Stancl\Tenancy\Exceptions\TenantDatabaseUserAlreadyExistsException;
use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager;
use Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\JobPipeline;
use Stancl\Tenancy\Events\TenantCreated;

13
tests/Etc/Tenant.php Normal file
View file

@ -0,0 +1,13 @@
<?php
namespace Stancl\Tenancy\Tests\Etc;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Database\Concerns\HasDatabase;
use Stancl\Tenancy\Database\Concerns\HasDomains;
use Stancl\Tenancy\Database\Models;
class Tenant extends Models\Tenant implements TenantWithDatabase
{
use HasDatabase, HasDomains;
}

View file

@ -5,7 +5,7 @@ namespace Stancl\Tenancy\Tests;
use Illuminate\Events\CallQueuedListener;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Queue;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Listeners\QueueableListener;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Tests\TestCase;

View file

@ -6,7 +6,7 @@ namespace Stancl\Tenancy\Tests\Features;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Features\CrossDomainRedirect;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Tests\TestCase;
class RedirectTest extends TestCase

View file

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests\Features;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Features\TenantConfig;

View file

@ -6,7 +6,7 @@ namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Facades\GlobalCache;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\TenancyBootstrappers\CacheTenancyBootstrapper;

View file

@ -5,7 +5,7 @@ namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Queue;
use Spatie\Valuestore\Valuestore;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Listeners\JobPipeline;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Tests\TestCase;
@ -51,7 +51,7 @@ class JobPipelineTest extends TestCase
FooJob::class,
])->send(function () {
return $this->valuestore;
})->queue(true)->toListener());
})->shouldBeQueued(true)->toListener());
Queue::assertNothingPushed();
@ -70,7 +70,7 @@ class JobPipelineTest extends TestCase
FooJob::class,
])->send(function () {
return $this->valuestore;
})->queue(true)->toListener());
})->shouldBeQueued(true)->toListener());
$this->assertFalse($this->valuestore->has('foo'));
Tenant::create();

View file

@ -3,7 +3,7 @@
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Exceptions\RouteIsMissingTenantParameterException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;

View file

@ -10,7 +10,7 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Event;
use Spatie\Valuestore\Valuestore;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\TenancyBootstrappers\QueueTenancyBootstrapper;

View file

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
use Stancl\Tenancy\Tests\TestCase;

View file

@ -11,8 +11,8 @@ use Stancl\Tenancy\Contracts\Syncable;
use Stancl\Tenancy\Contracts\SyncMaster;
use Stancl\Tenancy\Database\Concerns\CentralConnection;
use Stancl\Tenancy\Database\Concerns\ResourceSyncing;
use Stancl\Tenancy\Database\Models;
use Stancl\Tenancy\Database\Models\TenantPivot;
use Stancl\Tenancy\DatabaseConfig;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\JobPipeline;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
@ -25,7 +25,9 @@ use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Exceptions\ModelNotSyncMaster;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\TenancyBootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Tests\TestCase;
use Illuminate\Support\Str;
class ResourceSyncingTest extends TestCase
{
@ -41,6 +43,10 @@ class ResourceSyncingTest extends TestCase
return $event->tenant;
})->toListener());
DatabaseConfig::generateDatabaseNamesUsing(function () {
return 'db' . Str::random(16);
});
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
@ -568,7 +574,7 @@ class ResourceSyncingTest extends TestCase
}
}
class ResourceTenant extends Models\Tenant
class ResourceTenant extends Tenant
{
public function users()
{

View file

@ -5,9 +5,10 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Stancl\Tenancy\Controllers\TenantAssetsController;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
@ -17,6 +18,19 @@ use Stancl\Tenancy\Tests\TestCase;
class TenantAssetTest extends TestCase
{
public function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);
$app->booted(function () {
if (file_exists(base_path('routes/tenant.php'))) {
Route::middleware(['web'])
->namespace($this->app['config']['tenancy.tenant_route_namespace'] ?? 'App\Http\Controllers')
->group(base_path('routes/tenant.php'));
}
});
}
public function setUp(): void
{
parent::setUp();

View file

@ -6,7 +6,7 @@ namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Tests\TestCase;
class TenantAwareCommandTest extends TestCase

View file

@ -5,12 +5,13 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\DatabaseManager;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\JobPipeline;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\TenancyBootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager;
@ -19,6 +20,7 @@ use Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager;
use Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager;
use Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager;
use Stancl\Tenancy\Tests\TestCase;
use Illuminate\Support\Str;
class TenantDatabaseManagerTest extends TestCase
{
@ -60,8 +62,7 @@ class TenantDatabaseManagerTest extends TestCase
return $event->tenant;
})->toListener());
// todo if the prefix is _tenancy_, this blows up. write a tenantmodel test that the prefix can be _tenancy_
config(['tenancy.internal_prefix' => 'tenancy_',]);
config(['tenancy.internal_prefix' => 'tenancy_']);
$database = 'db' . $this->randomString();
@ -141,4 +142,25 @@ class TenantDatabaseManagerTest extends TestCase
$this->assertSame($tenant->database()->getName(), config('database.connections.' . config('database.default') . '.schema'));
$this->assertSame($originalDatabaseName, config(['database.connections.pgsql.database']));
}
/** @test */
public function a_tenants_database_cannot_be_created_when_the_database_already_exists()
{
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener());
$name = 'foo' . Str::random(8);
$tenant = Tenant::create([
'tenancy_db_name' => $name,
]);
$manager = $tenant->database()->manager();
$this->assertTrue($manager->databaseExists($tenant->database()->getName()));
$this->expectException(TenantDatabaseAlreadyExistsException::class);
$tenant2 = Tenant::create([
'tenancy_db_name' => $name,
]);
}
}

View file

@ -7,12 +7,18 @@ use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Tests\TestCase;
use Stancl\Tenancy\UniqueIDGenerators\UUIDGenerator;
use Stancl\Tenancy\Contracts;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\JobPipeline;
use Stancl\Tenancy\TenancyBootstrappers\DatabaseTenancyBootstrapper;
use Illuminate\Support\Str;
class TenantModelTest extends TestCase
{
@ -132,7 +138,40 @@ class TenantModelTest extends TestCase
$this->assertTrue(tenant() instanceof AnotherTenant);
}
// todo test that tenant can be created even in another DB context - that the central trait works
/** @test */
public function tenant_can_be_created_even_when_we_are_in_another_tenants_context()
{
config(['tenancy.bootstrappers' => [
DatabaseTenancyBootstrapper::class
]]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function ($event) {
return $event->tenant;
})->toListener());
$tenant1 = Tenant::create([
'id' => 'foo',
'tenancy_db_name' => 'db' . Str::random(16),
]);
tenancy()->initialize($tenant1);
$tenant2 = Tenant::create([
'id' => 'bar',
'tenancy_db_name' => 'db' . Str::random(16),
]);
tenancy()->end();
$this->assertSame(2, Tenant::count());
}
/** @test */
public function data_is_never_encoded_or_decoded_twice()
{
// todo. tests for registerPriorityListener
}
}
class MyTenant extends Tenant

View file

@ -7,6 +7,7 @@ namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Redis;
use Illuminate\Testing\Assert as PHPUnit;
use Illuminate\Testing\TestResponse;
use Stancl\Tenancy\Tests\Etc\Tenant;
abstract class TestCase extends \Orchestra\Testbench\TestCase
{
@ -83,6 +84,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
'central' => true,
],
'tenancy.seeder_parameters' => [],
'tenancy.tenant_model' => Tenant::class, // Use test tenant w/ DBs & domains
]);
$app->singleton(\Stancl\Tenancy\TenancyBootstrappers\RedisTenancyBootstrapper::class);