Skip to content

Commit

Permalink
SUPESC-755 Fixed tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kkicheglovspryker committed Dec 4, 2023
1 parent ae0150d commit f45d67e
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
namespace SprykerEco\Zed\Unzer\Business\Payment\OmsStateResolver;

use Generated\Shared\Transfer\UnzerPaymentTransfer;
use SprykerEco\Zed\Unzer\Business\Exception\UnzerException;
use SprykerEco\Zed\Unzer\UnzerConfig;
use SprykerEco\Zed\Unzer\UnzerConstants;

class UnzerOmsStateResolver implements UnzerOmsStateResolverInterface
{
/**
* @var \SprykerEco\Zed\Unzer\UnzerConfig
*/
protected UnzerConfig $unzerConfig;

/**
Expand Down Expand Up @@ -45,13 +49,57 @@ public function getUnzerPaymentOmsStatus(UnzerPaymentTransfer $unzerPaymentTrans
}

if ($chargeUnzerTransactionTransfer !== null) {
return $this->unzerConfig->mapUnzerChargePaymentStatusToOmsStatus($chargeUnzerTransactionTransfer->getStatusOrFail());
return $this->mapUnzerPaymentTransactionStatusToOmsStatus(
$this->unzerConfig->getUnzerChargePaymentStatusToOmsStatusMap(),
$chargeUnzerTransactionTransfer->getStatusOrFail(),
);
}

if ($authorizeUnzerTransactionTransfer !== null) {
return $this->unzerConfig->mapUnzerAuthorizePaymentStatusToOmsStatus($authorizeUnzerTransactionTransfer->getStatusOrFail());
return $this->mapUnzerPaymentTransactionStatusToOmsStatus(
$this->unzerConfig->getUnzerAuthorizePaymentStatusToOmsStatusMap(),
$authorizeUnzerTransactionTransfer->getStatusOrFail(),
);
}

return $this->mapUnzerPaymentStateToOmsStatus($unzerPaymentTransfer->getStateIdOrFail());
}

/**
* @param array<string, string> $unzerPaymentStatusToOmsStatusMap
* @param string $unzerPaymentStatus
*
* @throws \SprykerEco\Zed\Unzer\Business\Exception\UnzerException
*
* @return string
*/
protected function mapUnzerPaymentTransactionStatusToOmsStatus(array $unzerPaymentStatusToOmsStatusMap, string $unzerPaymentStatus): string
{
if (!isset($unzerPaymentStatusToOmsStatusMap[$unzerPaymentStatus])) {
$message = sprintf('Undefined payment transaction status provided by Unzer: %s', $unzerPaymentStatus);

throw new UnzerException($message);
}

return $unzerPaymentStatusToOmsStatusMap[$unzerPaymentStatus];
}

/**
* @param int $unzerPaymentStateId
*
* @throws \SprykerEco\Zed\Unzer\Business\Exception\UnzerException
*
* @return string
*/
protected function mapUnzerPaymentStateToOmsStatus(int $unzerPaymentStateId): string
{
$paymentStateToOmsStatusMap = $this->unzerConfig->getUnzerPaymentStateToOmsStatusMap();
if (!isset($paymentStateToOmsStatusMap[$unzerPaymentStateId])) {
$message = sprintf('Undefined payment state provided by Unzer: %s', $unzerPaymentStateId);

throw new UnzerException($message);
}

return $this->unzerConfig->mapUnzerPaymentStatusToOmsStatus($unzerPaymentTransfer->getStateIdOrFail());
return $paymentStateToOmsStatusMap[$unzerPaymentStateId];
}
}
2 changes: 2 additions & 0 deletions src/SprykerEco/Zed/Unzer/Business/UnzerFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public function saveOrderPayment(QuoteTransfer $quoteTransfer, SaveOrderTransfer
*
* @param \Generated\Shared\Transfer\UnzerNotificationTransfer $notificationTransfer
*
* @throws \SprykerEco\Zed\Unzer\Business\Exception\UnzerException
*
* @return \Generated\Shared\Transfer\UnzerNotificationTransfer
*/
public function processNotification(UnzerNotificationTransfer $notificationTransfer): UnzerNotificationTransfer
Expand Down
3 changes: 3 additions & 0 deletions src/SprykerEco/Zed/Unzer/Business/UnzerFacadeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ public function saveOrderPayment(QuoteTransfer $quoteTransfer, SaveOrderTransfer
* - Checks if provided Unzer notification is enabled.
* - Processes Unzer notification.
* - Updates payment details in DB.
* - Throws `UnzerException` if payment status cannot be mapped to OMS status correctly.
*
* @api
*
* @param \Generated\Shared\Transfer\UnzerNotificationTransfer $notificationTransfer
*
* @throws \SprykerEco\Zed\Unzer\Business\Exception\UnzerException
*
* @return \Generated\Shared\Transfer\UnzerNotificationTransfer
*/
public function processNotification(UnzerNotificationTransfer $notificationTransfer): UnzerNotificationTransfer;
Expand Down
35 changes: 23 additions & 12 deletions src/SprykerEco/Zed/Unzer/UnzerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ public function getOmsStatusNew(): string
*
* @api
*
* @deprecated Will be removed without replacement.
*
* @param int $unzerStateId
*
* @return string
Expand All @@ -364,6 +366,19 @@ public function mapUnzerPaymentStatusToOmsStatus(int $unzerStateId): string
return static::UNZER_PAYMENT_STATE_OMS_STATUS_MAP[$unzerStateId];
}

/**
* Specification:
* - Returns Unzer payment state to OMS status map.
*
* @api
*
* @return array<int, string>
*/
public function getUnzerPaymentStateToOmsStatusMap(): string

Check failure on line 377 in src/SprykerEco/Zed/Unzer/UnzerConfig.php

View workflow job for this annotation

GitHub Actions / Unzer (PHP 8.0)

PHPDoc tag @return with type array<int, string> is incompatible with native type string.

Check failure on line 377 in src/SprykerEco/Zed/Unzer/UnzerConfig.php

View workflow job for this annotation

GitHub Actions / Unzer (PHP 8.1)

PHPDoc tag @return with type array<int, string> is incompatible with native type string.
{
return static::UNZER_PAYMENT_STATE_OMS_STATUS_MAP;

Check failure on line 379 in src/SprykerEco/Zed/Unzer/UnzerConfig.php

View workflow job for this annotation

GitHub Actions / Unzer (PHP 8.0)

Method SprykerEco\Zed\Unzer\UnzerConfig::getUnzerPaymentStateToOmsStatusMap() should return string but returns array<int, string>.

Check failure on line 379 in src/SprykerEco/Zed/Unzer/UnzerConfig.php

View workflow job for this annotation

GitHub Actions / Unzer (PHP 8.1)

Method SprykerEco\Zed\Unzer\UnzerConfig::getUnzerPaymentStateToOmsStatusMap() should return string but returns array<int, string>.
}

/**
* Specification:
* - Is notification type enabled.
Expand Down Expand Up @@ -398,32 +413,28 @@ public function mapUnzerEventToOmsStatus(string $unzerEvent): string

/**
* Specification:
* - Maps Unzer charge transaction status to OMS status.
* - Returns Unzer charge transaction status to OMS status map.
*
* @api
*
* @param string $transactionStatus
*
* @return string
* @return array<string, string>
*/
public function mapUnzerChargePaymentStatusToOmsStatus(string $transactionStatus): string
public function getUnzerChargePaymentStatusToOmsStatusMap(): array
{
return static::UNZER_CHARGE_STATE_OMS_STATUS_MAP[$transactionStatus];
return static::UNZER_CHARGE_STATE_OMS_STATUS_MAP;
}

/**
* Specification:
* - Maps Unzer authorize transaction status to OMS status.
* - Returns Unzer authorize transaction status to OMS status map.
*
* @api
*
* @param string $transactionStatus
*
* @return string
* @return array<string, string>
*/
public function mapUnzerAuthorizePaymentStatusToOmsStatus(string $transactionStatus): string
public function getUnzerAuthorizePaymentStatusToOmsStatusMap(): array
{
return static::UNZER_AUTHORIZE_STATE_OMS_STATUS_MAP[$transactionStatus];
return static::UNZER_AUTHORIZE_STATE_OMS_STATUS_MAP;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,94 +11,154 @@
use Generated\Shared\Transfer\UnzerPaymentTransfer;
use Generated\Shared\Transfer\UnzerTransactionTransfer;
use SprykerEco\Zed\Unzer\Business\Payment\OmsStateResolver\UnzerOmsStateResolver;
use SprykerEco\Zed\Unzer\UnzerConfig;
use SprykerEco\Zed\Unzer\UnzerConstants;
use SprykerEcoTest\Zed\Unzer\UnzerBusinessTester;

class UnzerOmsStateResolverTest extends Unit
{
/**
* @dataProvider unzerPaymentStatusDataProvider
*
* @param string $expectedOmsState
* @param \Generated\Shared\Transfer\UnzerPaymentTransfer $paymentTransfer
*
* @var \SprykerEcoTest\Zed\Unzer\UnzerBusinessTester
*/
protected UnzerBusinessTester $tester;

/**
* @return void
*/
public function testShouldReturn(string $expectedOmsState, UnzerPaymentTransfer $paymentTransfer): void
public function getUnzerPaymentOmsStatusShouldReturnStatusAuthorizePendingWhenTransactionAuthorizePending(): void
{
// Arrange
$paymentTransfer = (new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_PENDING),
);

// Act
$omsState = (new UnzerOmsStateResolver(new UnzerConfig()))->getUnzerPaymentOmsStatus($paymentTransfer);
$omsState = (new UnzerOmsStateResolver($this->tester->createConfig()))->getUnzerPaymentOmsStatus($paymentTransfer);

// Assert
$this->assertSame($expectedOmsState, $omsState);
$this->assertSame(UnzerConstants::OMS_STATUS_AUTHORIZE_PENDING, $omsState);
}

/**
* @return array<string, mixed>
* @return void
*/
public function getUnzerPaymentOmsStatusShouldReturnStatusAuthorizeSuccessWhenTransactionAuthorizeSuccess(): void
{
// Arrange
$paymentTransfer = (new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
);

// Act
$omsState = (new UnzerOmsStateResolver($this->tester->createConfig()))->getUnzerPaymentOmsStatus($paymentTransfer);

// Assert
$this->assertSame(UnzerConstants::OMS_STATUS_AUTHORIZE_SUCCEEDED, $omsState);
}

/**
* @return void
*/
public function getUnzerPaymentOmsStatusShouldReturnStatusAuthorizeFailedWhenTransactionAuthorizeFailed(): void
{
// Arrange
$paymentTransfer = (new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_PENDING),
);

// Act
$omsState = (new UnzerOmsStateResolver($this->tester->createConfig()))->getUnzerPaymentOmsStatus($paymentTransfer);

// Assert
$this->assertSame(UnzerConstants::OMS_STATUS_AUTHORIZE_FAILED, $omsState);
}

/**
* @return void
*/
protected function unzerPaymentStatusDataProvider(): array
public function getUnzerPaymentOmsStatusShouldReturnStatusChargeFailedWhenTransactionChargeFailed(): void
{
return [
'Payment status authorize pending' => [
UnzerConstants::OMS_STATUS_AUTHORIZE_PENDING,
(new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_PENDING),
),
],
'Payment status authorize success' => [
UnzerConstants::OMS_STATUS_AUTHORIZE_SUCCEEDED,
(new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
),
],
'Payment status authorize failed' => [
UnzerConstants::OMS_STATUS_AUTHORIZE_FAILED,
(new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_PENDING),
),
],
'Payment status charge failed' => [
UnzerConstants::OMS_STATUS_CHARGE_FAILED,
(new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_CANCELED)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_CHARGE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_ERROR),
),
],
'Payment status payment completed' => [
UnzerConstants::OMS_STATUS_PAYMENT_COMPLETED,
(new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_CHARGE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
),
],
];
// Arrange
$paymentTransfer = (new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_CANCELED)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_CHARGE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_ERROR),
);

