Skip to content

Commit

Permalink
feature #204 Test PayPal order creation requests (Zales0123)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.3 branch.

Discussion
----------

From the very beginning of this plugin life, we knew it would be hard to test it. We should avoid testing what we don't own (PayPal API), as well as javascript libraries (like `PayPal.Buttons`).

I think, that the proposed approach is a good compromise. It's almost impossible to test the whole checkout flow with UI Behats, so I decided I would just test each action separately, basing on mocked responses from PayPal API. It's for sure not everything, we should extend this test suite as much as possible, to feel secured with Sylius<>PayPal communication 🚀 

Commits
-------

c520d49 Test for PayPal order creation request
0d3c17d Test other PayPal orders creation actions
04c2ab6 Extract common data from fixture file
  • Loading branch information
SirDomin authored Oct 27, 2021
2 parents 6968ae0 + 04c2ab6 commit 8685def
Show file tree
Hide file tree
Showing 17 changed files with 296 additions and 38 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"dmore/chrome-mink-driver": "^2.7",
"friends-of-behat/mink": "^1.8",
"friends-of-behat/mink-browserkit-driver": "^1.4",
"friends-of-behat/mink-debug-extension": "^2.0",
"friends-of-behat/mink-extension": "^2.4",
"friends-of-behat/page-object-extension": "^0.3",
"friends-of-behat/suite-settings-extension": "^1.0",
"friends-of-behat/mink-debug-extension": "^2.0",
"friends-of-behat/symfony-extension": "^2.1",
"friends-of-behat/variadic-extension": "^1.3",
"friendsofsymfony/oauth-server-bundle": ">2.0.0-alpha.0 ^2.0@dev",
Expand All @@ -40,7 +40,8 @@
"symfony/web-profiler-bundle": "^4.4 || ^5.2",
"symfony/web-server-bundle": "^4.4|^5.0",
"vimeo/psalm": "3.11.4",
"rector/rector": "^0.11.53"
"rector/rector": "^0.11.53",
"lchrusciel/api-test-case": "^5.1"
},
"conflict": {
"symfony/doctrine-bridge": "4.4.16",
Expand Down
2 changes: 2 additions & 0 deletions tests/Application/config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@
Sylius\Bundle\ApiBundle\SyliusApiBundle::class => ['all' => true],
Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle::class => ['all' => true],
SyliusLabs\DoctrineMigrationsExtraBundle\SyliusLabsDoctrineMigrationsExtraBundle::class => ['all' => true],
Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => ['dev' => true, 'test' => true],
Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => ['dev' => true, 'test' => true],
];
22 changes: 22 additions & 0 deletions tests/Application/config/services_test.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
imports:
- { resource: "../../../vendor/sylius/sylius/src/Sylius/Behat/Resources/config/services.xml" }
- { resource: "../../Behat/Resources/services.xml" }

services:
Sylius\PayPalPlugin\Downloader\ReportDownloaderInterface:
class: Tests\Sylius\PayPalPlugin\Service\DummyPayoutsReportDownloader

Sylius\PayPalPlugin\Processor\PaymentCompleteProcessorInterface:
class: Tests\Sylius\PayPalPlugin\Service\VoidPayPalPaymentCompleteProcessor

Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface:
class: Tests\Sylius\PayPalPlugin\Service\DummyAuthorizeClientApi

Sylius\PayPalPlugin\Api\OrderDetailsApiInterface:
class: Tests\Sylius\PayPalPlugin\Service\DummyOrderDetailsApi

Sylius\PayPalPlugin\Api\RefundPaymentApiInterface:
class: Tests\Sylius\PayPalPlugin\Service\DummyRefundPaymentApi

Sylius\PayPalPlugin\Provider\PayPalRefundDataProviderInterface:
class: Tests\Sylius\PayPalPlugin\Service\DummyPayPalRefundDataProvider

Sylius\PayPalPlugin\Api\CreateOrderApiInterface:
class: Tests\Sylius\PayPalPlugin\Service\FakeCreateOrderApi
30 changes: 0 additions & 30 deletions tests/Behat/Resources/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,5 @@
</service>

<service id="Tests\Sylius\PayPalPlugin\Behat\Page\Shop\Checkout\PayPalSelectPaymentPage" parent="sylius.behat.page.shop.checkout.select_payment" public="false"/>

<service
id="Sylius\PayPalPlugin\Downloader\ReportDownloaderInterface"
class="Tests\Sylius\PayPalPlugin\Behat\Service\DummyPayoutsReportDownloader"
/>

<service
id="Sylius\PayPalPlugin\Processor\PaymentCompleteProcessorInterface"
class="Tests\Sylius\PayPalPlugin\Behat\Service\VoidPayPalPaymentCompleteProcessor"
/>

