Skip to content

Commit 72d5e10

Browse files
committed
added behat tests
1 parent a9b5427 commit 72d5e10

File tree

8 files changed

+212
-88
lines changed

8 files changed

+212
-88
lines changed

features/shop/paying_with_ht_payway_offsite_for_order.feature

+36-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,46 @@ Feature: Paying with HT PayWay Offsite during checkout
88
Given the store operates on a single channel in "United States"
99
And there is a user "john@locastic.com" identified by "password123"
1010
And the store has a payment method "HT PayWay Offsite" with a code "ht_payway_offsite" and HT PayWay Offsite payment gateway
11-
And the store has a product "PHP T-Shirt" priced at "€19.99"
11+
And the store has a product "PHP T-Shirt" priced at "€0.99"
1212
And the store ships everywhere for free
1313
And I am logged in as "john@locastic.com"
1414

15-
@ui @javascript
15+
@ui
1616
Scenario: Successful payment
1717
Given I added product "PHP T-Shirt" to the cart
1818
And I have proceeded selecting "HT PayWay Offsite" payment method
1919
When I confirm my order with HT PayWay Offsite payment
20-
# And I payed successfully in to HT PayWay Offsite gateway
21-
# Then I should be notified that my payment has been completed
20+
And I payed successfully in to HT PayWay Offsite gateway
21+
Then I should be notified that my payment has been completed
22+
And I should see the thank you page
23+
24+
@ui
25+
Scenario: Cancelling the payment
26+
Given I added product "PHP T-Shirt" to the cart
27+
And I have proceeded selecting "HT PayWay Offsite" payment method
28+
When I confirm my order with HT PayWay Offsite payment
29+
And I cancel my HT PayWay Offsite payment
30+
Then I should be notified that my payment has been cancelled
31+
And I should be able to pay again
32+
33+
@ui
34+
Scenario: Retrying the payment with success
35+
Given I added product "PHP T-Shirt" to the cart
36+
And I have proceeded selecting "HT PayWay Offsite" payment method
37+
When I confirm my order with HT PayWay Offsite payment
38+
And I cancel my HT PayWay Offsite payment
39+
When I try to pay again
40+
And I payed successfully in to HT PayWay Offsite gateway
41+
Then I should be notified that my payment has been completed
42+
And I should see the thank you page
43+
44+
@ui
45+
Scenario: Retrying the payment and failing
46+
Given I added product "PHP T-Shirt" to the cart
47+
And I have proceeded selecting "HT PayWay Offsite" payment method
48+
When I confirm my order with HT PayWay Offsite payment
49+
And I cancel my HT PayWay Offsite payment
50+
When I try to pay again
51+
And I cancel my HT PayWay Offsite payment
52+
Then I should be notified that my payment has been cancelled
53+
And I should be able to pay again

src/Action/StatusAction.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,32 @@ public function execute($request)
3232

3333
$model = $request->getModel();
3434

