-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Execute delete and submit actions in database transaction
- Loading branch information
Dominic Tubach
committed
Jul 30, 2024
1 parent
fef1cef
commit d0df501
Showing
6 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
Civi/RemoteTools/ActionHandler/RemoteActionsHandlerTransactionDecorator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Civi\RemoteTools\ActionHandler; | ||
|
||
use Civi\RemoteTools\Api4\Action\RemoteDeleteAction; | ||
use Civi\RemoteTools\Api4\Action\RemoteSubmitCreateFormAction; | ||
use Civi\RemoteTools\Api4\Action\RemoteSubmitUpdateFormAction; | ||
use Civi\RemoteTools\Database\TransactionFactory; | ||
|
||
final class RemoteActionsHandlerTransactionDecorator extends AbstractRemoteEntityActionsHandlerDecorator { | ||
|
||
private TransactionFactory $transactionFactory; | ||
|
||
public function __construct(RemoteEntityActionsHandlerInterface $handler, TransactionFactory $transactionFactory) { | ||
parent::__construct($handler); | ||
$this->transactionFactory = $transactionFactory; | ||
} | ||
|
||
public function delete(RemoteDeleteAction $action): array { | ||
$transaction = $this->transactionFactory->createTransaction(); | ||
try { | ||
return parent::delete($action); | ||
} | ||
// @phpstan-ignore-next-line Dead catch clause. | ||
catch (\Throwable $e) { | ||
$transaction->rollback(); | ||
|
||
throw $e; | ||
} | ||
finally { | ||
$transaction->commit(); | ||
} | ||
} | ||
|
||
public function submitCreateForm(RemoteSubmitCreateFormAction $action): array { | ||
$transaction = $this->transactionFactory->createTransaction(); | ||
try { | ||
return parent::submitCreateForm($action); | ||
} | ||
// @phpstan-ignore-next-line Dead catch clause. | ||
catch (\Throwable $e) { | ||
$transaction->rollback(); | ||
|
||
throw $e; | ||
} | ||
finally { | ||
$transaction->commit(); | ||
} | ||
} | ||
|
||
public function submitUpdateForm(RemoteSubmitUpdateFormAction $action): array { | ||
$transaction = $this->transactionFactory->createTransaction(); | ||
try { | ||
return parent::submitUpdateForm($action); | ||
} | ||
// @phpstan-ignore-next-line Dead catch clause. | ||
catch (\Throwable $e) { | ||
$transaction->rollback(); | ||
|
||
throw $e; | ||
} | ||
finally { | ||
$transaction->commit(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/* | ||
* Copyright (C) 2022 SYSTOPIA GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation in version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Civi\RemoteTools\Database; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
class TransactionFactory { | ||
|
||
public function createTransaction(): \CRM_Core_Transaction { | ||
return \CRM_Core_Transaction::create(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Civi/RemoteTools/DependencyInjection/Compiler/Traits/DecorateServiceTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Civi\RemoteTools\DependencyInjection\Compiler\Traits; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
trait DecorateServiceTrait { | ||
|
||
/** | ||
* @phpstan-param class-string $decoratorClass | ||
* @param int|string $argumentKey | ||
*/ | ||
protected function decorateService( | ||
ContainerBuilder $container, | ||
string $id, | ||
string $decoratorClass, | ||
string $serviceIdPostfix, | ||
$argumentKey = 0 | ||
): void { | ||
$decoratorId = $decoratorClass . ':' . $serviceIdPostfix; | ||
$container->autowire($decoratorId, $decoratorClass) | ||
->setDecoratedService($id) | ||
->setArgument($argumentKey, new Reference($decoratorId . '.inner')); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
...s/phpunit/Civi/RemoteTools/ActionHandler/RemoteActionsHandlerTransactionDecoratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Civi\RemoteTools\ActionHandler; | ||
|
||
use Civi\RemoteTools\Api4\Action\RemoteDeleteAction; | ||
use Civi\RemoteTools\Api4\Action\RemoteSubmitCreateFormAction; | ||
use Civi\RemoteTools\Api4\Action\RemoteSubmitUpdateFormAction; | ||
use Civi\RemoteTools\Database\TransactionFactory; | ||
use Civi\RemoteTools\PHPUnit\Traits\CreateMockTrait; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Civi\RemoteTools\ActionHandler\RemoteActionsHandlerTransactionDecorator | ||
*/ | ||
final class RemoteActionsHandlerTransactionDecoratorTest extends TestCase { | ||
|
||
use CreateMockTrait; | ||
|
||
private RemoteActionsHandlerTransactionDecorator $decorator; | ||
|
||
/** | ||
* @var \Civi\RemoteTools\ActionHandler\RemoteEntityActionsHandlerInterface&\PHPUnit\Framework\MockObject\MockObject | ||
*/ | ||
private MockObject $handlerMock; | ||
|
||
/** | ||
* @var \Civi\RemoteTools\Database\TransactionFactory&\PHPUnit\Framework\MockObject\MockObject | ||
*/ | ||
private MockObject $transactionFactoryMock; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
$this->handlerMock = $this->createMock(RemoteEntityActionsHandlerInterface::class); | ||
$this->transactionFactoryMock = $this->createMock(TransactionFactory::class); | ||
$this->decorator = new RemoteActionsHandlerTransactionDecorator( | ||
$this->handlerMock, | ||
$this->transactionFactoryMock | ||
); | ||
} | ||
|
||
public function testDelete(): void { | ||
$action = $this->createApi4ActionMock(RemoteDeleteAction::class, 'RemoteEntity', 'delete'); | ||
|
||
$transactionMock = $this->createMock(\CRM_Core_Transaction::class); | ||
$this->transactionFactoryMock->method('createTransaction') | ||
->willReturn($transactionMock); | ||
|
||
$transactionMock->expects(static::once())->method('commit'); | ||
$this->handlerMock->method('delete') | ||
->with($action) | ||
->willReturn(['foo' => 'bar']); | ||
|
||
static::assertSame(['foo' => 'bar'], $this->decorator->delete($action)); | ||
} | ||
|
||
public function testDeleteRollback(): void { | ||
$action = $this->createApi4ActionMock(RemoteDeleteAction::class, 'RemoteEntity', 'delete'); | ||
|
||
$transactionMock = $this->createMock(\CRM_Core_Transaction::class); | ||
$this->transactionFactoryMock->method('createTransaction') | ||
->willReturn($transactionMock); | ||
|
||
$exception = new \Exception('test'); | ||
$transactionMock->expects(static::once())->method('rollback'); | ||
$transactionMock->expects(static::once())->method('commit'); | ||
$this->handlerMock->method('delete') | ||
->with($action) | ||
->willThrowException($exception); | ||
|
||
static::expectExceptionObject($exception); | ||
$this->decorator->delete($action); | ||
} | ||
|
||
public function testSubmitCreateForm(): void { | ||
$action = $this->createApi4ActionMock(RemoteSubmitCreateFormAction::class, 'RemoteEntity', 'submitCreateForm'); | ||
|
||
$transactionMock = $this->createMock(\CRM_Core_Transaction::class); | ||
$this->transactionFactoryMock->method('createTransaction') | ||
->willReturn($transactionMock); | ||
|
||
$transactionMock->expects(static::once())->method('commit'); | ||
$this->handlerMock->method('submitCreateForm') | ||
->with($action) | ||
->willReturn(['foo' => 'bar']); | ||
|
||
static::assertSame(['foo' => 'bar'], $this->decorator->submitCreateForm($action)); | ||
} | ||
|
||
public function testSubmitCreateFormRollback(): void { | ||
$action = $this->createApi4ActionMock(RemoteSubmitCreateFormAction::class, 'RemoteEntity', 'submitCreateForm'); | ||
|
||
$transactionMock = $this->createMock(\CRM_Core_Transaction::class); | ||
$this->transactionFactoryMock->method('createTransaction') | ||
->willReturn($transactionMock); | ||
|
||
$exception = new \Exception('test'); | ||
$transactionMock->expects(static::once())->method('rollback'); | ||
$transactionMock->expects(static::once())->method('commit'); | ||
$this->handlerMock->method('submitCreateForm') | ||
->with($action) | ||
->willThrowException($exception); | ||
|
||
static::expectExceptionObject($exception); | ||
$this->decorator->submitCreateForm($action); | ||
} | ||
|
||
public function testSubmitUpdateForm(): void { | ||
$action = $this->createApi4ActionMock(RemoteSubmitUpdateFormAction::class, 'RemoteEntity', 'submitUpdateForm'); | ||
|
||
$transactionMock = $this->createMock(\CRM_Core_Transaction::class); | ||
$this->transactionFactoryMock->method('createTransaction') | ||
->willReturn($transactionMock); | ||
|
||
$transactionMock->expects(static::once())->method('commit'); | ||
$this->handlerMock->method('submitUpdateForm') | ||
->with($action) | ||
->willReturn(['foo' => 'bar']); | ||
|
||
static::assertSame(['foo' => 'bar'], $this->decorator->submitUpdateForm($action)); | ||
} | ||
|
||
public function testSubmitUpdateFormRollback(): void { | ||
$action = $this->createApi4ActionMock(RemoteSubmitUpdateFormAction::class, 'RemoteEntity', 'submitUpdateForm'); | ||
|
||
$transactionMock = $this->createMock(\CRM_Core_Transaction::class); | ||
$this->transactionFactoryMock->method('createTransaction') | ||
->willReturn($transactionMock); | ||
|
||
$exception = new \Exception('test'); | ||
$transactionMock->expects(static::once())->method('rollback'); | ||
$transactionMock->expects(static::once())->method('commit'); | ||
$this->handlerMock->method('submitUpdateForm') | ||
->with($action) | ||
->willThrowException($exception); | ||
|
||
static::expectExceptionObject($exception); | ||
$this->decorator->submitUpdateForm($action); | ||
} | ||
|
||
} |