44
55use Illuminate \Foundation \Testing \RefreshDatabase ;
66use Illuminate \Support \Facades \Artisan ;
7+ use Illuminate \Support \Facades \Event ;
78use JTMcC \AtomicDeployments \Events \DeploymentFailed ;
89use JTMcC \AtomicDeployments \Events \DeploymentSuccessful ;
910use JTMcC \AtomicDeployments \Exceptions \InvalidPathException ;
@@ -15,13 +16,12 @@ class DeployCommandTest extends TestCase
1516{
1617 use RefreshDatabase;
1718
18- /**
19- * @test
20- */
21- public function it_allows_dry_run_with_no_mutations ()
19+ public function test_it_allows_dry_run_with_no_mutations ()
2220 {
21+ // Act
2322 Artisan::call ('atomic-deployments:deploy --dry-run --directory=test-dir-1 ' );
2423
24+ // Assert
2525 $ this ->seeInConsoleOutput ([
2626 'Deployment directory option set - Deployment will use directory: test-dir-1 ' ,
2727 'Running Deployment... ' ,
@@ -34,24 +34,21 @@ public function it_allows_dry_run_with_no_mutations()
3434 ]);
3535
3636 $ this ->dontSeeInConsoleOutput ('Atomic deployment rollback has been requested ' );
37-
38- $ this ->assertFalse ($ this ->fileSystem ->exists ($ this ->deploymentsPath .'/test-dir-1/ ' ));
37+ $ this ->assertFalse ($ this ->fileSystem ->exists ($ this ->deploymentsPath . '/test-dir-1/ ' ));
3938 $ this ->assertFalse ($ this ->fileSystem ->exists ($ this ->deploymentLink ));
4039 $ this ->assertEmpty (AtomicDeployment::all ());
4140 }
4241
43- /**
44- * @test
45- */
46- public function it_does_not_migrate_on_dry_run ()
42+ public function test_it_does_not_migrate_on_dry_run ()
4743 {
44+ // Collect
4845 Artisan::call ('atomic-deployments:deploy --directory=test-dir-1 ' );
46+ $ this ->fileSystem ->ensureDirectoryExists ($ this ->deploymentsPath . '/test-dir-1/migration/test-folder ' );
4947
50- //add file to 'deployment' that does not exist in 'build' for migrate test
51- $ this ->fileSystem ->ensureDirectoryExists ($ this ->deploymentsPath .'/test-dir-1/migration/test-folder ' );
52-
48+ // Act
5349 Artisan::call ('atomic-deployments:deploy --dry-run --directory=test-dir-2 ' );
5450
51+ // Assert
5552 $ this ->seeInConsoleOutput ([
5653 'Deployment directory option set - Deployment will use directory: test-dir-2 ' ,
5754 'Running Deployment... ' ,
@@ -60,18 +57,16 @@ public function it_does_not_migrate_on_dry_run()
6057 ]);
6158
6259 $ this ->dontSeeInConsoleOutput ('Atomic deployment rollback has been requested ' );
63-
64- $ this ->assertTrue ($ this ->fileSystem ->exists ($ this ->deploymentsPath .'/test-dir-1/migration/test-folder ' ));
65- $ this ->assertFalse ($ this ->fileSystem ->exists ($ this ->deploymentsPath .'/test-dir-2/migration/test-folder ' ));
60+ $ this ->assertTrue ($ this ->fileSystem ->exists ($ this ->deploymentsPath . '/test-dir-1/migration/test-folder ' ));
61+ $ this ->assertFalse ($ this ->fileSystem ->exists ($ this ->deploymentsPath . '/test-dir-2/migration/test-folder ' ));
6662 }
6763
68- /**
69- * @test
70- */
71- public function it_allows_run_with_mutations ()
64+ public function test_it_allows_run_with_mutations ()
7265 {
66+ // Act
7367 Artisan::call ('atomic-deployments:deploy --directory=test-dir ' );
7468
69+ // Assert
7570 $ this ->seeInConsoleOutput ([
7671 'Deployment directory option set - Deployment will use directory: test-dir ' ,
7772 'Running Deployment... ' ,
@@ -84,28 +79,25 @@ public function it_allows_run_with_mutations()
8479 'Atomic deployment rollback has been requested ' ,
8580 ]);
8681
87- $ this ->assertTrue ($ this ->fileSystem ->exists ($ this ->deploymentsPath . '/test-dir/build-contents-folder ' ));
82+ $ this ->assertTrue ($ this ->fileSystem ->exists ($ this ->deploymentsPath . '/test-dir/build-contents-folder ' ));
8883 $ this ->assertTrue ($ this ->fileSystem ->exists ($ this ->deploymentLink ));
8984
9085 $ deployment = AtomicDeployment::first ();
9186 $ this ->assertNotEmpty ($ deployment );
92- $ this ->assertTrue ((int ) $ deployment ->deployment_status === DeploymentStatus::SUCCESS );
87+ $ this ->assertTrue ((int )$ deployment ->deployment_status === DeploymentStatus::SUCCESS );
9388 }
9489
95- /**
96- * @test
97- */
98- public function it_allows_migrate_on_run ()
90+ public function test_it_allows_migrate_on_run ()
9991 {
92+ // Collect
10093 Artisan::call ('atomic-deployments:deploy --directory=test-dir-1 ' );
94+ $ this ->fileSystem ->ensureDirectoryExists ($ this ->deploymentsPath . '/test-dir-1/migration/test-folder ' );
95+ $ this ->assertFalse ($ this ->fileSystem ->exists ($ this ->deploymentsPath . '/test-dir-2/migration/test-folder ' ));
10196
102- //add file to 'deployment' that does not exist in 'build' for migrate test
103- $ this ->fileSystem ->ensureDirectoryExists ($ this ->deploymentsPath .'/test-dir-1/migration/test-folder ' );
104-
105- $ this ->assertFalse ($ this ->fileSystem ->exists ($ this ->deploymentsPath .'/test-dir-2/migration/test-folder ' ));
106-
97+ // Act
10798 Artisan::call ('atomic-deployments:deploy --directory=test-dir-2 ' );
10899
100+ // Assert
109101 $ this ->seeInConsoleOutput ([
110102 'Deployment directory option set - Deployment will use directory: test-dir-2 ' ,
111103 'Running Deployment... ' ,
@@ -114,118 +106,119 @@ public function it_allows_migrate_on_run()
114106 ]);
115107
116108 $ this ->dontSeeInConsoleOutput ('Atomic deployment rollback has been requested ' );
117-
118- $ this ->assertTrue ($ this ->fileSystem ->exists ($ this ->deploymentsPath .'/test-dir-1/migration/test-folder ' ));
119-
120- //confirm migrate logic copied test folder
121- $ this ->assertTrue ($ this ->fileSystem ->exists ($ this ->deploymentsPath .'/test-dir-2/migration/test-folder ' ));
109+ $ this ->assertTrue ($ this ->fileSystem ->exists ($ this ->deploymentsPath . '/test-dir-1/migration/test-folder ' ));
110+ $ this ->assertTrue ($ this ->fileSystem ->exists ($ this ->deploymentsPath . '/test-dir-2/migration/test-folder ' ));
122111 }
123112
124- /**
125- * @test
126- */
127- public function it_allows_swapping_between_deployments ()
113+ public function test_it_allows_swapping_between_deployments ()
128114 {
129-
130- //create two builds
115+ // Collect
131116 Artisan::call ('atomic-deployments:deploy --directory=test-dir-1 ' );
132117 Artisan::call ('atomic-deployments:deploy --directory=test-dir-2 ' );
118+ $ deployment1 = AtomicDeployment::where ('commit_hash ' , 'test-dir-1 ' )->first ()->append (
119+ 'isCurrentlyDeployed '
120+ )->toArray ();
121+ $ deployment2 = AtomicDeployment::where ('commit_hash ' , 'test-dir-2 ' )->first ()->append (
122+ 'isCurrentlyDeployed '
123+ )->toArray ();
133124
134- $ deployment1 = AtomicDeployment::where ('commit_hash ' , 'test-dir-1 ' )->first ()->append ('isCurrentlyDeployed ' )->toArray ();
135- $ deployment2 = AtomicDeployment::where ('commit_hash ' , 'test-dir-2 ' )->first ()->append ('isCurrentlyDeployed ' )->toArray ();
136-
137- //confirm our last build is currently deployed
138125 $ this ->assertFalse ($ deployment1 ['isCurrentlyDeployed ' ]);
139126 $ this ->assertTrue ($ deployment2 ['isCurrentlyDeployed ' ]);
140127
128+ // Act
141129 Artisan::call ('atomic-deployments:deploy --hash=test-dir-fake ' );
142130
143- //confirm build must exist when attempting to swap
131+ // Assert
144132 $ this ->seeInConsoleOutput ([
145133 'Updating symlink to previous build: test-dir-fake ' ,
146134 'Build not found for hash: test-dir-fake ' ,
147135 ]);
148136
137+ // Act
149138 Artisan::call ('atomic-deployments:deploy --hash=test-dir-1 ' );
150139
151- //swap build to our first deployment
140+ // Assert
152141 $ this ->seeInConsoleOutput ([
153142 'Updating symlink to previous build: test-dir-1 ' ,
154143 'Build link confirmed ' ,
155144 ]);
156145
157- $ deployment1 = AtomicDeployment::where ('commit_hash ' , 'test-dir-1 ' )->first ()->append ('isCurrentlyDeployed ' )->toArray ();
158- $ deployment2 = AtomicDeployment::where ('commit_hash ' , 'test-dir-2 ' )->first ()->append ('isCurrentlyDeployed ' )->toArray ();
146+ $ deployment1 = AtomicDeployment::where ('commit_hash ' , 'test-dir-1 ' )->first ()->append (
147+ 'isCurrentlyDeployed '
148+ )->toArray ();
149+ $ deployment2 = AtomicDeployment::where ('commit_hash ' , 'test-dir-2 ' )->first ()->append (
150+ 'isCurrentlyDeployed '
151+ )->toArray ();
159152
160- //confirm first deployment is now live and second is not
161153 $ this ->assertTrue ($ deployment1 ['isCurrentlyDeployed ' ]);
162154 $ this ->assertFalse ($ deployment2 ['isCurrentlyDeployed ' ]);
163155 }
164156
165- /**
166- * @test
167- */
168- public function it_cleans_old_build_folders_based_on_build_limit ()
157+ public function test_it_cleans_old_build_folders_based_on_build_limit ()
169158 {
170- $ this ->app ['config ' ]->set ('atomic-deployments.build-limit ' , 1 );
171-
172- Artisan::call ('atomic-deployments:deploy --directory=test-dir-1 ' );
173- Artisan::call ('atomic-deployments:deploy --directory=test-dir-2 ' );
174-
175- $ this ->assertTrue (AtomicDeployment::all ()->count () === 1 );
176- $ this ->assertTrue (AtomicDeployment::withTrashed ()->get ()->count () === 2 );
177-
178- AtomicDeployment::truncate ();
179-
159+ // Collect
180160 $ this ->app ['config ' ]->set ('atomic-deployments.build-limit ' , 3 );
181161
162+ // Act
182163 Artisan::call ('atomic-deployments:deploy --directory=test-dir-1 ' );
183164 Artisan::call ('atomic-deployments:deploy --directory=test-dir-2 ' );
184165 Artisan::call ('atomic-deployments:deploy --directory=test-dir-3 ' );
185166 Artisan::call ('atomic-deployments:deploy --directory=test-dir-4 ' );
186167 Artisan::call ('atomic-deployments:deploy --directory=test-dir-5 ' );
187168
169+ // Assert
188170 $ this ->assertTrue (AtomicDeployment::all ()->count () === 3 );
189171 $ this ->assertTrue (AtomicDeployment::withTrashed ()->get ()->count () === 5 );
190172 }
191173
192- /**
193- * @test
194- */
195- public function it_dispatches_deployment_successful_event_on_build ()
174+ public function test_it_dispatches_deployment_successful_event_on_build ()
196175 {
197- $ this ->expectsEvents (DeploymentSuccessful::class);
176+ // Collect
177+ Event::fake ();
178+
179+ // Act
198180 Artisan::call ('atomic-deployments:deploy --directory=test-dir-1 ' );
181+
182+ // Assert
183+ Event::assertDispatched (DeploymentSuccessful::class);
199184 }
200185
201- /**
202- * @test
203- */
204- public function it_dispatches_deployment_successful_event_on_deployment_swap ()
186+ public function test_it_dispatches_deployment_successful_event_on_deployment_swap ()
205187 {
188+ // Collect
189+ Event::fake ();
206190 Artisan::call ('atomic-deployments:deploy --directory=test-dir-1 ' );
207191 Artisan::call ('atomic-deployments:deploy --directory=test-dir-2 ' );
208-
209- $ deployment = AtomicDeployment::where ('commit_hash ' , 'test-dir-2 ' )->first ()->append ('isCurrentlyDeployed ' )->toArray ();
192+ $ deployment = AtomicDeployment::where ('commit_hash ' , 'test-dir-2 ' )->first ()->append (
193+ 'isCurrentlyDeployed '
194+ )->toArray ();
210195 $ this ->assertTrue ($ deployment ['isCurrentlyDeployed ' ]);
211196
212- $ this ->expectsEvents (DeploymentSuccessful::class);
213-
197+ // Act
214198 Artisan::call ('atomic-deployments:deploy --hash=test-dir-1 ' );
215- $ deployment = AtomicDeployment::where ('commit_hash ' , 'test-dir-1 ' )->first ()->append ('isCurrentlyDeployed ' )->toArray ();
199+
200+ // Assert
201+ $ deployment = AtomicDeployment::where ('commit_hash ' , 'test-dir-1 ' )->first ()->append (
202+ 'isCurrentlyDeployed '
203+ )->toArray ();
216204 $ this ->assertTrue ($ deployment ['isCurrentlyDeployed ' ]);
205+ Event::assertDispatched (DeploymentSuccessful::class);
217206 }
218207
219- /**
220- * @test
221- */
222- public function it_dispatches_deployment_failed_event_on_build_fail ()
208+ public function test_it_dispatches_deployment_failed_event_on_build_fail ()
223209 {
224- //force invalid path exception
225- $ this -> expectException (InvalidPathException::class );
210+ // Collect
211+ Event:: fake ( );
226212 $ this ->app ['config ' ]->set ('atomic-deployments.build-path ' , $ this ->buildPath );
227- $ this ->app ['config ' ]->set ('atomic-deployments.deployments-path ' , $ this ->buildPath .'/deployments ' );
228- $ this ->expectsEvents (DeploymentFailed::class);
213+ $ this ->app ['config ' ]->set ('atomic-deployments.deployments-path ' , $ this ->buildPath . '/deployments ' );
214+
215+ // Assert
216+ $ this ->expectException (InvalidPathException::class);
217+
218+ // Act
229219 Artisan::call ('atomic-deployments:deploy --directory=test-dir-1 ' );
220+
221+ // Assert
222+ Event::assertDispatched (DeploymentFailed::class);
230223 }
231- }
224+ }
0 commit comments