-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BRAIN-38 - Added deletion for old currency mappings
- Loading branch information
Showing
5 changed files
with
170 additions
and
5 deletions.
There are no files selected for viewing
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
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,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(); | ||
} | ||
} |
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
107 changes: 107 additions & 0 deletions
107
tests/unit/Braintree/Util/CurrencyMappingValidatorTest.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,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); | ||
} | ||
} |
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