mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 12:54:05 +00:00
Fix custom columns, add tests (#122)
* Fix custom columns & add test * Apply fixes from StyleCI
This commit is contained in:
parent
6de9ee9421
commit
f4421af6c5
3 changed files with 55 additions and 1 deletions
|
|
@ -90,7 +90,7 @@ class Tenant extends Model
|
||||||
|
|
||||||
public function put(string $key, $value)
|
public function put(string $key, $value)
|
||||||
{
|
{
|
||||||
if (\array_key_exists($key, $this->customColumns())) {
|
if (\in_array($key, $this->customColumns())) {
|
||||||
$this->update([$key => $value]);
|
$this->update([$key => $value]);
|
||||||
} else {
|
} else {
|
||||||
$obj = \json_decode($this->{$this->dataColumn()});
|
$obj = \json_decode($this->{$this->dataColumn()});
|
||||||
|
|
|
||||||
26
tests/Etc/2019_08_08_000001_add_custom_column.php
Normal file
26
tests/Etc/2019_08_08_000001_add_custom_column.php
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
class AddCustomColumn extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('tenants', function (Blueprint $table) {
|
||||||
|
$table->string('foo')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -170,4 +170,32 @@ class TenantStorageTest extends TestCase
|
||||||
$this->assertSame('bar', tenancy()->get('foo'));
|
$this->assertSame('bar', tenancy()->get('foo'));
|
||||||
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue