Skip to content

Commit

Permalink
Removed code duplication from tests and removed unnecessary dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Mar 15, 2024
1 parent 0a41232 commit f856d33
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 59 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
"ext-pdo": "*",
"mockery/mockery": "^1.3.1",
"orchestra/testbench": "^8.0 || ^9.0",
"pestphp/pest": "^2.34",
"pestphp/pest-plugin-laravel": "^2.3"
"pestphp/pest": "^2.34"
},
"minimum-stability": "stable",
"prefer-stable": true,
Expand Down
19 changes: 19 additions & 0 deletions tests/Helpers/string.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use Illuminate\Database\Console\DumpCommand;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Str;

use function PHPUnit\Framework\assertSame;
Expand All @@ -11,3 +13,20 @@

return $this;
});

expect()->extend('toBeDumped', function (int $count) {
$migrations = is_array($table = config('database.migrations')) ? $table['table'] : $table;

config()->set('database.schema.tables', [$migrations]);

$path = dumpStoragePath();

Artisan::call(DumpCommand::class, ['--path' => $path]);

expect(file_get_contents($path))
->toBeContainsMigrations()
->toBeWordsCount(sprintf($this->value, $migrations), $count)
->notToBeDataContains('foo')
->notToBeDataContains('bar')
->notToBeDataContains('baz');
});
5 changes: 2 additions & 3 deletions tests/Unit/Dumps/DumpTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

use Illuminate\Database\Console\DumpCommand;

use function Pest\Laravel\artisan;
use Illuminate\Support\Facades\Artisan;

it('checks the export of data from tables to a dump file', function () {
expect()
Expand All @@ -11,7 +10,7 @@

$path = dumpStoragePath();

artisan(DumpCommand::class, ['--path' => $path])->run();
Artisan::call(DumpCommand::class, ['--path' => $path]);

expect(file_get_contents($path))
->toBeContainsMigrations()
Expand Down
19 changes: 1 addition & 18 deletions tests/Unit/Migrations/MySQLTest.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
<?php

use Illuminate\Database\Console\DumpCommand;

use function Pest\Laravel\artisan;

test('checks an attempt to export the migration table from mysql', function () {
$migrations = is_array($table = config('database.migrations')) ? $table['table'] : $table;

config()->set('database.schema.tables', [$migrations]);

$path = dumpStoragePath();

artisan(DumpCommand::class, ['--path' => $path])->run();

expect(file_get_contents($path))
->toBeContainsMigrations()
->toBeWordsCount("INSERT INTO `$migrations`", 3)
->notToBeDataContains('foo')
->notToBeDataContains('bar')
->notToBeDataContains('baz');
expect('INSERT INTO `%s`')->toBeDumped(3);
})->group('MySQL');
19 changes: 1 addition & 18 deletions tests/Unit/Migrations/PostgresTest.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
<?php

use Illuminate\Database\Console\DumpCommand;

use function Pest\Laravel\artisan;

test('checks an attempt to export the migration table from postgres', function () {
$migrations = is_array($table = config('database.migrations')) ? $table['table'] : $table;

config()->set('database.schema.tables', [$migrations]);

$path = dumpStoragePath();

artisan(DumpCommand::class, ['--path' => $path])->run();

expect(file_get_contents($path))
->toBeContainsMigrations()
->toBeWordsCount("COPY public.$migrations", 1)
->notToBeDataContains('foo')
->notToBeDataContains('bar')
->notToBeDataContains('baz');
expect('COPY public.%s')->toBeDumped(1);
})->group('Postgres');
19 changes: 1 addition & 18 deletions tests/Unit/Migrations/SQLiteTest.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
<?php

use Illuminate\Database\Console\DumpCommand;

use function Pest\Laravel\artisan;

test('checks an attempt to export the migration table from sqlite', function () {
$migrations = is_array($table = config('database.migrations')) ? $table['table'] : $table;

config()->set('database.schema.tables', [$migrations]);

$path = dumpStoragePath();

artisan(DumpCommand::class, ['--path' => $path])->run();

expect(file_get_contents($path))
->toBeContainsMigrations()
->toBeWordsCount("INSERT INTO $migrations", 3)
->notToBeDataContains('foo')
->notToBeDataContains('bar')
->notToBeDataContains('baz');
expect('INSERT INTO %s')->toBeDumped(3);
})->group('SQLite');

0 comments on commit f856d33

Please sign in to comment.