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

[2.2.0] [WIP] Add functionality (#206)

* TenantDatabaseDoesNotExistException

* Apply fixes from StyleCI

* User post-creation callbacks

* Rename method

* postCreationActions

* pass $tenant as parameter

* pass $tenant to async actions

* WIP findBy()

* findBy\* ForwardsCalls

* Apply fixes from StyleCI

* findBy DB storage driver

* Redis  SD TODO message

* Apply fixes from StyleCI

* Fix chained jobs

* WIP event system

* import str

* instanceof closure check

* findBy instead of find

* Tenant -> Tenants

* dots

* Use DB hooks instead of a SC key

* Don't allow  callables for queue chain

* CanDeleteKeys interface

* Apply fixes from StyleCI

* CanFindByAnyKey interface

* Apply fixes from StyleCI

* Ditch models for custom repositories

* Resolve circular dependency

* Apply fixes from StyleCI

* Fix tests

* Apply fixes from StyleCI

* FutureTest

* Prefix tenant events with 'tenant.'

* Event listener arguments test
This commit is contained in:
Samuel Štancl 2019-10-27 21:10:41 +01:00 committed by GitHub
parent 7389f44de9
commit 869ac32983
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 654 additions and 254 deletions

View file

@ -8,15 +8,19 @@ use Stancl\Tenancy\Tenant;
class CacheManagerTest extends TestCase
{
public $autoInitTenancy = false;
/** @test */
public function default_tag_is_automatically_applied()
{
$this->initTenancy();
$this->assertArrayIsSubset([config('tenancy.cache.tag_base') . tenant('id')], cache()->tags('foo')->getTags()->getNames());
}
/** @test */
public function tags_are_merged_when_array_is_passed()
{
$this->initTenancy();
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo', 'bar'];
$this->assertEquals($expected, cache()->tags(['foo', 'bar'])->getTags()->getNames());
}
@ -24,6 +28,7 @@ class CacheManagerTest extends TestCase
/** @test */
public function tags_are_merged_when_string_is_passed()
{
$this->initTenancy();
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo'];
$this->assertEquals($expected, cache()->tags('foo')->getTags()->getNames());
}
@ -31,6 +36,7 @@ class CacheManagerTest extends TestCase
/** @test */
public function exception_is_thrown_when_zero_arguments_are_passed_to_tags_method()
{
$this->initTenancy();
$this->expectException(\Exception::class);
cache()->tags();
}
@ -38,6 +44,7 @@ class CacheManagerTest extends TestCase
/** @test */
public function exception_is_thrown_when_more_than_one_argument_is_passed_to_tags_method()
{
$this->initTenancy();
$this->expectException(\Exception::class);
cache()->tags(1, 2);
}

45
tests/FutureTest.php Normal file
View 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);
}
}

View file

@ -320,4 +320,14 @@ class TenantManagerTest extends TestCase
$this->expectException(TenantDoesNotExistException::class);
tenancy()->find('gjnfdgf');
}
/** @test */
public function event_listeners_can_accept_arguments()
{
tenancy()->hook('tenant.creating', function ($tenantManager, $tenant) {
$this->assertSame('bar', $tenant->foo);
});
Tenant::new()->withData(['foo' => 'bar'])->save();
}
}

View file

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Stancl\Tenancy\StorageDrivers\Database\TenantModel;
use Stancl\Tenancy\StorageDrivers\Database\TenantRepository;
use Stancl\Tenancy\Tenant;
class TenantStorageTest extends TestCase
@ -112,10 +112,11 @@ class TenantStorageTest extends TestCase
}
/** @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']);
$this->assertSame('foo', (new TenantModel)->getConnectionName());
$this->assertSame('foo', app(TenantRepository::class)->database->getName());
}
/** @test */
@ -156,6 +157,9 @@ class TenantStorageTest extends TestCase
tenancy()->create(['foo.localhost']);
tenancy()->init('foo.localhost');
tenant()->put('foo', '111');
$this->assertSame('111', tenant()->get('foo'));
tenant()->put(['foo' => 'bar', 'abc' => 'xyz']);
$this->assertSame(['foo' => 'bar', 'abc' => 'xyz'], tenant()->get(['foo', 'abc']));