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

Fix some tests

This commit is contained in:
Samuel Štancl 2019-09-18 19:48:15 +02:00
parent 1f88a1ff94
commit 12c05c0af6
6 changed files with 53 additions and 95 deletions

View file

@ -42,7 +42,14 @@ class DatabaseStorageDriver implements StorageDriver
public function findById(string $id): Tenant
{
return Tenant::fromStorage(Tenants::find($id)->decoded())
->withDomains(Domains::where('tenant_id', $id)->get()->only('domain')->toArray());
->withDomains($this->getTenantDomains($id));
}
protected function getTenantDomains($id)
{
return Domains::where('tenant_id', $id)->get()->map(function ($model) {
return $model->domain;
})->toArray();
}
public function ensureTenantCanBeCreated(Tenant $tenant): void
@ -107,8 +114,8 @@ class DatabaseStorageDriver implements StorageDriver
*/
public function all(array $ids = []): array
{
return Tenants::getAllTenants($ids)->map(function ($array) {
return Tenant::fromStorage($array)->withDomains([]); // todo domains
return Tenants::getAllTenants($ids)->map(function ($data) {
return Tenant::fromStorage($data)->withDomains($this->getTenantDomains($data['id']));
})->toArray();
}
@ -117,34 +124,34 @@ class DatabaseStorageDriver implements StorageDriver
*
* @return Tenant
*/
protected function tenant()
protected function currentTenant()
{
return $this->tenant ?? $this->app[Tenant::class];
}
public function get(string $key, Tenant $tenant = null)
{
$tenant = $tenant ?? $this->tenant();
$tenant = $tenant ?? $this->currentTenant();
return Tenants::find($tenant->id)->get($key);
}
public function getMany(array $keys, Tenant $tenant = null): array
{
$tenant = $tenant ?? $this->tenant();
$tenant = $tenant ?? $this->currentTenant();
return Tenants::find($tenant->id)->getMany($keys);
}
public function put(string $key, $value, Tenant $tenant = null): void
{
$tenant = $tenant ?? $this->tenant();
$tenant = $tenant ?? $this->currentTenant();
Tenants::find($tenant->id)->put($key, $value);
}
public function putMany(array $kvPairs, Tenant $tenant = null): void
{
$tenant = $tenant ?? $this->tenant();
$tenant = $tenant ?? $this->currentTenant();
Tenants::find($tenant->id)->putMany($kvPairs);
}
}

View file

@ -81,8 +81,8 @@ class TenantModel extends Model
public function getMany(array $keys): array
{
return array_reduce($keys, function ($result, $key) {
$result[$key] = $this->get[$key];
return array_reduce($keys, function ($result, $key) { // todo2 performance
$result[$key] = $this->get($key);
return $result;
}, []);
@ -105,7 +105,7 @@ class TenantModel extends Model
public function putMany(array $kvPairs)
{
$customColumns = [];
$jsonObj = json_decode($this->{$this->customColumns()});
$jsonObj = json_decode($this->{$this->dataColumn()});
foreach ($kvPairs as $key => $value) {
if (in_array($key, $this->customColumns())) {

View file

@ -85,7 +85,7 @@ class Tenant implements ArrayAccess
protected function persisted($persisted = null)
{
if (gettype($persisted) === 'bool') {
if (gettype($persisted) === 'boolean') {
$this->persisted = $persisted;
return $this;
@ -94,6 +94,11 @@ class Tenant implements ArrayAccess
return $this;
}
public function isPersisted(): bool
{
return $this->persisted;
}
/**
* Assign domains to the tenant.
*