|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pstk\Paystack\Test\Unit\Controller\Payment; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use PHPUnit\Framework\MockObject\MockObject; |
| 7 | +use Pstk\Paystack\Controller\Payment\Callback; |
| 8 | +use Pstk\Paystack\Gateway\PaystackApiClient; |
| 9 | +use Pstk\Paystack\Gateway\Exception\ApiException; |
| 10 | +use Pstk\Paystack\Model\Ui\ConfigProvider; |
| 11 | +use Magento\Framework\App\Action\Context; |
| 12 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 13 | +use Magento\Framework\App\Response\RedirectInterface; |
| 14 | +use Magento\Framework\Controller\Result\RedirectFactory; |
| 15 | +use Magento\Framework\Controller\Result\Redirect; |
| 16 | +use Magento\Framework\Event\Manager as EventManager; |
| 17 | +use Magento\Framework\View\Result\PageFactory; |
| 18 | +use Magento\Framework\Message\ManagerInterface as MessageManager; |
| 19 | +use Magento\Payment\Helper\Data as PaymentHelper; |
| 20 | +use Magento\Sales\Api\Data\OrderInterface; |
| 21 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 22 | +use Magento\Checkout\Model\Session as CheckoutSession; |
| 23 | +use Magento\Store\Model\StoreManagerInterface; |
| 24 | +use Psr\Log\LoggerInterface; |
| 25 | + |
| 26 | +class CallbackTest extends TestCase |
| 27 | +{ |
| 28 | + /** @var Callback */ |
| 29 | + private $controller; |
| 30 | + |
| 31 | + /** @var MockObject|PaystackApiClient */ |
| 32 | + private $paystackClient; |
| 33 | + |
| 34 | + /** @var MockObject|EventManager */ |
| 35 | + private $eventManager; |
| 36 | + |
| 37 | + /** @var MockObject|HttpRequest */ |
| 38 | + private $request; |
| 39 | + |
| 40 | + /** @var MockObject|OrderInterface */ |
| 41 | + private $orderInterface; |
| 42 | + |
| 43 | + /** @var MockObject|MessageManager */ |
| 44 | + private $messageManager; |
| 45 | + |
| 46 | + private function createController(): Callback |
| 47 | + { |
| 48 | + $this->paystackClient = $this->createMock(PaystackApiClient::class); |
| 49 | + $this->eventManager = $this->createMock(EventManager::class); |
| 50 | + $this->request = $this->createMock(HttpRequest::class); |
| 51 | + $this->orderInterface = $this->createMock(\Magento\Sales\Model\Order::class); |
| 52 | + $this->messageManager = $this->createMock(MessageManager::class); |
| 53 | + |
| 54 | + $redirect = $this->createMock(Redirect::class); |
| 55 | + $redirect->method('setUrl')->willReturnSelf(); |
| 56 | + |
| 57 | + $redirectFactory = $this->createMock(RedirectFactory::class); |
| 58 | + $redirectFactory->method('create')->willReturn($redirect); |
| 59 | + |
| 60 | + $context = $this->createMock(Context::class); |
| 61 | + $context->method('getRequest')->willReturn($this->request); |
| 62 | + $context->method('getResponse')->willReturn($this->createMock(\Magento\Framework\App\ResponseInterface::class)); |
| 63 | + $context->method('getObjectManager')->willReturn($this->createMock(\Magento\Framework\ObjectManagerInterface::class)); |
| 64 | + $context->method('getEventManager')->willReturn($this->eventManager); |
| 65 | + $context->method('getMessageManager')->willReturn($this->messageManager); |
| 66 | + $context->method('getRedirect')->willReturn($this->createMock(RedirectInterface::class)); |
| 67 | + $context->method('getActionFlag')->willReturn($this->createMock(\Magento\Framework\App\ActionFlag::class)); |
| 68 | + $context->method('getView')->willReturn($this->createMock(\Magento\Framework\App\ViewInterface::class)); |
| 69 | + $context->method('getUrl')->willReturn($this->createMock(\Magento\Framework\UrlInterface::class)); |
| 70 | + $context->method('getResultRedirectFactory')->willReturn($redirectFactory); |
| 71 | + $context->method('getResultFactory')->willReturn( |
| 72 | + $this->createMock(\Magento\Framework\Controller\ResultFactory::class) |
| 73 | + ); |
| 74 | + |
| 75 | + return new Callback( |
| 76 | + $context, |
| 77 | + $this->createMock(PageFactory::class), |
| 78 | + $this->createMock(OrderRepositoryInterface::class), |
| 79 | + $this->orderInterface, |
| 80 | + $this->createMock(CheckoutSession::class), |
| 81 | + $this->createMock(PaymentHelper::class), |
| 82 | + $this->messageManager, |
| 83 | + $this->createMock(ConfigProvider::class), |
| 84 | + $this->createMock(StoreManagerInterface::class), |
| 85 | + $this->eventManager, |
| 86 | + $this->request, |
| 87 | + $this->createMock(LoggerInterface::class), |
| 88 | + $this->paystackClient |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + public function testSuccessfulCallbackDispatchesEvent(): void |
| 93 | + { |
| 94 | + $controller = $this->createController(); |
| 95 | + |
| 96 | + $this->request->method('get') |
| 97 | + ->with('reference') |
| 98 | + ->willReturn('000000001_suffix'); |
| 99 | + |
| 100 | + $verifyResponse = (object) [ |
| 101 | + 'data' => (object) [ |
| 102 | + 'reference' => '000000001_suffix', |
| 103 | + 'status' => 'success', |
| 104 | + ], |
| 105 | + ]; |
| 106 | + $this->paystackClient->method('verifyTransaction') |
| 107 | + ->with('000000001_suffix') |
| 108 | + ->willReturn($verifyResponse); |
| 109 | + |
| 110 | + $order = $this->createMock(\Magento\Sales\Model\Order::class); |
| 111 | + $order->method('getIncrementId')->willReturn('000000001'); |
| 112 | + $this->orderInterface->method('loadByIncrementId') |
| 113 | + ->with('000000001') |
| 114 | + ->willReturn($order); |
| 115 | + |
| 116 | + $this->eventManager->expects($this->once()) |
| 117 | + ->method('dispatch') |
| 118 | + ->with('paystack_payment_verify_after', ['paystack_order' => $order]); |
| 119 | + |
| 120 | + $controller->execute(); |
| 121 | + } |
| 122 | + |
| 123 | + public function testMissingReferenceRedirectsToFailure(): void |
| 124 | + { |
| 125 | + $controller = $this->createController(); |
| 126 | + |
| 127 | + $this->request->method('get') |
| 128 | + ->with('reference') |
| 129 | + ->willReturn(null); |
| 130 | + |
| 131 | + $this->messageManager->expects($this->once()) |
| 132 | + ->method('addErrorMessage'); |
| 133 | + |
| 134 | + $this->eventManager->expects($this->never())->method('dispatch'); |
| 135 | + |
| 136 | + $controller->execute(); |
| 137 | + } |
| 138 | + |
| 139 | + public function testApiExceptionRedirectsToFailure(): void |
| 140 | + { |
| 141 | + $controller = $this->createController(); |
| 142 | + |
| 143 | + $this->request->method('get')->willReturn('bad_ref'); |
| 144 | + |
| 145 | + $this->paystackClient->method('verifyTransaction') |
| 146 | + ->willThrowException(new ApiException('Transaction failed')); |
| 147 | + |
| 148 | + $this->messageManager->expects($this->once()) |
| 149 | + ->method('addErrorMessage'); |
| 150 | + |
| 151 | + $this->eventManager->expects($this->never())->method('dispatch'); |
| 152 | + |
| 153 | + $controller->execute(); |
| 154 | + } |
| 155 | + |
| 156 | + public function testOrderNotFoundRedirectsToFailure(): void |
| 157 | + { |
| 158 | + $controller = $this->createController(); |
| 159 | + |
| 160 | + $this->request->method('get')->willReturn('000000099'); |
| 161 | + |
| 162 | + $verifyResponse = (object) [ |
| 163 | + 'data' => (object) [ |
| 164 | + 'reference' => '000000099', |
| 165 | + 'status' => 'success', |
| 166 | + ], |
| 167 | + ]; |
| 168 | + $this->paystackClient->method('verifyTransaction')->willReturn($verifyResponse); |
| 169 | + |
| 170 | + $order = $this->createMock(\Magento\Sales\Model\Order::class); |
| 171 | + $order->method('getIncrementId')->willReturn(null); |
| 172 | + $this->orderInterface->method('loadByIncrementId')->willReturn($order); |
| 173 | + |
| 174 | + $this->eventManager->expects($this->never())->method('dispatch'); |
| 175 | + |
| 176 | + $controller->execute(); |
| 177 | + } |
| 178 | +} |
0 commit comments