35-
if (false == isset($model['tcompayway_response'])) {
35+
if (false === isset($model['tcompayway_response'])) {
3636
$request->markNew();
3737

3838
return;
3939
}
4040

41-
$statusCode = $model['tcompayway_response']['pgw_result_code'];
41+
$statusCode = (int)$model['tcompayway_response']['pgw_result_code'];
4242

43-
if (0 == $statusCode && 0 == $this->api->getPgwAuthorizationType()) {
44-
// because Sylius doesnt recognize authorized payment state we mark it as captured
45-
//$request->markAuthorized();
43+
if (0 === $statusCode && 0 === (int)$this->api->getPgwAuthorizationType()) {
4644
$request->markCaptured();
4745

4846
return;
4947
}
5048

51-
if (0 == $statusCode && 1 == $this->api->getPgwAuthorizationType()) {
49+
if (0 === $statusCode && 1 === (int)$this->api->getPgwAuthorizationType()) {
5250
$request->markCaptured();
5351

5452
return;
5553
}
5654

55+
if ($statusCode === 3) {
56+
$request->markCanceled();
57+
58+
return;
59+
}
60+
5761
if ($statusCode > 0) {
5862
$request->markFailed();
5963

tests/Behat/Context/Setup/HTPayWayOffsiteContext.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ private function createPaymentMethodHTPayWayOffsite(
6969
bool $addForCurrentChannel = true,
7070
int $position = null
7171
): PaymentMethodInterface {
72-
7372
/** @var PaymentMethodInterface $paymentMethod */
7473
$paymentMethod = $this->paymentMethodExampleFactory->create(
7574
[
@@ -84,6 +83,7 @@ private function createPaymentMethodHTPayWayOffsite(
8483
)) ? [$this->sharedStorage->get('channel')] : [],
8584
]
8685
);
86+
8787
if (null !== $position) {
8888
$paymentMethod->setPosition($position);
8989
}

tests/Behat/Context/Ui/Shop/CheckoutContext.php

+26-16
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,46 @@
22

33
namespace Tests\Locastic\SyliusHTPayWayPlugin\Behat\Context\Ui\Shop;
44

5-
65
use Behat\Behat\Context\Context;
7-
use Tests\Locastic\SyliusHTPayWayPlugin\Behat\Mocker\HTPayWayOffsiteMocker;
86
use Sylius\Behat\Page\Shop\Checkout\CompletePage;
7+
use Tests\Locastic\SyliusHTPayWayPlugin\Behat\Page\External\HTPayWayPaymentPage;
8+
99

1010
final class CheckoutContext implements Context
1111
{
12-
private $HTPayWayOffsiteMocker;
13-
1412
private $completePage;
1513

16-
/**
17-
* CheckoutContext constructor.
18-
* @param HTPayWayOffsiteMocker $HTPayWayOffsiteMocker
19-
*/
20-
public function __construct(HTPayWayOffsiteMocker $HTPayWayOffsiteMocker, CompletePage $completePage)
21-
{
22-
$this->HTPayWayOffsiteMocker = $HTPayWayOffsiteMocker;
14+
private $paymentPage;
15+
16+
public function __construct(
17+
CompletePage $completePage,
18+
HTPayWayPaymentPage $paymentPage
19+
) {
2320
$this->completePage = $completePage;
21+
$this->paymentPage = $paymentPage;
2422
}
2523

2624
/**
2725
* @When I confirm my order with HT PayWay Offsite payment
2826
*/
2927
public function iConfirmMyOrderWithHtPaywayOffsitePayment()
3028
{
31-
$this->HTPayWayOffsiteMocker->mockApiCreatePayment(
32-
function () {
33-
$this->completePage->confirmOrder();
34-
}
35-
);
29+
$this->completePage->confirmOrder();
30+
}
31+
32+
/**
33+
* @When I payed successfully in to HT PayWay Offsite gateway
34+
*/
35+
public function iPayedSuccessfullyInToHtPaywayOffsiteGateway()
36+
{
37+
$this->paymentPage->paySuccessfully();
38+
}
39+
40+
/**
41+
* @When I cancel my HT PayWay Offsite payment
42+
*/
43+
public function iCancelMyHtPaywayOffsitePayment()
44+
{
45+
$this->paymentPage->payCanceled();
3646
}
3747
}

tests/Behat/Mocker/HTPayWayOffsiteMocker.php

-51
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace Tests\Locastic\SyliusHTPayWayPlugin\Behat\Page\External;
4+
5+
use Behat\Mink\Session;
6+
use Locastic\TcomPayWay\AuthorizeForm\Helpers\SignatureGenerator;
7+
use Payum\Core\Security\TokenInterface;
8+
use Sylius\Behat\Page\Page;
9+
use Sylius\Bundle\CoreBundle\Doctrine\ORM\PaymentRepository;
10+
use Sylius\Component\Core\Model\Payment;
11+
use Sylius\Component\Resource\Repository\RepositoryInterface;
12+
use Symfony\Component\BrowserKit\Client;
13+
14+
final class HTPayWayPaymentPage extends Page
15+
{
16+
private $securityTokenRepository;
17+
18+
private $client;
19+
20+
private $paymentRepository;
21+
22+
public function __construct(
23+
Session $session,
24+
array $parameters,
25+
RepositoryInterface $securityTokenRepository,
26+
Client $client,
27+
PaymentRepository $paymentRepository
28+
) {
29+
parent::__construct($session, $parameters);
30+
31+
$this->securityTokenRepository = $securityTokenRepository;
32+
$this->client = $client;
33+
$this->paymentRepository = $paymentRepository;
34+
}
35+
36+
public function paySuccessfully(): void
37+
{
38+
$token = $this->findToken();
39+
40+
$data = $this->getSuccessResultData($token);
41+
42+
$this->client->request('POST', $token->getTargetUrl(), $data);
43+
$this->getDriver()->visit($token->getAfterUrl());
44+
}
45+
46+
public function payCanceled(): void
47+
{
48+
$token = $this->findToken();
49+
50+
$data = $this->getFailedResultData($token, 3);
51+
52+
$this->client->request('POST', $token->getTargetUrl(), $data);
53+
$this->getDriver()->visit($token->getAfterUrl());
54+
}
55+
56+
private function getSuccessResultData(TokenInterface $token): array
57+
{
58+
/** @var Payment $payment */
59+
$payment = $this->paymentRepository->find($token->getDetails()->getId());
60+
61+
$details = $payment->getDetails();
62+
63+
$resultData = [
64+
'pgw_merchant_data' => '',
65+
'pgw_transaction_id' => 'test-123-123',
66+
'pgw_order_id' => $details['pgwOrderId'],
67+
'pgw_amount' => $details['pgwAmount'],
68+
'pgw_card_type_id' => 1,
69+
'pgw_signature' => '',
70+
'pgw_installments' => 1,
71+
'pgw_trace_ref' => '20000445-4d1618b5c0bc4f4b904231176e301966-20180505160557499',
72+
];
73+
74+
$resultData['pgw_signature'] = SignatureGenerator::generateSignatureFromArray(
75+
'test_key',
76+
$resultData
77+
);
78+
79+
return $resultData;
80+
}
81+
82+
private function getFailedResultData(TokenInterface $token, int $resultCode = 1): array
83+
{
84+
/** @var Payment $payment */
85+
$payment = $this->paymentRepository->find($token->getDetails()->getId());
86+
87+
$details = $payment->getDetails();
88+
89+
$resultData = [
90+
'pgw_merchant_data' => '',
91+
'pgw_order_id' => $details['pgwOrderId'],
92+
'pgw_result_code' => $resultCode,
93+
'pgw_signature' => '',
94+
'pgw_trace_ref' => '20000445-4d1618b5c0bc4f4b904231176e301966-20180505160557499',
95+
];
96+
97+
$resultData['pgw_signature'] = SignatureGenerator::generateSignatureFromArray(
98+
'test_key',
99+
$resultData
100+
);
101+
102+
return $resultData;
103+
}
104+
105+
protected function getUrl(array $urlParameters = []): string
106+
{
107+
return 'https://pgwtest.ht.hr/services/payment/api/authorize-form';
108+
}
109+
110+
private function findToken(string $type = 'capture'): TokenInterface
111+
{
112+
$tokens = [];
113+
114+
/** @var TokenInterface $token */
115+
foreach ($this->securityTokenRepository->findAll() as $token) {
116+
if (strpos($token->getTargetUrl(), $type)) {
117+
$tokens[] = $token;
118+
}
119+
}
120+
121+
if (count($tokens) > 0) {
122+
return end($tokens);
123+
}
124+
125+
throw new \RuntimeException('Cannot find capture token, check if you are after proper checkout steps');
126+
}
127+
}

tests/Behat/Resources/services.yml

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
services:
2-
locastic.sylius_ht_pay_way_plugin.behat.ht_pay_way_offline_mocker:
3-
class: Tests\Locastic\SyliusHTPayWayPlugin\Behat\Mocker\HTPayWayOffsiteMocker
4-
arguments:
5-
- "@sylius.behat.mocker"
6-
72
locastic.sylius_ht_pay_way_plugin.behat.context.setup.ht_pay_way_offsite:
83
class: Tests\Locastic\SyliusHTPayWayPlugin\Behat\Context\Setup\HTPayWayOffsiteContext
94
arguments:
@@ -19,9 +14,16 @@ services:
1914
locastic.sylius_ht_pay_way_plugin.behat.context.ui.shop.checkout:
2015
class: Tests\Locastic\SyliusHTPayWayPlugin\Behat\Context\Ui\Shop\CheckoutContext
2116
arguments:
22-
# - "@bitbag_sylius_mollie_plugin.page.shop.checkout.complete"
23-
# - "@sylius.behat.page.shop.order.show"
24-
- "@locastic.sylius_ht_pay_way_plugin.behat.ht_pay_way_offline_mocker"
2517
- "@sylius.behat.page.shop.checkout.complete"
18+
- "@locastic.sylius_ht_pay_way_plugin.page.external.payment"
2619
tags:
27-
- { name: fob.context_service }
20+
- { name: fob.context_service }
21+
22+
locastic.sylius_ht_pay_way_plugin.page.external.payment:
23+
class: Tests\Locastic\SyliusHTPayWayPlugin\Behat\Page\External\HTPayWayPaymentPage
24+
parent: "sylius.behat.page"
25+
public: "false"
26+
arguments:
27+
- "@__symfony__.sylius.repository.payment_security_token"
28+
- "@__symfony__.test.client"
29+
- "@__symfony__.sylius.repository.payment"

tests/Behat/Resources/suites.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ default:
4343

4444
- locastic.sylius_ht_pay_way_plugin.behat.context.ui.shop.checkout
4545
filters:
46-
tags: "@paying_with_ht_payway_offsite_for_order && @ui && @javascript"
46+
tags: "@paying_with_ht_payway_offsite_for_order && @ui"

0 commit comments

Comments
 (0)