From f4421af6c52718bd6f630cfd1a7204b3b6045c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 4 Sep 2019 16:53:01 +0200 Subject: [PATCH] Fix custom columns, add tests (#122) * Fix custom columns & add test * Apply fixes from StyleCI --- src/Tenant.php | 2 +- .../2019_08_08_000001_add_custom_column.php | 26 +++++++++++++++++ tests/TenantStorageTest.php | 28 +++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/Etc/2019_08_08_000001_add_custom_column.php diff --git a/src/Tenant.php b/src/Tenant.php index 4c9cb04d..b9d612d3 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -90,7 +90,7 @@ class Tenant extends Model public function put(string $key, $value) { - if (\array_key_exists($key, $this->customColumns())) { + if (\in_array($key, $this->customColumns())) { $this->update([$key => $value]); } else { $obj = \json_decode($this->{$this->dataColumn()}); diff --git a/tests/Etc/2019_08_08_000001_add_custom_column.php b/tests/Etc/2019_08_08_000001_add_custom_column.php new file mode 100644 index 00000000..5b5ac3f4 --- /dev/null +++ b/tests/Etc/2019_08_08_000001_add_custom_column.php @@ -0,0 +1,26 @@ +string('foo')->nullable(); + }); + } + + public function down() + { + } +} diff --git a/tests/TenantStorageTest.php b/tests/TenantStorageTest.php index 86230f1f..f5f2dcee 100644 --- a/tests/TenantStorageTest.php +++ b/tests/TenantStorageTest.php @@ -170,4 +170,32 @@ class TenantStorageTest extends TestCase $this->assertSame('bar', tenancy()->get('foo')); $this->assertSame(['bar'], tenancy()->get(['foo'])); } + + /** @test */ + public function custom_columns_work_with_db_storage_driver() + { + if (config('tenancy.storage_driver') != 'Stancl\Tenancy\StorageDrivers\DatabaseStorageDriver') { + $this->markTestSkipped(); + } + + tenancy()->end(); + + $this->loadMigrationsFrom([ + '--path' => __DIR__ . '/Etc', + '--database' => 'central', + ]); + config(['database.default' => 'sqlite']); // fix issue caused by loadMigrationsFrom + + config(['tenancy.storage.db.custom_columns' => [ + 'foo', + ]]); + + tenancy()->create('foo.localhost'); + tenancy()->init('foo.localhost'); + + tenancy()->put(['foo' => 'bar', 'abc' => 'xyz']); + $this->assertSame(['bar', 'xyz'], tenancy()->get(['foo', 'abc'])); + + $this->assertSame('bar', \DB::connection('central')->table('tenants')->where('uuid', tenant('uuid'))->first()->foo); + } }