Skip to content

Commit 1f8bbc8

Browse files
candemiralpRokPopovraoulritter
authored
[ECP-8677] Make /donations endpoints more testable and write API functional tests (#2356)
* setup api functional tests for donations * [ECP-8677] Update the payload * [ECP-8677] Make the donation functionality more testable and update the test * [ECP-8677] Use https for base url * [ECP-8677] Formatting * [ECP-8677] Fix class name * [ECP-8677] Remove type declaration * Revert "[ECP-8677] Use https for base url" This reverts commit 0636b89. * [ECP-8677] Write unit tests * [ECP-8677] Update test mock * [ECP-8677] Use generated mock for factory classes * [ECP-8677] Remove method argument and use property instead * [ECP-8677] Write unit tests * [ECP-8677] Exclude view directory from SonarCloud * [ECP-8677] Update unit test * [ECP-8677] Update unit test mocks --------- Co-authored-by: RokPopov <[email protected]> Co-authored-by: raoulritter <[email protected]>
1 parent e6e8cfe commit 1f8bbc8

18 files changed

+733
-97
lines changed

.github/Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ graphql:
103103
@cd ${MAGENTO_ROOT}/dev/tests/api-functional && \
104104
${MAGENTO_ROOT}/vendor/bin/phpunit --prepend ${GRAPHQL_PHP} --configuration ${GRAPHQL_XML} ${GRAPHQL_SUITE}
105105

106+
# REST API tests
107+
restapi:
108+
@cd ${MAGENTO_ROOT}/dev/tests/api-functional && \
109+
${MAGENTO_ROOT}/vendor/bin/phpunit --prepend /data/extensions/workdir/Test/phpunit_rest.php \
110+
--configuration ${MAGENTO_ROOT}/dev/tests/api-functional/phpunit_rest.xml.dist \
111+
${MAGENTO_ROOT}/vendor/adyen/module-payment/Test/api-functional/Webapi
112+
106113
# Destroy services
107114
clean:
108115
docker-compose -f workflows/templates/docker-compose.yml down --volumes --rmi local

.github/workflows/restapi-test.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: REST API Tests
2+
on: [pull_request]
3+
4+
jobs:
5+
build:
6+
strategy:
7+
matrix:
8+
php-version: ["8.1"]
9+
magento-version: ["2.4.5"]
10+
runs-on: ubuntu-latest
11+
env:
12+
PHP_VERSION: ${{ matrix.php-version }}
13+
MAGENTO_VERSION: ${{ matrix.magento-version }}
14+
ADMIN_USERNAME: ${{secrets.MAGENTO_ADMIN_USERNAME}}
15+
ADMIN_PASSWORD: ${{secrets.MAGENTO_ADMIN_PASSWORD}}
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
20+
- name: Install Magento
21+
run: docker-compose -f .github/workflows/templates/docker-compose.yml run --rm web make magento
22+
23+
- name: Start web server in background
24+
run: docker-compose -f .github/workflows/templates/docker-compose.yml up -d web
25+
env:
26+
DONATION_ACCOUNT: ${{secrets.DONATION_ACCOUNT}}
27+
ADYEN_MERCHANT: ${{secrets.ADYEN_MERCHANT}}
28+
ADYEN_API_KEY: ${{secrets.ADYEN_API_KEY}}
29+
ADYEN_CLIENT_KEY: ${{secrets.ADYEN_CLIENT_KEY}}
30+
31+
- name: Setup permissions
32+
run: docker exec magento2-container make fs
33+
34+
- name: Check install
35+
run: docker exec magento2-container make sys-check
36+
37+
- name: Install plugin
38+
run: docker exec -u www-data magento2-container make plugin
39+
40+
- run: docker exec magento2-container /etc/init.d/cron stop
41+
42+
- name: Run REST API tests
43+
run: docker exec magento2-container make restapi

Api/AdyenDonationsInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ interface AdyenDonationsInterface
1717
/**
1818
* Build and send donation payment request
1919
*
20+
* @param int $orderId
2021
* @param string $payload
2122
* @return void
2223
*/
23-
public function donate(string $payload): void;
24+
public function donate(int $orderId, string $payload): void;
2425
}

Api/GuestAdyenDonationsInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ interface GuestAdyenDonationsInterface
1717
/**
1818
* Build and send donation payment request for guest shoppers
1919
*
20+
* @param string $cartId
2021
* @param string $payload
21-
* @param string $orderId
2222
* @return void
2323
*/
24-
public function donate(string $payload, string $orderId): void;
24+
public function donate(string $cartId, string $payload): void;
2525
}

Block/Checkout/Success.php

