mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 11:34:02 +00:00
Get old tests to pass
This commit is contained in:
parent
65ebc043dc
commit
439b5b1dc1
28 changed files with 154 additions and 100 deletions
|
|
@ -8,11 +8,10 @@ use Stancl\Tenancy\Tenant;
|
|||
|
||||
class CacheManagerTest extends TestCase
|
||||
{
|
||||
public $autoInitTenancy = false;
|
||||
|
||||
/** @test */
|
||||
public function default_tag_is_automatically_applied()
|
||||
{
|
||||
$this->createTenant();
|
||||
$this->initTenancy();
|
||||
$this->assertArrayIsSubset([config('tenancy.cache.tag_base') . tenant('id')], cache()->tags('foo')->getTags()->getNames());
|
||||
}
|
||||
|
|
@ -20,6 +19,7 @@ class CacheManagerTest extends TestCase
|
|||
/** @test */
|
||||
public function tags_are_merged_when_array_is_passed()
|
||||
{
|
||||
$this->createTenant();
|
||||
$this->initTenancy();
|
||||
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo', 'bar'];
|
||||
$this->assertEquals($expected, cache()->tags(['foo', 'bar'])->getTags()->getNames());
|
||||
|
|
@ -28,6 +28,7 @@ class CacheManagerTest extends TestCase
|
|||
/** @test */
|
||||
public function tags_are_merged_when_string_is_passed()
|
||||
{
|
||||
$this->createTenant();
|
||||
$this->initTenancy();
|
||||
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo'];
|
||||
$this->assertEquals($expected, cache()->tags('foo')->getTags()->getNames());
|
||||
|
|
@ -36,6 +37,7 @@ class CacheManagerTest extends TestCase
|
|||
/** @test */
|
||||
public function exception_is_thrown_when_zero_arguments_are_passed_to_tags_method()
|
||||
{
|
||||
$this->createTenant();
|
||||
$this->initTenancy();
|
||||
$this->expectException(\Exception::class);
|
||||
cache()->tags();
|
||||
|
|
@ -44,6 +46,7 @@ class CacheManagerTest extends TestCase
|
|||
/** @test */
|
||||
public function exception_is_thrown_when_more_than_one_argument_is_passed_to_tags_method()
|
||||
{
|
||||
$this->createTenant();
|
||||
$this->initTenancy();
|
||||
$this->expectException(\Exception::class);
|
||||
cache()->tags(1, 2);
|
||||
|
|
|
|||
|
|
@ -12,15 +12,9 @@ use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
|||
|
||||
class CommandsTest extends TestCase
|
||||
{
|
||||
public $autoCreateTenant = true;
|
||||
public $autoInitTenancy = false;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
config(['tenancy.migration_paths', [database_path('../migrations')]]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function migrate_command_doesnt_change_the_db_connection()
|
||||
{
|
||||
|
|
@ -173,6 +167,7 @@ class CommandsTest extends TestCase
|
|||
$tenant = tenancy()->all()[1]; // a tenant is autocreated prior to this
|
||||
$data = $tenant->data;
|
||||
unset($data['id']);
|
||||
unset($data['_tenancy_db_name']);
|
||||
|
||||
$this->assertSame(['plan' => 'free', 'email' => 'foo@test.local'], $data);
|
||||
$this->assertSame(['aaa.localhost', 'bbb.localhost'], $tenant->domains);
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Redis;
|
|||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Stancl\Tenancy\Tests\Etc\User;
|
||||
|
||||
class DataSeparationTest extends TestCase
|
||||
{
|
||||
|
|
@ -151,8 +152,3 @@ class DataSeparationTest extends TestCase
|
|||
$this->assertFalse(Storage::disk('public')->exists('abc'));
|
||||
}
|
||||
}
|
||||
|
||||
class User extends \Illuminate\Database\Eloquent\Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,16 +5,16 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Stancl\Tenancy\DatabaseManager;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
|
||||
class DatabaseManagerTest extends TestCase
|
||||
{
|
||||
public $autoInitTenancy = false;
|
||||
|
||||
/** @test */
|
||||
public function reconnect_method_works()
|
||||
{
|
||||
$old_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
|
||||
tenancy()->init('test.localhost');
|
||||
$this->createTenant();
|
||||
$this->initTenancy();
|
||||
app(\Stancl\Tenancy\DatabaseManager::class)->reconnect();
|
||||
$new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
|
||||
|
||||
|
|
@ -25,22 +25,30 @@ class DatabaseManagerTest extends TestCase
|
|||
/** @test */
|
||||
public function db_name_is_prefixed_with_db_path_when_sqlite_is_used()
|
||||
{
|
||||
if (file_exists(database_path('foodb'))) {
|
||||
unlink(database_path('foodb')); // cleanup
|
||||
}
|
||||
config(['database.connections.fooconn.driver' => 'sqlite']);
|
||||
app(DatabaseManager::class)->createTenantConnection('foodb', 'fooconn');
|
||||
$tenant = Tenant::new()->withData([
|
||||
'_tenancy_db_name' => 'foodb',
|
||||
'_tenancy_db_connection' => 'fooconn',
|
||||
])->save();
|
||||
app(DatabaseManager::class)->createTenantConnection($tenant);
|
||||
|
||||
$this->assertSame(config('database.connections.fooconn.database'), database_path('foodb'));
|
||||
$this->assertSame(config('database.connections.tenant.database'), database_path('foodb'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function the_default_db_is_used_when_template_connection_is_null()
|
||||
{
|
||||
$this->assertSame('sqlite', config('database.default'));
|
||||
$this->assertSame('central', config('database.default'));
|
||||
config([
|
||||
'database.connections.sqlite.foo' => 'bar',
|
||||
'database.connections.central.foo' => 'bar',
|
||||
'tenancy.database.template_connection' => null,
|
||||
]);
|
||||
|
||||
tenancy()->init('test.localhost');
|
||||
$this->createTenant();
|
||||
$this->initTenancy();
|
||||
|
||||
$this->assertSame('tenant', config('database.default'));
|
||||
$this->assertSame('bar', config('database.connections.' . config('database.default') . '.foo'));
|
||||
|
|
|
|||
|
|
@ -4,11 +4,14 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Str;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Stancl\Tenancy\Tests\Etc\User;
|
||||
|
||||
class DatabaseSchemaManagerTest extends TestCase
|
||||
{
|
||||
public $autoCreateTenant = true;
|
||||
public $autoInitTenancy = false;
|
||||
|
||||
protected function getEnvironmentSetUp($app)
|
||||
|
|
@ -17,6 +20,7 @@ class DatabaseSchemaManagerTest extends TestCase
|
|||
|
||||
$app['config']->set([
|
||||
'database.default' => 'pgsql',
|
||||
'tenancy.storage_drivers.db.connection' => 'pgsql',
|
||||
'database.connections.pgsql.database' => 'main',
|
||||
'database.connections.pgsql.schema' => 'public',
|
||||
'tenancy.database.template_connection' => null,
|
||||
|
|
@ -42,8 +46,6 @@ class DatabaseSchemaManagerTest extends TestCase
|
|||
/** @test */
|
||||
public function the_default_db_is_used_when_template_connection_is_null()
|
||||
{
|
||||
config(['database.default' => 'pgsql']);
|
||||
|
||||
$this->assertSame('pgsql', config('database.default'));
|
||||
config([
|
||||
'database.connections.pgsql.foo' => 'bar',
|
||||
|
|
@ -53,7 +55,7 @@ class DatabaseSchemaManagerTest extends TestCase
|
|||
tenancy()->init('test.localhost');
|
||||
|
||||
$this->assertSame('tenant', config('database.default'));
|
||||
$this->assertSame('bar', config('database.connections.' . config('database.default') . '.foo'));
|
||||
$this->assertSame('bar', config('database.connections.tenant.foo'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
|
@ -95,6 +97,7 @@ class DatabaseSchemaManagerTest extends TestCase
|
|||
|
||||
$tenant1 = Tenant::create('tenant1.localhost');
|
||||
$tenant2 = Tenant::create('tenant2.localhost');
|
||||
|
||||
\Artisan::call('tenants:migrate', [
|
||||
'--tenants' => [$tenant1['id'], $tenant2['id']],
|
||||
]);
|
||||
|
|
|
|||
8
tests/Etc/User.php
Normal file
8
tests/Etc/User.php
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Tests\Etc;
|
||||
|
||||
class User extends \Illuminate\Database\Eloquent\Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
}
|
||||
|
|
@ -9,6 +9,9 @@ use Tenant;
|
|||
|
||||
class FacadeTest extends TestCase
|
||||
{
|
||||
public $autoCreateTenant = true;
|
||||
public $autoInitTenancy = true;
|
||||
|
||||
/** @test */
|
||||
public function tenant_manager_can_be_accessed_using_the_Tenancy_facade()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -14,6 +14,9 @@ use Illuminate\Support\Facades\Event;
|
|||
|
||||
class QueueTest extends TestCase
|
||||
{
|
||||
public $autoCreateTenant = true;
|
||||
public $autoInitTenancy = true;
|
||||
|
||||
/** @test */
|
||||
public function queues_use_non_tenant_db_connection()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ use Stancl\Tenancy\Tenant;
|
|||
|
||||
class ReidentificationTest extends TestCase
|
||||
{
|
||||
public $autoCreateTenant = true;
|
||||
public $autoInitTenancy = false;
|
||||
|
||||
/**
|
||||
* These tests are run when a tenant is identified after another tenant has already been identified.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Redis;
|
|||
|
||||
class TenancyBootstrappersTest extends TestCase
|
||||
{
|
||||
public $autoCreateTenant = true;
|
||||
public $autoInitTenancy = false;
|
||||
|
||||
/** @test */
|
||||
|
|
|
|||
|
|
@ -54,8 +54,8 @@ class TenantClassTest extends TestCase
|
|||
$tenant = Tenant::create(['foo.localhost'], ['id' => $id]);
|
||||
$tenant->foo = 'bar';
|
||||
$tenant->save();
|
||||
$this->assertEquals(['id' => $id, 'foo' => 'bar'], $tenant->data);
|
||||
$this->assertEquals(['id' => $id, 'foo' => 'bar'], tenancy()->find($id)->data);
|
||||
$this->assertEquals(['id' => $id, 'foo' => 'bar', '_tenancy_db_name' => $tenant->database()->getName()], $tenant->data);
|
||||
$this->assertEquals(['id' => $id, 'foo' => 'bar', '_tenancy_db_name' => $tenant->database()->getName()], tenancy()->find($id)->data);
|
||||
|
||||
$tenant->addDomains('abc.localhost');
|
||||
$tenant->save();
|
||||
|
|
@ -102,6 +102,7 @@ class TenantClassTest extends TestCase
|
|||
|
||||
$data = tenancy()->all()->first()->data;
|
||||
unset($data['id']);
|
||||
unset($data['_tenancy_db_name']);
|
||||
|
||||
$this->assertSame(['foo' => 'bar'], $data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,25 +28,46 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
$this->markTestSkipped('As to not bloat your computer with test databases, this test is not run by default.');
|
||||
}
|
||||
|
||||
config()->set([
|
||||
"tenancy.database_managers.$driver" => $databaseManager,
|
||||
]);
|
||||
|
||||
$name = 'db' . $this->randomString();
|
||||
$tenant = Tenant::new()->withData([
|
||||
'_tenancy_db_name' => $name,
|
||||
'_tenancy_db_connection' => $driver,
|
||||
]);
|
||||
|
||||
$this->assertFalse(app($databaseManager)->databaseExists($name));
|
||||
app($databaseManager)->createDatabase($name);
|
||||
$tenant->save(); // generate credentials & create DB
|
||||
$this->assertTrue(app($databaseManager)->databaseExists($name));
|
||||
app($databaseManager)->deleteDatabase($name);
|
||||
app($databaseManager)->deleteDatabase($tenant);
|
||||
$this->assertFalse(app($databaseManager)->databaseExists($name));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function dbs_can_be_created_when_another_driver_is_used_for_the_central_db()
|
||||
{
|
||||
$this->assertSame('sqlite', config('database.default'));
|
||||
$this->assertSame('central', config('database.default'));
|
||||
|
||||
$database = 'db' . $this->randomString();
|
||||
app(MySQLDatabaseManager::class)->createDatabase($database);
|
||||
$tenant = Tenant::new()->withData([
|
||||
'_tenancy_db_name' => $database,
|
||||
'_tenancy_db_connection' => 'mysql',
|
||||
]);
|
||||
|
||||
$this->assertFalse(app(MySQLDatabaseManager::class)->databaseExists($database));
|
||||
$tenant->save(); // create DB
|
||||
$this->assertTrue(app(MySQLDatabaseManager::class)->databaseExists($database));
|
||||
|
||||
$database = 'db2' . $this->randomString();
|
||||
app(PostgreSQLDatabaseManager::class)->createDatabase($database);
|
||||
$database = 'db' . $this->randomString();
|
||||
$tenant = Tenant::new()->withData([
|
||||
'_tenancy_db_name' => $database,
|
||||
'_tenancy_db_connection' => 'pgsql',
|
||||
]);
|
||||
|
||||
$this->assertFalse(app(PostgreSQLDatabaseManager::class)->databaseExists($database));
|
||||
$tenant->save(); // create DB
|
||||
$this->assertTrue(app(PostgreSQLDatabaseManager::class)->databaseExists($database));
|
||||
}
|
||||
|
||||
|
|
@ -60,16 +81,25 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
$this->markTestSkipped('As to not bloat your computer with test databases, this test is not run by default.');
|
||||
}
|
||||
|
||||
config()->set('database.default', $driver);
|
||||
config()->set([
|
||||
'database.default' => $driver,
|
||||
"tenancy.database_managers.$driver" => $databaseManager,
|
||||
]);
|
||||
|
||||
$name = 'db' . $this->randomString();
|
||||
$tenant = Tenant::new()->withData([
|
||||
'_tenancy_db_name' => $name,
|
||||
'_tenancy_db_connection' => $driver,
|
||||
]);
|
||||
$tenant->database()->makeCredentials();
|
||||
|
||||
$this->assertFalse(app($databaseManager)->databaseExists($name));
|
||||
$job = new QueuedTenantDatabaseCreator(app($databaseManager), $name);
|
||||
$job = new QueuedTenantDatabaseCreator(app($databaseManager), $tenant);
|
||||
|
||||
$job->handle();
|
||||
$this->assertTrue(app($databaseManager)->databaseExists($name));
|
||||
|
||||
$job = new QueuedTenantDatabaseDeleter(app($databaseManager), $name);
|
||||
$job = new QueuedTenantDatabaseDeleter(app($databaseManager), $tenant);
|
||||
$job->handle();
|
||||
$this->assertFalse(app($databaseManager)->databaseExists($name));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -167,6 +167,7 @@ class TenantManagerTest extends TestCase
|
|||
|
||||
$tenant_data = $tenant->data;
|
||||
unset($tenant_data['id']);
|
||||
unset($tenant_data['_tenancy_db_name']);
|
||||
|
||||
$this->assertSame($data, $tenant_data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,9 @@ use Stancl\Tenancy\Tenant;
|
|||
|
||||
class TenantStorageTest extends TestCase
|
||||
{
|
||||
public $autoCreateTenant = true;
|
||||
public $autoInitTenancy = true;
|
||||
|
||||
/** @test */
|
||||
public function deleting_a_tenant_works()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ use Stancl\Tenancy\Tenant;
|
|||
|
||||
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
public $autoCreateTenant = true;
|
||||
public $autoInitTenancy = true;
|
||||
public $autoCreateTenant = false;
|
||||
public $autoInitTenancy = false;
|
||||
|
||||
/**
|
||||
* Setup the test environment.
|
||||
|
|
@ -24,12 +24,12 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
Redis::connection('tenancy')->flushdb();
|
||||
Redis::connection('cache')->flushdb();
|
||||
|
||||
$originalConnection = config('database.default');
|
||||
$this->loadMigrationsFrom([
|
||||
'--path' => realpath(__DIR__ . '/../assets/migrations'),
|
||||
'--database' => 'central',
|
||||
file_put_contents(database_path('central.sqlite'), '');
|
||||
$this->artisan('migrate:fresh', [
|
||||
'--force' => true,
|
||||
'--path' => __DIR__ . '/../assets/migrations',
|
||||
'--realpath' => true,
|
||||
]);
|
||||
config(['database.default' => $originalConnection]); // fix issue caused by loadMigrationsFrom
|
||||
|
||||
if ($this->autoCreateTenant) {
|
||||
$this->createTenant();
|
||||
|
|
@ -62,9 +62,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
\Dotenv\Dotenv::create(__DIR__ . '/..')->load();
|
||||
}
|
||||
|
||||
fclose(fopen(database_path('central.sqlite'), 'w'));
|
||||
|
||||
$app['config']->set([
|
||||
'database.default' => 'central',
|
||||
'database.redis.cache.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||
'database.redis.default.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||
'database.redis.options.prefix' => 'foo',
|
||||
|
|
@ -80,9 +79,10 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'database.connections.central' => [
|
||||
'driver' => 'sqlite',
|
||||
'database' => database_path('central.sqlite'),
|
||||
// 'database' => ':memory:',
|
||||
],
|
||||
'tenancy.database' => [
|
||||
'template_connection' => 'sqlite',
|
||||
'template_connection' => 'central',
|
||||
'prefix' => 'tenant',
|
||||
'suffix' => '.sqlite',
|
||||
],
|
||||
|
|
@ -97,7 +97,11 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'tenancy.redis.tenancy' => env('TENANCY_TEST_REDIS_TENANCY', true),
|
||||
'database.redis.client' => env('TENANCY_TEST_REDIS_CLIENT', 'phpredis'),
|
||||
'tenancy.redis.prefixed_connections' => ['default'],
|
||||
'tenancy.migration_paths' => [database_path('../migrations')],
|
||||
'tenancy.migration_parameters' => [
|
||||
'--path' => [database_path('../migrations')],
|
||||
'--realpath' => true,
|
||||
'--force' => true,
|
||||
],
|
||||
'tenancy.storage_drivers.db.connection' => 'central',
|
||||
'tenancy.bootstrappers.redis' => \Stancl\Tenancy\TenancyBootstrappers\RedisTenancyBootstrapper::class,
|
||||
'queue.connections.central' => [
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue