mirror of
https://github.com/archtechx/virtualcolumn.git
synced 2025-12-12 12:14:03 +00:00
Merge branch 'master' into encrypted-casts
This commit is contained in:
commit
36493d0a84
3 changed files with 116 additions and 0 deletions
|
|
@ -107,6 +107,26 @@ class VirtualColumnTest extends TestCase
|
||||||
$this->assertSame($virtualColumnName, $model->getColumnForQuery('foo'));
|
$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?
|
// maybe add an explicit test that the saving() and updating() listeners don't run twice?
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
@ -189,3 +209,37 @@ class EncryptedCast implements CastsAttributes
|
||||||
return Crypt::encryptString($value);
|
return Crypt::encryptString($value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 CreateBarChildsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('bar_childs', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
|
||||||
|
$table->string('bar')->nullable();
|
||||||
|
|
||||||
|
$table->json('data')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('bar_childs');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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 CreateFooChildsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('foo_childs', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
|
||||||
|
$table->string('foo')->nullable();
|
||||||
|
|
||||||
|
$table->json('data')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('foo_childs');
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue