Skip to content

Commit

Permalink
BRAIN-38 - Added deletion for old currency mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
cyl3x committed Jul 26, 2024
1 parent b0d710d commit d599df1
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 5 deletions.
3 changes: 2 additions & 1 deletion assets/src/views/sw-braintree-app-config-page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ export default defineComponent({
this.saving = true;
return this.$api.patch<BraintreeConnection>('/entity/shop', this.shop)
.then((connection) => {
.then(async (connection) => {
this.connection = connection;
this.notifyConnectionStatus(connection.connectionStatus);
await this.getShopConfig();
})
.catch((e) => this.$notify.error('save_config', e))
.finally(() => this.saving = false);
Expand Down
34 changes: 34 additions & 0 deletions src/Braintree/Util/CurrencyMappingValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace Swag\Braintree\Braintree\Util;

use Braintree\MerchantAccount;
use Swag\Braintree\Braintree\Gateway\BraintreeConnectionService;
use Swag\Braintree\Entity\ShopEntity;
use Swag\Braintree\Repository\CurrencyMappingRepository;

class CurrencyMappingValidator
{
public function __construct(
private readonly CurrencyMappingRepository $currencyMappingRepository,
private readonly BraintreeConnectionService $connectionService,
) {
}

public function deleteInvalidCurrencyMappings(ShopEntity $shop): void
{
$merchantAccountIds = \array_map(
static fn (MerchantAccount $merchantAccount) => $merchantAccount->id,
$this->connectionService->fromShop($shop)->getAllMerchantAccounts(),
);

$qb = $this->currencyMappingRepository->createQueryBuilder('currencyMapping');
$qb
->delete()
->where($qb->expr()->notIn('currencyMapping.merchantAccountId', ':merchantAccountIds'))
->andWhere('currencyMapping.shop = :shop')
->setParameters(['shop' => $shop, 'merchantAccountIds' => $merchantAccountIds])
->getQuery()
->execute();
}
}
10 changes: 8 additions & 2 deletions src/Controller/EntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Swag\Braintree\Braintree\Gateway\BraintreeConnectionService;
use Swag\Braintree\Braintree\Gateway\Connection\BraintreeConnectionStatus;
use Swag\Braintree\Braintree\Util\CurrencyMappingValidator;
use Swag\Braintree\Entity\ConfigEntity;
use Swag\Braintree\Entity\CurrencyMappingEntity;
use Swag\Braintree\Entity\ShopEntity;
Expand All @@ -27,6 +28,7 @@ public function __construct(
private readonly ConfigRepository $configRepository,
private readonly CurrencyMappingRepository $currencyMappingRepository,
private readonly BraintreeConnectionService $connectionService,
private readonly CurrencyMappingValidator $currencyMappingValidator,
) {
}

Expand All @@ -42,12 +44,16 @@ public function updateShopEntity(Request $request, ShopEntity $shop): BraintreeC
/** @var ShopEntity $shop */
$shop = $this->shopRepository->deserializeInto($shop, $request->getContent());

$connectionService = $this->connectionService->fromShop($shop);
$connection = $this->connectionService->fromShop($shop)->testConnection();

$this->entityManager->persist($shop);
$this->entityManager->flush();

return $connectionService->testConnection();
if ($connection->connectionStatus === BraintreeConnectionStatus::STATUS_CONNECTED) {
$this->currencyMappingValidator->deleteInvalidCurrencyMappings($shop);
}

return $connection;
}

/**
Expand Down
107 changes: 107 additions & 0 deletions tests/unit/Braintree/Util/CurrencyMappingValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php declare(strict_types=1);

namespace Swag\Braintree\Tests\Unit\Braintree\Util;

use Braintree\MerchantAccount;
use Doctrine\ORM\QueryBuilder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Swag\Braintree\Braintree\Gateway\BraintreeConnectionService;
use Swag\Braintree\Braintree\Util\CurrencyMappingValidator;
use Swag\Braintree\Entity\ShopEntity;
use Swag\Braintree\Repository\CurrencyMappingRepository;

#[CoversClass(CurrencyMappingValidator::class)]
class CurrencyMappingValidatorTest extends TestCase
{
private MockObject&CurrencyMappingRepository $currencyMappingRepository;

private MockObject&BraintreeConnectionService $connectionService;

private CurrencyMappingValidator $validator;

protected function setUp(): void
{
$this->currencyMappingRepository = $this->createMock(CurrencyMappingRepository::class);
$this->connectionService = $this->createMock(BraintreeConnectionService::class);

$this->validator = new CurrencyMappingValidator(
$this->currencyMappingRepository,
$this->connectionService,
);
}

public function testDeleteInvalidCurrencyMappings(): void
{
$accounts = [
MerchantAccount::factory(['id' => 'merchant-id-1']),
MerchantAccount::factory(['id' => 'merchant-id-2']),
];
$shop = new ShopEntity('shop-id', '', '');

$expressionBuilder = $this->createMock(\Doctrine\ORM\Query\Expr::class);

$expressionBuilder
->expects(static::once())
->method('notIn')
->with('currencyMapping.merchantAccountId', ':merchantAccountIds');

$queryBuilder = $this->createMock(QueryBuilder::class);

$queryBuilder
->expects(static::once())
->method('expr')
->willReturn($expressionBuilder);

$queryBuilder
->expects(static::once())
->method('delete')
->willReturn($queryBuilder);

$queryBuilder
->expects(static::once())
->method('where')
->willReturn($queryBuilder);

$queryBuilder
->expects(static::once())
->method('andWhere')
->with('currencyMapping.shop = :shop')
->willReturn($queryBuilder);

$queryBuilder
->expects(static::once())
->method('setParameters')
->with(['shop' => $shop, 'merchantAccountIds' => ['merchant-id-1', 'merchant-id-2']])
->willReturn($queryBuilder);

$query = $this->createMock(\Doctrine\ORM\Query::class);

$queryBuilder
->expects(static::once())
->method('getQuery')
->willReturn($query);

$query
->expects(static::once())
->method('execute');

$this->connectionService
->expects(static::once())
->method('fromShop')
->willReturn($this->connectionService);

$this->connectionService
->expects(static::once())
->method('getAllMerchantAccounts')
->willReturn($accounts);

$this->currencyMappingRepository
->expects(static::once())
->method('createQueryBuilder')
->willReturn($queryBuilder);

$this->validator->deleteInvalidCurrencyMappings($shop);
}
}
21 changes: 19 additions & 2 deletions tests/unit/Controller/EntityControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Framework\TestCase;
use Swag\Braintree\Braintree\Gateway\BraintreeConnectionService;
use Swag\Braintree\Braintree\Gateway\Connection\BraintreeConnectionStatus;
use Swag\Braintree\Braintree\Util\CurrencyMappingValidator;
use Swag\Braintree\Controller\EntityController;
use Swag\Braintree\Entity\ConfigEntity;
use Swag\Braintree\Entity\ShopEntity;
Expand All @@ -32,6 +33,8 @@ class EntityControllerTest extends TestCase

private MockObject&BraintreeConnectionService $connectionService;

private MockObject&CurrencyMappingValidator $currencyMappingValidator;

private EntityController $entityController;

protected function setUp(): void
Expand All @@ -41,8 +44,16 @@ protected function setUp(): void
$this->configRepository = $this->createMock(ConfigRepository::class);
$this->currencyMappingRepository = $this->createMock(CurrencyMappingRepository::class);
$this->connectionService = $this->createMock(BraintreeConnectionService::class);

$this->entityController = new EntityController($this->entityManager, $this->shopRepository, $this->configRepository, $this->currencyMappingRepository, $this->connectionService);
$this->currencyMappingValidator = $this->createMock(CurrencyMappingValidator::class);

$this->entityController = new EntityController(
$this->entityManager,
$this->shopRepository,
$this->configRepository,
$this->currencyMappingRepository,
$this->connectionService,
$this->currencyMappingValidator,
);
}

public function testGetShopEntity(): void
Expand Down Expand Up @@ -82,6 +93,12 @@ public function testUpdateShopEntity(): void
->expects(static::once())
->method('flush');


$this->currencyMappingValidator
->expects(static::once())
->method('deleteInvalidCurrencyMappings')
->with($shop);

$this->entityController->updateShopEntity($request, $shop);
}

Expand Down

0 comments on commit d599df1

Please sign in to comment.