Skip to content

Commit

Permalink
tests - replace crypt instances with mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mustapayev committed Sep 27, 2024
1 parent a4dc8d3 commit 387c379
Show file tree
Hide file tree
Showing 17 changed files with 691 additions and 288 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Mews\Pos\Tests\Unit\DataMapper\RequestDataMapper;

use Mews\Pos\Crypt\CryptInterface;
use Mews\Pos\DataMapper\RequestDataMapper\PayFlexCPV4PosRequestDataMapper;
use Mews\Pos\Entity\Account\AbstractPosAccount;
use Mews\Pos\Entity\Account\PayFlexAccount;
Expand All @@ -13,13 +14,10 @@
use Mews\Pos\Exceptions\UnsupportedTransactionTypeException;
use Mews\Pos\Factory\AccountFactory;
use Mews\Pos\Factory\CreditCardFactory;
use Mews\Pos\Factory\CryptFactory;
use Mews\Pos\Gateways\PayFlexCPV4Pos;
use Mews\Pos\PosInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\NullLogger;

/**
* @covers \Mews\Pos\DataMapper\RequestDataMapper\PayFlexCPV4PosRequestDataMapper
Expand All @@ -34,6 +32,9 @@ class PayFlexCPV4PosRequestDataMapperTest extends TestCase
/** @var EventDispatcherInterface & MockObject */
private EventDispatcherInterface $dispatcher;

/** @var CryptInterface & MockObject */
private CryptInterface $crypt;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -47,8 +48,8 @@ protected function setUp(): void
);

$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$crypt = CryptFactory::createGatewayCrypt(PayFlexCPV4Pos::class, new NullLogger());
$this->requestDataMapper = new PayFlexCPV4PosRequestDataMapper($this->dispatcher, $crypt);
$this->crypt = $this->createMock(CryptInterface::class);
$this->requestDataMapper = new PayFlexCPV4PosRequestDataMapper($this->dispatcher, $this->crypt);
}

/**
Expand Down Expand Up @@ -129,14 +130,22 @@ public function testMapInstallment($installment, $expected): void
*/
public function testCreate3DEnrollmentCheckData(AbstractPosAccount $posAccount, array $order, string $txType, ?CreditCard $creditCard, array $expectedData): void
{
$hashCalculationData = $expectedData;
unset($hashCalculationData['HashedData']);

$this->crypt->expects(self::once())
->method('create3DHash')
->with($this->account, $hashCalculationData)
->willReturn($expectedData['HashedData']);

$actual = $this->requestDataMapper->create3DEnrollmentCheckRequestData(
$posAccount,
$order,
$txType,
PosInterface::MODEL_3D_SECURE,
$creditCard
);
$this->assertEquals($expectedData, $actual);
$this->assertSame($expectedData, $actual);
}

/**
Expand Down Expand Up @@ -228,7 +237,6 @@ public static function registerDataProvider(): iterable
'AllowNotEnrolledCard' => 'false',
'SuccessUrl' => 'https://domain.com/success',
'FailUrl' => 'https://domain.com/fail_url',
'HashedData' => 'apZ/1+eWzqCRk9qqACxN0bBZQ8g=',
'RequestLanguage' => 'tr-TR',
'Extract' => '',
'CustomItems' => '',
Expand All @@ -238,6 +246,7 @@ public static function registerDataProvider(): iterable
'ExpireMonth' => '12',
'ExpireYear' => '21',
'CardHoldersName' => 'ahmet',
'HashedData' => 'apZ/1+eWzqCRk9qqACxN0bBZQ8g=',
],
];

Expand All @@ -258,10 +267,10 @@ public static function registerDataProvider(): iterable
'AllowNotEnrolledCard' => 'false',
'SuccessUrl' => 'https://domain.com/success',
'FailUrl' => 'https://domain.com/fail_url',
'HashedData' => 'apZ/1+eWzqCRk9qqACxN0bBZQ8g=',
'RequestLanguage' => 'tr-TR',
'Extract' => '',
'CustomItems' => '',
'HashedData' => 'apZ/1+eWzqCRk9qqACxN0bBZQ8g=',
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
namespace Mews\Pos\Tests\Unit\DataMapper\RequestDataMapper;

use InvalidArgumentException;
use Mews\Pos\Crypt\CryptInterface;
use Mews\Pos\DataMapper\RequestDataMapper\PosNetRequestDataMapper;
use Mews\Pos\Entity\Account\PosNetAccount;
use Mews\Pos\Entity\Card\CreditCardInterface;
use Mews\Pos\Exceptions\UnsupportedTransactionTypeException;
use Mews\Pos\Factory\AccountFactory;
use Mews\Pos\Factory\CreditCardFactory;
use Mews\Pos\Factory\CryptFactory;
use Mews\Pos\Gateways\PosNet;
use Mews\Pos\PosInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\NullLogger;

/**
* @covers \Mews\Pos\DataMapper\RequestDataMapper\PosNetRequestDataMapper
Expand All @@ -37,6 +35,9 @@ class PosNetRequestDataMapperTest extends TestCase
/** @var EventDispatcherInterface & MockObject */
private EventDispatcherInterface $dispatcher;

/** @var CryptInterface & MockObject */
private CryptInterface $crypt;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -61,8 +62,8 @@ protected function setUp(): void
];

$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$crypt = CryptFactory::createGatewayCrypt(PosNet::class, new NullLogger());
$this->requestDataMapper = new PosNetRequestDataMapper($this->dispatcher, $crypt);
$this->crypt = $this->createMock(CryptInterface::class);
$this->requestDataMapper = new PosNetRequestDataMapper($this->dispatcher, $this->crypt);
$this->card = CreditCardFactory::create('5555444433332222', '22', '01', '123', 'ahmet');
}

Expand Down Expand Up @@ -191,11 +192,16 @@ public function testCreateCancelRequestData(array $order, array $expectedData):
/**
* @dataProvider create3DPaymentRequestDataDataProvider
*/
public function testCreate3DPaymentRequestData(array $order, string $txType, array $responseData, array $expected): void
public function testCreate3DPaymentRequestData(array $order, array $mappedOrder, string $txType, array $responseData, array $expected): void
{
$this->crypt->expects(self::once())
->method('create3DHash')
->with($this->account, $mappedOrder)
->willReturn($expected['oosTranData']['mac']);

$actual = $this->requestDataMapper->create3DPaymentRequestData($this->account, $order, $txType, $responseData);

$this->assertEquals($expected, $actual);
$this->assertSame($expected, $actual);
}

/**
Expand All @@ -221,11 +227,16 @@ public function testCreate3DEnrollmentCheckRequestDataFailTooLongOrderId(): void
/**
* @dataProvider resolveMerchantDataDataProvider
*/
public function testCreate3DResolveMerchantRequestData(array $order, array $responseData, array $expectedData): void
public function testCreate3DResolveMerchantRequestData(array $order, array $mappedOrder, array $responseData, array $expectedData): void
{
$this->crypt->expects(self::once())
->method('create3DHash')
->with($this->account, $mappedOrder)
->willReturn($expectedData['oosResolveMerchantData']['mac']);

$actualData = $this->requestDataMapper->create3DResolveMerchantRequestData($this->account, $order, $responseData);

$this->assertEquals($expectedData, $actualData);
$this->assertSame($expectedData, $actualData);
}

/**
Expand Down Expand Up @@ -349,6 +360,12 @@ public static function create3DPaymentRequestDataDataProvider(): array
'installment' => '0',
'currency' => PosInterface::CURRENCY_TRY,
],
'mapped_order' => [
'id' => '000000002020110828BC',
'amount' => 10001,
'installment' => '0',
'currency' => 'TL',
],
'txType' => PosInterface::TX_TYPE_PAY_AUTH,
'responseData' => [
'BankPacket' => 'F61E1D0C0FB6EC5203A748124F309998F61E1D0C0FB6EC5203A748124F30',
Expand Down Expand Up @@ -587,6 +604,12 @@ public static function resolveMerchantDataDataProvider(): array
'installment' => '0',
'currency' => PosInterface::CURRENCY_TRY,
],
'mapped_order' => [
'id' => '000000002020110828BC',
'amount' => 10001,
'installment' => '0',
'currency' => 'TL',
],
'response_data' => [
'BankPacket' => 'F61E1D0C0FB6EC5203A748124F309998F61E1D0C0FB6EC5203A748124F30',
'MerchantPacket' => 'E1D0C0FB6EC5203A748124F309998F61E1D0C0FB6EC5203A748124F309998F61E1D0C0FB6EC5203A748124F30',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@
namespace Mews\Pos\Tests\Unit\DataMapper\RequestDataMapper;

use InvalidArgumentException;
use Mews\Pos\Crypt\CryptInterface;
use Mews\Pos\DataMapper\RequestDataMapper\PosNetV1PosRequestDataMapper;
use Mews\Pos\Entity\Account\PosNetAccount;
use Mews\Pos\Entity\Card\CreditCardInterface;
use Mews\Pos\Event\Before3DFormHashCalculatedEvent;
use Mews\Pos\Exceptions\UnsupportedTransactionTypeException;
use Mews\Pos\Factory\AccountFactory;
use Mews\Pos\Factory\CreditCardFactory;
use Mews\Pos\Factory\CryptFactory;
use Mews\Pos\Gateways\PosNetV1Pos;
use Mews\Pos\PosInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\NullLogger;

/**
* @covers \Mews\Pos\DataMapper\RequestDataMapper\PosNetV1PosRequestDataMapper
Expand All @@ -36,6 +35,9 @@ class PosNetV1PosRequestDataMapperTest extends TestCase
/** @var EventDispatcherInterface & MockObject */
private EventDispatcherInterface $dispatcher;

/** @var CryptInterface & MockObject */
private CryptInterface $crypt;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -53,8 +55,8 @@ protected function setUp(): void

$this->card = CreditCardFactory::create('5400619360964581', '20', '01', '056', 'ahmet');

$crypt = CryptFactory::createGatewayCrypt(PosNetV1Pos::class, new NullLogger());
$this->requestDataMapper = new PosNetV1PosRequestDataMapper($this->dispatcher, $crypt);
$this->crypt = $this->createMock(CryptInterface::class);
$this->requestDataMapper = new PosNetV1PosRequestDataMapper($this->dispatcher, $this->crypt);
}

/**
Expand Down Expand Up @@ -154,29 +156,53 @@ public function testFormatOrderIdFail(): void
*/
public function testCreateNonSecurePostAuthPaymentRequestData(array $order, array $expectedData): void
{
$hashCalculationData = $expectedData;
unset($hashCalculationData['MAC']);

$this->crypt->expects(self::once())
->method('hashFromParams')
->with($this->account->getStoreKey(), $hashCalculationData, 'MACParams', ':')
->willReturn($expectedData['MAC']);

$actual = $this->requestDataMapper->createNonSecurePostAuthPaymentRequestData($this->account, $order);

$this->assertEquals($expectedData, $actual);
$this->assertSame($expectedData, $actual);
}

/**
* @dataProvider nonSecurePaymentRequestDataDataProvider
*/
public function testCreateNonSecurePaymentRequestData(array $order, array $expectedData): void
{
$hashCalculationData = $expectedData;
unset($hashCalculationData['MAC']);

$this->crypt->expects(self::once())
->method('hashFromParams')
->with($this->account->getStoreKey(), $hashCalculationData, 'MACParams', ':')
->willReturn($expectedData['MAC']);

$actual = $this->requestDataMapper->createNonSecurePaymentRequestData($this->account, $order, PosInterface::TX_TYPE_PAY_AUTH, $this->card);

$this->assertEquals($expectedData, $actual);
$this->assertSame($expectedData, $actual);
}

/**
* @dataProvider create3DPaymentRequestDataProvider
*/
public function testCreate3DPaymentRequestData(array $order, string $txType, array $responseData, array $expectedData): void
{
$hashCalculationData = $expectedData;
unset($hashCalculationData['MAC']);

$this->crypt->expects(self::once())
->method('createHash')
->with($this->account, $hashCalculationData)
->willReturn($expectedData['MAC']);

$actual = $this->requestDataMapper->create3DPaymentRequestData($this->account, $order, $txType, $responseData);

$this->assertEquals($expectedData, $actual);
$this->assertSame($expectedData, $actual);
}

/**
Expand All @@ -193,6 +219,14 @@ public function testCreate3DFormData(array $order, string $txType, string $gatew
&& $paymentModel === $dispatchedEvent->getPaymentModel()
&& count($dispatchedEvent->getFormInputs()) > 3));

$hashCalculationData = $expected['inputs'];
unset($hashCalculationData['Mac']);

$this->crypt->expects(self::once())
->method('create3DHash')
->with($this->account, $hashCalculationData)
->willReturn($expected['inputs']['Mac']);

$actual = $this->requestDataMapper->create3DFormData(
$this->account,
$order,
Expand All @@ -210,15 +244,31 @@ public function testCreate3DFormData(array $order, string $txType, string $gatew
*/
public function testCreateStatusRequestData(array $order, array $expected): void
{
$hashCalculationData = $expected;
unset($hashCalculationData['MAC']);

$this->crypt->expects(self::once())
->method('hashFromParams')
->with($this->account->getStoreKey(), $hashCalculationData, 'MACParams', ':')
->willReturn($expected['MAC']);

$actual = $this->requestDataMapper->createStatusRequestData($this->account, $order);
$this->assertEquals($expected, $actual);
$this->assertSame($expected, $actual);
}

/**
* @dataProvider createRefundRequestDataDataProvider
*/
public function testCreateRefundRequestData(array $order, string $txType, array $expected): void
{
$hashCalculationData = $expected;
unset($hashCalculationData['MAC']);

$this->crypt->expects(self::once())
->method('hashFromParams')
->with($this->account->getStoreKey(), $hashCalculationData, 'MACParams', ':')
->willReturn($expected['MAC']);

$actual = $this->requestDataMapper->createRefundRequestData($this->account, $order, $txType);

ksort($actual);
Expand All @@ -232,8 +282,19 @@ public function testCreateRefundRequestData(array $order, string $txType, array
*/
public function testCreateCancelRequestData(array $order, array $expected): void
{
$hashCalculationData = $expected;
unset($hashCalculationData['MAC']);

$this->crypt->expects(self::once())
->method('hashFromParams')
->with($this->account->getStoreKey(), $hashCalculationData, 'MACParams', ':')
->willReturn($expected['MAC']);

$actual = $this->requestDataMapper->createCancelRequestData($this->account, $order);
$this->assertEquals($expected, $actual);

ksort($actual);
ksort($expected);
$this->assertSame($expected, $actual);
}

public function testCreateHistoryRequestData(): void
Expand Down Expand Up @@ -789,12 +850,12 @@ public static function createStatusRequestDataDataProvider(): iterable
'MerchantNo' => '6700950031',
'TerminalNo' => '67540050',
'MACParams' => 'MerchantNo:TerminalNo',
'MAC' => 'wgyfAJPbEPtTtce/+HRlXajSRfYA0J6mUcH+16EbB78=',
'CipheredData' => null,
'DealerData' => null,
'IsEncrypted' => 'N',
'PaymentFacilitatorData' => null,
'OrderId' => 'TDS_000000002020110828BC',
'MAC' => 'wgyfAJPbEPtTtce/+HRlXajSRfYA0J6mUcH+16EbB78=',
],
];
}
Expand Down
Loading

0 comments on commit 387c379

Please sign in to comment.