From 4c0613fee85004a6889a6863c760900f31fbb9be Mon Sep 17 00:00:00 2001 From: lukinovec Date: Wed, 1 Nov 2023 15:47:00 +0100 Subject: [PATCH] Test that using different models extending the same class with VirtualColumn doesn't work correctly --- tests/VirtualColumnTest.php | 66 +++++++++++++++++++ ...3_05_11_000001_create_bar_models_table.php | 31 +++++++++ 2 files changed, 97 insertions(+) create mode 100644 tests/etc/migrations/2023_05_11_000001_create_bar_models_table.php diff --git a/tests/VirtualColumnTest.php b/tests/VirtualColumnTest.php index 247b189..e441b44 100644 --- a/tests/VirtualColumnTest.php +++ b/tests/VirtualColumnTest.php @@ -2,6 +2,7 @@ namespace Stancl\VirtualColumn\Tests; +use Exception; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; use Orchestra\Testbench\TestCase; @@ -105,6 +106,23 @@ class VirtualColumnTest extends TestCase $this->assertSame($virtualColumnName, $model->getColumnForQuery('foo')); } + /** @test */ + public function multiple_classes_cannot_extend_a_parent_that_uses_virtualcolumn() + { + + // Create a model that extends a parent using VirtualColumn + FirstChildModel::create(['custom1' => 'foo model', 'foo' => 'foo']); + + // Expect creating a model that extends the same parent using VirtualColumn + // Not to work correctly + $this->expectException(Exception::class); + + // Try creating a model that extends the same parent + SecondChildModel::create(['custom1' => 'bar', 'bar' => 'custom property']); + } + + + // maybe add an explicit test that the saving() and updating() listeners don't run twice? } @@ -146,3 +164,51 @@ class FooModel extends Model return 'virtual'; } } + +class ParentModel extends Model +{ + use VirtualColumn; + + public $table = 'foo_models'; + + protected $guarded = []; + public $timestamps = false; + + public static function getCustomColumns(): array + { + return [ + 'id', + 'custom1', + 'custom2', + ]; + } + + public static function getDataColumn(): string + { + return 'virtual'; + } +} + + +class FirstChildModel extends ParentModel +{ +} +class SecondChildModel extends ParentModel +{ + public $table = 'bar_models'; + protected $guarded = []; + public $timestamps = false; + + public static function getCustomColumns(): array + { + return [ + 'id', + 'custom1', + ]; + } + + public static function getDataColumn(): string + { + return 'data'; + } +} diff --git a/tests/etc/migrations/2023_05_11_000001_create_bar_models_table.php b/tests/etc/migrations/2023_05_11_000001_create_bar_models_table.php new file mode 100644 index 0000000..4f8d42d --- /dev/null +++ b/tests/etc/migrations/2023_05_11_000001_create_bar_models_table.php @@ -0,0 +1,31 @@ +increments('id'); + + $table->string('custom1'); + + $table->json('data')->nullable(); + }); + } + + public function down() + { + Schema::dropIfExists('bar_models'); + } +}