Skip to content

Commit

Permalink
Add more behat tests to handle rare cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed Apr 7, 2020
1 parent 2b9b674 commit 12f0b92
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,44 @@ Feature: Paying with Stripe during checkout
And I have proceeded selecting "Stripe" payment method
When I confirm my order with Stripe payment
And I get redirected to Stripe and complete my payment
Then I should be notified that my payment has been completed
Then I should be notified that my payment has been completed

@ui
Scenario: Cancelling the payment
Given I added product "PHP T-Shirt" to the cart
And I have proceeded selecting "Stripe" payment method
When I confirm my order with Stripe payment
And I cancel my Stripe payment
Then I should be notified that my payment has been cancelled
And I should be able to pay again

@ui
Scenario: Retrying the payment with success
Given I added product "PHP T-Shirt" to the cart
And I have proceeded selecting "Stripe" payment method
And I have confirmed my order with Stripe payment
But I have cancelled Stripe payment
When I try to pay again Stripe payment
And I get redirected to Stripe and complete my payment
Then I should be notified that my payment has been completed
And I should see the thank you page

@ui
Scenario: Retrying the payment and failing
Given I added product "PHP T-Shirt" to the cart
And I have proceeded selecting "Stripe" payment method
And I have confirmed my order with Stripe payment
But I have cancelled Stripe payment
When I try to pay again Stripe payment
And I cancel my Stripe payment
Then I should be notified that my payment has been cancelled
And I should be able to pay again

@ui
Scenario: Cancelling the payment
Given I added product "PHP T-Shirt" to the cart
And I have proceeded selecting "Stripe" payment method
When I confirm my order with Stripe payment
And I never fill any credit card field on my Stripe payment
Then I should be notified that my payment has been cancelled
And I should be able to pay again
71 changes: 63 additions & 8 deletions tests/Behat/Context/Ui/Shop/StripeShopContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Behat\Behat\Context\Context;
use Behat\MinkExtension\Context\MinkContext;
use Stripe\Checkout\Session;
use Stripe\Event;
use Stripe\PaymentIntent;
use Sylius\Behat\Page\Shop\Checkout\CompletePageInterface;
use Sylius\Behat\Page\Shop\Order\ShowPageInterface;
use Tests\Prometee\SyliusPayumStripeCheckoutSessionPlugin\Behat\Mocker\StripeSessionCheckoutMocker;
Expand Down Expand Up @@ -38,7 +41,8 @@ public function __construct(
}

/**
* @When /^I confirm my order with Stripe payment$/
* @When I confirm my order with Stripe payment
* @Given I have confirmed my order with Stripe payment
*/
public function iConfirmMyOrderWithStripePayment()
{
Expand All @@ -52,16 +56,48 @@ public function iConfirmMyOrderWithStripePayment()
*/
public function iGetRedirectedToStripe(): void
{
$this->stripeSessionCheckoutMocker->mockSuccessfulPayment(function () {
$this->stripeSessionCheckoutMocker->mockSuccessfulPayment(
function () {
$jsonEvent = [
'id' => 'evt_00000000000000',
'type' => Event::CHECKOUT_SESSION_COMPLETED,
'object' => 'event',
'data' => [
'object' => [
'id' => 'cs_00000000000000',
'object' => Session::OBJECT_NAME,
'payment_intent' => 'pi_00000000000000',
'metadata' => [
'token_hash' => '%s',
],
],
],
];
$payload = json_encode($jsonEvent);

$this->paymentPage->notify($payload);
},
function () {
$this->paymentPage->capture();
}
);
}

/**
* @When I cancel my Stripe payment
* @Given I have cancelled Stripe payment
*/
public function iCancelMyStripePayment()
{
$this->stripeSessionCheckoutMocker->mockCancelledPayment(function () {
$jsonEvent = [
'id' => 'evt_00000000000000',
'type' => 'checkout.session.completed',
'id' => 'evt_11111111111111',
'type' => Event::PAYMENT_INTENT_CANCELED,
'object' => 'event',
'data' => [
'object' => [
'id' => 'cs_00000000000000',
'object' => 'checkout.session',
'payment_intent' => 'pi_00000000000000',
'id' => 'pi_00000000000000',
'object' => PaymentIntent::OBJECT_NAME,
'metadata' => [
'token_hash' => '%s',
],
Expand All @@ -71,9 +107,28 @@ public function iGetRedirectedToStripe(): void
$payload = json_encode($jsonEvent);

$this->paymentPage->notify($payload);
},
function () {
$this->paymentPage->capture();
});
}

/**
* @When I try to pay again Stripe payment
*/
public function iTryToPayAgainStripePayment(): void
{
$this->stripeSessionCheckoutMocker->mockCreatePayment(function () {
$this->orderDetails->pay();
});
}

$this->stripeSessionCheckoutMocker->mockSuccessfulPayment(function () {
/**
* @Given /^I never fill any credit card field on my Stripe payment$/
*/
public function iNeverFillAnyCreditCardFieldOnMyStripePayment()
{
$this->stripeSessionCheckoutMocker->mockPaymentIntentRequiredStatus(function() {
$this->paymentPage->capture();
});
}
Expand Down
32 changes: 30 additions & 2 deletions tests/Behat/Mocker/StripeSessionCheckoutMocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,40 @@ public function mockCreatePayment(callable $action): void
$this->mocker->unmockAll();
}

public function mockSuccessfulPayment(callable $action): void
public function mockCancelledPayment(
callable $notifyAction,
callable $captureAction
): void
{
$this->mockPaymentIntentSync($notifyAction, PaymentIntent::STATUS_CANCELED);
$this->mockPaymentIntentSync($captureAction, PaymentIntent::STATUS_CANCELED);
}

public function mockSuccessfulPayment(
callable $notifyAction,
callable $captureAction
): void
{
$this->mockPaymentIntentSync($notifyAction, PaymentIntent::STATUS_SUCCEEDED);
$this->mockPaymentIntentSync($captureAction, PaymentIntent::STATUS_SUCCEEDED);
}

/**
* @param callable $captureAction
*
* @see https://stripe.com/docs/payments/intents#payment-intent
*/
public function mockPaymentIntentRequiredStatus(callable $captureAction)
{
$this->mockPaymentIntentSync($captureAction, PaymentIntent::STATUS_REQUIRES_PAYMENT_METHOD);
}

public function mockPaymentIntentSync(callable $action, string $status): void
{
$model = [
'id' => 'pi_1',
'object' => PaymentIntent::OBJECT_NAME,
'status' => PaymentIntent::STATUS_SUCCEEDED,
'status' => $status,
];

$mock = $this->mocker->mockService('tests.prometee.sylius_payum_stripe_checkout_session_plugin.behat.mocker.action.retrieve_payment_intent', AbstractRetrieveAction::class);
Expand Down

0 comments on commit 12f0b92

Please sign in to comment.