From ceb012f3fd9223cf5513c80f50bd4daa218313e3 Mon Sep 17 00:00:00 2001 From: Sam Lee Date: Mon, 6 Jan 2025 17:13:20 +0900 Subject: [PATCH] Fix a bug of `hasTimestamps()` method in `ModelGenerator` class --- src/Table.php | 5 ++++- tests/Unit/Generators/ModelGeneratorTest.php | 4 ++-- tests/Unit/Generators/RelationGeneratorTest.php | 4 ++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Table.php b/src/Table.php index 52306c9..c7fa96b 100644 --- a/src/Table.php +++ b/src/Table.php @@ -151,7 +151,10 @@ public function migration(): string */ public function hasTimestamps(): bool { - return in_array('created_at', array_column($this->columns, 'field')) && in_array('updated_at', array_column($this->columns, 'field')); + return ! ( + in_array('created_at', array_column($this->columns, 'field')) && + in_array('updated_at', array_column($this->columns, 'field')) + ); } /** diff --git a/tests/Unit/Generators/ModelGeneratorTest.php b/tests/Unit/Generators/ModelGeneratorTest.php index a647e0d..dfb5e41 100644 --- a/tests/Unit/Generators/ModelGeneratorTest.php +++ b/tests/Unit/Generators/ModelGeneratorTest.php @@ -37,7 +37,7 @@ public function test_it_can_generate_timestamps(): void { $file = File::system()->read(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php'); - $this->assertStringContainsString('timestamps', $file); + $this->assertStringNotContainsString('timestamps', $file); } public function test_it_can_generate_without_timestamps(): void @@ -53,6 +53,6 @@ public function test_it_can_generate_without_timestamps(): void $file = File::system()->read(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php'); - $this->assertStringNotContainsString('timestamps', $file); + $this->assertStringContainsString('timestamps', $file); } } diff --git a/tests/Unit/Generators/RelationGeneratorTest.php b/tests/Unit/Generators/RelationGeneratorTest.php index c6b51a0..12ffc0a 100644 --- a/tests/Unit/Generators/RelationGeneratorTest.php +++ b/tests/Unit/Generators/RelationGeneratorTest.php @@ -22,12 +22,16 @@ protected function setUp(): void $this->table = new Table('samples', [ Column::make('id', 'bigInteger'), Column::make('related_id', 'bigInteger'), + Column::make('created_at', 'timestamp'), + Column::make('updated_at', 'timestamp'), ], [ ForeignKey::make('samples_related_fk', 'Sample', 'related_id', 'Related', 'id'), ]); $this->related = new Table('related', [ Column::make('id', 'bigInteger'), + Column::make('created_at', 'timestamp'), + Column::make('updated_at', 'timestamp'), ]); ModelGenerator::make(