Skip to content

Commit 4664d00

Browse files
authored
Merge pull request #24 from archtechx/fix-tests
Include timestamps in `getCustomColumns()` by default, fix tests (use new #[Test] attribute)
2 parents 9da2d6f + 2fd259b commit 4664d00

3 files changed

Lines changed: 61 additions & 5 deletions

File tree

src/VirtualColumn.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ public static function getCustomColumns(): array
182182
{
183183
return [
184184
'id',
185+
static::CREATED_AT,
186+
static::UPDATED_AT,
185187
];
186188
}
187189

tests/VirtualColumnTest.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Model;
99
use Stancl\VirtualColumn\VirtualColumn;
1010
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
11+
use PHPUnit\Framework\Attributes\Test;
1112

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

21-
/** @test */
22+
#[Test]
2223
public function keys_which_dont_have_their_own_column_go_into_data_json_column()
2324
{
2425
$model = MyModel::create([
@@ -59,7 +60,7 @@ public function keys_which_dont_have_their_own_column_go_into_data_json_column()
5960
$this->assertSame(null, $model->data);
6061
}
6162

62-
/** @test */
63+
#[Test]
6364
public function model_is_always_decoded_when_accessed_by_user_event()
6465
{
6566
MyModel::retrieved(function (MyModel $model) {
@@ -90,7 +91,7 @@ public function model_is_always_decoded_when_accessed_by_user_event()
9091
MyModel::first();
9192
}
9293

93-
/** @test */
94+
#[Test]
9495
public function column_names_are_generated_correctly()
9596
{
9697
// FooModel's virtual data column name is 'virtual'
@@ -107,7 +108,7 @@ public function column_names_are_generated_correctly()
107108
$this->assertSame($virtualColumnName, $model->getColumnForQuery('foo'));
108109
}
109110

110-
/** @test */
111+
#[Test]
111112
public function models_extending_a_parent_model_using_virtualcolumn_get_encoded_correctly()
112113
{
113114
// Create a model that extends a parent model using VirtualColumn
@@ -130,7 +131,7 @@ public function models_extending_a_parent_model_using_virtualcolumn_get_encoded_
130131

131132
// maybe add an explicit test that the saving() and updating() listeners don't run twice?
132133
133-
/** @test */
134+
#[Test]
134135
public function encrypted_casts_work_with_virtual_column() {
135136
// Custom encrypted castables have to be specified in the $customEncryptedCastables static property
136137
MyModel::$customEncryptedCastables = [EncryptedCast::class];
@@ -165,6 +166,29 @@ public function encrypted_casts_work_with_virtual_column() {
165166
// Reset static property
166167
MyModel::$customEncryptedCastables = [];
167168
}
169+
170+
#[Test]
171+
public function updating_model_does_not_store_timestamps_in_the_virtual_column() {
172+
/** @var TimestampModel $model */
173+
$model = TimestampModel::create();
174+
$dbRecordDataColumn = fn () => DB::selectOne('select * from timestamp_models where id = ?', [$model->id])->data;
175+
176+
$this->assertStringNotContainsString('created_at', $dbRecordDataColumn());
177+
$this->assertStringNotContainsString('updated_at', $dbRecordDataColumn());
178+
179+
$model->update(['data' => ['virtual' => 'bar']]);
180+
181+
$this->assertStringNotContainsString('created_at', $dbRecordDataColumn());
182+
$this->assertStringNotContainsString('updated_at', $dbRecordDataColumn());
183+
}
184+
}
185+
186+
class TimestampModel extends Model
187+
{
188+
use VirtualColumn;
189+
190+
public $timestamps = true;
191+
protected $guarded = [];
168192
}
169193

170194
class ParentModel extends Model
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 CreateTimestampModelsTable extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*
14+
* @return void
15+
*/
16+
public function up()
17+
{
18+
Schema::create('timestamp_models', function (Blueprint $table) {
19+
$table->increments('id');
20+
21+
$table->timestamps();
22+
$table->json('data');
23+
});
24+
}
25+
26+
public function down()
27+
{
28+
Schema::dropIfExists('timestamp_models');
29+
}
30+
}

0 commit comments

Comments
 (0)