22
33namespace Tests ;
44
5- use Illuminate \Testing \ PendingCommand ;
5+ use Illuminate \Filesystem \ Filesystem ;
66use Illuminate \Support \Facades \Schema ;
77use Illuminate \Support \Facades \Artisan ;
88use Illuminate \Database \Schema \Blueprint ;
@@ -18,10 +18,9 @@ protected function setUp(): void
1818 {
1919 parent ::setUp ();
2020
21- Schema::create ('products ' , function (Blueprint $ table ) {
21+ Schema::create ('users ' , function (Blueprint $ table ) {
2222 $ table ->increments ('id ' );
23- $ table ->string ('ian ' )->unique ();
24- $ table ->string ('name ' );
23+ $ table ->string ('username ' );
2524 });
2625
2726 if (empty (static ::$ published )) {
@@ -35,8 +34,7 @@ protected function setUp(): void
3534
3635 public static function tearDownAfterClass (): void
3736 {
38- @unlink (static ::$ published );
39- @rmdir (static ::$ published );
37+ (new Filesystem )->cleanDirectory (static ::$ published );
4038 }
4139
4240 // =========================================================================
@@ -45,93 +43,99 @@ public static function tearDownAfterClass(): void
4543
4644 public function test_call_artisan_command ()
4745 {
48- // =====================================================================
49- // = Step 1: Install specified patch file.
50- // =====================================================================
51-
5246 $ command = $ this ->artisan ('db:patch ' );
5347
5448 $ command ->expectsChoice (
5549 '選擇補丁檔案 ' ,
56- $ this ->parseLabel ('2022_07_19_000000_add_priority_to_products_table.php ' ),
50+ $ this ->parseLabel ('2022_07_19_000000_add_soft_deletes_to_users_table ' ),
5751 [
58- $ this ->parseLabel ('2022_07_19_000000_add_priority_to_products_table.php ' )
52+ $ this ->parseLabel ('2022_07_19_000000_add_soft_deletes_to_users_table ' )
5953 ],
6054 );
6155
6256 $ command ->expectsOutput (sprintf (
6357 'Running: php artisan migrate --path=%s ' ,
64- $ this ->resolvePath ('/database/patches/2022_07_19_000000_add_priority_to_products_table .php ' )
58+ $ this ->resolvePath ('/database/patches/2022_07_19_000000_add_soft_deletes_to_users_table .php ' )
6559 ));
6660
6761 $ command ->assertExitCode (0 );
6862 $ command ->run ();
6963
7064 $ this ->assertDatabaseHas (
7165 'migrations ' ,
72- ['migration ' => '2022_07_19_000000_add_priority_to_products_table ' ]
66+ ['migration ' => '2022_07_19_000000_add_soft_deletes_to_users_table ' ]
7367 );
7468
75- $ this ->assertDatabaseTableHasColumn ('products ' , 'priority ' );
69+ $ this ->assertDatabaseTableHasColumn ('users ' , 'deleted_at ' );
70+ }
7671
77- // =====================================================================
78- // = Step 2: Revert it.
79- // =====================================================================
72+ public function test_call_artisan_command_and_revert ()
73+ {
74+ $ this ->artisan ('db:patch ' )
75+ ->expectsChoice (
76+ '選擇補丁檔案 ' ,
77+ $ this ->parseLabel ('2022_07_19_000000_add_soft_deletes_to_users_table ' ),
78+ [
79+ $ this ->parseLabel ('2022_07_19_000000_add_soft_deletes_to_users_table ' )
80+ ],
81+ )
82+ ->assertExitCode (0 )
83+ ->run ();
8084
8185 $ command2 = $ this ->artisan ('db:patch ' , ['--revert ' => true ]);
8286
8387 $ command2 ->expectsChoice (
8488 '選擇補丁檔案 ' ,
85- $ this ->parseLabel ( ' 2022_07_19_000000_add_priority_to_products_table.php ' ),
89+ $ this ->parseInstalledLabel ( ' 2022_07_19_000000_add_soft_deletes_to_users_table ' ),
8690 [
87- $ this ->parseLabel ( ' 2022_07_19_000000_add_priority_to_products_table.php ' )
91+ $ this ->parseInstalledLabel ( ' 2022_07_19_000000_add_soft_deletes_to_users_table ' )
8892 ],
8993 );
9094
9195 $ command2 ->expectsOutput (sprintf (
9296 'Running: php artisan migrate:rollback --path=%s ' ,
93- $ this ->resolvePath ('/database/patches/2022_07_19_000000_add_priority_to_products_table .php ' )
97+ $ this ->resolvePath ('/database/patches/2022_07_19_000000_add_soft_deletes_to_users_table .php ' )
9498 ));
9599
96100 $ command2 ->assertExitCode (0 );
97101 $ command2 ->run ();
98102
99- $ this ->assertDatabaseTableMissingColumn ('products ' , 'priority ' );
103+ $ this ->assertDatabaseTableMissingColumn ('users ' , 'deleted_at ' );
100104
101105 $ this ->assertDatabaseMissing (
102106 'migrations ' ,
103- ['migration ' => '2022_07_19_000000_add_priority_to_products_table ' ]
107+ ['migration ' => '2022_07_19_000000_add_soft_deletes_to_users_table ' ]
104108 );
105109 }
106110
107111 public function test_call_artisan_command_with_filter ()
108112 {
109113 $ command = $ this ->artisan ('db:patch ' , [
110- 'filter ' => 'product ' ,
114+ 'filter ' => 'users ' ,
111115 ]);
112116
113117 $ command ->expectsChoice (
114118 '選擇補丁檔案 ' ,
115- $ this ->parseLabel ('2022_07_19_000000_add_priority_to_products_table.php ' ),
119+ $ this ->parseLabel ('2022_07_19_000000_add_soft_deletes_to_users_table ' ),
116120 [
117- $ this ->parseLabel ('2022_07_19_000000_add_priority_to_products_table.php ' )
121+ $ this ->parseLabel ('2022_07_19_000000_add_soft_deletes_to_users_table ' )
118122 ],
119123 );
120124
121125 $ command ->expectsOutput (sprintf (
122126 'Running: php artisan migrate --path=%s ' ,
123- $ this ->resolvePath ('/database/patches/2022_07_19_000000_add_priority_to_products_table .php ' )
127+ $ this ->resolvePath ('/database/patches/2022_07_19_000000_add_soft_deletes_to_users_table .php ' )
124128 ));
125129
126130 $ command ->assertExitCode (0 );
127131 $ command ->run ();
128132
129133 $ this ->assertDatabaseHas (
130134 'migrations ' ,
131- ['migration ' => '2022_07_19_000000_add_priority_to_products_table ' ]
135+ ['migration ' => '2022_07_19_000000_add_soft_deletes_to_users_table ' ]
132136 );
133137
134- $ this ->assertDatabaseTableHasColumn ('products ' , 'priority ' );
138+ $ this ->assertDatabaseTableHasColumn ('users ' , 'deleted_at ' );
135139 }
136140
137141 public function test_call_artisan_command_with_filter_then_not_found ()
0 commit comments