mirror of
https://github.com/archtechx/virtualcolumn.git
synced 2025-12-12 20:14:03 +00:00
Test that using different models extending the same class with VirtualColumn doesn't work correctly
This commit is contained in:
parent
0b108903b5
commit
4c0613fee8
2 changed files with 97 additions and 0 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Stancl\VirtualColumn\Tests;
|
namespace Stancl\VirtualColumn\Tests;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Orchestra\Testbench\TestCase;
|
use Orchestra\Testbench\TestCase;
|
||||||
|
|
@ -105,6 +106,23 @@ class VirtualColumnTest extends TestCase
|
||||||
$this->assertSame($virtualColumnName, $model->getColumnForQuery('foo'));
|
$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?
|
// 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';
|
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';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class CreateBarModelsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('bar_models', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
|
||||||
|
$table->string('custom1');
|
||||||
|
|
||||||
|
$table->json('data')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('bar_models');
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue