Skip to content

Commit 1493ce1

Browse files
committed
execute action in transaction and always rollback so that testing an action has no side effects
1 parent 88093f2 commit 1493ce1

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/Backend/Action/Action/Execute.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace Fusio\Impl\Backend\Action\Action;
2222

23+
use Doctrine\DBAL\Connection;
2324
use Fusio\Engine\ActionInterface;
2425
use Fusio\Engine\ContextInterface;
2526
use Fusio\Engine\ParametersInterface;
@@ -31,6 +32,8 @@
3132
use PSX\Http\Environment\HttpResponseInterface;
3233
use PSX\Http\Response;
3334
use PSX\Http\Writer\WriterInterface;
35+
use stdClass;
36+
use Throwable;
3437

3538
/**
3639
* Execute
@@ -39,14 +42,12 @@
3942
* @license http://www.apache.org/licenses/LICENSE-2.0
4043
* @link https://www.fusio-project.org
4144
*/
42-
class Execute implements ActionInterface
45+
readonly class Execute implements ActionInterface
4346
{
44-
private Action\Executor $actionExecutorService;
4547
private Converter $exceptionConverter;
4648

47-
public function __construct(Action\Executor $actionExecutorService)
49+
public function __construct(private Action\Executor $actionExecutorService, private Connection $connection)
4850
{
49-
$this->actionExecutorService = $actionExecutorService;
5051
$this->exceptionConverter = new Converter(true);
5152
}
5253

@@ -56,6 +57,8 @@ public function handle(RequestInterface $request, ParametersInterface $configura
5657

5758
assert($body instanceof ActionExecuteRequest);
5859

60+
$this->connection->beginTransaction();
61+
5962
try {
6063
$response = $this->actionExecutorService->execute(
6164
$request->get('action_id'),
@@ -74,30 +77,34 @@ public function handle(RequestInterface $request, ParametersInterface $configura
7477
$body = (string) $tempResponse->getBody();
7578
}
7679

77-
return [
80+
$return = [
7881
'statusCode' => $response->getStatusCode(),
7982
'headers' => $headers,
8083
'body' => $body,
8184
];
8285
} else {
83-
return [
86+
$return = [
8487
'statusCode' => 200,
85-
'headers' => new \stdClass(),
88+
'headers' => new stdClass(),
8689
'body' => $response,
8790
];
8891
}
89-
} catch (\Throwable $e) {
92+
} catch (Throwable $e) {
9093
if ($e instanceof MessageException) {
9194
$body = $e->getPayload();
9295
} else {
9396
$body = $this->exceptionConverter->convert($e);
9497
}
9598

96-
return [
99+
$return = [
97100
'statusCode' => 500,
98-
'headers' => new \stdClass(),
101+
'headers' => new stdClass(),
99102
'body' => $body,
100103
];
101104
}
105+
106+
$this->connection->rollBack();
107+
108+
return $return;
102109
}
103110
}

0 commit comments

Comments
 (0)