<service
id="Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface"
class="Tests\Sylius\PayPalPlugin\Behat\Service\DummyAuthorizeClientApi"
/>

<service
id="Sylius\PayPalPlugin\Api\OrderDetailsApiInterface"
class="Tests\Sylius\PayPalPlugin\Behat\Service\DummyOrderDetailsApi"
/>

<service
id="Sylius\PayPalPlugin\Api\RefundPaymentApiInterface"
class="Tests\Sylius\PayPalPlugin\Behat\Service\DummyRefundPaymentApi"
/>

<service
id="Sylius\PayPalPlugin\Provider\PayPalRefundDataProviderInterface"
class="Tests\Sylius\PayPalPlugin\Behat\Service\DummyPayPalRefundDataProvider"
/>
</services>
</container>
30 changes: 30 additions & 0 deletions tests/DataFixtures/ORM/resources/new_cart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Sylius\Component\Core\Model\Order:
new_cart:
channel: "@channel_web"
items: ["@sw_mug_item"]
currencyCode: "USD"
localeCode: "en_US"
customer: "@customer_oliver"
state: "cart"
checkoutState: "shipping_selected"
tokenValue: "TOKEN"
payments: ["@paypal_payment"]

Sylius\Component\Core\Model\OrderItem:
sw_mug_item:
units: ["@sw_mug_item_unit1", "@sw_mug_item_unit2"]
variant: "@mug_sw"
order: "@new_cart"

Sylius\Component\Core\Model\OrderItemUnit:
sw_mug_item_unit1:
__construct: ["@sw_mug_item"]
sw_mug_item_unit2:
__construct: ["@sw_mug_item"]

Sylius\Component\Core\Model\Payment:
paypal_payment:
method: "@paypal"
currencyCode: "USD"
amount: 40
state: "cart"
30 changes: 30 additions & 0 deletions tests/DataFixtures/ORM/resources/new_order.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Sylius\Component\Core\Model\Order:
new_order:
channel: "@channel_web"
items: ["@sw_mug_item"]
currencyCode: "USD"
localeCode: "en_US"
customer: "@customer_oliver"
state: "new"
checkoutState: "completed"
tokenValue: "TOKEN"
payments: ["@paypal_payment"]

Sylius\Component\Core\Model\OrderItem:
sw_mug_item:
units: ["@sw_mug_item_unit1", "@sw_mug_item_unit2"]
variant: "@mug_sw"
order: "@new_order"

Sylius\Component\Core\Model\OrderItemUnit:
sw_mug_item_unit1:
__construct: ["@sw_mug_item"]
sw_mug_item_unit2:
__construct: ["@sw_mug_item"]

Sylius\Component\Core\Model\Payment:
paypal_payment:
method: "@paypal"
currencyCode: "USD"
amount: 40
state: "new"
111 changes: 111 additions & 0 deletions tests/DataFixtures/ORM/resources/shop.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
Sylius\Component\Core\Model\Product:
mug:
code: "MUG"
channels: ["@channel_web"]
currentLocale: "en_US"
translations:
en_US: "@mug_translation"

Sylius\Component\Core\Model\ProductTranslation:
mug_translation:
slug: "mug"
locale: "en_US"
name: "Mug"
description: "<paragraph(2)>"
translatable: "@mug"

Sylius\Component\Core\Model\ProductVariant:
mug_sw:
code: "MUG_SW"
product: "@mug"
currentLocale: "en_US"
translations:
en_US: "@sw_mug_translation"
updatedAt: "<(new \\DateTime('2015-10-10'))>"
channelPricings:
WEB: "@sw_mug_web_channel_pricing"
mug_lotr:
code: "MUG_LOTR"
product: "@mug"
currentLocale: "en_US"
translations:
en_US: "@lotr_mug_translation"
updatedAt: "<(new \\DateTime('2015-10-04'))>"
channelPricings:
WEB: "@lotr_mug_web_channel_pricing"

Sylius\Component\Product\Model\ProductVariantTranslation:
sw_mug_translation:
locale: "en_US"
name: "Star wars mug"
translatable: "@mug_sw"
lotr_mug_translation:
locale: "en_US"
name: "Lotr mug"
translatable: "@mug_lotr"

Sylius\Component\Core\Model\Channel:
channel_web:
code: "WEB"
name: "Web Channel"
hostname: "localhost"
description: "Lorem ipsum"
baseCurrency: "@currency"
defaultLocale: "@locale"
locales: ["@locale"]
color: "black"
enabled: true
taxCalculationStrategy: "order_items_based"

Sylius\Component\Currency\Model\Currency:
currency:
code: "USD"

Sylius\Component\Locale\Model\Locale:
locale:
code: "en_US"

Sylius\Component\Core\Model\Customer:
customer_oliver:
firstName: "Oliver"
lastName: "Queen"
email: "[email protected]"
emailCanonical: "[email protected]"
birthday: "<(new \\DateTime())>"

Sylius\Component\Core\Model\ChannelPricing:
sw_mug_web_channel_pricing:
channelCode: "WEB"
price: 20
lotr_mug_web_channel_pricing:
channelCode: "WEB"
price: 20

Sylius\Component\Core\Model\PaymentMethod:
paypal:
code: PAYPAL
enabled: true
gatewayConfig: "@paypal_config"
currentLocale: en_US
translations:
- "@paypal_translation"
channels: ["@channel_web"]

Sylius\Bundle\PayumBundle\Model\GatewayConfig:
paypal_config:
gatewayName: "paypal"
factoryName: "sylius.pay_pal"
config:
client_id: "CLIENT_ID"
client_secret: "CLIENT_SECRET"
merchant_id: "MERCHANT_ID"
sylius_merchant_id: "SYLIUS_MERCHANT_ID"
partner_attribution_id: "PARTNER_ATTRIBUTION_ID"
use_authorize: true

Sylius\Component\Payment\Model\PaymentMethodTranslation:
paypal_translation:
name: "PayPal"
locale: "en_US"
description: <paragraph(2)>
translatable: "@paypal"
24 changes: 24 additions & 0 deletions tests/Functional/CreatePayPalOrderActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\PayPalPlugin\Functional;

use ApiTestCase\JsonApiTestCase;

final class CreatePayPalOrderActionTest extends JsonApiTestCase
{
/** @test */
function it_creates_pay_pal_order_and_returns_its_data(): void
{
$this->loadFixturesFromFiles(['resources/shop.yaml', 'resources/new_order.yaml']);

$this->client->request('POST', '/en_US/create-pay-pal-order/TOKEN');

$response = $this->client->getResponse();
$content = (array) json_decode($response->getContent(), true);

$this->assertSame($content['orderID'], 'PAYPAL_ORDER_ID');
$this->assertSame($content['status'], 'processing');
}
}
27 changes: 27 additions & 0 deletions tests/Functional/CreatePayPalOrderFromCartActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\PayPalPlugin\Functional;

use ApiTestCase\JsonApiTestCase;

final class CreatePayPalOrderFromCartActionTest extends JsonApiTestCase
{
/** @test */
function it_creates_pay_pal_order_from_cart_and_returns_its_data(): void
{
$order = $this->loadFixturesFromFiles(['resources/shop.yaml', 'resources/new_cart.yaml']);
/** @var int $orderId */
$orderId = $order['new_cart']->getId();

$this->client->request('POST', '/en_US/create-pay-pal-order-from-cart/' . $orderId);

$response = $this->client->getResponse();
$content = (array) json_decode($response->getContent(), true);

$this->assertSame($content['id'], $orderId);
$this->assertSame($content['orderID'], 'PAYPAL_ORDER_ID');
$this->assertSame($content['status'], 'cart');
}
}
25 changes: 25 additions & 0 deletions tests/Functional/CreatePayPalOrderFromPaymentPageActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\PayPalPlugin\Functional;

use ApiTestCase\JsonApiTestCase;

final class CreatePayPalOrderFromPaymentPageActionTest extends JsonApiTestCase
{
/** @test */
function it_creates_pay_pal_order_from_payment_page_and_returns_its_data(): void
{
$order = $this->loadFixturesFromFiles(['resources/shop.yaml', 'resources/new_cart.yaml']);
/** @var int $orderId */
$orderId = $order['new_cart']->getId();

$this->client->request('POST', '/en_US/pay-pal-order-payment-page/' . $orderId . '/create');

$response = $this->client->getResponse();
$content = (array) json_decode($response->getContent(), true);

$this->assertSame($content['order_id'], 'PAYPAL_ORDER_ID');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Sylius\PayPalPlugin\Behat\Service;
namespace Tests\Sylius\PayPalPlugin\Service;

use Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Sylius\PayPalPlugin\Behat\Service;
namespace Tests\Sylius\PayPalPlugin\Service;

use Sylius\PayPalPlugin\Api\OrderDetailsApiInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Sylius\PayPalPlugin\Behat\Service;
namespace Tests\Sylius\PayPalPlugin\Service;

use Sylius\PayPalPlugin\Provider\PayPalRefundDataProviderInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Sylius\PayPalPlugin\Behat\Service;
namespace Tests\Sylius\PayPalPlugin\Service;

use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\PayPalPlugin\Downloader\PayoutsReportDownloaderInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Sylius\PayPalPlugin\Behat\Service;
namespace Tests\Sylius\PayPalPlugin\Service;

use Sylius\PayPalPlugin\Api\RefundPaymentApiInterface;

Expand Down
Loading

0 comments on commit 8685def

Please sign in to comment.