Skip to content

Commit

Permalink
Merge pull request #3 from TheDragonCode/1.x
Browse files Browse the repository at this point in the history
Added the ability to specify links to model classes
  • Loading branch information
andrey-helldar authored Jul 10, 2024
2 parents 92eaac6 + b3d9ec7 commit a77e274
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions config/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'tables' => [
// 'foo',
// 'bar',
// App\Models\Settings::class,
],
],
];
24 changes: 18 additions & 6 deletions src/Service/Tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
12 changes: 12 additions & 0 deletions tests/Fixtures/Models/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Models;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
protected $table = 'table_articles';
}
9 changes: 9 additions & 0 deletions tests/Fixtures/migrations/2024_07_10_222137_articles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Tests\Fixtures\Database\Migration;

return new class extends Migration {
protected string $tableName = 'articles';
};
3 changes: 2 additions & 1 deletion tests/Helpers/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
expect($this->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');
});
2 changes: 1 addition & 1 deletion tests/Helpers/names.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

function names(): array
{
return ['foo', 'bar', 'baz'];
return ['foo', 'bar', 'baz', 'articles'];
}

function tableName(string $name): string
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Dumps/DumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
->toBeContainsMigrations()
->toBeDataContains('foo')
->toBeDataContains('bar')
->toBeDataContains('articles')
->notToBeDataContains('baz');
})->group('SQLite', 'MySQL', 'Postgres');
2 changes: 1 addition & 1 deletion tests/Unit/Migrations/MySQLTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

test('checks an attempt to export the migration table from mysql', function () {
expect('INSERT INTO `%s`')->toBeDumped(3);
expect('INSERT INTO `%s`')->toBeDumped(4);
})->group('MySQL');
2 changes: 1 addition & 1 deletion tests/Unit/Migrations/SQLiteTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

test('checks an attempt to export the migration table from sqlite', function () {
expect('INSERT INTO %s')->toBeDumped(3);
expect('INSERT INTO %s')->toBeDumped(4);
})->group('SQLite');

0 comments on commit a77e274

Please sign in to comment.