Skip to content

Commit ff30292

Browse files
committed
OP-561 - Create phpUnit tests
1 parent b9fb3b4 commit ff30292

File tree

5 files changed

+268
-1
lines changed

5 files changed

+268
-1
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ jobs:
162162
name: Run Behat
163163
run: vendor/bin/behat --colors --strict -vvv --no-interaction || vendor/bin/behat --colors --strict -vvv --no-interaction --rerun
164164

165-
166165
-
167166
name: Failed build Slack notification
168167
uses: rtCamp/action-slack-notify@v2

phpunit.xml.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.5/phpunit.xsd"
5+
colors="true"
6+
bootstrap="tests/Application/config/bootstrap.php">
7+
<testsuites>
8+
<testsuite name="BitBagSyliusImojePlugin Test Suite">
9+
<directory>tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
13+
<php>
14+
<ini name="error_reporting" value="-1" />
15+
16+
<server name="KERNEL_CLASS_PATH" value="/tests/Application/AppKernel.php" />
17+
<server name="IS_DOCTRINE_ORM_SUPPORTED" value="true" />
18+
19+
<env name="APP_ENV" value="test"/>
20+
<env name="SHELL_VERBOSITY" value="-1" />
21+
</php>
22+
</phpunit>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Bridge;
13+
14+
use BitBag\SyliusPayUPlugin\Bridge\OpenPayUBridge;
15+
use BitBag\SyliusPayUPlugin\Bridge\OpenPayUBridgeInterface;
16+
use PHPUnit\Framework\TestCase;
17+
18+
final class OpenPayUBridgeTest extends TestCase
19+
{
20+
private OpenPayUBridge $bridge;
21+
22+
protected function setUp(): void
23+
{
24+
$this->bridge = new OpenPayUBridge();
25+
}
26+
27+
public function testItImplementsPayUBridgeInterface(): void
28+
{
29+
$this->assertInstanceOf(OpenPayUBridgeInterface::class, $this->bridge);
30+
}
31+
32+
public function testSetAuthorizationData(): void
33+
{
34+
$this->bridge->setAuthorizationData(OpenPayUBridgeInterface::SANDBOX_ENVIRONMENT, 'test', 'test', 'test', 'test');
35+
$this->assertTrue(true);
36+
}
37+
}
38+
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\Provider;
13+
14+
use BitBag\SyliusPayUPlugin\Api\Provider\PayUDataProvider;
15+
use PHPUnit\Framework\TestCase;
16+
use Sylius\Bundle\CoreBundle\OrderPay\Provider\UrlProviderInterface;
17+
use Sylius\Bundle\PayumBundle\Provider\PaymentDescriptionProviderInterface;
18+
use Sylius\Component\Core\Model\CustomerInterface;
19+
use Sylius\Component\Core\Model\OrderInterface;
20+
use Sylius\Component\Core\Model\PaymentInterface;
21+
use Sylius\Component\Payment\Model\PaymentRequestInterface;
22+
23+
final class PayUDataProviderTest extends TestCase
24+
{
25+
private PayUDataProvider $payUDataProvider;
26+
private UrlProviderInterface $urlProvider;
27+
private PaymentDescriptionProviderInterface $paymentDescriptionProvider;
28+
29+
protected function setUp(): void
30+
{
31+
$this->urlProvider = $this->createMock(UrlProviderInterface::class);
32+
$this->paymentDescriptionProvider = $this->createMock(PaymentDescriptionProviderInterface::class);
33+
34+
$this->payUDataProvider = new PayUDataProvider(
35+
$this->urlProvider,
36+
$this->paymentDescriptionProvider
37+
);
38+
}
39+
40+
public function testPreparePayUCreateData(): void
41+
{
42+
$paymentRequest = $this->createMock(PaymentRequestInterface::class);
43+
$payment = $this->createMock(PaymentInterface::class);
44+
$order = $this->createMock(OrderInterface::class);
45+
$customer = $this->createMock(CustomerInterface::class);
46+
47+
$paymentRequest->method('getPayment')->willReturn($payment);
48+
$payment->method('getOrder')->willReturn($order);
49+
$order->method('getCustomerIp')->willReturn('127.0.0.1');
50+
$order->method('getCurrencyCode')->willReturn('PLN');
51+
$order->method('getTotal')->willReturn(10000);
52+
$order->method('getTokenValue')->willReturn('test-token');
53+
$order->method('getCustomer')->willReturn($customer);
54+
$order->method('getLocaleCode')->willReturn('pl_PL');
55+
56+
$customer->method('getEmail')->willReturn('test@example.com');
57+
$customer->method('getFirstName')->willReturn('John');
58+
$customer->method('getLastName')->willReturn('Doe');
59+
60+
$this->urlProvider
61+
->method('getUrl')
62+
->willReturn('https://example.com/continue');
63+
64+
$this->paymentDescriptionProvider
65+
->method('getPaymentDescription')
66+
->willReturn('Test payment');
67+
68+
$result = $this->payUDataProvider->preparePayUCreateData($paymentRequest);
69+
70+
$this->assertSame('https://example.com/continue', $result['continueUrl']);
71+
$this->assertSame('127.0.0.1', $result['customerIp']);
72+
$this->assertSame('PLN', $result['currencyCode']);
73+
$this->assertSame(10000, $result['totalAmount']);
74+
$this->assertSame('test-token', $result['tokenValue']);
75+
$this->assertSame(
76+
[
77+
'email' => 'test@example.com',
78+
'firstName' => 'John',
79+
'lastName' => 'Doe',
80+
'language' => 'pl'
81+
],
82+
$result['buyer']
83+
);
84+
}
85+
}

0 commit comments

Comments
 (0)