From 7a2a5ea0440260cbbd2529fdca397b87e85659e2 Mon Sep 17 00:00:00 2001 From: Samgu Lee Date: Mon, 6 Jan 2025 18:10:52 +0900 Subject: [PATCH] Fix remove useless fields from nova resources (#96) * Fix remove useless fields from nova resources * Refactoring a delete method --- src/Generators/NovaResourceGenerator.php | 4 +- tests/Unit/Generators/ModelGeneratorTest.php | 2 +- .../Generators/NovaResourceGeneratorTest.php | 43 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/Generators/NovaResourceGeneratorTest.php diff --git a/src/Generators/NovaResourceGenerator.php b/src/Generators/NovaResourceGenerator.php index bd68116..ed579c8 100644 --- a/src/Generators/NovaResourceGenerator.php +++ b/src/Generators/NovaResourceGenerator.php @@ -41,7 +41,9 @@ public function run(bool $force = false): void { $novaFieldsString = ''; foreach ($this->table->getColumns() as $column) { - $novaFieldsString .= self::INTENT.$column->novaField().PHP_EOL; + if (! in_array($column->field, ['created_at', 'updated_at'])) { + $novaFieldsString .= self::INTENT.$column->novaField().PHP_EOL; + } } $novaFieldsString = rtrim($novaFieldsString, PHP_EOL.PHP_EOL); diff --git a/tests/Unit/Generators/ModelGeneratorTest.php b/tests/Unit/Generators/ModelGeneratorTest.php index dfb5e41..f58ba73 100644 --- a/tests/Unit/Generators/ModelGeneratorTest.php +++ b/tests/Unit/Generators/ModelGeneratorTest.php @@ -42,7 +42,7 @@ public function test_it_can_generate_timestamps(): void public function test_it_can_generate_without_timestamps(): void { - unlink(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php'); + File::system()->delete(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php'); ModelGenerator::make( new Table('samples', [ diff --git a/tests/Unit/Generators/NovaResourceGeneratorTest.php b/tests/Unit/Generators/NovaResourceGeneratorTest.php new file mode 100644 index 0000000..3dcc322 --- /dev/null +++ b/tests/Unit/Generators/NovaResourceGeneratorTest.php @@ -0,0 +1,43 @@ +run(); + } + + protected function tearDown(): void + { + File::system()->delete(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php'); + } + + public function test_it_can_be_existed(): void + { + $this->assertFileExists(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php'); + } + + public function test_it_can_be_without_timestamps(): void + { + $file = File::system()->read(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php'); + + $this->assertStringNotContainsString('Created At', $file); + $this->assertStringNotContainsString('Updated At', $file); + } +}