|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file has been created by developers from BitBag. |
| 5 | + * Feel free to contact us once you face any issues or want to start |
| 6 | + * You can find more information about us on https://bitbag.io and write us |
| 7 | + * an email on hello@bitbag.io. |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace Tests\BitBag\SyliusPayUPlugin\Unit\EventListener; |
| 13 | + |
| 14 | +use BitBag\SyliusPayUPlugin\EventListener\PayUResponseExceptionEventListener; |
| 15 | +use BitBag\SyliusPayUPlugin\Exception\PayUResponseException; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Psr\Log\LoggerInterface; |
| 19 | +use Symfony\Component\HttpFoundation\Request; |
| 20 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 21 | +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
| 22 | +use Symfony\Component\HttpFoundation\Session\Session; |
| 23 | +use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
| 24 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 25 | +use Symfony\Component\Routing\RouterInterface; |
| 26 | + |
| 27 | +final class PayUResponseExceptionEventListenerTest extends TestCase |
| 28 | +{ |
| 29 | + private PayUResponseExceptionEventListener $listener; |
| 30 | + |
| 31 | + private MockObject|RouterInterface $router; |
| 32 | + private MockObject|RequestStack $requestStack; |
| 33 | + private MockObject|LoggerInterface $logger; |
| 34 | + |
| 35 | + protected function setUp(): void |
| 36 | + { |
| 37 | + $this->router = $this->createMock(RouterInterface::class); |
| 38 | + $this->requestStack = $this->createMock(RequestStack::class); |
| 39 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 40 | + |
| 41 | + $this->listener = new PayUResponseExceptionEventListener( |
| 42 | + $this->router, |
| 43 | + $this->requestStack, |
| 44 | + $this->logger |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + public function test_it_redirects_to_order_payment_page_if_token_value_is_set(): void |
| 49 | + { |
| 50 | + $order = ['tokenValue' => 'D8gHAy3dpj12x']; |
| 51 | + $exception = new PayUResponseException('ERROR_INCONSISTENT_CURRENCIES', 500, $order); |
| 52 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 53 | + $event = new ExceptionEvent($kernel, new Request(), 500, $exception); |
| 54 | + |
| 55 | + $session = $this->createMock(Session::class); |
| 56 | + $flashBag = $this->createMock(FlashBagInterface::class); |
| 57 | + $this->requestStack->method('getSession')->willReturn($session); |
| 58 | + $session->method('getBag')->with('flashes')->willReturn($flashBag); |
| 59 | + |
| 60 | + $this->router->method('generate') |
| 61 | + ->with('sylius_shop_order_show', ['tokenValue' => $order['tokenValue']]) |
| 62 | + ->willReturn(sprintf('/order/%s', $order['tokenValue'])); |
| 63 | + |
| 64 | + $flashBag->expects($this->once()) |
| 65 | + ->method('add') |
| 66 | + ->with('error', PayUResponseException::getTranslationByMessage($exception->getMessage())); |
| 67 | + |
| 68 | + $this->listener->onPayuOpenException($event); |
| 69 | + } |
| 70 | + |
| 71 | + public function test_it_redirects_to_empty_cart_page_if_token_value_is_not_set(): void |
| 72 | + { |
| 73 | + $order = []; |
| 74 | + $exception = new PayUResponseException('ERROR_INCONSISTENT_CURRENCIES', 500, $order); |
| 75 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 76 | + $event = new ExceptionEvent($kernel, new Request(), 500, $exception); |
| 77 | + |
| 78 | + $session = $this->createMock(Session::class); |
| 79 | + $flashBag = $this->createMock(FlashBagInterface::class); |
| 80 | + $this->requestStack->method('getSession')->willReturn($session); |
| 81 | + $session->method('getBag')->with('flashes')->willReturn($flashBag); |
| 82 | + |
| 83 | + $this->router->method('generate')->with('sylius_shop_cart_summary')->willReturn('/cart'); |
| 84 | + |
| 85 | + $flashBag->expects($this->once()) |
| 86 | + ->method('add') |
| 87 | + ->with('error', PayUResponseException::getTranslationByMessage($exception->getMessage())); |
| 88 | + |
| 89 | + $this->listener->onPayuOpenException($event); |
| 90 | + } |
| 91 | + |
| 92 | + public function test_it_redirects_to_empty_cart_page_if_token_value_is_not_set_and_currency_is_correct(): void |
| 93 | + { |
| 94 | + $order = []; |
| 95 | + $exception = new PayUResponseException('SOME_ERROR', 500, $order); |
| 96 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 97 | + $event = new ExceptionEvent($kernel, new Request(), 500, $exception); |
| 98 | + |
| 99 | + $session = $this->createMock(Session::class); |
| 100 | + $flashBag = $this->createMock(FlashBagInterface::class); |
| 101 | + $this->requestStack->method('getSession')->willReturn($session); |
| 102 | + $session->method('getBag')->with('flashes')->willReturn($flashBag); |
| 103 | + |
| 104 | + $this->router->method('generate')->with('sylius_shop_cart_summary')->willReturn('/cart'); |
| 105 | + |
| 106 | + $flashBag->expects($this->once()) |
| 107 | + ->method('add') |
| 108 | + ->with('error', PayUResponseException::getTranslationByMessage($exception->getMessage())); |
| 109 | + |
| 110 | + $this->listener->onPayuOpenException($event); |
| 111 | + } |
| 112 | + |
| 113 | + public function test_it_logs_errors(): void |
| 114 | + { |
| 115 | + $exception = new \Exception('An error occurred'); |
| 116 | + |
| 117 | + $this->logger->expects($this->once()) |
| 118 | + ->method('error') |
| 119 | + ->with('[PayU] Error while placing order An error occurred'); |
| 120 | + |
| 121 | + $this->listener->logError($exception); |
| 122 | + } |
| 123 | +} |
0 commit comments