|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file was created by developers working at BitBag |
| 5 | + * Do you need more information about us and what we do? Visit our https://bitbag.io website! |
| 6 | + * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
| 7 | +*/ |
| 8 | + |
| 9 | +namespace spec\BitBag\SyliusPayUPlugin\EventListener; |
| 10 | + |
| 11 | +use BitBag\SyliusPayUPlugin\EventListener\PayUResponseExceptionEventListener; |
| 12 | +use BitBag\SyliusPayUPlugin\Exception\PayUResponseException; |
| 13 | +use PhpSpec\ObjectBehavior; |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 18 | +use Symfony\Component\HttpFoundation\Session\Session; |
| 19 | +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
| 20 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 21 | +use Symfony\Component\Routing\RouterInterface; |
| 22 | +use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
| 23 | + |
| 24 | +final class PayUResponseExceptionEventListenerSpec extends ObjectBehavior |
| 25 | +{ |
| 26 | + public function let( |
| 27 | + RouterInterface $router, |
| 28 | + RequestStack $requestStack, |
| 29 | + LoggerInterface $logger |
| 30 | + ): void |
| 31 | + { |
| 32 | + $this->beConstructedWith($router, $requestStack, $logger); |
| 33 | + } |
| 34 | + |
| 35 | + public function it_is_initializable(): void |
| 36 | + { |
| 37 | + $this->shouldHaveType(PayUResponseExceptionEventListener::class); |
| 38 | + } |
| 39 | + |
| 40 | + public function it_should_redirect_to_order_payment_page_if_token_value_is_set( |
| 41 | + RequestStack $requestStack, |
| 42 | + Session $session, |
| 43 | + FlashBagInterface $flashBag, |
| 44 | + RouterInterface $router, |
| 45 | + HttpKernelInterface $kernel, |
| 46 | + RedirectResponse $response, |
| 47 | + ): void |
| 48 | + { |
| 49 | + $order = ['tokenValue' => 'D8gHAy3dpj12x']; |
| 50 | + $exception = new PayUResponseException("ERROR_INCONSISTENT_CURRENCIES", 500, $order); |
| 51 | + $event = new ExceptionEvent( |
| 52 | + $kernel->getWrappedObject(), |
| 53 | + new Request(), |
| 54 | + 500, |
| 55 | + $exception |
| 56 | + ); |
| 57 | + |
| 58 | + $requestStack->getSession()->willReturn($session); |
| 59 | + $session->getBag('flashes')->willReturn($flashBag); |
| 60 | + $router->generate('sylius_shop_order_show', ['tokenValue' => $order['tokenValue']]) |
| 61 | + ->willReturn(sprintf('/order/%s', $order['tokenValue'])); |
| 62 | + $response->getTargetUrl()->willReturn(sprintf('/order/%s', $order['tokenValue'])); |
| 63 | + |
| 64 | + $flashBag->add('error', PayUResponseException::getTranslationByMessage($exception->getMessage())) |
| 65 | + ->shouldBeCalled(); |
| 66 | + |
| 67 | + $this->onPayuOpenException($event); |
| 68 | + } |
| 69 | + |
| 70 | + public function it_should_redirect_to_empty_cart_page_if_token_value_is_not_set( |
| 71 | + RequestStack $requestStack, |
| 72 | + Session $session, |
| 73 | + FlashBagInterface $flashBag, |
| 74 | + RouterInterface $router, |
| 75 | + HttpKernelInterface $kernel, |
| 76 | + RedirectResponse $response, |
| 77 | + ): void |
| 78 | + { |
| 79 | + $order = []; |
| 80 | + $exception = new PayUResponseException("ERROR_INCONSISTENT_CURRENCIES", 500, $order); |
| 81 | + $event = new ExceptionEvent( |
| 82 | + $kernel->getWrappedObject(), |
| 83 | + new Request(), |
| 84 | + 500, |
| 85 | + $exception |
| 86 | + ); |
| 87 | + |
| 88 | + $requestStack->getSession()->willReturn($session); |
| 89 | + $session->getBag('flashes')->willReturn($flashBag); |
| 90 | + $router->generate('sylius_shop_cart_summary')->willReturn('/cart'); |
| 91 | + $response->getTargetUrl()->willReturn('/cart'); |
| 92 | + |
| 93 | + $flashBag->add('error', PayUResponseException::getTranslationByMessage($exception->getMessage())) |
| 94 | + ->shouldBeCalled(); |
| 95 | + |
| 96 | + $this->onPayuOpenException($event); |
| 97 | + } |
| 98 | + |
| 99 | + public function it_should_redirect_to_empty_cart_page_if_token_value_is_not_set_and_currency_is_correct( |
| 100 | + RequestStack $requestStack, |
| 101 | + Session $session, |
| 102 | + FlashBagInterface $flashBag, |
| 103 | + RouterInterface $router, |
| 104 | + HttpKernelInterface $kernel, |
| 105 | + RedirectResponse $response, |
| 106 | + ): void |
| 107 | + { |
| 108 | + $order = []; |
| 109 | + $exception = new PayUResponseException("SOME_ERROR", 500, $order); |
| 110 | + $event = new ExceptionEvent( |
| 111 | + $kernel->getWrappedObject(), |
| 112 | + new Request(), |
| 113 | + 500, |
| 114 | + $exception |
| 115 | + ); |
| 116 | + |
| 117 | + $requestStack->getSession()->willReturn($session); |
| 118 | + $session->getBag('flashes')->willReturn($flashBag); |
| 119 | + $router->generate('sylius_shop_cart_summary')->willReturn('/cart'); |
| 120 | + $response->getTargetUrl()->willReturn('/cart'); |
| 121 | + |
| 122 | + $flashBag->add('error', PayUResponseException::getTranslationByMessage($exception->getMessage())) |
| 123 | + ->shouldBeCalled(); |
| 124 | + |
| 125 | + $this->onPayuOpenException($event); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + public function it_logs_errors(LoggerInterface $logger): void |
| 130 | + { |
| 131 | + $exception = new \Exception('An error occurred'); |
| 132 | + $logger->error('[PayU] Error while placing order An error occurred')->shouldBeCalled(); |
| 133 | + |
| 134 | + $this->logError($exception); |
| 135 | + } |
| 136 | +} |
0 commit comments