mirror of
https://github.com/archtechx/virtualcolumn.git
synced 2025-12-16 18:04:03 +00:00
add virtual columns array
This commit is contained in:
parent
ab3f943990
commit
6ca7c419c2
3 changed files with 101 additions and 1 deletions
|
|
@ -57,6 +57,49 @@ class VirtualColumnTest extends TestCase
|
|||
$this->assertSame(null, $model->data);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function keys_defined_in_the_virtual_columns_function_go_into_data_json_column()
|
||||
{
|
||||
$model = VirtualModel::create([
|
||||
'id' => 1,
|
||||
'foo' => 'bar',
|
||||
]);
|
||||
|
||||
// Test that model works correctly
|
||||
$this->assertSame(1, $model->id);
|
||||
$this->assertSame('bar', $model->foo);
|
||||
$this->assertSame(null, $model->data);
|
||||
|
||||
// Low level test to assert database structure
|
||||
$this->assertSame(['foo' => 'bar'], json_decode(DB::table('virtual_models')->where('id', $model->id)->first()->data, true));
|
||||
$this->assertSame(null, DB::table('virtual_models')->where('id', $model->id)->first()->foo ?? null);
|
||||
|
||||
// Model has the correct structure when retrieved
|
||||
$model = VirtualModel::first();
|
||||
$this->assertSame('bar', $model->foo);
|
||||
$this->assertSame('bar', $model->getOriginal('foo'));
|
||||
$this->assertSame(null, $model->data);
|
||||
|
||||
// Model can be updated
|
||||
$model->update([
|
||||
'foo' => 'baz',
|
||||
'abc' => 'xyz',
|
||||
]);
|
||||
|
||||
$this->assertSame('baz', $model->foo);
|
||||
$this->assertSame('baz', $model->getOriginal('foo'));
|
||||
$this->assertSame('xyz', $model->abc);
|
||||
$this->assertSame('xyz', $model->getOriginal('abc'));
|
||||
$this->assertSame(null, $model->data);
|
||||
|
||||
// Model can be retrieved after update & is structure correctly
|
||||
$model = VirtualModel::first();
|
||||
|
||||
$this->assertSame('baz', $model->foo);
|
||||
$this->assertSame('xyz', $model->abc);
|
||||
$this->assertSame(null, $model->data);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function model_is_always_decoded_when_accessed_by_user_event()
|
||||
{
|
||||
|
|
@ -125,6 +168,24 @@ class MyModel extends Model
|
|||
}
|
||||
}
|
||||
|
||||
class VirtualModel extends Model
|
||||
{
|
||||
use VirtualColumn;
|
||||
|
||||
protected $guarded = [];
|
||||
public $timestamps = false;
|
||||
|
||||
public static function getVirtualColumns(): array
|
||||
{
|
||||
return [
|
||||
'foo',
|
||||
'abc',
|
||||
'baz',
|
||||
'xyz',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class FooModel extends Model
|
||||
{
|
||||
use VirtualColumn;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue