Skip to content

Commit

Permalink
tests - cover event dispatch block
Browse files Browse the repository at this point in the history
  • Loading branch information
mustapayev committed Sep 6, 2024
1 parent 5b9b15a commit d34ef10
Show file tree
Hide file tree
Showing 26 changed files with 433 additions and 149 deletions.
21 changes: 14 additions & 7 deletions src/Gateways/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ public function makeRegularPayment(array $order, CreditCardInterface $creditCard
$order,
PosInterface::MODEL_NON_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down Expand Up @@ -282,7 +283,8 @@ public function makeRegularPostPayment(array $order): PosInterface
$order,
PosInterface::MODEL_NON_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down Expand Up @@ -325,7 +327,8 @@ public function refund(array $order): PosInterface
$order,
PosInterface::MODEL_NON_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down Expand Up @@ -368,7 +371,8 @@ public function cancel(array $order): PosInterface
$order,
PosInterface::MODEL_NON_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down Expand Up @@ -411,7 +415,8 @@ public function status(array $order): PosInterface
$order,
PosInterface::MODEL_NON_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down Expand Up @@ -450,7 +455,8 @@ public function history(array $data): PosInterface
$data,
PosInterface::MODEL_NON_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down Expand Up @@ -489,7 +495,8 @@ public function orderHistory(array $order): PosInterface
$order,
PosInterface::MODEL_NON_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
3 changes: 2 additions & 1 deletion src/Gateways/AkbankPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr
$order,
PosInterface::MODEL_3D_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
3 changes: 2 additions & 1 deletion src/Gateways/EstPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr
$order,
PosInterface::MODEL_3D_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
3 changes: 2 additions & 1 deletion src/Gateways/GarantiPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr
$order,
PosInterface::MODEL_3D_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
3 changes: 2 additions & 1 deletion src/Gateways/InterPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr
$order,
PosInterface::MODEL_3D_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
6 changes: 4 additions & 2 deletions src/Gateways/KuveytPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr
$order,
PosInterface::MODEL_3D_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down Expand Up @@ -345,7 +346,8 @@ private function getCommon3DFormData(KuveytPosAccount $kuveytPosAccount, array $
$order,
$paymentModel
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
6 changes: 4 additions & 2 deletions src/Gateways/PayFlexCPV4Pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public function make3DPayPayment(Request $request, array $order, string $txType)
$order,
PosInterface::MODEL_3D_PAY
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down Expand Up @@ -254,7 +255,8 @@ private function registerPayment(array $order, string $txType, string $paymentMo
$order,
$paymentModel
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
6 changes: 4 additions & 2 deletions src/Gateways/PayFlexV4Pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr
$order,
PosInterface::MODEL_3D_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down Expand Up @@ -231,7 +232,8 @@ private function sendEnrollmentRequest(array $order, CreditCardInterface $credit
$order,
$paymentModel
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
3 changes: 2 additions & 1 deletion src/Gateways/PayForPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr
$order,
PosInterface::MODEL_3D_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
9 changes: 6 additions & 3 deletions src/Gateways/PosNet.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr
$order,
PosInterface::MODEL_3D_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down Expand Up @@ -118,7 +119,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr
$order,
PosInterface::MODEL_3D_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down Expand Up @@ -266,7 +268,8 @@ private function getOosTransactionData(array $order, string $txType, string $pay
$order,
$paymentModel
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
3 changes: 2 additions & 1 deletion src/Gateways/PosNetV1Pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr
$order,
PosInterface::MODEL_3D_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
3 changes: 2 additions & 1 deletion src/Gateways/ToslaPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ private function registerPayment(array $order, string $paymentModel, string $txT
$order,
$paymentModel
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
6 changes: 4 additions & 2 deletions src/Gateways/VakifKatilimPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr
$order,
PosInterface::MODEL_3D_SECURE
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down Expand Up @@ -209,7 +210,8 @@ private function sendEnrollmentRequest(KuveytPosAccount $kuveytPosAccount, array
$order,
$paymentModel
);
$this->eventDispatcher->dispatch($event);
/** @var RequestDataPreparedEvent $event */
$event = $this->eventDispatcher->dispatch($event);
if ($requestData !== $event->getRequestData()) {
$this->logger->debug('Request data is changed via listeners', [
'txType' => $event->getTxType(),
Expand Down
31 changes: 23 additions & 8 deletions tests/Unit/Gateways/AkbankPosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @license MIT
*/

namespace Gateways;
namespace Mews\Pos\Tests\Unit\Gateways;

use Mews\Pos\Client\HttpClient;
use Mews\Pos\Crypt\CryptInterface;
Expand Down Expand Up @@ -887,14 +887,16 @@ private function configureClientResponse(
?int $statusCode = null
): void
{
$updatedRequestDataPreparedEvent = null;

$this->cryptMock->expects(self::once())
->method('hashString')
->with($encodedRequestData, $this->account->getStoreKey())
->willReturn('request-body-hash');

$this->serializerMock->expects(self::once())
->method('encode')
->with($requestData, $txType)
->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType)
->willReturn($encodedRequestData);

$this->serializerMock->expects(self::once())
Expand All @@ -918,11 +920,24 @@ private function configureClientResponse(

$this->eventDispatcherMock->expects(self::once())
->method('dispatch')
->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent
&& get_class($this->pos) === $dispatchedEvent->getGatewayClass()
&& $txType === $dispatchedEvent->getTxType()
&& $requestData === $dispatchedEvent->getRequestData()
&& $order === $dispatchedEvent->getOrder()
&& $paymentModel === $dispatchedEvent->getPaymentModel()));
->with($this->logicalAnd(
$this->isInstanceOf(RequestDataPreparedEvent::class),
$this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) {
$updatedRequestDataPreparedEvent = $dispatchedEvent;

return get_class($this->pos) === $dispatchedEvent->getGatewayClass()
&& $txType === $dispatchedEvent->getTxType()
&& $requestData === $dispatchedEvent->getRequestData()
&& $order === $dispatchedEvent->getOrder()
&& $paymentModel === $dispatchedEvent->getPaymentModel();
}
)))
->willReturnCallback(function () use (&$updatedRequestDataPreparedEvent) {
$updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData();
$updatedRequestData['test-update-request-data-with-event'] = true;
$updatedRequestDataPreparedEvent->setRequestData($updatedRequestData);

return $updatedRequestDataPreparedEvent;
});
}
}
29 changes: 22 additions & 7 deletions tests/Unit/Gateways/EstPosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,11 @@ private function configureClientResponse(
string $paymentModel
): void
{
$updatedRequestDataPreparedEvent = null;

$this->serializerMock->expects(self::once())
->method('encode')
->with($requestData, $txType)
->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType)
->willReturn($encodedRequestData);

$this->serializerMock->expects(self::once())
Expand All @@ -722,11 +724,24 @@ private function configureClientResponse(

$this->eventDispatcherMock->expects(self::once())
->method('dispatch')
->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent
&& get_class($this->pos) === $dispatchedEvent->getGatewayClass()
&& $txType === $dispatchedEvent->getTxType()
&& $requestData === $dispatchedEvent->getRequestData()
&& $order === $dispatchedEvent->getOrder()
&& $paymentModel === $dispatchedEvent->getPaymentModel()));
->with($this->logicalAnd(
$this->isInstanceOf(RequestDataPreparedEvent::class),
$this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) {
$updatedRequestDataPreparedEvent = $dispatchedEvent;

return get_class($this->pos) === $dispatchedEvent->getGatewayClass()
&& $txType === $dispatchedEvent->getTxType()
&& $requestData === $dispatchedEvent->getRequestData()
&& $order === $dispatchedEvent->getOrder()
&& $paymentModel === $dispatchedEvent->getPaymentModel();
}
)))
->willReturnCallback(function () use (&$updatedRequestDataPreparedEvent) {
$updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData();
$updatedRequestData['test-update-request-data-with-event'] = true;
$updatedRequestDataPreparedEvent->setRequestData($updatedRequestData);

return $updatedRequestDataPreparedEvent;
});
}
}
Loading

0 comments on commit d34ef10

Please sign in to comment.