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

Add tests, fix bugs

This commit is contained in:
Samuel Štancl 2019-02-08 15:28:32 +01:00
parent 3857c4a27d
commit f05a4bca41
6 changed files with 122 additions and 16 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
.vscode
.env .env
composer.lock composer.lock
vendor/ vendor/

View file

@ -212,7 +212,7 @@ class TenantManager
return $this->storage->getMany($uuid, $key); return $this->storage->getMany($uuid, $key);
} }
return $this->storage->get($this->tenant['uuid'], $key); return $this->storage->get($uuid, $key);
} }
/** /**
@ -247,7 +247,11 @@ class TenantManager
throw new \Exception("No value supplied for key $key."); throw new \Exception("No value supplied for key $key.");
} }
return $target[$key] = $this->storage->putMany($uuid, $key); foreach ($this->storage->putMany($uuid, $key) as $key => $value) {
$target[$key] = $value;
}
return $key;
} }
/** /**
@ -260,9 +264,7 @@ class TenantManager
*/ */
public function set($key, $value = null, string $uuid = null) public function set($key, $value = null, string $uuid = null)
{ {
$uuid = $uuid ?: $this->tenant['uuid']; return $this->put($key, $value, $uuid);
return $this->put($this->put($key, $value));
} }
/** /**

View file

@ -6,7 +6,7 @@ use Illuminate\Support\Facades\Redis;
class BootstrapsTenancyTest extends TestCase class BootstrapsTenancyTest extends TestCase
{ {
public $initTenancy = false; public $autoInitTenancy = false;
/** @test */ /** @test */
public function database_connection_is_switched() public function database_connection_is_switched()

View file

@ -0,0 +1,21 @@
<?php
namespace Stancl\Tenancy\Tests;
class TenantManagerTest extends TestCase
{
public $autoCreateTenant = false;
public $autoInitTenancy = false;
/** @test */
public function current_tenant_is_stored_in_the_tenant_property()
{
$tenant = tenant()->create('localhost');
tenancy()->init('localhost');
$this->assertSame($tenant, tenancy()->tenant);
}
// todo write more tests
}

View file

@ -0,0 +1,74 @@
<?php
namespace Stancl\Tenancy\Tests;
use Stancl\Tenancy\Interfaces\StorageDriver;
class TenantStorageTest extends TestCase
{
public function setUp()
{
parent::setUp();
// todo find a way to run this for each storage driver (once there are more of them)
$this->storage = app(StorageDriver::class);
}
/** @test */
public function put_works_with_key_and_value_as_separate_args()
{
tenancy()->put('foo', 'bar');
$this->assertSame('bar', $this->storage->get(tenant('uuid'), 'foo'));
}
/** @test */
public function put_works_with_key_and_value_as_a_single_arg()
{
$keys = ['foo', 'abc'];
$vals = ['bar', 'xyz'];
$data = array_combine($keys, $vals);
tenancy()->put($data);
$this->assertSame($vals, tenant()->get($keys));
}
/** @test */
public function put_on_the_current_tenant_pushes_the_value_into_the_tenant_property_array()
{
tenancy()->put('foo', 'bar');
$this->assertSame('bar', tenancy()->tenant['foo']);
}
/** @test */
public function put_works_on_a_tenant_different_than_the_current_one_when_two_args_are_used()
{
$tenant = tenant()->create('second.localhost');
$uuid = $tenant['uuid'];
tenancy()->put('foo', 'bar', $uuid);
$this->assertSame('bar', tenancy()->get('foo', $uuid));
$this->assertNotSame('bar', tenant('foo'));
}
/** @test */
public function put_works_on_a_tenant_different_than_the_current_one_when_a_single_arg_is_used()
{
$tenant = tenant()->create('second.localhost');
$uuid = $tenant['uuid'];
$keys = ['foo', 'abc'];
$vals = ['bar', 'xyz'];
$data = array_combine($keys, $vals);
tenancy()->put($data, null, $uuid);
$this->assertSame($vals, tenancy()->get($keys, $uuid));
$this->assertNotSame($vals, tenancy()->get($keys));
$this->assertFalse(array_intersect($data, tenant()->tenant) == $data); // assert array not subset
}
}

View file

@ -6,7 +6,8 @@ use Illuminate\Support\Facades\Redis;
class TestCase extends \Orchestra\Testbench\TestCase class TestCase extends \Orchestra\Testbench\TestCase
{ {
public $initTenancy = true; public $autoCreateTenant = true;
public $autoInitTenancy = true;
/** /**
* Setup the test environment * Setup the test environment
@ -19,11 +20,23 @@ class TestCase extends \Orchestra\Testbench\TestCase
Redis::connection('tenancy')->flushdb(); Redis::connection('tenancy')->flushdb();
tenant()->create('localhost'); if ($this->autoCreateTenant) {
$this->createTenant();
if ($this->initTenancy) {
tenancy()->init('localhost');
} }
if ($this->autoInitTenancy) {
$this->initTenancy();
}
}
public function createTenant($domain = 'localhost')
{
tenant()->create($domain);
}
public function initTenancy($domain = 'localhost')
{
tenancy()->init($domain);
} }
/** /**
@ -84,9 +97,4 @@ class TestCase extends \Orchestra\Testbench\TestCase
// set one of these environment vars on their computer. // set one of these environment vars on their computer.
return env('CI') && env('TRAVIS') && env('CONTINUOUS_INTEGRATION'); return env('CI') && env('TRAVIS') && env('CONTINUOUS_INTEGRATION');
} }
public function initTenancy($domain = 'localhost')
{
tenancy()->init($domain);
}
} }