+30-53
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
use Adyen\Payment\Helper\Data;
1616
use Adyen\Payment\Helper\PaymentResponseHandler;
1717
use Adyen\Payment\Model\Ui\AdyenCheckoutSuccessConfigProvider;
18-
use Magento\Checkout\Model\Session;
18+
use Magento\Checkout\Model\Session as CheckoutSession;
19+
use Magento\Customer\Model\Session as CustomerSession;
20+
use Magento\Framework\Exception\NoSuchEntityException;
21+
use Magento\Quote\Model\QuoteIdToMaskedQuoteId;
1922
use Magento\Framework\Serialize\SerializerInterface;
2023
use Magento\Framework\View\Element\Template;
2124
use Magento\Framework\View\Element\Template\Context;
@@ -25,62 +28,22 @@
2528

2629
class Success extends Template
2730
{
28-
29-
/**
30-
* @var Order $order
31-
*/
3231
protected $order;
32+
protected CheckoutSession $checkoutSession;
33+
protected CustomerSession $customerSession;
34+
protected OrderFactory $orderFactory;
35+
protected Data $adyenHelper;
36+
protected StoreManagerInterface $storeManager;
37+
private Config $configHelper;
38+
private SerializerInterface $serializerInterface;
39+
private AdyenCheckoutSuccessConfigProvider $configProvider;
40+
private QuoteIdToMaskedQuoteId $quoteIdToMaskedQuoteId;
3341

34-
/**
35-
* @var Session
36-
*/
37-
protected $checkoutSession;
38-
39-
/**
40-
* @var OrderFactory
41-
*/
42-
protected $orderFactory;
43-
44-
45-
/**
46-
* @var Data
47-
*/
48-
protected $adyenHelper;
49-
50-
/**
51-
* @var StoreManagerInterface
52-
*/
53-
protected $storeManager;
54-
55-
/**
56-
* @var Config
57-
*/
58-
private $configHelper;
59-
60-
/**
61-
* @var SerializerInterface
62-
*/
63-
private $serializerInterface;
64-
65-
/**
66-
* @var AdyenCheckoutSuccessConfigProvider
67-
*/
68-
private $configProvider;
69-
70-
/**
71-
* Success constructor.
72-
*
73-
* @param Context $context
74-
* @param Session $checkoutSession
75-
* @param OrderFactory $orderFactory
76-
* @param Data $adyenHelper
77-
* @param Config $configHelper
78-
* @param StoreManagerInterface $storeManager
79-
* @param array $data
80-
*/
8142
public function __construct(
8243
Context $context,
83-
Session $checkoutSession,
44+
CheckoutSession $checkoutSession,
45+
CustomerSession $customerSession,
46+
QuoteIdToMaskedQuoteId $quoteIdToMaskedQuoteId,
8447
OrderFactory $orderFactory,
8548
Data $adyenHelper,
8649
Config $configHelper,
@@ -90,6 +53,8 @@ public function __construct(
9053
array $data = []
9154
) {
9255
$this->checkoutSession = $checkoutSession;
56+
$this->customerSession = $customerSession;
57+
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
9358
$this->orderFactory = $orderFactory;
9459
$this->adyenHelper = $adyenHelper;
9560
$this->configHelper = $configHelper;
@@ -204,4 +169,16 @@ public function getOrder()
204169
return $this->order;
205170
}
206171

172+
/**
173+
* @throws NoSuchEntityException
174+
*/
175+
public function getMaskedQuoteId(): ?string
176+
{
177+
return $this->quoteIdToMaskedQuoteId->execute($this->getOrder()->getQuoteId());
178+
}
179+
180+
public function getIsCustomerLoggedIn(): bool
181+
{
182+
return $this->customerSession->isLoggedIn();
183+
}
207184
}

Model/Api/AdyenDonations.php

+22-12
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,63 @@
1717
use Adyen\Payment\Helper\Config;
1818
use Adyen\Payment\Helper\Data;
1919
use Adyen\Payment\Helper\PaymentMethods;
20+
use Adyen\Payment\Model\Sales\OrderRepository;
2021
use Adyen\Payment\Model\Ui\AdyenCcConfigProvider;
2122
use Adyen\Util\Uuid;
22-
use Magento\Checkout\Model\Session;
23+
use Magento\Framework\Exception\InputException;
2324
use Magento\Framework\Exception\LocalizedException;
25+
use Magento\Framework\Exception\NoSuchEntityException;
2426
use Magento\Framework\Serialize\Serializer\Json;
2527
use Magento\Payment\Gateway\Command\CommandPoolInterface;
28+
use Magento\Sales\Api\Data\OrderInterface;
2629
use Magento\Sales\Model\Order;
27-
use Magento\Sales\Model\OrderFactory;
2830

2931
class AdyenDonations implements AdyenDonationsInterface
3032
{
3133
private CommandPoolInterface $commandPool;
32-
private Session $checkoutSession;
33-
private OrderFactory $orderFactory;
3434
private Json $jsonSerializer;
3535
protected Data $dataHelper;
3636
private ChargedCurrency $chargedCurrency;
3737
private Config $config;
3838
private PaymentMethods $paymentMethodsHelper;
39+
private OrderRepository $orderRepository;
3940

4041
private $donationTryCount;
4142

4243
public function __construct(
4344
CommandPoolInterface $commandPool,
44-
OrderFactory $orderFactory,
45-
Session $checkoutSession,
4645
Json $jsonSerializer,
4746
Data $dataHelper,
4847
ChargedCurrency $chargedCurrency,
4948
Config $config,
50-
PaymentMethods $paymentMethodsHelper
49+
PaymentMethods $paymentMethodsHelper,
50+
OrderRepository $orderRepository
5151
) {
5252
$this->commandPool = $commandPool;
53-
$this->orderFactory = $orderFactory;
54-
$this->checkoutSession = $checkoutSession;
5553
$this->jsonSerializer = $jsonSerializer;
5654
$this->dataHelper = $dataHelper;
5755
$this->chargedCurrency = $chargedCurrency;
5856
$this->config = $config;
5957
$this->paymentMethodsHelper = $paymentMethodsHelper;
58+
$this->orderRepository = $orderRepository;
6059
}
6160

62-
public function donate(string $payload): void
61+
/**
62+
* @throws NoSuchEntityException
63+
* @throws LocalizedException
64+
* @throws InputException
65+
*/
66+
public function donate(int $orderId, string $payload): void
67+
{
68+
$order = $this->orderRepository->get($orderId);
69+
70+
$this->makeDonation($payload, $order);
71+
}
72+
73+
public function makeDonation(string $payload, OrderInterface $order): void
6374
{
6475
$payload = $this->jsonSerializer->unserialize($payload);
65-
/** @var Order */
66-
$order = $this->orderFactory->create()->load($this->checkoutSession->getLastOrderId());
76+
6777
$paymentMethodInstance = $order->getPayment()->getMethodInstance();
6878
$donationToken = $order->getPayment()->getAdditionalInformation('donationToken');
6979

Model/Api/GuestAdyenDonations.php

+22-14
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,46 @@
1212

1313
namespace Adyen\Payment\Model\Api;
1414

15+
use Adyen\AdyenException;
1516
use Adyen\Payment\Api\GuestAdyenDonationsInterface;
16-
use Magento\Checkout\Model\Session;
17+
use Adyen\Payment\Model\Sales\OrderRepository;
1718
use Magento\Framework\Exception\LocalizedException;
18-
19+
use Magento\Quote\Model\QuoteIdMaskFactory;
1920

2021
class GuestAdyenDonations implements GuestAdyenDonationsInterface
2122
{
22-
private $checkoutSession;
23-
private $adyenDonationsModel;
23+
private AdyenDonations $adyenDonationsModel;
24+
private QuoteIdMaskFactory $quoteIdMaskFactory;
25+
private OrderRepository $orderRepository;
2426

2527
public function __construct(
26-
Session $checkoutSession,
27-
AdyenDonations $adyenDonationsModel
28+
AdyenDonations $adyenDonationsModel,
29+
QuoteIdMaskFactory $quoteIdMaskFactory,
30+
OrderRepository $orderRepository
2831
) {
29-
$this->checkoutSession = $checkoutSession;
3032
$this->adyenDonationsModel = $adyenDonationsModel;
33+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
34+
$this->orderRepository = $orderRepository;
3135
}
3236

3337
/**
38+
* @param string $cartId
3439
* @param string $payload
35-
* @param string $orderId
3640
* @return void
41+
* @throws AdyenException
3742
* @throws LocalizedException
3843
*/
39-
public function donate(string $payload, string $orderId): void
44+
public function donate(string $cartId, string $payload): void
4045
{
41-
if ($this->checkoutSession->getLastOrderId() !== $orderId) {
42-
throw new LocalizedException(
43-
__("Donation failed!")
44-
);
46+
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
47+
$quoteId = $quoteIdMask->getQuoteId();
48+
49+
$order = $this->orderRepository->getOrderByQuoteId($quoteId);
50+
51+
if (!$order) {
52+
throw new AdyenException('Donation Failed!');
4553
}
4654

47-
$this->adyenDonationsModel->donate($payload);
55+
$this->adyenDonationsModel->makeDonation($payload, $order);
4856
}
4957
}

0 commit comments

Comments
 (0)