Skip to content

Commit 36f762b

Browse files
author
Andrey Helldar
committed
Method to be called on runtime error
1 parent 6d760e6 commit 36f762b

File tree

5 files changed

+126
-27
lines changed

5 files changed

+126
-27
lines changed

src/Support/Actionable.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,13 @@ public function allow(): bool
126126
public function success(): void
127127
{
128128
}
129+
130+
/**
131+
* The method will be called if an error occurs.
132+
*
133+
* @return void
134+
*/
135+
public function failed(): void
136+
{
137+
}
129138
}

src/Support/Migrator.php

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace DragonCode\LaravelActions\Support;
44

5+
use DragonCode\Contracts\LaravelActions\Actionable as ActionableContract;
56
use DragonCode\LaravelActions\Concerns\Infoable;
67
use Illuminate\Database\Migrations\Migrator as BaseMigrator;
78
use Illuminate\Support\Facades\DB;
9+
use Throwable;
810

911
class Migrator extends BaseMigrator
1012
{
@@ -73,6 +75,8 @@ protected function runUp($file, $batch, $pretend)
7375
* @param string $file
7476
* @param object $migration
7577
* @param bool $pretend
78+
*
79+
* @throws \Throwable
7680
*/
7781
protected function runDown($file, $migration, $pretend)
7882
{
@@ -87,31 +91,29 @@ protected function runDown($file, $migration, $pretend)
8791
}
8892

8993
parent::runDown($file, $migration, $pretend);
90-
91-
$this->runSuccess($instance);
9294
}
9395

9496
/**
9597
* Starts the execution of code, starting database transactions, if necessary.
9698
*
9799
* @param object $migration
98100
* @param string $method
101+
*
102+
* @throws \Throwable
99103
*/
100104
protected function runMigration($migration, $method)
101105
{
102-
if ($this->enabledTransactions($migration)) {
103-
DB::transaction(function () use ($migration, $method) {
104-
parent::runMigration($migration, $method);
105-
106-
$this->runSuccess($migration);
107-
}, $this->transactionAttempts($migration));
108-
109-
return;
110-
}
106+
$this->runMigrationHandle($migration, function ($migration) use ($method) {
107+
if ($this->enabledTransactions($migration)) {
108+
DB::transaction(function () use ($migration, $method) {
109+
parent::runMigration($migration, $method);
110+
}, $this->transactionAttempts($migration));
111111

112-
parent::runMigration($migration, $method);
112+
return;
113+
}
113114

114-
$this->runSuccess($migration);
115+
parent::runMigration($migration, $method);
116+
});
115117
}
116118

117119
/**
@@ -182,8 +184,43 @@ protected function transactionAttempts($migration): int
182184
return (int) abs($value);
183185
}
184186

185-
protected function runSuccess($migration): void
187+
/**
188+
* @param \DragonCode\Contracts\LaravelActions\Actionable|object $migration
189+
* @param callable $handle
190+
*
191+
* @throws \Throwable
192+
* @return void
193+
*/
194+
protected function runMigrationHandle(ActionableContract $migration, callable $handle)
195+
{
196+
try {
197+
$handle($migration);
198+
199+
$this->runSuccess($migration);
200+
} catch (Throwable $e) {
201+
$this->runFailed($migration);
202+
203+
throw $e;
204+
}
205+
}
206+
207+
/**
208+
* @param \DragonCode\Contracts\LaravelActions\Actionable|object $migration
209+
*
210+
* @return void
211+
*/
212+
protected function runSuccess(ActionableContract $migration): void
186213
{
187214
$migration->success();
188215
}
216+
217+
/**
218+
* @param \DragonCode\Contracts\LaravelActions\Actionable|object $migration
219+
*
220+
* @return void
221+
*/
222+
protected function runFailed(ActionableContract $migration): void
223+
{
224+
//$migration->failed();
225+
}
189226
}

tests/Commands/MigrateTest.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Tests\Commands;
44

55
use Exception;
6+
use Illuminate\Support\Str;
67
use Tests\TestCase;
8+
use Throwable;
79

810
class MigrateTest extends TestCase
911
{
@@ -131,7 +133,6 @@ public function testSingleEnvironment()
131133
$this->assertDatabaseMigrationHas($this->table, 'run_on_testing');
132134
$this->assertDatabaseMigrationHas($this->table, 'run_except_production');
133135
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_testing');
134-
$this->artisan('migrate:actions')->run();
135136
}
136137

137138
public function testManyEnvironments()
@@ -173,7 +174,6 @@ public function testManyEnvironments()
173174
$this->assertDatabaseMigrationHas($this->table, 'run_except_production');
174175
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_testing');
175176
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_except_many_environments');
176-
$this->artisan('migrate:actions')->run();
177177
}
178178

179179
public function testAllow()
@@ -200,7 +200,6 @@ public function testAllow()
200200
$this->assertDatabaseCount($this->table, 7);
201201
$this->assertDatabaseMigrationHas($this->table, 'run_allow');
202202
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_disallow');
203-
$this->artisan('migrate:actions')->run();
204203
}
205204

206205
public function testUpSuccess()
@@ -219,24 +218,40 @@ public function testUpSuccess()
219218
$this->assertDatabaseCount($table, 2);
220219
$this->assertDatabaseCount($this->table, 7);
221220
$this->assertDatabaseMigrationHas($this->table, 'run_success');
222-
$this->artisan('migrate:actions')->run();
223221
}
224222

225-
public function testUpSuccessOnFailed()
223+
public function testUpFailed()
226224
{
227-
$this->expectException(Exception::class);
228-
$this->expectExceptionMessage('Custom exception');
229-
230-
$this->copyFiles(true);
225+
$this->copyFiles();
231226

232227
$table = 'success';
233228

234229
$this->artisan('migrate:actions:install')->run();
235230

236231
$this->assertDatabaseCount($table, 0);
237232
$this->assertDatabaseCount($this->table, 0);
238-
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_success');
233+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_success_on_failed');
239234
$this->artisan('migrate:actions')->run();
235+
236+
$this->assertDatabaseCount($table, 2);
237+
$this->assertDatabaseCount($this->table, 7);
238+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_success_on_failed');
239+
240+
try {
241+
$this->copyFiles(true);
242+
243+
$this->artisan('migrate:actions')->run();
244+
} catch (Throwable $e) {
245+
$this->assertInstanceOf(Exception::class, $e);
246+
247+
$this->assertSame('Custom exception', $e->getMessage());
248+
249+
$this->assertTrue(Str::contains($e->getFile(), 'run_success_on_failed'));
250+
}
251+
252+
$this->assertDatabaseCount($table, 2);
253+
$this->assertDatabaseCount($this->table, 7);
254+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_success_on_failed');
240255
}
241256

242257
public function testPathAsFileWithExtension()

tests/Commands/RollbackTest.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Tests\Commands;
44

5+
use Exception;
6+
use Illuminate\Support\Str;
57
use Tests\TestCase;
8+
use Throwable;
69

710
class RollbackTest extends TestCase
811
{
@@ -92,11 +95,46 @@ public function testUpSuccess()
9295
$this->assertDatabaseCount($table, 2);
9396
$this->assertDatabaseCount($this->table, 7);
9497
$this->assertDatabaseMigrationHas($this->table, 'run_success');
95-
$this->artisan('migrate:actions')->run();
9698

9799
$this->artisan('migrate:actions:rollback')->run();
98-
$this->assertDatabaseCount($table, 5);
100+
$this->assertDatabaseCount($table, 4);
99101
$this->assertDatabaseCount($this->table, 0);
100102
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_success');
101103
}
104+
105+
public function testUpFailed()
106+
{
107+
$this->copyFiles();
108+
109+
$table = 'success';
110+
111+
$this->artisan('migrate:actions:install')->run();
112+
113+
$this->assertDatabaseCount($table, 0);
114+
$this->assertDatabaseCount($this->table, 0);
115+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_success_on_failed');
116+
$this->artisan('migrate:actions')->run();
117+
118+
$this->assertDatabaseCount($table, 2);
119+
$this->assertDatabaseCount($this->table, 7);
120+
$this->assertDatabaseMigrationDoesntLike($this->table, 'run_success_on_failed');
121+
122+
try {
123+
$this->copyFiles(true);
124+
125+
$this->table()->insert(['migration' => 'run_success_on_failed', 'batch' => 999]);
126+
127+
$this->artisan('migrate:actions:rollback')->run();
128+
} catch (Throwable $e) {
129+
$this->assertInstanceOf(Exception::class, $e);
130+
131+
$this->assertSame('Custom exception', $e->getMessage());
132+
133+
$this->assertTrue(Str::contains($e->getFile(), 'run_success_on_failed'));
134+
}
135+
136+
$this->assertDatabaseCount($table, 2);
137+
$this->assertDatabaseCount($this->table, 8);
138+
$this->assertDatabaseMigrationHas($this->table, 'run_success_on_failed');
139+
}
102140
}

tests/fixtures/actions_failed/2021_12_23_165047_run_success_on_failed.php renamed to tests/fixtures/actions_failed/2021_12_23_165048_run_success_on_failed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function up(): void
1414

1515
public function down(): void
1616
{
17-
throw new Exception();
17+
throw new Exception('Custom exception');
1818
}
1919

2020
public function success(): void

0 commit comments

Comments
 (0)