From 3546784e93d972fa05e02b882ec62d51c3fff36f Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 10 Jul 2024 22:33:32 +0300 Subject: [PATCH 1/2] Added the ability to specify links to model classes --- config/settings.php | 1 + src/Service/Tables.php | 24 ++++++++++++++----- tests/Fixtures/Models/Article.php | 12 ++++++++++ .../migrations/2024_07_10_222137_articles.php | 9 +++++++ tests/Helpers/data.php | 3 ++- tests/Helpers/names.php | 2 +- tests/TestCase.php | 1 + tests/Unit/Dumps/DumpTest.php | 1 + 8 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 tests/Fixtures/Models/Article.php create mode 100644 tests/Fixtures/migrations/2024_07_10_222137_articles.php diff --git a/config/settings.php b/config/settings.php index c60759b..eefd2d8 100644 --- a/config/settings.php +++ b/config/settings.php @@ -15,6 +15,7 @@ 'tables' => [ // 'foo', // 'bar', + // App\Models\Settings::class, ], ], ]; diff --git a/src/Service/Tables.php b/src/Service/Tables.php index ea10000..29a5735 100644 --- a/src/Service/Tables.php +++ b/src/Service/Tables.php @@ -4,27 +4,39 @@ namespace DragonCode\LaravelDataDumper\Service; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Schema; class Tables { public function dumpable(): array { - return array_intersect($this->flatten(), $this->toDump()); + return array_intersect($this->available(), $this->tables()); } - protected function toDump(): array + protected function tables(): array { - return config('database.schema.tables', []); + return collect(config('database.schema.tables', [])) + ->map(fn (string $table) => $this->fromModels($table)) + ->all(); } - protected function flatten(): array + protected function available(): array { - return array_column($this->getTables(), 'name'); + return array_column($this->tableSchema(), 'name'); } - protected function getTables(): array + protected function tableSchema(): array { return Schema::getTables(); } + + protected function fromModels(Model|string $table): string + { + if (! class_exists($table)) { + return $table; + } + + return (new $table())->getTable(); + } } diff --git a/tests/Fixtures/Models/Article.php b/tests/Fixtures/Models/Article.php new file mode 100644 index 0000000..711404d --- /dev/null +++ b/tests/Fixtures/Models/Article.php @@ -0,0 +1,12 @@ +value) ->toContain('2024_03_15_000001_foo') ->toContain('2024_03_15_000002_bar') - ->toContain('2024_03_15_000003_baz'); + ->toContain('2024_03_15_000003_baz') + ->toContain('2024_07_10_222137_articles'); }); diff --git a/tests/Helpers/names.php b/tests/Helpers/names.php index b1d9362..05e526d 100644 --- a/tests/Helpers/names.php +++ b/tests/Helpers/names.php @@ -4,7 +4,7 @@ function names(): array { - return ['foo', 'bar', 'baz']; + return ['foo', 'bar', 'baz', 'articles']; } function tableName(string $name): string diff --git a/tests/TestCase.php b/tests/TestCase.php index 788bd85..6cb58b7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -26,6 +26,7 @@ protected function getEnvironmentSetUp($app): void $config->set('database.schema.tables', [ tableName('foo'), tableName('bar'), + tableName('articles'), ]); $config->set('database.default', env('DB_CONNECTION', 'sqlite')); diff --git a/tests/Unit/Dumps/DumpTest.php b/tests/Unit/Dumps/DumpTest.php index 3f7820e..12eacbc 100644 --- a/tests/Unit/Dumps/DumpTest.php +++ b/tests/Unit/Dumps/DumpTest.php @@ -16,5 +16,6 @@ ->toBeContainsMigrations() ->toBeDataContains('foo') ->toBeDataContains('bar') + ->toBeDataContains('articles') ->notToBeDataContains('baz'); })->group('SQLite', 'MySQL', 'Postgres'); From b3d9ec7351166d726f1d6ed0cb611ec2ca730750 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 10 Jul 2024 22:46:19 +0300 Subject: [PATCH 2/2] Fixed tests --- tests/Unit/Migrations/MySQLTest.php | 2 +- tests/Unit/Migrations/SQLiteTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Migrations/MySQLTest.php b/tests/Unit/Migrations/MySQLTest.php index bd7fe47..3bf83a4 100644 --- a/tests/Unit/Migrations/MySQLTest.php +++ b/tests/Unit/Migrations/MySQLTest.php @@ -1,5 +1,5 @@ toBeDumped(3); + expect('INSERT INTO `%s`')->toBeDumped(4); })->group('MySQL'); diff --git a/tests/Unit/Migrations/SQLiteTest.php b/tests/Unit/Migrations/SQLiteTest.php index dde27d9..c9d88db 100644 --- a/tests/Unit/Migrations/SQLiteTest.php +++ b/tests/Unit/Migrations/SQLiteTest.php @@ -1,5 +1,5 @@ toBeDumped(3); + expect('INSERT INTO %s')->toBeDumped(4); })->group('SQLite');