diff --git a/tests/VirtualColumnTest.php b/tests/VirtualColumnTest.php index 812b599..ab45274 100644 --- a/tests/VirtualColumnTest.php +++ b/tests/VirtualColumnTest.php @@ -88,6 +88,23 @@ class VirtualColumnTest extends TestCase MyModel::first(); } + /** @test */ + public function column_names_are_generated_correctly() + { + // AnotherModel's virtual data column name is 'virtual' + $virtualColumnName = 'virtual->foo'; + $customColumnName = 'custom1'; + + /** @var FooModel $model */ + $model = FooModel::create([ + 'custom1' => $customColumnName, + 'foo' => $virtualColumnName + ]); + + $this->assertSame($customColumnName, $model->generateColumnName('custom1')); + $this->assertSame($virtualColumnName, $model->generateColumnName('foo')); + } + // maybe add an explicit test that the saving() and updating() listeners don't run twice? } @@ -107,3 +124,25 @@ class MyModel extends Model ]; } } + +class FooModel extends Model +{ + use VirtualColumn; + + protected $guarded = []; + public $timestamps = false; + + public static function getCustomColumns(): array + { + return [ + 'id', + 'custom1', + 'custom2', + ]; + } + + public static function getDataColumn(): string + { + return 'virtual'; + } +} diff --git a/tests/etc/migrations/2022_10_21_000001_create_foo_models_table.php b/tests/etc/migrations/2022_10_21_000001_create_foo_models_table.php new file mode 100644 index 0000000..98aa537 --- /dev/null +++ b/tests/etc/migrations/2022_10_21_000001_create_foo_models_table.php @@ -0,0 +1,32 @@ +increments('id'); + + $table->string('custom1')->nullable(); + $table->string('custom2')->nullable(); + + $table->json('virtual'); + }); + } + + public function down() + { + Schema::dropIfExists('foo_models'); + } +}