mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 11:14:03 +00:00
Merge branch '1.x' into telescope-tags
This commit is contained in:
commit
a00113b3d1
36 changed files with 558 additions and 707 deletions
|
|
@ -86,7 +86,7 @@ class BootstrapsTenancyTest extends TestCase
|
|||
$current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix();
|
||||
|
||||
if ($override = config("tenancy.filesystem.root_override.{$disk}")) {
|
||||
$correct_path_prefix = str_replace('%storage_path%', storage_path(), $override);
|
||||
$correct_path_prefix = \str_replace('%storage_path%', storage_path(), $override);
|
||||
} else {
|
||||
if ($base = $old_storage_facade_roots[$disk]) {
|
||||
$correct_path_prefix = $base . "/$suffix/";
|
||||
|
|
|
|||
|
|
@ -114,17 +114,19 @@ class CommandsTest extends TestCase
|
|||
->expectsOutput('xyz');
|
||||
}
|
||||
|
||||
// todo check that multiple tenants can be migrated at once using all database engines
|
||||
|
||||
/** @test */
|
||||
public function install_command_works()
|
||||
{
|
||||
if (! is_dir($dir = app_path('Http'))) {
|
||||
mkdir($dir, 0777, true);
|
||||
if (! \is_dir($dir = app_path('Http'))) {
|
||||
\mkdir($dir, 0777, true);
|
||||
}
|
||||
if (! is_dir($dir = base_path('routes'))) {
|
||||
mkdir($dir, 0777, true);
|
||||
if (! \is_dir($dir = base_path('routes'))) {
|
||||
\mkdir($dir, 0777, true);
|
||||
}
|
||||
|
||||
file_put_contents(app_path('Http/Kernel.php'), "<?php
|
||||
\file_put_contents(app_path('Http/Kernel.php'), "<?php
|
||||
|
||||
namespace App\Http;
|
||||
|
||||
|
|
@ -210,6 +212,8 @@ class Kernel extends HttpKernel
|
|||
->expectsQuestion('Do you want to publish the default database migration?', 'yes');
|
||||
$this->assertFileExists(base_path('routes/tenant.php'));
|
||||
$this->assertFileExists(base_path('config/tenancy.php'));
|
||||
$this->assertFileExists(database_path('migrations/2019_08_08_000000_create_tenants_table.php'));
|
||||
$this->assertDirectoryExists(database_path('migrations/tenant'));
|
||||
$this->assertSame("<?php
|
||||
|
||||
namespace App\Http;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Stancl\Tenancy\DatabaseManager;
|
||||
|
||||
class DatabaseManagerTest extends TestCase
|
||||
{
|
||||
public $autoInitTenancy = false;
|
||||
|
|
@ -17,4 +19,14 @@ class DatabaseManagerTest extends TestCase
|
|||
$this->assertSame($old_connection_name, $new_connection_name);
|
||||
$this->assertNotEquals('tenant', $new_connection_name);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function db_name_is_prefixed_with_db_path_when_sqlite_is_used()
|
||||
{
|
||||
// make `tenant` not sqlite so that it has to detect sqlite from fooconn
|
||||
config(['database.connections.tenant.driver' => 'mysql']);
|
||||
app(DatabaseManager::class)->createTenantConnection('foodb', 'fooconn');
|
||||
|
||||
$this->assertSame(config('database.connections.fooconn.database'), database_path('foodb'));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class ReidentificationTest extends TestCase
|
|||
$current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix();
|
||||
|
||||
if ($override = config("tenancy.filesystem.root_override.{$disk}")) {
|
||||
$correct_path_prefix = str_replace('%storage_path%', storage_path(), $override);
|
||||
$correct_path_prefix = \str_replace('%storage_path%', storage_path(), $override);
|
||||
} else {
|
||||
if ($base = $originals[$disk]) {
|
||||
$correct_path_prefix = $base . "/$suffix/";
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@ class TenantAssetTest extends TestCase
|
|||
$this->get(tenant_asset($filename))->assertSuccessful();
|
||||
$this->assertFileExists($path);
|
||||
|
||||
$f = fopen($path, 'r');
|
||||
$content = fread($f, filesize($path));
|
||||
fclose($f);
|
||||
$f = \fopen($path, 'r');
|
||||
$content = \fread($f, \filesize($path));
|
||||
\fclose($f);
|
||||
|
||||
$this->assertSame('bar', $content);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
|
||||
config()->set('database.default', 'pgsql');
|
||||
|
||||
$db_name = strtolower('testdatabase' . $this->randomString(10));
|
||||
$db_name = \strtolower('testdatabase' . $this->randomString(10));
|
||||
$this->assertTrue(app(DatabaseManager::class)->create($db_name, 'pgsql'));
|
||||
$this->assertNotEmpty(DB::select("SELECT datname FROM pg_database WHERE datname = '$db_name'"));
|
||||
|
||||
|
|
@ -104,7 +104,7 @@ class TenantDatabaseManagerTest extends TestCase
|
|||
|
||||
config()->set('database.default', 'pgsql');
|
||||
|
||||
$db_name = strtolower('testdatabase' . $this->randomString(10));
|
||||
$db_name = \strtolower('testdatabase' . $this->randomString(10));
|
||||
|
||||
$databaseManagers = config('tenancy.database_managers');
|
||||
$job = new QueuedTenantDatabaseCreator(app($databaseManagers['pgsql']), $db_name);
|
||||
|
|
|
|||
|
|
@ -24,6 +24,9 @@ class TenantManagerTest extends TestCase
|
|||
/** @test */
|
||||
public function invoke_works()
|
||||
{
|
||||
tenant()->create('foo.localhost');
|
||||
tenancy()->init('foo.localhost');
|
||||
|
||||
$this->assertSame(tenant('uuid'), tenant()('uuid'));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Stancl\Tenancy\StorageDrivers\RedisStorageDriver;
|
||||
use Stancl\Tenancy\StorageDrivers\DatabaseStorageDriver;
|
||||
|
||||
class TenantStorageTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
|
|
@ -37,7 +41,7 @@ class TenantStorageTest extends TestCase
|
|||
{
|
||||
$keys = ['foo', 'abc'];
|
||||
$vals = ['bar', 'xyz'];
|
||||
$data = array_combine($keys, $vals);
|
||||
$data = \array_combine($keys, $vals);
|
||||
|
||||
tenancy()->put($data);
|
||||
|
||||
|
|
@ -72,13 +76,13 @@ class TenantStorageTest extends TestCase
|
|||
|
||||
$keys = ['foo', 'abc'];
|
||||
$vals = ['bar', 'xyz'];
|
||||
$data = array_combine($keys, $vals);
|
||||
$data = \array_combine($keys, $vals);
|
||||
|
||||
tenancy()->put($data, null, $uuid);
|
||||
|
||||
$this->assertSame($vals, tenancy()->get($keys, $uuid));
|
||||
$this->assertNotSame($vals, tenancy()->get($keys));
|
||||
$this->assertFalse(array_intersect($data, tenant()->tenant) == $data); // assert array not subset
|
||||
$this->assertFalse(\array_intersect($data, tenant()->tenant) == $data); // assert array not subset
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
|
@ -111,4 +115,41 @@ class TenantStorageTest extends TestCase
|
|||
|
||||
$this->assertSame($value, tenancy()->put($value));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function correct_storage_driver_is_used()
|
||||
{
|
||||
if (config('tenancy.storage_driver') == DatabaseStorageDriver::class) {
|
||||
$this->assertSame('DatabaseStorageDriver', class_basename(tenancy()->storage));
|
||||
} elseif (config('tenancy.storage_driver') == RedisStorageDriver::class) {
|
||||
$this->assertSame('RedisStorageDriver', class_basename(tenancy()->storage));
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function data_is_stored_with_correct_data_types()
|
||||
{
|
||||
tenancy()->put('someBool', false);
|
||||
$this->assertSame('boolean', \gettype(tenancy()->get('someBool')));
|
||||
|
||||
tenancy()->put('someInt', 5);
|
||||
$this->assertSame('integer', \gettype(tenancy()->get('someInt')));
|
||||
|
||||
tenancy()->put('someDouble', 11.40);
|
||||
$this->assertSame('double', \gettype(tenancy()->get('someDouble')));
|
||||
|
||||
tenancy()->put('string', 'foo');
|
||||
$this->assertSame('string', \gettype(tenancy()->get('string')));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function tenant_model_uses_correct_connection()
|
||||
{
|
||||
config(['tenancy.storage.db.connection' => 'foo']);
|
||||
$this->assertSame('foo', (new Tenant)->getConnectionName());
|
||||
|
||||
config(['tenancy.storage.db.connection' => null]);
|
||||
config(['database.default' => 'foobar']);
|
||||
$this->assertSame('foobar', (new Tenant)->getConnectionName());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,19 +3,14 @@
|
|||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Stancl\Tenancy\StorageDrivers\RedisStorageDriver;
|
||||
use Stancl\Tenancy\StorageDrivers\DatabaseStorageDriver;
|
||||
|
||||
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
public $autoCreateTenant = true;
|
||||
public $autoInitTenancy = true;
|
||||
|
||||
private function checkRequirements(): void
|
||||
{
|
||||
parent::checkRequirements();
|
||||
|
||||
dd($this->getAnnotations());
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the test environment.
|
||||
*
|
||||
|
|
@ -28,6 +23,12 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
Redis::connection('tenancy')->flushdb();
|
||||
Redis::connection('cache')->flushdb();
|
||||
|
||||
$this->loadMigrationsFrom([
|
||||
'--path' => \realpath(__DIR__ . '/../assets/migrations'),
|
||||
'--database' => 'central',
|
||||
]);
|
||||
config(['database.default' => 'sqlite']); // fix issue caused by loadMigrationsFrom
|
||||
|
||||
if ($this->autoCreateTenant) {
|
||||
$this->createTenant();
|
||||
}
|
||||
|
|
@ -37,6 +38,13 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
}
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
// config(['database.default' => 'central']);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function createTenant($domain = 'localhost')
|
||||
{
|
||||
tenant()->create($domain);
|
||||
|
|
@ -55,10 +63,12 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
*/
|
||||
protected function getEnvironmentSetUp($app)
|
||||
{
|
||||
if (file_exists(__DIR__ . '/../.env')) {
|
||||
if (\file_exists(__DIR__ . '/../.env')) {
|
||||
\Dotenv\Dotenv::create(__DIR__ . '/..')->load();
|
||||
}
|
||||
|
||||
\fclose(\fopen(database_path('central.sqlite'), 'w'));
|
||||
|
||||
$app['config']->set([
|
||||
'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'),
|
||||
|
|
@ -72,6 +82,10 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'database' => env('TENANCY_TEST_REDIS_DB', 14),
|
||||
'prefix' => 'abc', // todo unrelated to tenancy, but this doesn't seem to have an effect? try to replicate in a fresh laravel installation
|
||||
],
|
||||
'database.connections.central' => [
|
||||
'driver' => 'sqlite',
|
||||
'database' => database_path('central.sqlite'),
|
||||
],
|
||||
'tenancy.database' => [
|
||||
'based_on' => 'sqlite',
|
||||
'prefix' => 'tenant',
|
||||
|
|
@ -90,11 +104,27 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'tenancy.redis.prefixed_connections' => ['default'],
|
||||
'tenancy.migrations_directory' => database_path('../migrations'),
|
||||
]);
|
||||
|
||||
if (env('TENANCY_TEST_STORAGE_DRIVER', 'redis') === 'redis') {
|
||||
$app['config']->set([
|
||||
'tenancy.storage_driver' => RedisStorageDriver::class,
|
||||
]);
|
||||
|
||||
tenancy()->storage = $app->make(RedisStorageDriver::class);
|
||||
} elseif (env('TENANCY_TEST_STORAGE_DRIVER', 'redis') === 'db') {
|
||||
$app['config']->set([
|
||||
'tenancy.storage_driver' => DatabaseStorageDriver::class,
|
||||
]);
|
||||
|
||||
tenancy()->storage = $app->make(DatabaseStorageDriver::class);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
{
|
||||
return [\Stancl\Tenancy\TenancyServiceProvider::class];
|
||||
return [
|
||||
\Stancl\Tenancy\TenancyServiceProvider::class,
|
||||
];
|
||||
}
|
||||
|
||||
protected function getPackageAliases($app)
|
||||
|
|
@ -130,7 +160,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
|
||||
public function randomString(int $length = 10)
|
||||
{
|
||||
return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length);
|
||||
return \substr(\str_shuffle(\str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', \ceil($length / \strlen($x)))), 1, $length);
|
||||
}
|
||||
|
||||
public function isContainerized()
|
||||
|
|
@ -140,6 +170,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
|
||||
public function assertArrayIsSubset($subset, $array, string $message = ''): void
|
||||
{
|
||||
parent::assertTrue(array_intersect($subset, $array) == $subset, $message);
|
||||
parent::assertTrue(\array_intersect($subset, $array) == $subset, $message);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue