-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests and additional changes to make Visa Mobile API and UI
- Loading branch information
Showing
18 changed files
with
289 additions
and
10 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
2 changes: 2 additions & 0 deletions
2
src/Tpay/Factory/CreateVisaMobilePaymentPayloadFactoryInterface.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
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
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,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(), | ||
); | ||
} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
tests/Unit/Api/Factory/NextCommand/PayByVisaMobileFactoryTest.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,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(); | ||
} | ||
|
||
} |
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
Oops, something went wrong.