Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/VirtualColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ public static function getCustomColumns(): array
{
return [
'id',
static::CREATED_AT,
static::UPDATED_AT,
];
}

Expand Down
34 changes: 29 additions & 5 deletions tests/VirtualColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Model;
use Stancl\VirtualColumn\VirtualColumn;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use PHPUnit\Framework\Attributes\Test;

class VirtualColumnTest extends TestCase
{
Expand All @@ -18,7 +19,7 @@ public function setUp(): void
$this->loadMigrationsFrom(__DIR__ . '/etc/migrations');
}

/** @test */
#[Test]
public function keys_which_dont_have_their_own_column_go_into_data_json_column()
{
$model = MyModel::create([
Expand Down Expand Up @@ -59,7 +60,7 @@ public function keys_which_dont_have_their_own_column_go_into_data_json_column()
$this->assertSame(null, $model->data);
}

/** @test */
#[Test]
public function model_is_always_decoded_when_accessed_by_user_event()
{
MyModel::retrieved(function (MyModel $model) {
Expand Down Expand Up @@ -90,7 +91,7 @@ public function model_is_always_decoded_when_accessed_by_user_event()
MyModel::first();
}

/** @test */
#[Test]
public function column_names_are_generated_correctly()
{
// FooModel's virtual data column name is 'virtual'
Expand All @@ -107,7 +108,7 @@ public function column_names_are_generated_correctly()
$this->assertSame($virtualColumnName, $model->getColumnForQuery('foo'));
}

/** @test */
#[Test]
public function models_extending_a_parent_model_using_virtualcolumn_get_encoded_correctly()
{
// Create a model that extends a parent model using VirtualColumn
Expand All @@ -130,7 +131,7 @@ public function models_extending_a_parent_model_using_virtualcolumn_get_encoded_

// 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() {
// Custom encrypted castables have to be specified in the $customEncryptedCastables static property
MyModel::$customEncryptedCastables = [EncryptedCast::class];
Expand Down Expand Up @@ -162,6 +163,29 @@ public function encrypted_casts_work_with_virtual_column() {
// Reset static property
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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