mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:34:04 +00:00
Fix some tests
This commit is contained in:
parent
93d2a281eb
commit
50a77ee826
10 changed files with 76 additions and 61 deletions
|
|
@ -40,8 +40,9 @@
|
||||||
"Stancl\\Tenancy\\TenancyServiceProvider"
|
"Stancl\\Tenancy\\TenancyServiceProvider"
|
||||||
],
|
],
|
||||||
"aliases": {
|
"aliases": {
|
||||||
"Tenancy": "Stancl\\Tenancy\\Facades\\Tenancy",
|
"Tenant": "Stancl\\Tenancy\\Facades\\TenantFacade",
|
||||||
"GlobalCache": "Stancl\\Tenancy\\Facades\\GlobalCache"
|
"Tenancy": "Stancl\\Tenancy\\Facades\\TenancyFacade",
|
||||||
|
"GlobalCache": "Stancl\\Tenancy\\Facades\\GlobalCacheFacade"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ namespace Stancl\Tenancy\Facades;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Facade;
|
use Illuminate\Support\Facades\Facade;
|
||||||
|
|
||||||
class GlobalCache extends Facade
|
class GlobalCacheFacade extends Facade
|
||||||
{
|
{
|
||||||
protected static function getFacadeAccessor()
|
protected static function getFacadeAccessor()
|
||||||
{
|
{
|
||||||
|
|
@ -7,7 +7,7 @@ namespace Stancl\Tenancy\Facades;
|
||||||
use Illuminate\Support\Facades\Facade;
|
use Illuminate\Support\Facades\Facade;
|
||||||
use Stancl\Tenancy\TenantManager;
|
use Stancl\Tenancy\TenantManager;
|
||||||
|
|
||||||
class Tenancy extends Facade
|
class TenancyFacade extends Facade
|
||||||
{
|
{
|
||||||
protected static function getFacadeAccessor()
|
protected static function getFacadeAccessor()
|
||||||
{
|
{
|
||||||
16
src/Facades/TenantFacade.php
Normal file
16
src/Facades/TenantFacade.php
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Facades;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Facade;
|
||||||
|
use Stancl\Tenancy\Tenant as Tenant;
|
||||||
|
|
||||||
|
class TenantFacade extends Facade
|
||||||
|
{
|
||||||
|
protected static function getFacadeAccessor()
|
||||||
|
{
|
||||||
|
return Tenant::class;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -77,6 +77,11 @@ class Tenant implements ArrayAccess
|
||||||
return static::new()->withData($data)->persisted(true);
|
return static::new()->withData($data)->persisted(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function create($domains, array $data = []): self
|
||||||
|
{
|
||||||
|
return static::new()->withDomains((array) $domains)->withData($data)->save();
|
||||||
|
}
|
||||||
|
|
||||||
protected function persisted($persisted = null)
|
protected function persisted($persisted = null)
|
||||||
{
|
{
|
||||||
if (gettype($persisted) === 'bool') {
|
if (gettype($persisted) === 'bool') {
|
||||||
|
|
@ -132,7 +137,7 @@ class Tenant implements ArrayAccess
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function withData($data): self
|
public function withData(array $data): self
|
||||||
{
|
{
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
|
|
||||||
|
|
@ -234,7 +239,7 @@ class Tenant implements ArrayAccess
|
||||||
|
|
||||||
public function put($key, $value = null): self
|
public function put($key, $value = null): self
|
||||||
{
|
{
|
||||||
if ($this->storage->getIdKey() === $key) {
|
if ($key === 'id') {
|
||||||
throw new TenantStorageException("The tenant's id can't be changed.");
|
throw new TenantStorageException("The tenant's id can't be changed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,8 @@ if (! \function_exists('tenant')) {
|
||||||
if (! is_null($key)) {
|
if (! is_null($key)) {
|
||||||
return app(Tenant::class)->get($key);
|
return app(Tenant::class)->get($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return app(Tenant::class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Redis;
|
use Illuminate\Support\Facades\Redis;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
class DataSeparationTest extends TestCase
|
class DataSeparationTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
@ -18,8 +19,8 @@ class DataSeparationTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function databases_are_separated()
|
public function databases_are_separated()
|
||||||
{
|
{
|
||||||
$tenant1 = tenancy()->create('tenant1.localhost');
|
$tenant1 = Tenant::create('tenant1.localhost');
|
||||||
$tenant2 = tenancy()->create('tenant2.localhost');
|
$tenant2 = Tenant::create('tenant2.localhost');
|
||||||
\Artisan::call('tenants:migrate', [
|
\Artisan::call('tenants:migrate', [
|
||||||
'--tenants' => [$tenant1['id'], $tenant2['id']],
|
'--tenants' => [$tenant1['id'], $tenant2['id']],
|
||||||
]);
|
]);
|
||||||
|
|
@ -52,7 +53,7 @@ class DataSeparationTest extends TestCase
|
||||||
$this->assertSame('foo', User::first()->name);
|
$this->assertSame('foo', User::first()->name);
|
||||||
$this->assertSame('foo@bar.com', User::first()->email);
|
$this->assertSame('foo@bar.com', User::first()->email);
|
||||||
|
|
||||||
$tenant3 = tenancy()->create('tenant3.localhost');
|
$tenant3 = Tenant::create('tenant3.localhost');
|
||||||
\Artisan::call('tenants:migrate', [
|
\Artisan::call('tenants:migrate', [
|
||||||
'--tenants' => [$tenant1['id'], $tenant3['id']],
|
'--tenants' => [$tenant1['id'], $tenant3['id']],
|
||||||
]);
|
]);
|
||||||
|
|
@ -72,8 +73,8 @@ class DataSeparationTest extends TestCase
|
||||||
$this->markTestSkipped('Redis tenancy disabled.');
|
$this->markTestSkipped('Redis tenancy disabled.');
|
||||||
}
|
}
|
||||||
|
|
||||||
tenancy()->create('tenant1.localhost');
|
Tenant::create('tenant1.localhost');
|
||||||
tenancy()->create('tenant2.localhost');
|
Tenant::create('tenant2.localhost');
|
||||||
|
|
||||||
tenancy()->init('tenant1.localhost');
|
tenancy()->init('tenant1.localhost');
|
||||||
Redis::set('foo', 'bar');
|
Redis::set('foo', 'bar');
|
||||||
|
|
@ -90,7 +91,7 @@ class DataSeparationTest extends TestCase
|
||||||
$this->assertSame('bar', Redis::get('foo'));
|
$this->assertSame('bar', Redis::get('foo'));
|
||||||
$this->assertSame(null, Redis::get('abc'));
|
$this->assertSame(null, Redis::get('abc'));
|
||||||
|
|
||||||
tenancy()->create('tenant3.localhost');
|
Tenant::create('tenant3.localhost');
|
||||||
tenancy()->init('tenant3.localhost');
|
tenancy()->init('tenant3.localhost');
|
||||||
$this->assertSame(null, Redis::get('foo'));
|
$this->assertSame(null, Redis::get('foo'));
|
||||||
$this->assertSame(null, Redis::get('abc'));
|
$this->assertSame(null, Redis::get('abc'));
|
||||||
|
|
@ -99,8 +100,8 @@ class DataSeparationTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function cache_is_separated()
|
public function cache_is_separated()
|
||||||
{
|
{
|
||||||
tenancy()->create('tenant1.localhost');
|
Tenant::create('tenant1.localhost');
|
||||||
tenancy()->create('tenant2.localhost');
|
Tenant::create('tenant2.localhost');
|
||||||
|
|
||||||
tenancy()->init('tenant1.localhost');
|
tenancy()->init('tenant1.localhost');
|
||||||
Cache::put('foo', 'bar', 60);
|
Cache::put('foo', 'bar', 60);
|
||||||
|
|
@ -117,7 +118,7 @@ class DataSeparationTest extends TestCase
|
||||||
$this->assertSame('bar', Cache::get('foo'));
|
$this->assertSame('bar', Cache::get('foo'));
|
||||||
$this->assertSame(null, Cache::get('abc'));
|
$this->assertSame(null, Cache::get('abc'));
|
||||||
|
|
||||||
tenancy()->create('tenant3.localhost');
|
Tenant::create('tenant3.localhost');
|
||||||
tenancy()->init('tenant3.localhost');
|
tenancy()->init('tenant3.localhost');
|
||||||
$this->assertSame(null, Cache::get('foo'));
|
$this->assertSame(null, Cache::get('foo'));
|
||||||
$this->assertSame(null, Cache::get('abc'));
|
$this->assertSame(null, Cache::get('abc'));
|
||||||
|
|
@ -126,8 +127,8 @@ class DataSeparationTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function filesystem_is_separated()
|
public function filesystem_is_separated()
|
||||||
{
|
{
|
||||||
tenancy()->create('tenant1.localhost');
|
Tenant::create('tenant1.localhost');
|
||||||
tenancy()->create('tenant2.localhost');
|
Tenant::create('tenant2.localhost');
|
||||||
|
|
||||||
tenancy()->init('tenant1.localhost');
|
tenancy()->init('tenant1.localhost');
|
||||||
Storage::disk('public')->put('foo', 'bar');
|
Storage::disk('public')->put('foo', 'bar');
|
||||||
|
|
@ -144,7 +145,7 @@ class DataSeparationTest extends TestCase
|
||||||
$this->assertSame('bar', Storage::disk('public')->get('foo'));
|
$this->assertSame('bar', Storage::disk('public')->get('foo'));
|
||||||
$this->assertFalse(Storage::disk('public')->exists('abc'));
|
$this->assertFalse(Storage::disk('public')->exists('abc'));
|
||||||
|
|
||||||
tenancy()->create('tenant3.localhost');
|
Tenant::create('tenant3.localhost');
|
||||||
tenancy()->init('tenant3.localhost');
|
tenancy()->init('tenant3.localhost');
|
||||||
$this->assertFalse(Storage::disk('public')->exists('foo'));
|
$this->assertFalse(Storage::disk('public')->exists('foo'));
|
||||||
$this->assertFalse(Storage::disk('public')->exists('abc'));
|
$this->assertFalse(Storage::disk('public')->exists('abc'));
|
||||||
|
|
|
||||||
|
|
@ -4,25 +4,14 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
use Tenancy;
|
|
||||||
use Tenant;
|
use Tenant;
|
||||||
|
|
||||||
class FacadeTest extends TestCase
|
class FacadeTest extends TestCase
|
||||||
{
|
{
|
||||||
/** @test */
|
|
||||||
public function tenant_manager_can_be_accessed_using_the_Tenancy_facade()
|
|
||||||
{
|
|
||||||
tenancy()->put('foo', 'bar');
|
|
||||||
Tenancy::put('abc', 'xyz');
|
|
||||||
|
|
||||||
$this->assertSame('bar', Tenancy::get('foo'));
|
|
||||||
$this->assertSame('xyz', Tenancy::get('abc'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function tenant_manager_can_be_accessed_using_the_Tenant_facade()
|
public function tenant_manager_can_be_accessed_using_the_Tenant_facade()
|
||||||
{
|
{
|
||||||
tenancy()->put('foo', 'bar');
|
tenant()->put('foo', 'bar');
|
||||||
Tenant::put('abc', 'xyz');
|
Tenant::put('abc', 'xyz');
|
||||||
|
|
||||||
$this->assertSame('bar', Tenant::get('foo'));
|
$this->assertSame('bar', Tenant::get('foo'));
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ class TenantStorageTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function put_works_with_key_and_value_as_separate_args()
|
public function put_works_with_key_and_value_as_separate_args()
|
||||||
{
|
{
|
||||||
tenancy()->put('foo', 'bar');
|
tenant()->put('foo', 'bar');
|
||||||
|
|
||||||
$this->assertSame('bar', tenant()->get('foo'));
|
$this->assertSame('bar', tenant()->get('foo'));
|
||||||
}
|
}
|
||||||
|
|
@ -46,7 +46,7 @@ class TenantStorageTest extends TestCase
|
||||||
$vals = ['bar', 'xyz'];
|
$vals = ['bar', 'xyz'];
|
||||||
$data = \array_combine($keys, $vals);
|
$data = \array_combine($keys, $vals);
|
||||||
|
|
||||||
tenancy()->put($data);
|
tenant()->put($data);
|
||||||
|
|
||||||
$this->assertSame($vals, tenant()->get($keys));
|
$this->assertSame($vals, tenant()->get($keys));
|
||||||
}
|
}
|
||||||
|
|
@ -54,7 +54,7 @@ class TenantStorageTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function put_on_the_current_tenant_pushes_the_value_into_the_tenant_property_array()
|
public function put_on_the_current_tenant_pushes_the_value_into_the_tenant_property_array()
|
||||||
{
|
{
|
||||||
tenancy()->put('foo', 'bar');
|
tenant()->put('foo', 'bar');
|
||||||
|
|
||||||
$this->assertSame('bar', tenancy()->tenant['foo']);
|
$this->assertSame('bar', tenancy()->tenant['foo']);
|
||||||
}
|
}
|
||||||
|
|
@ -65,9 +65,9 @@ class TenantStorageTest extends TestCase
|
||||||
$tenant = Tenant::new()->withDomains(['second.localhost'])->save();
|
$tenant = Tenant::new()->withDomains(['second.localhost'])->save();
|
||||||
$id = $tenant['id'];
|
$id = $tenant['id'];
|
||||||
|
|
||||||
tenancy()->put('foo', 'bar', $id);
|
tenant()->put('foo', 'bar', $id);
|
||||||
|
|
||||||
$this->assertSame('bar', tenancy()->get('foo', $id));
|
$this->assertSame('bar', tenant()->get('foo', $id));
|
||||||
$this->assertNotSame('bar', tenant('foo'));
|
$this->assertNotSame('bar', tenant('foo'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,10 +81,10 @@ class TenantStorageTest extends TestCase
|
||||||
$vals = ['bar', 'xyz'];
|
$vals = ['bar', 'xyz'];
|
||||||
$data = \array_combine($keys, $vals);
|
$data = \array_combine($keys, $vals);
|
||||||
|
|
||||||
tenancy()->put($data, null, $id);
|
tenant()->put($data, null, $id);
|
||||||
|
|
||||||
$this->assertSame($vals, tenancy()->get($keys, $id));
|
$this->assertSame($vals, tenant()->get($keys, $id));
|
||||||
$this->assertNotSame($vals, tenancy()->get($keys));
|
$this->assertNotSame($vals, tenant()->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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -116,7 +116,7 @@ class TenantStorageTest extends TestCase
|
||||||
{
|
{
|
||||||
$value = ['foo' => 'bar', 'abc' => 'xyz'];
|
$value = ['foo' => 'bar', 'abc' => 'xyz'];
|
||||||
|
|
||||||
$this->assertSame($value, tenancy()->put($value));
|
$this->assertSame($value, tenant()->put($value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
@ -132,21 +132,21 @@ class TenantStorageTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function data_is_stored_with_correct_data_types()
|
public function data_is_stored_with_correct_data_types()
|
||||||
{
|
{
|
||||||
tenancy()->put('someBool', false);
|
tenant()->put('someBool', false);
|
||||||
$this->assertSame('boolean', \gettype(tenancy()->get('someBool')));
|
$this->assertSame('boolean', \gettype(tenant()->get('someBool')));
|
||||||
$this->assertSame('boolean', \gettype(tenancy()->get(['someBool'])[0]));
|
$this->assertSame('boolean', \gettype(tenant()->get(['someBool'])[0]));
|
||||||
|
|
||||||
tenancy()->put('someInt', 5);
|
tenant()->put('someInt', 5);
|
||||||
$this->assertSame('integer', \gettype(tenancy()->get('someInt')));
|
$this->assertSame('integer', \gettype(tenant()->get('someInt')));
|
||||||
$this->assertSame('integer', \gettype(tenancy()->get(['someInt'])[0]));
|
$this->assertSame('integer', \gettype(tenant()->get(['someInt'])[0]));
|
||||||
|
|
||||||
tenancy()->put('someDouble', 11.40);
|
tenant()->put('someDouble', 11.40);
|
||||||
$this->assertSame('double', \gettype(tenancy()->get('someDouble')));
|
$this->assertSame('double', \gettype(tenant()->get('someDouble')));
|
||||||
$this->assertSame('double', \gettype(tenancy()->get(['someDouble'])[0]));
|
$this->assertSame('double', \gettype(tenant()->get(['someDouble'])[0]));
|
||||||
|
|
||||||
tenancy()->put('string', 'foo');
|
tenant()->put('string', 'foo');
|
||||||
$this->assertSame('string', \gettype(tenancy()->get('string')));
|
$this->assertSame('string', \gettype(tenant()->get('string')));
|
||||||
$this->assertSame('string', \gettype(tenancy()->get(['string'])[0]));
|
$this->assertSame('string', \gettype(tenant()->get(['string'])[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
@ -162,14 +162,14 @@ class TenantStorageTest extends TestCase
|
||||||
Tenant::new()->withDomains(['foo.localhost'])->save();
|
Tenant::new()->withDomains(['foo.localhost'])->save();
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
|
|
||||||
tenancy()->put('foo', 'bar');
|
tenant()->put('foo', 'bar');
|
||||||
$this->assertSame('bar', tenancy()->get('foo'));
|
$this->assertSame('bar', tenant()->get('foo'));
|
||||||
$this->assertSame(['bar'], tenancy()->get(['foo']));
|
$this->assertSame(['bar'], tenant()->get(['foo']));
|
||||||
|
|
||||||
tenancy()->endTenancy();
|
tenancy()->endTenancy();
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
$this->assertSame('bar', tenancy()->get('foo'));
|
$this->assertSame('bar', tenant()->get('foo'));
|
||||||
$this->assertSame(['bar'], tenancy()->get(['foo']));
|
$this->assertSame(['bar'], tenant()->get(['foo']));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
@ -191,11 +191,11 @@ class TenantStorageTest extends TestCase
|
||||||
'foo',
|
'foo',
|
||||||
]]);
|
]]);
|
||||||
|
|
||||||
tenancy()->create('foo.localhost');
|
tenant()->create(['foo.localhost']);
|
||||||
tenancy()->init('foo.localhost');
|
tenancy()->init('foo.localhost');
|
||||||
|
|
||||||
tenancy()->put(['foo' => 'bar', 'abc' => 'xyz']);
|
tenant()->put(['foo' => 'bar', 'abc' => 'xyz']);
|
||||||
$this->assertSame(['bar', 'xyz'], tenancy()->get(['foo', 'abc']));
|
$this->assertSame(['bar', 'xyz'], tenant()->get(['foo', 'abc']));
|
||||||
|
|
||||||
$this->assertSame('bar', \DB::connection('central')->table('tenants')->where('id', tenant('id'))->first()->foo);
|
$this->assertSame('bar', \DB::connection('central')->table('tenants')->where('id', tenant('id'))->first()->foo);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -126,8 +126,9 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
protected function getPackageAliases($app)
|
protected function getPackageAliases($app)
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'Tenancy' => \Stancl\Tenancy\Facades\Tenancy::class,
|
'Tenant' => \Stancl\Tenancy\Facades\TenantFacade::class,
|
||||||
'GlobalCache' => \Stancl\Tenancy\Facades\GlobalCache::class,
|
'Tenancy' => \Stancl\Tenancy\Facades\TenancyFacade::class,
|
||||||
|
'GlobalCache' => \Stancl\Tenancy\Facades\GlobalCacheFacade::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue