Skip to content

Commit 925249b

Browse files
authored
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
1 parent 0b10890 commit 925249b

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

tests/VirtualColumnTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ public function column_names_are_generated_correctly()
105105
$this->assertSame($virtualColumnName, $model->getColumnForQuery('foo'));
106106
}
107107

108+
/** @test */
109+
public function models_extending_a_parent_model_using_virtualcolumn_get_encoded_correctly()
110+
{
111+
// Create a model that extends a parent model using VirtualColumn
112+
// 'foo' is a custom column, 'data' is the virtual column
113+
FooChild::create(['foo' => 'foo']);
114+
$encodedFoo = DB::select('select * from foo_childs limit 1')[0];
115+
// Assert that the model was encoded correctly
116+
$this->assertNull($encodedFoo->data);
117+
$this->assertSame($encodedFoo->foo, 'foo');
118+
119+
// Create another child model of the same parent
120+
// 'bar' is a custom column, 'data' is the virtual column
121+
BarChild::create(['bar' => 'bar']);
122+
$encodedBar = DB::select('select * from bar_childs limit 1')[0];
123+
124+
$this->assertNull($encodedBar->data);
125+
$this->assertSame($encodedBar->bar, 'bar');
126+
}
127+
108128
// maybe add an explicit test that the saving() and updating() listeners don't run twice?
109129
}
110130

@@ -146,3 +166,37 @@ public static function getDataColumn(): string
146166
return 'virtual';
147167
}
148168
}
169+
170+
class ParentModel extends Model
171+
{
172+
use VirtualColumn;
173+
174+
public $timestamps = false;
175+
protected $guarded = [];
176+
}
177+
178+
179+
class FooChild extends ParentModel
180+
{
181+
public $table = 'foo_childs';
182+
183+
public static function getCustomColumns(): array
184+
{
185+
return [
186+
'id',
187+
'foo',
188+
];
189+
}
190+
}
191+
class BarChild extends ParentModel
192+
{
193+
public $table = 'bar_childs';
194+
195+
public static function getCustomColumns(): array
196+
{
197+
return [
198+
'id',
199+
'bar',
200+
];
201+
}
202+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
class CreateBarChildsTable extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*
14+
* @return void
15+
*/
16+
public function up()
17+
{
18+
Schema::create('bar_childs', function (Blueprint $table) {
19+
$table->increments('id');
20+
21+
$table->string('bar')->nullable();
22+
23+
$table->json('data')->nullable();
24+
});
25+
}
26+
27+
public function down()
28+
{
29+
Schema::dropIfExists('bar_childs');
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
class CreateFooChildsTable extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*
14+
* @return void
15+
*/
16+
public function up()
17+
{
18+
Schema::create('foo_childs', function (Blueprint $table) {
19+
$table->increments('id');
20+
21+
$table->string('foo')->nullable();
22+
23+
$table->json('data')->nullable();
24+
});
25+
}
26+
27+
public function down()
28+
{
29+
Schema::dropIfExists('foo_childs');
30+
}
31+
}

0 commit comments

Comments
 (0)