diff --git a/.gitignore b/.gitignore index 6733b4e1..f22f138a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.vscode .env composer.lock vendor/ diff --git a/src/TenantManager.php b/src/TenantManager.php index 57c66c04..cded69ab 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -212,7 +212,7 @@ class TenantManager 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."); } - 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) { - $uuid = $uuid ?: $this->tenant['uuid']; - - return $this->put($this->put($key, $value)); + return $this->put($key, $value, $uuid); } /** diff --git a/tests/BootstrapsTenancyTest.php b/tests/BootstrapsTenancyTest.php index ec31d4ea..8950a5ab 100644 --- a/tests/BootstrapsTenancyTest.php +++ b/tests/BootstrapsTenancyTest.php @@ -6,7 +6,7 @@ use Illuminate\Support\Facades\Redis; class BootstrapsTenancyTest extends TestCase { - public $initTenancy = false; + public $autoInitTenancy = false; /** @test */ public function database_connection_is_switched() diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php new file mode 100644 index 00000000..e350427c --- /dev/null +++ b/tests/TenantManagerTest.php @@ -0,0 +1,21 @@ +create('localhost'); + + tenancy()->init('localhost'); + + $this->assertSame($tenant, tenancy()->tenant); + } + + // todo write more tests +} diff --git a/tests/TenantStorageTest.php b/tests/TenantStorageTest.php new file mode 100644 index 00000000..65c1aee4 --- /dev/null +++ b/tests/TenantStorageTest.php @@ -0,0 +1,74 @@ +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 + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 9312b19b..f28335ff 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,7 +6,8 @@ use Illuminate\Support\Facades\Redis; class TestCase extends \Orchestra\Testbench\TestCase { - public $initTenancy = true; + public $autoCreateTenant = true; + public $autoInitTenancy = true; /** * Setup the test environment @@ -19,11 +20,23 @@ class TestCase extends \Orchestra\Testbench\TestCase Redis::connection('tenancy')->flushdb(); - tenant()->create('localhost'); - - if ($this->initTenancy) { - tenancy()->init('localhost'); + if ($this->autoCreateTenant) { + $this->createTenant(); } + + 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. return env('CI') && env('TRAVIS') && env('CONTINUOUS_INTEGRATION'); } - - public function initTenancy($domain = 'localhost') - { - tenancy()->init($domain); - } }