mirror of
https://github.com/archtechx/virtualcolumn.git
synced 2025-12-16 14:04:03 +00:00
Compare commits
No commits in common. "4664d000995be64455660be31b329e1a7be9fced" and "75718edcfeeb19abc1970f5395043f7d43cce5bc" have entirely different histories.
4664d00099
...
75718edcfe
3 changed files with 5 additions and 64 deletions
|
|
@ -182,8 +182,6 @@ trait VirtualColumn
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'id',
|
'id',
|
||||||
static::CREATED_AT,
|
|
||||||
static::UPDATED_AT,
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ use Illuminate\Support\Facades\Crypt;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Stancl\VirtualColumn\VirtualColumn;
|
use Stancl\VirtualColumn\VirtualColumn;
|
||||||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
||||||
use PHPUnit\Framework\Attributes\Test;
|
|
||||||
|
|
||||||
class VirtualColumnTest extends TestCase
|
class VirtualColumnTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
@ -19,7 +18,7 @@ class VirtualColumnTest extends TestCase
|
||||||
$this->loadMigrationsFrom(__DIR__ . '/etc/migrations');
|
$this->loadMigrationsFrom(__DIR__ . '/etc/migrations');
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Test]
|
/** @test */
|
||||||
public function keys_which_dont_have_their_own_column_go_into_data_json_column()
|
public function keys_which_dont_have_their_own_column_go_into_data_json_column()
|
||||||
{
|
{
|
||||||
$model = MyModel::create([
|
$model = MyModel::create([
|
||||||
|
|
@ -60,7 +59,7 @@ class VirtualColumnTest extends TestCase
|
||||||
$this->assertSame(null, $model->data);
|
$this->assertSame(null, $model->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Test]
|
/** @test */
|
||||||
public function model_is_always_decoded_when_accessed_by_user_event()
|
public function model_is_always_decoded_when_accessed_by_user_event()
|
||||||
{
|
{
|
||||||
MyModel::retrieved(function (MyModel $model) {
|
MyModel::retrieved(function (MyModel $model) {
|
||||||
|
|
@ -91,7 +90,7 @@ class VirtualColumnTest extends TestCase
|
||||||
MyModel::first();
|
MyModel::first();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Test]
|
/** @test */
|
||||||
public function column_names_are_generated_correctly()
|
public function column_names_are_generated_correctly()
|
||||||
{
|
{
|
||||||
// FooModel's virtual data column name is 'virtual'
|
// FooModel's virtual data column name is 'virtual'
|
||||||
|
|
@ -108,7 +107,7 @@ class VirtualColumnTest extends TestCase
|
||||||
$this->assertSame($virtualColumnName, $model->getColumnForQuery('foo'));
|
$this->assertSame($virtualColumnName, $model->getColumnForQuery('foo'));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Test]
|
/** @test */
|
||||||
public function models_extending_a_parent_model_using_virtualcolumn_get_encoded_correctly()
|
public function models_extending_a_parent_model_using_virtualcolumn_get_encoded_correctly()
|
||||||
{
|
{
|
||||||
// Create a model that extends a parent model using VirtualColumn
|
// Create a model that extends a parent model using VirtualColumn
|
||||||
|
|
@ -131,7 +130,7 @@ class VirtualColumnTest extends TestCase
|
||||||
|
|
||||||
// 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 */
|
||||||
public function encrypted_casts_work_with_virtual_column() {
|
public function encrypted_casts_work_with_virtual_column() {
|
||||||
// Custom encrypted castables have to be specified in the $customEncryptedCastables static property
|
// Custom encrypted castables have to be specified in the $customEncryptedCastables static property
|
||||||
MyModel::$customEncryptedCastables = [EncryptedCast::class];
|
MyModel::$customEncryptedCastables = [EncryptedCast::class];
|
||||||
|
|
@ -147,14 +146,11 @@ class VirtualColumnTest extends TestCase
|
||||||
'null_value' => null, // 'encrypted'
|
'null_value' => null, // 'encrypted'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$databaseRecord = json_decode(DB::select('SELECT data FROM my_models')[0]->data);
|
|
||||||
|
|
||||||
foreach($encryptedAttributes as $key => $expectedValue) {
|
foreach($encryptedAttributes as $key => $expectedValue) {
|
||||||
$savedValue = $model->getAttributes()[$key]; // Encrypted
|
$savedValue = $model->getAttributes()[$key]; // Encrypted
|
||||||
|
|
||||||
if ($savedValue !== null) {
|
if ($savedValue !== null) {
|
||||||
$this->assertTrue($model->valueEncrypted($savedValue));
|
$this->assertTrue($model->valueEncrypted($savedValue));
|
||||||
$this->assertEquals($savedValue, $databaseRecord->$key); // Encrypted in DB
|
|
||||||
$this->assertNotEquals($expectedValue, $savedValue);
|
$this->assertNotEquals($expectedValue, $savedValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -166,29 +162,6 @@ class VirtualColumnTest extends TestCase
|
||||||
// Reset static property
|
// Reset static property
|
||||||
MyModel::$customEncryptedCastables = [];
|
MyModel::$customEncryptedCastables = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Test]
|
|
||||||
public function updating_model_does_not_store_timestamps_in_the_virtual_column() {
|
|
||||||
/** @var TimestampModel $model */
|
|
||||||
$model = TimestampModel::create();
|
|
||||||
$dbRecordDataColumn = fn () => DB::selectOne('select * from timestamp_models where id = ?', [$model->id])->data;
|
|
||||||
|
|
||||||
$this->assertStringNotContainsString('created_at', $dbRecordDataColumn());
|
|
||||||
$this->assertStringNotContainsString('updated_at', $dbRecordDataColumn());
|
|
||||||
|
|
||||||
$model->update(['data' => ['virtual' => 'bar']]);
|
|
||||||
|
|
||||||
$this->assertStringNotContainsString('created_at', $dbRecordDataColumn());
|
|
||||||
$this->assertStringNotContainsString('updated_at', $dbRecordDataColumn());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TimestampModel extends Model
|
|
||||||
{
|
|
||||||
use VirtualColumn;
|
|
||||||
|
|
||||||
public $timestamps = true;
|
|
||||||
protected $guarded = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ParentModel extends Model
|
class ParentModel extends Model
|
||||||
|
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Support\Facades\Schema;
|
|
||||||
|
|
||||||
class CreateTimestampModelsTable extends Migration
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Run the migrations.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function up()
|
|
||||||
{
|
|
||||||
Schema::create('timestamp_models', function (Blueprint $table) {
|
|
||||||
$table->increments('id');
|
|
||||||
|
|
||||||
$table->timestamps();
|
|
||||||
$table->json('data');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function down()
|
|
||||||
{
|
|
||||||
Schema::dropIfExists('timestamp_models');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue