Skip to content

Commit

Permalink
Merge pull request #3 from cgauge/abdala-patch-1
Browse files Browse the repository at this point in the history
Throw exception after rollback
  • Loading branch information
abdala authored Jun 19, 2023
2 parents a4a062f + e136dad commit 6b93be7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/Strategy/RollbackOnFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ public function execute(callable $callback, array $context = []): void
$task = $callback();

array_unshift($this->executed, $task);
} catch (Throwable) {
} catch (Throwable $e) {
$this->rollback();

throw $e;
}
}

Expand All @@ -59,7 +61,10 @@ public function rollback(): void
});

foreach ($tasks as $task) {
$task->reverse($this->context);
try {
$task->reverse($this->context);
} catch (Throwable) {
}
}
}
}
8 changes: 7 additions & 1 deletion tests/Strategy/RollbackOnFailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function test_it_rollback_when_a_task_fail(): void
->method('reverse');

$createFolder->method('run')
->willThrowException(new Exception());
->willThrowException(new CreateFolderException());

$strategy = new RollbackOnFailure();

Expand All @@ -53,12 +53,18 @@ public function test_it_rollback_when_a_task_fail(): void
return $createEmail;
});

$this->expectException(CreateFolderException::class);

$strategy->execute(static function () use ($createFolder): void {
$createFolder->run([]);
});
}
}

class CreateFolderException extends Exception
{
}

interface ReversibleTask extends Task, Reversible
{
}

0 comments on commit 6b93be7

Please sign in to comment.