// Act
$omsState = (new UnzerOmsStateResolver($this->tester->createConfig()))->getUnzerPaymentOmsStatus($paymentTransfer);

// Assert
$this->assertSame(UnzerConstants::OMS_STATUS_CHARGE_FAILED, $omsState);
}

/**
* @return void
*/
public function getUnzerPaymentOmsStatusShouldReturnPaymentCompletedWhenPaymentCompleted(): void
{
// Arrange
$paymentTransfer = (new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_CHARGE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
);

// Act
$omsState = (new UnzerOmsStateResolver($this->tester->createConfig()))->getUnzerPaymentOmsStatus($paymentTransfer);

// Assert
$this->assertSame(UnzerConstants::OMS_STATUS_PAYMENT_COMPLETED, $omsState);
}

/**
* @return void
*/
public function getUnzerPaymentOmsStatusShouldThrowExceptionWhenTransactionStatusIsUnknown(): void
{
// Arrange
$paymentTransfer = (new UnzerPaymentTransfer())
->setStateId(UnzerConstants::UNZER_PAYMENT_STATUS_PENDING)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_AUTHORIZE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
)
->addTransaction(
(new UnzerTransactionTransfer())
->setType(UnzerConstants::TRANSACTION_TYPE_CHARGE)
->setStatus(UnzerConstants::TRANSACTION_STATUS_SUCCESS),
);

// Act
$omsState = (new UnzerOmsStateResolver($this->tester->createConfig()))->getUnzerPaymentOmsStatus($paymentTransfer);

// Assert
$this->assertSame(UnzerConstants::OMS_STATUS_PAYMENT_COMPLETED, $omsState);
}
}

0 comments on commit f45d67e

Please sign in to comment.