mirror of
https://github.com/archtechx/virtualcolumn.git
synced 2025-12-12 08:04:04 +00:00
Test that child models extending a parent that uses VirtualColumn get encoded correctly (#16)
* Test that using different models extending the same class with VirtualColumn doesn't work correctly * Add regression test for faulty logic * Delete unused import * Add migration * Fix migrations * Make the assertions check the intended behavior
This commit is contained in:
parent
0b108903b5
commit
925249bb72
3 changed files with 116 additions and 0 deletions
|
|
@ -105,6 +105,26 @@ class VirtualColumnTest extends TestCase
|
|||
$this->assertSame($virtualColumnName, $model->getColumnForQuery('foo'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function models_extending_a_parent_model_using_virtualcolumn_get_encoded_correctly()
|
||||
{
|
||||
// Create a model that extends a parent model using VirtualColumn
|
||||
// 'foo' is a custom column, 'data' is the virtual column
|
||||
FooChild::create(['foo' => 'foo']);
|
||||
$encodedFoo = DB::select('select * from foo_childs limit 1')[0];
|
||||
// Assert that the model was encoded correctly
|
||||
$this->assertNull($encodedFoo->data);
|
||||
$this->assertSame($encodedFoo->foo, 'foo');
|
||||
|
||||
// Create another child model of the same parent
|
||||
// 'bar' is a custom column, 'data' is the virtual column
|
||||
BarChild::create(['bar' => 'bar']);
|
||||
$encodedBar = DB::select('select * from bar_childs limit 1')[0];
|
||||
|
||||
$this->assertNull($encodedBar->data);
|
||||
$this->assertSame($encodedBar->bar, 'bar');
|
||||
}
|
||||
|
||||
// maybe add an explicit test that the saving() and updating() listeners don't run twice?
|
||||
}
|
||||
|
||||
|
|
@ -146,3 +166,37 @@ class FooModel extends Model
|
|||
return 'virtual';
|
||||
}
|
||||
}
|
||||
|
||||
class ParentModel extends Model
|
||||
{
|
||||
use VirtualColumn;
|
||||
|
||||
public $timestamps = false;
|
||||
protected $guarded = [];
|
||||
}
|
||||
|
||||
|
||||
class FooChild extends ParentModel
|
||||
{
|
||||
public $table = 'foo_childs';
|
||||
|
||||
public static function getCustomColumns(): array
|
||||
{
|
||||
return [
|
||||
'id',
|
||||
'foo',
|
||||
];
|
||||
}
|
||||
}
|
||||
class BarChild extends ParentModel
|
||||
{
|
||||
public $table = 'bar_childs';
|
||||
|
||||
public static function getCustomColumns(): array
|
||||
{
|
||||
return [
|
||||
'id',
|
||||
'bar',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue