Skip to content

Commit

Permalink
Add tests and additional changes to make Visa Mobile API and UI
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 committed Oct 15, 2024
1 parent 3f6495d commit f1521f0
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/Payum/Action/Api/CreateVisaMobileTransactionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function __construct(
parent::__construct();
}

/**
* @param CreateTransaction $request
*/
public function execute($request): void
{
/** @var PaymentInterface $model */
Expand Down Expand Up @@ -63,7 +66,6 @@ public function supports($request): bool

$details = $model->getDetails();

// TODO: Refactor after task #72
return isset($details['tpay']['visa_mobile']);
}
}
8 changes: 5 additions & 3 deletions src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public function createFrom(PaymentInterface $payment, string $notifyUrl, string
/** @var array{pay: array<string, mixed>} $payload */
$payload = $this->createRedirectBasedPaymentPayloadFactory->createFrom($payment, $notifyUrl, $localeCode);

/** @var array{tpay?: array{visa_mobile?: string}} $paymentDetails */
/** @phpstan-param array{tpay?: array{visa_mobile?: bool}} $paymentDetails */
$paymentDetails = $payment->getDetails();

Assert::keyExists($paymentDetails['tpay'], 'visa_mobile',
'The given payment is not visa mobile payment type.'
Assert::keyExists(
$paymentDetails['tpay'],
'visa_mobile',
'The given payment is not visa mobile payment type.',
);

$payload['pay']['groupId'] = PayGroup::VISA_MOBILE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Factory;

use Sylius\Component\Core\Model\PaymentInterface;
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Api/Command/PayByBlikHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function test_it_creates_a_blik_based_transaction(): void
'payment_url' => null,
'success_url' => null,
'failure_url' => null,
'visa_mobile' => false,
],
])->shouldBeCalled();

Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Api/Command/PayByCardHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function test_it_creates_a_card_based_transaction(): void
'payment_url' => null,
'success_url' => null,
'failure_url' => null,
'visa_mobile' => false,
],
])->shouldBeCalled();

Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Api/Command/PayByGooglePayHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function test_it_throws_an_exception_if_a_payment_status_is_null(): void
'payment_url' => null,
'success_url' => null,
'failure_url' => null,
'visa_mobile' => false,
],
])->shouldBeCalled();
$gateway->execute($createTransaction, catchReply: true)->shouldBeCalled();
Expand Down Expand Up @@ -126,6 +127,7 @@ public function test_it_creates_a_google_pay_based_transaction(): void
'payment_url' => null,
'success_url' => null,
'failure_url' => null,
'visa_mobile' => false,
],
])->shouldBeCalled();
$gateway->execute($createTransaction, catchReply: true)->shouldBeCalled();
Expand Down
159 changes: 159 additions & 0 deletions tests/Unit/Api/Command/PayByVisaMobileHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

declare(strict_types=1);

namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\Api\Command;

use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByVisaMobile;
use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByVisaMobileHandler;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateTransactionFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction;
use Payum\Core\GatewayInterface;
use Payum\Core\Model\GatewayConfigInterface;
use Payum\Core\Payum;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\PaymentRepositoryInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Webmozart\Assert\InvalidArgumentException;

class PayByVisaMobileHandlerTest extends TestCase
{
use ProphecyTrait;

private PaymentRepositoryInterface|ObjectProphecy $paymentRepository;

private Payum|ObjectProphecy $payum;

private CreateTransactionFactoryInterface|ObjectProphecy $createTransactionFactory;

protected function setUp(): void
{
$this->paymentRepository = $this->prophesize(PaymentRepositoryInterface::class);
$this->payum = $this->prophesize(Payum::class);
$this->createTransactionFactory = $this->prophesize(CreateTransactionFactoryInterface::class);
}

public function test_it_throw_an_exception_if_a_payment_cannot_be_found(): void
{
$this->expectException(NotFoundHttpException::class);
$this->expectExceptionMessage('Payment with id "1" cannot be found.');

$this->paymentRepository->find(1)->willReturn(null);

$this->createTestSubject()->__invoke(new PayByVisaMobile(1));
}

public function test_it_throws_an_exception_if_a_gateway_name_cannot_be_determined(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Gateway name cannot be determined.');

$payment = $this->prophesize(PaymentInterface::class);
$payment->getDetails()->willReturn([]);
$payment->setDetails(Argument::any());
$payment->getMethod()->willReturn(null);

$this->paymentRepository->find(1)->willReturn($payment);

$this->createTestSubject()->__invoke(new PayByVisaMobile(1));
}

public function test_it_throws_an_exception_if_payment_details_does_not_have_a_set_status(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Payment status is required to create a result.');

$gatewayConfig = $this->prophesize(GatewayConfigInterface::class);
$gatewayConfig->getGatewayName()->willReturn('tpay');

$paymentMethod = $this->prophesize(PaymentMethodInterface::class);
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);

$payment = $this->prophesize(PaymentInterface::class);
$payment->getMethod()->willReturn($paymentMethod);
$payment->getDetails()->willReturn(['tpay' => ['status' => null]]);
$payment->setDetails([
'tpay' => [
'transaction_id' => null,
'result' => null,
'status' => null,
'blik_token' => null,
'google_pay_token' => null,
'card' => null,
'payment_url' => null,
'success_url' => null,
'failure_url' => null,
'visa_mobile' => true,
],
])->shouldBeCalled();

$this->paymentRepository->find(1)->willReturn($payment);

$createTransaction = $this->prophesize(CreateTransaction::class);

$this->createTransactionFactory->createNewWithModel($payment)->willReturn($createTransaction);

$gateway = $this->prophesize(GatewayInterface::class);

$this->payum->getGateway('tpay')->willReturn($gateway);

$this->createTestSubject()->__invoke(new PayByVisaMobile(1));
}

public function test_it_creates_a_visa_mobile_based_transaction(): void
{
$gatewayConfig = $this->prophesize(GatewayConfigInterface::class);
$gatewayConfig->getGatewayName()->willReturn('tpay');

$paymentMethod = $this->prophesize(PaymentMethodInterface::class);
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);

$payment = $this->prophesize(PaymentInterface::class);
$payment->getMethod()->willReturn($paymentMethod);
$payment->getDetails()->willReturn(['tpay' => ['status' => 'pending', 'payment_url' => 'https://cw.org/pay']]);
$payment->setDetails([
'tpay' => [
'transaction_id' => null,
'result' => null,
'status' => 'pending',
'blik_token' => null,
'google_pay_token' => null,
'card' => null,
'payment_url' => 'https://cw.org/pay',
'success_url' => null,
'failure_url' => null,
'visa_mobile' => true,
],
])->shouldBeCalled();

$this->paymentRepository->find(1)->willReturn($payment);

$createTransaction = $this->prophesize(CreateTransaction::class);

$this->createTransactionFactory->createNewWithModel($payment)->willReturn($createTransaction);

$gateway = $this->prophesize(GatewayInterface::class);
$gateway->execute($createTransaction, catchReply: true)->shouldBeCalled();

$this->payum->getGateway('tpay')->willReturn($gateway);

$result = $this->createTestSubject()->__invoke(new PayByVisaMobile(1));

self::assertSame('pending', $result->status);
self::assertSame('https://cw.org/pay', $result->transactionPaymentUrl);
}

private function createTestSubject(): PayByVisaMobileHandler
{
return new PayByVisaMobileHandler(
$this->paymentRepository->reveal(),
$this->payum->reveal(),
$this->createTransactionFactory->reveal(),
);
}
}
1 change: 1 addition & 0 deletions tests/Unit/Api/Command/PayHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function test_it_executes_pay_by_blik_command_if_a_blik_token_is_passed()
'payment_url' => null,
'success_url' => 'https://cw.nonexisting/success',
'failure_url' => 'https://cw.nonexisting/failure',
'visa_mobile' => false,
],
])->shouldBeCalled();

Expand Down
79 changes: 79 additions & 0 deletions tests/Unit/Api/Factory/NextCommand/PayByVisaMobileFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\Api\Factory\NextCommand;

use CommerceWeavers\SyliusTpayPlugin\Api\Command\Pay;
use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByVisaMobile;
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\Exception\UnsupportedNextCommandFactory;
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommand\PayByVisaMobileFactory;
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommandFactoryInterface;
use Sylius\Component\Core\Model\Payment;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Sylius\Component\Core\Model\PaymentInterface;

class PayByVisaMobileFactoryTest extends TestCase
{
use ProphecyTrait;

public function test_it_does_not_support_a_command_without_an_visa_mobile_data(): void
{
$factory = $this->createTestSubject();

$this->assertFalse($factory->supports($this->createCommand(isVisaMobilePayment: false), $this->createPayment()));
}

public function test_it_does_not_support_a_command_without_a_payment_with_id(): void
{
$factory = $this->createTestSubject();

$this->assertFalse($factory->supports($this->createCommand(), new Payment()));
}

public function test_it_supports_a_command_with_a_visa_mobile_payment_set_as_true(): void
{
$factory = $this->createTestSubject();

$this->assertTrue($factory->supports($this->createCommand(), $this->createPayment()));
}

public function test_it_creates_a_pay_by_visa_mobile_command(): void
{
$command = $this->createTestSubject()->create($this->createCommand(), $this->createPayment());

$this->assertInstanceOf(PayByVisaMobile::class, $command);
}

public function test_it_throws_an_exception_when_trying_to_create_a_command_with_unsupported_factory(): void
{
$this->expectException(UnsupportedNextCommandFactory::class);

$this->createTestSubject()->create($this->createCommand(), new Payment());
}

private function createCommand(?string $token = null, bool $isVisaMobilePayment = true): Pay
{
return new Pay(
$token ?? 'token',
'https://cw.nonexisting/success',
'https://cw.nonexisting/failure',
isVisaMobilePayment: $isVisaMobilePayment,
);
}

private function createPayment(int $id = 1): PaymentInterface
{
$payment = $this->prophesize(PaymentInterface::class);
$payment->getId()->willReturn($id);

return $payment->reveal();
}

private function createTestSubject(): NextCommandFactoryInterface
{
return new PayByVisaMobileFactory();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function test_it_leaves_blik_field_once_blik_token_is_set(): void
$form->remove('card')->shouldBeCalled()->willReturn($form);
$form->remove('blik_token')->shouldNotBeCalled();
$form->remove('pay_by_link_channel_id')->shouldBeCalled()->willReturn($form);
$form->remove('visa_mobile')->shouldBeCalled()->willReturn($form);

$event = new FormEvent($form->reveal(), ['blik_token' => '123456']);

Expand All @@ -32,6 +33,7 @@ public function test_it_leaves_card_field_once_card_is_set(): void
$form->remove('card')->shouldNotBeCalled();
$form->remove('blik_token')->shouldBeCalled()->willReturn($form);
$form->remove('pay_by_link_channel_id')->shouldBeCalled()->willReturn($form);
$form->remove('visa_mobile')->shouldBeCalled()->willReturn($form);

$event = new FormEvent($form->reveal(), ['card' => 'h45h']);

Expand All @@ -44,18 +46,33 @@ public function test_it_leaves_pbl_channel_id_field_once_pbl_channel_id_is_set()
$form->remove('card')->shouldBeCalled()->willReturn($form);
$form->remove('blik_token')->shouldBeCalled()->willReturn($form);
$form->remove('pay_by_link_channel_id')->shouldNotBeCalled();
$form->remove('visa_mobile')->shouldBeCalled()->willReturn($form);

$event = new FormEvent($form->reveal(), ['pay_by_link_channel_id' => 1]);

$this->createTestSubject()->__invoke($event);
}

public function test_it_leaves_visa_mobile_field_once_visa_mobile_is_set(): void
{
$form = $this->prophesize(FormInterface::class);
$form->remove('card')->shouldBeCalled()->willReturn($form);
$form->remove('blik_token')->shouldBeCalled()->willReturn($form);
$form->remove('pay_by_link_channel_id')->shouldBeCalled()->willReturn($form);
$form->remove('visa_mobile')->shouldNotBeCalled();

$event = new FormEvent($form->reveal(), ['visa_mobile' => true]);

$this->createTestSubject()->__invoke($event);
}

public function test_it_removes_all_additional_fields_if_none_of_them_are_passed(): void
{
$form = $this->prophesize(FormInterface::class);
$form->remove('card')->shouldBeCalled()->willReturn($form);
$form->remove('blik_token')->shouldBeCalled()->willReturn($form);
$form->remove('pay_by_link_channel_id')->shouldBeCalled()->willReturn($form);
$form->remove('visa_mobile')->shouldBeCalled()->willReturn($form);

$event = new FormEvent($form->reveal(), []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public function test_it_creates_a_payment_and_requests_paying_it_with_a_provided
'payment_url' => null,
'success_url' => null,
'failure_url' => null,
'visa_mobile' => false,
],
])->shouldBeCalled();

Expand Down Expand Up @@ -185,6 +186,7 @@ public function test_it_tries_to_determine_a_gateway_name_by_model_once_token_is
'payment_url' => null,
'success_url' => null,
'failure_url' => null,
'visa_mobile' => false,
],
])->shouldBeCalled();

Expand Down
Loading

0 comments on commit f1521f0

Please sign in to comment.