mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 03:14:04 +00:00
Fix tests
This commit is contained in:
parent
e872139c88
commit
a535e3e6b7
7 changed files with 67 additions and 37 deletions
|
|
@ -207,7 +207,7 @@ class DatabaseManager
|
||||||
$database = $tenant->getDatabaseName();
|
$database = $tenant->getDatabaseName();
|
||||||
$manager = $this->getTenantDatabaseManager($tenant);
|
$manager = $this->getTenantDatabaseManager($tenant);
|
||||||
|
|
||||||
$this->event('database.deleting', $database, $tenant);
|
$this->tenancy->event('database.deleting', $database, $tenant);
|
||||||
|
|
||||||
if ($this->app['config']['tenancy.queue_database_deletion'] ?? false) {
|
if ($this->app['config']['tenancy.queue_database_deletion'] ?? false) {
|
||||||
QueuedTenantDatabaseDeleter::dispatch($manager, $database);
|
QueuedTenantDatabaseDeleter::dispatch($manager, $database);
|
||||||
|
|
@ -215,7 +215,7 @@ class DatabaseManager
|
||||||
$manager->deleteDatabase($database);
|
$manager->deleteDatabase($database);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->event('database.deleted', $database, $tenant);
|
$this->tenancy->event('database.deleted', $database, $tenant);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -38,8 +38,8 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
||||||
{
|
{
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
$this->centralDatabase = $this->getCentralConnection();
|
$this->centralDatabase = $this->getCentralConnection();
|
||||||
$this->tenants = new TenantRepository($this->centralDatabase, $config);
|
$this->tenants = new TenantRepository($config);
|
||||||
$this->domains = new DomainRepository($this->centralDatabase, $config);
|
$this->domains = new DomainRepository($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -121,11 +121,7 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
||||||
public function createTenant(Tenant $tenant): void
|
public function createTenant(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
$this->centralDatabase->transaction(function () use ($tenant) {
|
$this->centralDatabase->transaction(function () use ($tenant) {
|
||||||
$this->tenants->insert(array_merge(
|
$this->tenants->insert($tenant);
|
||||||
$this->tenants->encodeData($tenant->data),
|
|
||||||
[] // ['id' => $tenant->id] // todo remove this line if things work
|
|
||||||
));
|
|
||||||
|
|
||||||
$this->domains->insertTenantDomains($tenant);
|
$this->domains->insertTenantDomains($tenant);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -142,7 +138,7 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
||||||
public function deleteTenant(Tenant $tenant): void
|
public function deleteTenant(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
$this->centralDatabase->transaction(function () use ($tenant) {
|
$this->centralDatabase->transaction(function () use ($tenant) {
|
||||||
$this->tenants->find($tenant)->delete();
|
$this->tenants->where('id', $tenant->id)->delete();
|
||||||
$this->domains->where('tenant_id', $tenant->id)->delete();
|
$this->domains->where('tenant_id', $tenant->id)->delete();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -156,7 +152,8 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
||||||
public function all(array $ids = []): array
|
public function all(array $ids = []): array
|
||||||
{
|
{
|
||||||
return $this->tenants->all($ids)->map(function ($data) {
|
return $this->tenants->all($ids)->map(function ($data) {
|
||||||
return Tenant::fromStorage($data)->withDomains($this->domains->getTenantDomains($data['id']));
|
return Tenant::fromStorage($data)
|
||||||
|
->withDomains($this->domains->getTenantDomains($data['id']));
|
||||||
})->toArray();
|
})->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
class DomainRepository extends Repository
|
class DomainRepository extends Repository
|
||||||
{
|
{
|
||||||
public function getTenantIdByDomain(string $domain): string
|
public function getTenantIdByDomain(string $domain): ?string
|
||||||
{
|
{
|
||||||
return $this->where('domain', $domain)->first()->tenant_id ?? null;
|
return $this->where('domain', $domain)->first()->tenant_id ?? null;
|
||||||
}
|
}
|
||||||
|
|
@ -20,7 +20,7 @@ class DomainRepository extends Repository
|
||||||
public function getTenantDomains($tenant)
|
public function getTenantDomains($tenant)
|
||||||
{
|
{
|
||||||
$id = $tenant instanceof Tenant ? $tenant->id : $tenant;
|
$id = $tenant instanceof Tenant ? $tenant->id : $tenant;
|
||||||
return $this->where('tenant_id', $id)->get('domain')->toArray();
|
return $this->where('tenant_id', $id)->get('domain')->pluck('domain')->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function insertTenantDomains(Tenant $tenant)
|
public function insertTenantDomains(Tenant $tenant)
|
||||||
|
|
@ -32,11 +32,11 @@ class DomainRepository extends Repository
|
||||||
|
|
||||||
public function updateTenantDomains(Tenant $tenant)
|
public function updateTenantDomains(Tenant $tenant)
|
||||||
{
|
{
|
||||||
$originalDomains = $this->domains->getTenantDomains($tenant);
|
$originalDomains = $this->getTenantDomains($tenant);
|
||||||
$deletedDomains = array_diff($originalDomains, $tenant->domains);
|
$deletedDomains = array_diff($originalDomains, $tenant->domains);
|
||||||
$newDomains = array_intersect($originalDomains, $tenant->domains);
|
$newDomains = array_diff($tenant->domains, $originalDomains);
|
||||||
|
|
||||||
$this->domains->whereIn('domain', $deletedDomains)->delete();
|
$this->whereIn('domain', $deletedDomains)->delete();
|
||||||
|
|
||||||
foreach ($newDomains as $domain) {
|
foreach ($newDomains as $domain) {
|
||||||
$this->insert([
|
$this->insert([
|
||||||
|
|
|
||||||
|
|
@ -10,21 +10,30 @@ use Illuminate\Database\Query\Builder;
|
||||||
abstract class Repository
|
abstract class Repository
|
||||||
{
|
{
|
||||||
/** @var Connection */
|
/** @var Connection */
|
||||||
protected $database;
|
public $database;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $tableName;
|
||||||
|
|
||||||
/** @var Builder */
|
/** @var Builder */
|
||||||
protected $table;
|
private $table;
|
||||||
|
|
||||||
public function __construct(Connection $database, ConfigRepository $config)
|
public function __construct(ConfigRepository $config)
|
||||||
{
|
{
|
||||||
$this->database = $database;
|
$this->database = DatabaseStorageDriver::getCentralConnection();
|
||||||
$this->table = $database->table($this->getTable($config));
|
$this->tableName = $this->getTable($config);
|
||||||
|
$this->table = $this->database->table($this->tableName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function table()
|
||||||
|
{
|
||||||
|
return $this->table->newQuery()->from($this->tableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public function getTable(ConfigRepository $config);
|
abstract public function getTable(ConfigRepository $config);
|
||||||
|
|
||||||
public function __call($method, $parameters)
|
public function __call($method, $parameters)
|
||||||
{
|
{
|
||||||
return $this->table->$method(...$parameters);
|
return $this->table()->$method(...$parameters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -4,21 +4,26 @@ namespace Stancl\Tenancy\StorageDrivers\Database;
|
||||||
|
|
||||||
use Illuminate\Config\Repository as ConfigRepository;
|
use Illuminate\Config\Repository as ConfigRepository;
|
||||||
use Stancl\Tenancy\Tenant;
|
use Stancl\Tenancy\Tenant;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
class TenantRepository extends Repository
|
class TenantRepository extends Repository
|
||||||
{
|
{
|
||||||
public function all($ids = [])
|
public function all($ids = [])
|
||||||
{
|
{
|
||||||
if ($ids) {
|
if ($ids) {
|
||||||
return $this->whereIn('id', $ids)->get();
|
$data = $this->whereIn('id', $ids)->get();
|
||||||
|
} else {
|
||||||
|
$data = $this->table()->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->table->get();
|
return $data->map(function (stdClass $obj) {
|
||||||
|
return $this->decodeData((array) $obj);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function find($tenant)
|
public function find($tenant)
|
||||||
{
|
{
|
||||||
return $this->table->find(
|
return (array) $this->table()->find(
|
||||||
$tenant instanceof Tenant ? $tenant->id : $tenant
|
$tenant instanceof Tenant ? $tenant->id : $tenant
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -35,17 +40,17 @@ class TenantRepository extends Repository
|
||||||
|
|
||||||
public function get(string $key, Tenant $tenant)
|
public function get(string $key, Tenant $tenant)
|
||||||
{
|
{
|
||||||
return $this->decodedData($tenant)[$key];
|
return $this->decodeFreshDataForTenant($tenant)[$key] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMany(array $keys, Tenant $tenant)
|
public function getMany(array $keys, Tenant $tenant)
|
||||||
{
|
{
|
||||||
$decodedData = $this->decodedData($tenant);
|
$decodedData = $this->decodeFreshDataForTenant($tenant);
|
||||||
|
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
foreach ($keys as $key) {
|
foreach ($keys as $key) {
|
||||||
$result[$key] = $decodedData[$key];
|
$result[$key] = $decodedData[$key] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
|
|
@ -58,7 +63,7 @@ class TenantRepository extends Repository
|
||||||
if (in_array($key, static::customColumns())) {
|
if (in_array($key, static::customColumns())) {
|
||||||
$record->update([$key => $value]);
|
$record->update([$key => $value]);
|
||||||
} else {
|
} else {
|
||||||
$data = json_decode($record->first(static::dataColumn()), true);
|
$data = json_decode($record->first(static::dataColumn())->data, true);
|
||||||
$data[$key] = $value;
|
$data[$key] = $value;
|
||||||
|
|
||||||
$record->update([static::dataColumn() => $data]);
|
$record->update([static::dataColumn() => $data]);
|
||||||
|
|
@ -70,11 +75,11 @@ class TenantRepository extends Repository
|
||||||
$record = $this->where('id', $tenant->id);
|
$record = $this->where('id', $tenant->id);
|
||||||
|
|
||||||
$data = [];
|
$data = [];
|
||||||
$jsonData = json_decode($record->first(static::dataColumn()), true);
|
$jsonData = json_decode($record->first(static::dataColumn())->data, true);
|
||||||
foreach ($kvPairs as $key => $value) {
|
foreach ($kvPairs as $key => $value) {
|
||||||
if (in_array($key, static::customColumns())) {
|
if (in_array($key, static::customColumns())) {
|
||||||
$data[$key] = $value;
|
$data[$key] = $value;
|
||||||
return;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
$jsonData[$key] = $value;
|
$jsonData[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
@ -90,7 +95,7 @@ class TenantRepository extends Repository
|
||||||
$record = $this->where('id', $tenant->id);
|
$record = $this->where('id', $tenant->id);
|
||||||
|
|
||||||
$data = [];
|
$data = [];
|
||||||
$jsonData = json_decode($record->first(static::dataColumn()), true);
|
$jsonData = json_decode($record->first(static::dataColumn())->data, true);
|
||||||
foreach ($keys as $key => $key) {
|
foreach ($keys as $key => $key) {
|
||||||
if (in_array($key, static::customColumns())) {
|
if (in_array($key, static::customColumns())) {
|
||||||
$data[$key] = null;
|
$data[$key] = null;
|
||||||
|
|
@ -105,10 +110,10 @@ class TenantRepository extends Repository
|
||||||
$record->update($data);
|
$record->update($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function decodedData($tenant): array
|
public function decodeFreshDataForTenant(Tenant $tenant): array
|
||||||
{
|
{
|
||||||
return $this->decodeData(
|
return $this->decodeData(
|
||||||
$this->table->where('id', $tenant->id)->get()->toArray()
|
(array) $this->table()->where('id', $tenant->id)->first()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -126,6 +131,14 @@ class TenantRepository extends Repository
|
||||||
return $columns;
|
return $columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function insert(Tenant $tenant)
|
||||||
|
{
|
||||||
|
$this->table()->insert(array_merge(
|
||||||
|
$this->encodeData($tenant->data),
|
||||||
|
['id' => $tenant->id]
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
public static function encodeData(array $data): array
|
public static function encodeData(array $data): array
|
||||||
{
|
{
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
|
||||||
|
|
@ -8,15 +8,19 @@ use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
class CacheManagerTest extends TestCase
|
class CacheManagerTest extends TestCase
|
||||||
{
|
{
|
||||||
|
public $autoInitTenancy = false;
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function default_tag_is_automatically_applied()
|
public function default_tag_is_automatically_applied()
|
||||||
{
|
{
|
||||||
|
$this->initTenancy();
|
||||||
$this->assertArrayIsSubset([config('tenancy.cache.tag_base') . tenant('id')], cache()->tags('foo')->getTags()->getNames());
|
$this->assertArrayIsSubset([config('tenancy.cache.tag_base') . tenant('id')], cache()->tags('foo')->getTags()->getNames());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function tags_are_merged_when_array_is_passed()
|
public function tags_are_merged_when_array_is_passed()
|
||||||
{
|
{
|
||||||
|
$this->initTenancy();
|
||||||
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo', 'bar'];
|
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo', 'bar'];
|
||||||
$this->assertEquals($expected, cache()->tags(['foo', 'bar'])->getTags()->getNames());
|
$this->assertEquals($expected, cache()->tags(['foo', 'bar'])->getTags()->getNames());
|
||||||
}
|
}
|
||||||
|
|
@ -24,6 +28,7 @@ class CacheManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function tags_are_merged_when_string_is_passed()
|
public function tags_are_merged_when_string_is_passed()
|
||||||
{
|
{
|
||||||
|
$this->initTenancy();
|
||||||
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo'];
|
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo'];
|
||||||
$this->assertEquals($expected, cache()->tags('foo')->getTags()->getNames());
|
$this->assertEquals($expected, cache()->tags('foo')->getTags()->getNames());
|
||||||
}
|
}
|
||||||
|
|
@ -31,6 +36,7 @@ class CacheManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function exception_is_thrown_when_zero_arguments_are_passed_to_tags_method()
|
public function exception_is_thrown_when_zero_arguments_are_passed_to_tags_method()
|
||||||
{
|
{
|
||||||
|
$this->initTenancy();
|
||||||
$this->expectException(\Exception::class);
|
$this->expectException(\Exception::class);
|
||||||
cache()->tags();
|
cache()->tags();
|
||||||
}
|
}
|
||||||
|
|
@ -38,6 +44,7 @@ class CacheManagerTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function exception_is_thrown_when_more_than_one_argument_is_passed_to_tags_method()
|
public function exception_is_thrown_when_more_than_one_argument_is_passed_to_tags_method()
|
||||||
{
|
{
|
||||||
|
$this->initTenancy();
|
||||||
$this->expectException(\Exception::class);
|
$this->expectException(\Exception::class);
|
||||||
cache()->tags(1, 2);
|
cache()->tags(1, 2);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
use Stancl\Tenancy\StorageDrivers\Database\TenantModel;
|
use Stancl\Tenancy\StorageDrivers\Database\TenantRepository;
|
||||||
use Stancl\Tenancy\Tenant;
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
class TenantStorageTest extends TestCase
|
class TenantStorageTest extends TestCase
|
||||||
|
|
@ -112,10 +112,11 @@ class TenantStorageTest extends TestCase
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function tenant_model_uses_correct_connection()
|
public function tenant_repository_uses_correct_connection()
|
||||||
{
|
{
|
||||||
|
config(['database.connections.foo' => config('database.connections.sqlite')]);
|
||||||
config(['tenancy.storage_drivers.db.connection' => 'foo']);
|
config(['tenancy.storage_drivers.db.connection' => 'foo']);
|
||||||
$this->assertSame('foo', (new TenantModel)->getConnectionName());
|
$this->assertSame('foo', app(TenantRepository::class)->database->getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
@ -156,6 +157,9 @@ class TenantStorageTest extends TestCase
|
||||||
tenancy()->create(['foo.localhost']);
|
tenancy()->create(['foo.localhost']);
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
|
|
||||||
|
tenant()->put('foo', '111');
|
||||||
|
$this->assertSame('111', tenant()->get('foo'));
|
||||||
|
|
||||||
tenant()->put(['foo' => 'bar', 'abc' => 'xyz']);
|
tenant()->put(['foo' => 'bar', 'abc' => 'xyz']);
|
||||||
$this->assertSame(['foo' => 'bar', 'abc' => 'xyz'], tenant()->get(['foo', 'abc']));
|
$this->assertSame(['foo' => 'bar', 'abc' => 'xyz'], tenant()->get(['foo', 'abc']));
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue