mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 07:54:03 +00:00
FutureTest
This commit is contained in:
parent
71d2ea1b62
commit
d97f45b587
6 changed files with 82 additions and 15 deletions
|
|
@ -9,6 +9,7 @@ use Illuminate\Database\Connection;
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Stancl\Tenancy\Contracts\Future\CanDeleteKeys;
|
use Stancl\Tenancy\Contracts\Future\CanDeleteKeys;
|
||||||
|
use Stancl\Tenancy\Contracts\Future\CanFindByAnyKey;
|
||||||
use Stancl\Tenancy\Contracts\StorageDriver;
|
use Stancl\Tenancy\Contracts\StorageDriver;
|
||||||
use Stancl\Tenancy\DatabaseManager;
|
use Stancl\Tenancy\DatabaseManager;
|
||||||
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
|
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
|
||||||
|
|
@ -17,7 +18,7 @@ use Stancl\Tenancy\Exceptions\TenantDoesNotExistException;
|
||||||
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
|
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
|
||||||
use Stancl\Tenancy\Tenant;
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys, CanFindByAnyKey
|
||||||
{
|
{
|
||||||
/** @var Application */
|
/** @var Application */
|
||||||
protected $app;
|
protected $app;
|
||||||
|
|
@ -89,15 +90,14 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
||||||
*/
|
*/
|
||||||
public function findBy(string $key, $value): Tenant
|
public function findBy(string $key, $value): Tenant
|
||||||
{
|
{
|
||||||
// The key has to be a custom column. It's recommended to set up an index // TODO can we query JSON?
|
$tenant = $this->tenants->findBy($key, $value);
|
||||||
$tenant = $this->tenants->where($key, $value)->first()->toArray();
|
|
||||||
|
|
||||||
if (! $tenant) {
|
if (! $tenant) {
|
||||||
throw new TenantDoesNotExistException($value, $key);
|
throw new TenantDoesNotExistException($value, $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Tenant::fromStorage($tenant->decoded())
|
return Tenant::fromStorage($this->tenants->decodeData($tenant))
|
||||||
->withDomains($this->domains->getTenantDomains($tenant->id));
|
->withDomains($this->domains->getTenantDomains($tenant['id']));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ensureTenantCanBeCreated(Tenant $tenant): void
|
public function ensureTenantCanBeCreated(Tenant $tenant): void
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,18 @@ class TenantRepository extends Repository
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function findBy(string $key, $value)
|
||||||
|
{
|
||||||
|
if (in_array($key, static::customColumns())) {
|
||||||
|
return (array) $this->table()->where($key, $value)->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (array) $this->table()->where(
|
||||||
|
static::dataColumn() . '->' . $key,
|
||||||
|
$value
|
||||||
|
)->first();
|
||||||
|
}
|
||||||
|
|
||||||
public function updateTenant(Tenant $tenant)
|
public function updateTenant(Tenant $tenant)
|
||||||
{
|
{
|
||||||
$this->putMany($tenant->data, $tenant);
|
$this->putMany($tenant->data, $tenant);
|
||||||
|
|
@ -98,11 +110,11 @@ class TenantRepository extends Repository
|
||||||
|
|
||||||
$data = [];
|
$data = [];
|
||||||
$jsonData = json_decode($record->first(static::dataColumn())->data, true);
|
$jsonData = json_decode($record->first(static::dataColumn())->data, true);
|
||||||
foreach ($keys as $key => $key) {
|
foreach ($keys as $key) {
|
||||||
if (in_array($key, static::customColumns())) {
|
if (in_array($key, static::customColumns())) {
|
||||||
$data[$key] = null;
|
$data[$key] = null;
|
||||||
|
|
||||||
return;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
unset($jsonData[$key]);
|
unset($jsonData[$key]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -236,6 +236,6 @@ class RedisStorageDriver implements StorageDriver, CanDeleteKeys
|
||||||
{
|
{
|
||||||
$tenant = $tenant ?? $this->tenant();
|
$tenant = $tenant ?? $this->tenant();
|
||||||
|
|
||||||
$this->redis->hdel("tenants:{$tenant->id}", $keys);
|
$this->redis->hdel("tenants:{$tenant->id}", ...$keys);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -351,24 +351,34 @@ class Tenant implements ArrayAccess
|
||||||
return $this->put($key, $value);
|
return $this->put($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo also deleteKey()?
|
/**
|
||||||
|
* Delete a key from the tenant's storage.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function deleteKey(string $key): self
|
||||||
|
{
|
||||||
|
return $this->deleteKeys([$key]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete keys from the tenant's storage.
|
* Delete keys from the tenant's storage.
|
||||||
*
|
*
|
||||||
* @param string|string[] $keys
|
* @param string[] $keys
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
public function deleteKeys($keys): self
|
public function deleteKeys(array $keys): self
|
||||||
{
|
{
|
||||||
$keys = (array) $keys;
|
|
||||||
|
|
||||||
if (! $this->storage instanceof CanDeleteKeys) {
|
if (! $this->storage instanceof CanDeleteKeys) {
|
||||||
throw new NotImplementedException(get_class($this->storage), 'deleteMany',
|
throw new NotImplementedException(get_class($this->storage), 'deleteMany',
|
||||||
'This method was added to storage drivers provided by the package in 2.2.0 and will be part of the StorageDriver contract in 3.0.0.'
|
'This method was added to storage drivers provided by the package in 2.2.0 and will be part of the StorageDriver contract in 3.0.0.'
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
$this->storage->deleteMany($keys);
|
$this->storage->deleteMany($keys);
|
||||||
|
foreach ($keys as $key) {
|
||||||
|
unset($this->data[$key]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Support\Traits\ForwardsCalls;
|
use Illuminate\Support\Traits\ForwardsCalls;
|
||||||
use Stancl\Tenancy\Contracts\Future\CanFindByAnyKeys;
|
use Stancl\Tenancy\Contracts\Future\CanFindByAnyKey;
|
||||||
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
||||||
use Stancl\Tenancy\Exceptions\NotImplementedException;
|
use Stancl\Tenancy\Exceptions\NotImplementedException;
|
||||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||||
|
|
@ -232,7 +232,7 @@ class TenantManager
|
||||||
throw new Exception('No value supplied.');
|
throw new Exception('No value supplied.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $this->storage instanceof CanFindByAnyKeys) {
|
if (! $this->storage instanceof CanFindByAnyKey) {
|
||||||
throw new NotImplementedException(get_class($this->storage), 'findBy',
|
throw new NotImplementedException(get_class($this->storage), 'findBy',
|
||||||
'This method was added to the DB storage driver provided by the package in 2.2.0 and might be part of the StorageDriver contract in 3.0.0.'
|
'This method was added to the DB storage driver provided by the package in 2.2.0 and might be part of the StorageDriver contract in 3.0.0.'
|
||||||
);
|
);
|
||||||
|
|
|
||||||
45
tests/FutureTest.php
Normal file
45
tests/FutureTest.php
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
|
use Stancl\Tenancy\Contracts\Future\CanFindByAnyKey;
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
|
class FutureTest extends TestCase
|
||||||
|
{
|
||||||
|
public $autoCreateTenant = false;
|
||||||
|
public $autoInitTenancy = false;
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function keys_can_be_deleted_from_tenant_storage()
|
||||||
|
{
|
||||||
|
$tenant = Tenant::new()->withData(['email' => 'foo@example.com', 'role' => 'admin'])->save();
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('email', $tenant->data);
|
||||||
|
$tenant->deleteKey('email');
|
||||||
|
$this->assertArrayNotHasKey('email', $tenant->data);
|
||||||
|
$this->assertArrayNotHasKey('email', tenancy()->all()->first()->data);
|
||||||
|
|
||||||
|
$tenant->put(['foo' => 'bar', 'abc' => 'xyz']);
|
||||||
|
$this->assertArrayHasKey('foo', $tenant->data);
|
||||||
|
$this->assertArrayHasKey('abc', $tenant->data);
|
||||||
|
|
||||||
|
$tenant->deleteKeys(['foo', 'abc']);
|
||||||
|
$this->assertArrayNotHasKey('foo', $tenant->data);
|
||||||
|
$this->assertArrayNotHasKey('abc', $tenant->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function tenant_can_be_identified_using_an_arbitrary_string()
|
||||||
|
{
|
||||||
|
if (! tenancy()->storage instanceof CanFindByAnyKey) {
|
||||||
|
$this->markTestSkipped(get_class(tenancy()->storage) . ' does not implement the CanFindByAnyKey interface.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$tenant = Tenant::new()->withData(['email' => 'foo@example.com'])->save();
|
||||||
|
|
||||||
|
$this->assertSame($tenant->id, tenancy()->findByEmail('foo@example.com')->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue