From 696c0ab1d9e6bd5c803b1146e22f29ff92c396a9 Mon Sep 17 00:00:00 2001 From: Shisha Date: Fri, 22 Jul 2022 19:30:30 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E6=B8=AC=E8=A9=A6=E6=AA=94=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 8 ++ phpunit.xml.dist | 12 +-- src/Commands/DbPatchCommand.php | 84 ++++++++++++++----- tests/CallDbPatchArtisanCommandTest.php | 47 +++++++++++ tests/DbPatchCommandTest.php | 38 +++++++++ tests/TestCase.php | 36 ++++++++ ..._000000_add_priority_to_products_table.php | 35 ++++++++ 7 files changed, 233 insertions(+), 27 deletions(-) create mode 100644 tests/CallDbPatchArtisanCommandTest.php create mode 100644 tests/DbPatchCommandTest.php create mode 100644 tests/TestCase.php create mode 100644 tests/fixtures/patches/2022_07_19_000000_add_priority_to_products_table.php diff --git a/composer.json b/composer.json index c711a99..5c9c3a9 100644 --- a/composer.json +++ b/composer.json @@ -30,5 +30,13 @@ "A2Workspace\\DatabasePatcher\\ServiceProvider" ] } + }, + "scripts": { + "test": [ + "vendor/bin/phpunit" + ], + "test-coverage": [ + "vendor/bin/phpunit --coverage-html coverage" + ] } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b9fa7c2..23acb3a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -13,12 +13,14 @@ stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" > + + + src + + - - ./tests/Unit - - - ./tests/Feature + + ./tests/ diff --git a/src/Commands/DbPatchCommand.php b/src/Commands/DbPatchCommand.php index c9bd80b..ae370a4 100644 --- a/src/Commands/DbPatchCommand.php +++ b/src/Commands/DbPatchCommand.php @@ -47,7 +47,7 @@ class DbPatchCommand extends Command * * @return int */ - public function handle() + public function handle(): int { $file = $this->determinePatchFile(); if (!$file) { @@ -55,36 +55,24 @@ public function handle() } $path = $file->getRealPath(); - $path = Str::after($path, base_path()); - // 這邊我們判斷 - // 若為回復模式則呼叫滾回命令並傳入補丁檔案路徑 - if ($this->option('revert')) { - $this->info("Running: php artisan migrate:rollback --path={$path}"); - - $this->call('migrate:rollback', [ - '--path' => $path, - ]); - } - - // 呼叫遷移命令並傳入補丁檔案路徑 - else { - $this->info("Running: php artisan migrate --path={$path}"); - - $this->call('migrate', [ - '--path' => $path, - ]); + if ($this->usingRevertion()) { + return $this->callMigrateCommand($path, 'migrate:rollback'); } - return 0; + return $this->callMigrateCommand($path); } + // ========================================================================= + // = DeterminePatchFile() + // ========================================================================= + /** * 決定要被使用的檔案 * * @return \Symfony\Component\Finder\SplFileInfo|null */ - protected function determinePatchFile() + protected function determinePatchFile(): ?SplFileInfo { // 取得 patches 目錄的檔案列表,若結果為空則提前終止 $files = $this->getFileList(); @@ -125,7 +113,7 @@ protected function getFilterInput() */ protected function getFileList(): Collection { - $paths = [database_path('patches')]; + $paths = $this->laravel['config']['database.patcher.paths'] ?? database_path('patches'); return collect($paths) ->map(fn ($path) => $this->getFileListInDirectory($path)) @@ -144,6 +132,7 @@ protected function getFileListInDirectory(string $path): array ->filter(function (SplFileInfo $file) { return !in_array($file->getRelativePathname(), $this->excludedNames); }) + ->ignoreDotFiles(true) ->files() ->in($path) ->depth(0) @@ -178,4 +167,55 @@ protected function choiceFromFileList($question, Collection $files): SplFileInfo return $value[0] === $input; })[1]; } + + // ========================================================================= + // = UsingRevertion() + // ========================================================================= + + /** + * 判定是否為 revert 模式 + * + * @return bool + */ + protected function usingRevertion(): bool + { + return $this->input->hasOption('revert') && $this->option('revert'); + } + + // ========================================================================= + // = CallMigrateCommand() + // ========================================================================= + + /** + * 呼叫運行 migrate 指令並傳入路徑 + * + * @param string $path + * @param string $command + * @return int + */ + protected function callMigrateCommand($path, string $command = 'migrate'): int + { + if ($this->shouldUseRealPath($path)) { + $this->info("Running: php artisan {$command} --realpath={$path}"); + $this->call($command, ['--realpath' => $path]); + } else { + $path = Str::after($path, base_path()); + + $this->info("Running: php artisan {$command} --path={$path}"); + $this->call($command, ['--path' => $path]); + } + + return 0; + } + + /** + * 判定是否該使用完整路徑傳入 + * + * @param string $path + * @return bool + */ + protected function shouldUseRealPath($path): bool + { + return ! Str::startsWith($path, base_path()); + } } diff --git a/tests/CallDbPatchArtisanCommandTest.php b/tests/CallDbPatchArtisanCommandTest.php new file mode 100644 index 0000000..ab4ee07 --- /dev/null +++ b/tests/CallDbPatchArtisanCommandTest.php @@ -0,0 +1,47 @@ + $this->parseLabel($option), $options); + + if (is_string($input)) { + $answer = $this->parseLabel($input); + } else if (is_integer($input)) { + $answer = $options[$input]; + } + + return $command->expectsChoice( + '選擇補丁檔案', + $answer, + $options, + ); + } + + private function parseLabel(string $option): string + { + return "-> {$option}"; + } + + // ========================================================================= + // = Tests + // ========================================================================= + + public function test_call_artisan_command() + { + $command = $this->artisan('db:patch'); + + $this->expectsCommandChoice($command, 0); + } +} diff --git a/tests/DbPatchCommandTest.php b/tests/DbPatchCommandTest.php new file mode 100644 index 0000000..0fe771a --- /dev/null +++ b/tests/DbPatchCommandTest.php @@ -0,0 +1,38 @@ +setLaravel($this->app); + + return $command; + } + + // ========================================================================= + // = Tests + // ========================================================================= + + public function test_example() + { + $this->assertTrue(true); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..04eee99 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,36 @@ +set('database.patcher.paths', [ + __DIR__ . '/fixtures/patches', + __DIR__ . '/../publishes/patches', + ]); + } + + protected function getPackageProviders($app) + { + return [ServiceProvider::class]; + } + + // ========================================================================= + // = Helpers + // ========================================================================= + + private function refMethod($name, $scope): Closure + { + $ref = function () use ($name) { + return $this->$name(...func_get_args()); + }; + + return $ref->bindTo($scope, $scope); + } +} diff --git a/tests/fixtures/patches/2022_07_19_000000_add_priority_to_products_table.php b/tests/fixtures/patches/2022_07_19_000000_add_priority_to_products_table.php new file mode 100644 index 0000000..82baf24 --- /dev/null +++ b/tests/fixtures/patches/2022_07_19_000000_add_priority_to_products_table.php @@ -0,0 +1,35 @@ +tinyInteger('priority')->comment('優先度') + ->default(0); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('products', function (Blueprint $table) { + if (Schema::hasColumn($table->getTable(), 'priority')) { + $table->dropColumn('priority'); + } + }); + } +} From b826e0509e5c31e738da9396999401310574133a Mon Sep 17 00:00:00 2001 From: Shisha Date: Sun, 24 Jul 2022 21:46:53 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=88=90=E5=8A=9F=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=B8=AC=E8=A9=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Commands/DbPatchCommand.php | 8 +- tests/CallDbPatchArtisanCommandTest.php | 90 +++++++++++++++---- tests/TestCase.php | 25 ++++-- ..._000000_add_priority_to_products_table.php | 35 -------- 4 files changed, 99 insertions(+), 59 deletions(-) delete mode 100644 tests/fixtures/patches/2022_07_19_000000_add_priority_to_products_table.php diff --git a/src/Commands/DbPatchCommand.php b/src/Commands/DbPatchCommand.php index ae370a4..af3bbba 100644 --- a/src/Commands/DbPatchCommand.php +++ b/src/Commands/DbPatchCommand.php @@ -196,8 +196,12 @@ protected function usingRevertion(): bool protected function callMigrateCommand($path, string $command = 'migrate'): int { if ($this->shouldUseRealPath($path)) { - $this->info("Running: php artisan {$command} --realpath={$path}"); - $this->call($command, ['--realpath' => $path]); + $this->info("Running: php artisan {$command} --path={$path} --realpath"); + + $this->call($command, [ + '--path' => $path, + '--realpath' => true, + ]); } else { $path = Str::after($path, base_path()); diff --git a/tests/CallDbPatchArtisanCommandTest.php b/tests/CallDbPatchArtisanCommandTest.php index ab4ee07..443c627 100644 --- a/tests/CallDbPatchArtisanCommandTest.php +++ b/tests/CallDbPatchArtisanCommandTest.php @@ -2,46 +2,102 @@ namespace Tests; -use Illuminate\Testing\PendingCommand; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\Artisan; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase; class CallDbPatchArtisanCommandTest extends TestCase { - private function expectsCommandChoice(PendingCommand $command, $input): PendingCommand + use DatabaseMigrations; + + protected static $published; + + protected function setUp(): void { - $options = [ - '2022_07_19_000000_add_priority_to_products_table.php', - '2022_07_19_000000_add_priority_to_products_table.php', - ]; + parent::setUp(); - $options = array_map(fn ($option) => $this->parseLabel($option), $options); + if (empty(static::$published)) { + Artisan::call('vendor:publish', [ + '--tag' => '@a2workspace/laravel-database-patcher', + ]); - if (is_string($input)) { - $answer = $this->parseLabel($input); - } else if (is_integer($input)) { - $answer = $options[$input]; + static::$published = database_path('patches'); } + } - return $command->expectsChoice( - '選擇補丁檔案', - $answer, - $options, - ); + public static function tearDownAfterClass(): void + { + @unlink(static::$published); + @rmdir(static::$published); } + /** + * @param string $option + * @return string + */ private function parseLabel(string $option): string { return "-> {$option}"; } + /** + * @param string $table + * @param string $column + * @return self + */ + private function assertDatabaseTableHasColumn($table, $column) + { + $this->assertTrue( + Schema::hasColumn($table, $column), + sprintf( + 'The table [%s] doesn\'t have the column named %s', + $table, + $column + ) + ); + + return $this; + } + // ========================================================================= // = Tests // ========================================================================= public function test_call_artisan_command() { + Schema::create('products', function (Blueprint $table) { + $table->increments('id'); + $table->string('ian')->unique(); + $table->string('name'); + }); + $command = $this->artisan('db:patch'); - $this->expectsCommandChoice($command, 0); + $command->expectsChoice( + '選擇補丁檔案', + $this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php'), + [ + $this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php'), + ], + ); + + $command->expectsOutput(sprintf( + 'Running: php artisan migrate --path=%s', + $this->resolvePath('/database/patches/2022_07_19_000000_add_priority_to_products_table.php') + )); + + $command->run(); + + $this->assertDatabaseHas( + 'migrations', + ['migration' => '2022_07_19_000000_add_priority_to_products_table'] + ); + + $this->assertDatabaseTableHasColumn('products', 'priority'); } + + // @depends } diff --git a/tests/TestCase.php b/tests/TestCase.php index 04eee99..5b4285e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -10,10 +10,7 @@ abstract class TestCase extends BaseTestCase { protected function getEnvironmentSetUp($app) { - $app['config']->set('database.patcher.paths', [ - __DIR__ . '/fixtures/patches', - __DIR__ . '/../publishes/patches', - ]); + // ... } protected function getPackageProviders($app) @@ -25,7 +22,14 @@ protected function getPackageProviders($app) // = Helpers // ========================================================================= - private function refMethod($name, $scope): Closure + /** + * 生成能執行被保護方法的反射函數 + * + * @param string $name + * @param object $scope + * @return \Closure + */ + protected function refMethod($name, $scope): Closure { $ref = function () use ($name) { return $this->$name(...func_get_args()); @@ -33,4 +37,15 @@ private function refMethod($name, $scope): Closure return $ref->bindTo($scope, $scope); } + + /** + * 處理不同作業系統下的路徑 + * + * @param string $path + * @return string + */ + protected function resolvePath(string $path): string + { + return str_replace('/', DIRECTORY_SEPARATOR, $path); + } } diff --git a/tests/fixtures/patches/2022_07_19_000000_add_priority_to_products_table.php b/tests/fixtures/patches/2022_07_19_000000_add_priority_to_products_table.php deleted file mode 100644 index 82baf24..0000000 --- a/tests/fixtures/patches/2022_07_19_000000_add_priority_to_products_table.php +++ /dev/null @@ -1,35 +0,0 @@ -tinyInteger('priority')->comment('優先度') - ->default(0); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::table('products', function (Blueprint $table) { - if (Schema::hasColumn($table->getTable(), 'priority')) { - $table->dropColumn('priority'); - } - }); - } -} From 58228f5c82fe0cd74abcea6625a69fa2322612c7 Mon Sep 17 00:00:00 2001 From: Shisha Date: Sun, 24 Jul 2022 22:25:14 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/CallDbPatchArtisanCommandTest.php | 94 +++++++++++++++---------- tests/TestArtisanCommandHelpers.php | 36 ++++++++++ 2 files changed, 93 insertions(+), 37 deletions(-) create mode 100644 tests/TestArtisanCommandHelpers.php diff --git a/tests/CallDbPatchArtisanCommandTest.php b/tests/CallDbPatchArtisanCommandTest.php index 443c627..9795af8 100644 --- a/tests/CallDbPatchArtisanCommandTest.php +++ b/tests/CallDbPatchArtisanCommandTest.php @@ -2,16 +2,15 @@ namespace Tests; -use Illuminate\Support\Facades\DB; +use Illuminate\Testing\PendingCommand; use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Artisan; use Illuminate\Database\Schema\Blueprint; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase; class CallDbPatchArtisanCommandTest extends TestCase { - use DatabaseMigrations; + use TestArtisanCommandHelpers; protected static $published; @@ -19,6 +18,12 @@ protected function setUp(): void { parent::setUp(); + Schema::create('products', function (Blueprint $table) { + $table->increments('id'); + $table->string('ian')->unique(); + $table->string('name'); + }); + if (empty(static::$published)) { Artisan::call('vendor:publish', [ '--tag' => '@a2workspace/laravel-database-patcher', @@ -34,32 +39,19 @@ public static function tearDownAfterClass(): void @rmdir(static::$published); } - /** - * @param string $option - * @return string - */ - private function parseLabel(string $option): string + private function expectsCommandChoice(PendingCommand $command, $answer): PendingCommand { - return "-> {$option}"; - } + $options = [ + $this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php'), + ]; - /** - * @param string $table - * @param string $column - * @return self - */ - private function assertDatabaseTableHasColumn($table, $column) - { - $this->assertTrue( - Schema::hasColumn($table, $column), - sprintf( - 'The table [%s] doesn\'t have the column named %s', - $table, - $column - ) + $command->expectsChoice( + '選擇補丁檔案', + $this->parseLabel($answer), + $options, ); - return $this; + return $command; } // ========================================================================= @@ -68,20 +60,11 @@ private function assertDatabaseTableHasColumn($table, $column) public function test_call_artisan_command() { - Schema::create('products', function (Blueprint $table) { - $table->increments('id'); - $table->string('ian')->unique(); - $table->string('name'); - }); - $command = $this->artisan('db:patch'); - $command->expectsChoice( - '選擇補丁檔案', - $this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php'), - [ - $this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php'), - ], + $this->expectsCommandChoice( + $command, + '2022_07_19_000000_add_priority_to_products_table.php' ); $command->expectsOutput(sprintf( @@ -100,4 +83,41 @@ public function test_call_artisan_command() } // @depends + + public function test_call_artisan_command_with_filter() + { + $command = $this->artisan('db:patch', [ + 'filter' => 'product', + ]); + + $this->expectsCommandChoice( + $command, + '2022_07_19_000000_add_priority_to_products_table.php' + ); + + $command->expectsOutput(sprintf( + 'Running: php artisan migrate --path=%s', + $this->resolvePath('/database/patches/2022_07_19_000000_add_priority_to_products_table.php') + )); + + $command->run(); + + $this->assertDatabaseHas( + 'migrations', + ['migration' => '2022_07_19_000000_add_priority_to_products_table'] + ); + + $this->assertDatabaseTableHasColumn('products', 'priority'); + } + + public function test_call_artisan_command_with_filter_then_not_found() + { + $command = $this->artisan('db:patch', [ + 'filter' => '__IMIFUMEI__', + ]); + + $command->expectsOutput('找不到符合的補丁檔案'); + $command->assertExitCode(1); + $command->run(); + } } diff --git a/tests/TestArtisanCommandHelpers.php b/tests/TestArtisanCommandHelpers.php new file mode 100644 index 0000000..7bcbe21 --- /dev/null +++ b/tests/TestArtisanCommandHelpers.php @@ -0,0 +1,36 @@ + {$option}"; + } + + /** + * @param string $table + * @param string $column + * @return self + */ + private function assertDatabaseTableHasColumn($table, $column) + { + $this->assertTrue( + Schema::hasColumn($table, $column), + sprintf( + 'The table [%s] doesn\'t have the column named %s', + $table, + $column + ) + ); + + return $this; + } +} From 717abf3ce61709bf1f2c6bb04547acd0a666021e Mon Sep 17 00:00:00 2001 From: Shisha Date: Sun, 24 Jul 2022 22:38:53 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E7=A7=BB=E9=99=A4=20realpath=20=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Commands/DbPatchCommand.php | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/src/Commands/DbPatchCommand.php b/src/Commands/DbPatchCommand.php index af3bbba..c1f1af8 100644 --- a/src/Commands/DbPatchCommand.php +++ b/src/Commands/DbPatchCommand.php @@ -2,10 +2,9 @@ namespace A2Workspace\DatabasePatcher\Commands; +use Illuminate\Support\Str; use Illuminate\Console\Command; use Illuminate\Support\Collection; -use Illuminate\Support\Str; -use Illuminate\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; @@ -195,31 +194,11 @@ protected function usingRevertion(): bool */ protected function callMigrateCommand($path, string $command = 'migrate'): int { - if ($this->shouldUseRealPath($path)) { - $this->info("Running: php artisan {$command} --path={$path} --realpath"); - - $this->call($command, [ - '--path' => $path, - '--realpath' => true, - ]); - } else { - $path = Str::after($path, base_path()); - - $this->info("Running: php artisan {$command} --path={$path}"); - $this->call($command, ['--path' => $path]); - } + $path = Str::after($path, base_path()); - return 0; - } + $this->info("Running: php artisan {$command} --path={$path}"); + $this->call($command, ['--path' => $path]); - /** - * 判定是否該使用完整路徑傳入 - * - * @param string $path - * @return bool - */ - protected function shouldUseRealPath($path): bool - { - return ! Str::startsWith($path, base_path()); + return 0; } } From 73a1905289a8b144a283d66c5017c0bba414a7ee Mon Sep 17 00:00:00 2001 From: Shisha Date: Mon, 25 Jul 2022 00:03:49 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20revert=20=E6=B8=AC?= =?UTF-8?q?=E8=A9=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 3 +- tests/CallDbPatchArtisanCommandTest.php | 70 +++++++++++++++++-------- tests/TestArtisanCommandHelpers.php | 19 +++++++ 3 files changed, 68 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index 5c9c3a9..4a48939 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "php": "^7.4|^8.0" }, "require-dev": { - "orchestra/testbench": "6.x" + "orchestra/testbench": "6.x", + "doctrine/dbal": ">=2.12.1" }, "autoload": { "psr-4": { diff --git a/tests/CallDbPatchArtisanCommandTest.php b/tests/CallDbPatchArtisanCommandTest.php index 9795af8..5962377 100644 --- a/tests/CallDbPatchArtisanCommandTest.php +++ b/tests/CallDbPatchArtisanCommandTest.php @@ -39,32 +39,24 @@ public static function tearDownAfterClass(): void @rmdir(static::$published); } - private function expectsCommandChoice(PendingCommand $command, $answer): PendingCommand - { - $options = [ - $this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php'), - ]; - - $command->expectsChoice( - '選擇補丁檔案', - $this->parseLabel($answer), - $options, - ); - - return $command; - } - // ========================================================================= // = Tests // ========================================================================= public function test_call_artisan_command() { + // ===================================================================== + // = Step 1: Install specified patch file. + // ===================================================================== + $command = $this->artisan('db:patch'); - $this->expectsCommandChoice( - $command, - '2022_07_19_000000_add_priority_to_products_table.php' + $command->expectsChoice( + '選擇補丁檔案', + $this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php'), + [ + $this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php') + ], ); $command->expectsOutput(sprintf( @@ -72,6 +64,7 @@ public function test_call_artisan_command() $this->resolvePath('/database/patches/2022_07_19_000000_add_priority_to_products_table.php') )); + $command->assertExitCode(0); $command->run(); $this->assertDatabaseHas( @@ -80,9 +73,36 @@ public function test_call_artisan_command() ); $this->assertDatabaseTableHasColumn('products', 'priority'); - } - // @depends + // ===================================================================== + // = Step 2: Revert it. + // ===================================================================== + + $command2 = $this->artisan('db:patch', ['--revert' => true]); + + $command2->expectsChoice( + '選擇補丁檔案', + $this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php'), + [ + $this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php') + ], + ); + + $command2->expectsOutput(sprintf( + 'Running: php artisan migrate:rollback --path=%s', + $this->resolvePath('/database/patches/2022_07_19_000000_add_priority_to_products_table.php') + )); + + $command2->assertExitCode(0); + $command2->run(); + + $this->assertDatabaseTableMissingColumn('products', 'priority'); + + $this->assertDatabaseMissing( + 'migrations', + ['migration' => '2022_07_19_000000_add_priority_to_products_table'] + ); + } public function test_call_artisan_command_with_filter() { @@ -90,9 +110,12 @@ public function test_call_artisan_command_with_filter() 'filter' => 'product', ]); - $this->expectsCommandChoice( - $command, - '2022_07_19_000000_add_priority_to_products_table.php' + $command->expectsChoice( + '選擇補丁檔案', + $this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php'), + [ + $this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php') + ], ); $command->expectsOutput(sprintf( @@ -100,6 +123,7 @@ public function test_call_artisan_command_with_filter() $this->resolvePath('/database/patches/2022_07_19_000000_add_priority_to_products_table.php') )); + $command->assertExitCode(0); $command->run(); $this->assertDatabaseHas( diff --git a/tests/TestArtisanCommandHelpers.php b/tests/TestArtisanCommandHelpers.php index 7bcbe21..342ceec 100644 --- a/tests/TestArtisanCommandHelpers.php +++ b/tests/TestArtisanCommandHelpers.php @@ -33,4 +33,23 @@ private function assertDatabaseTableHasColumn($table, $column) return $this; } + + /** + * @param string $table + * @param string $column + * @return self + */ + private function assertDatabaseTableMissingColumn($table, $column) + { + $this->assertFalse( + Schema::hasColumn($table, $column), + sprintf( + 'The table [%s] have the column named %s', + $table, + $column + ) + ); + + return $this; + } } From fc7347d96dc0f337388a9a797c3cc24502df674c Mon Sep 17 00:00:00 2001 From: Shisha Date: Mon, 25 Jul 2022 00:08:24 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=A6=86=E8=93=8B?= =?UTF-8?q?=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/CallDbPatchArtisanCommandTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/CallDbPatchArtisanCommandTest.php b/tests/CallDbPatchArtisanCommandTest.php index 5962377..e489b46 100644 --- a/tests/CallDbPatchArtisanCommandTest.php +++ b/tests/CallDbPatchArtisanCommandTest.php @@ -144,4 +144,15 @@ public function test_call_artisan_command_with_filter_then_not_found() $command->assertExitCode(1); $command->run(); } + + public function test_call_artisan_command_when_directory_is_empty() + { + $this->app->config->set('database.patcher.paths', []); + + $command = $this->artisan('db:patch'); + + $command->expectsOutput('找不到任何補丁檔案'); + $command->assertExitCode(1); + $command->run(); + } } From ab09de7aa5a60e577f281ed878382a39bc2b160b Mon Sep 17 00:00:00 2001 From: Shisha Date: Mon, 25 Jul 2022 00:09:12 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=A3=84=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/DbPatchCommandTest.php | 38 ------------------------------------ 1 file changed, 38 deletions(-) delete mode 100644 tests/DbPatchCommandTest.php diff --git a/tests/DbPatchCommandTest.php b/tests/DbPatchCommandTest.php deleted file mode 100644 index 0fe771a..0000000 --- a/tests/DbPatchCommandTest.php +++ /dev/null @@ -1,38 +0,0 @@ -setLaravel($this->app); - - return $command; - } - - // ========================================================================= - // = Tests - // ========================================================================= - - public function test_example() - { - $this->assertTrue(true); - } -} From 7a86e28b514535e4488265a363182a1d86d1de05 Mon Sep 17 00:00:00 2001 From: Shisha Date: Mon, 25 Jul 2022 00:12:05 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 4a48939..ce0c555 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ }, "require-dev": { "orchestra/testbench": "6.x", - "doctrine/dbal": ">=2.12.1" + "doctrine/dbal": "^2.12.1" }, "autoload": { "psr-4": {