Completed
Push — master ( 284884...b70f30 )
by Kamil
20:25
created

PaypalContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 13
nc 1
nop 6
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\Context\Ui;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\Page\Shop\Checkout\FinalizeStepInterface;
16
use Sylius\Behat\Page\External\PaypalExpressCheckoutPageInterface;
17
use Sylius\Behat\Page\Shop\Order\OrderPaymentsPageInterface;
18
use Sylius\Behat\PaypalApiMocker;
19
use Sylius\Component\Core\Model\OrderInterface;
20
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
21
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
22
23
/**
24
 * @author Arkadiusz Krakowiak <[email protected]>
25
 */
26
final class PaypalContext implements Context
27
{
28
    /**
29
     * @var SharedStorageInterface
30
     */
31
    private $sharedStorage;
32
33
    /**
34
     * @var OrderPaymentsPageInterface
35
     */
36
    private $orderPaymentsPage;
37
38
    /**
39
     * @var PaypalExpressCheckoutPageInterface
40
     */
41
    private $paypalExpressCheckoutPage;
42
43
    /**
44
     * @var FinalizeStepInterface
45
     */
46
    private $checkoutFinalizeStep;
47
48
    /**
49
     * @var PaypalApiMocker
50
     */
51
    private $paypalApiMocker;
52
53
    /**
54
     * @var OrderRepositoryInterface
55
     */
56
    private $orderRepository;
57
58
    /**
59
     * @param SharedStorageInterface $sharedStorage
60
     * @param OrderPaymentsPageInterface $orderPaymentsPage
61
     * @param PaypalExpressCheckoutPageInterface $paypalExpressCheckoutPage
62
     * @param FinalizeStepInterface $checkoutFinalizeStep
63
     * @param PaypalApiMocker $paypalApiMocker
64
     * @param OrderRepositoryInterface $orderRepository
65
     */
66
    public function __construct(
67
        SharedStorageInterface $sharedStorage,
68
        OrderPaymentsPageInterface $orderPaymentsPage,
69
        PaypalExpressCheckoutPageInterface $paypalExpressCheckoutPage,
70
        FinalizeStepInterface $checkoutFinalizeStep,
71
        PaypalApiMocker $paypalApiMocker,
72
        OrderRepositoryInterface $orderRepository
73
    ) {
74
        $this->sharedStorage = $sharedStorage;
75
        $this->orderPaymentsPage = $orderPaymentsPage;
76
        $this->paypalExpressCheckoutPage = $paypalExpressCheckoutPage;
77
        $this->checkoutFinalizeStep = $checkoutFinalizeStep;
78
        $this->paypalApiMocker = $paypalApiMocker;
79
        $this->orderRepository = $orderRepository;
80
    }
81
82
    /**
83
     * @Given /^I confirm my order with paypal payment$/
84
     */
85
    public function iConfirmMyOrderWithPaypalPayment()
86
    {
87
        $this->paypalApiMocker->mockApiPaymentInitializeResponse();
88
        $this->checkoutFinalizeStep->confirmOrder();
89
    }
90
91
    /**
92
     * @Then I should be redirected to PayPal Express Checkout page
93
     */
94
    public function iShouldBeRedirectedToPaypalExpressCheckoutPage()
95
    {
96
        expect($this->paypalExpressCheckoutPage->isOpen())->toBe(true);
97
    }
98
99
    /**
100
     * @When I sign in to PayPal and pay successfully
101
     */
102
    public function iSignInToPaypalAndPaySuccessfully()
103
    {
104
        $this->paypalApiMocker->mockApiSuccessfulPaymentResponse();
105
        $this->paypalExpressCheckoutPage->pay();
106
    }
107
108
    /**
109
     * @When I cancel my PayPal payment
110
     */
111
    public function iCancelMyPaypalPayment()
112
    {
113
        $this->paypalExpressCheckoutPage->cancel();
114
    }
115
116
    /**
117
     * @When I try to pay again
118
     */
119
    public function iTryToPayAgain()
120
    {
121
        $order = $this->getLastOrder();
122
        $payment = $order->getLastPayment();
123
        $this->paypalApiMocker->mockApiPaymentInitializeResponse();
124
        $this->orderPaymentsPage->clickPayButtonForGivenPayment($payment);
125
    }
126
127
    /**
128
     * @return OrderInterface
129
     *
130
     * @throws \RuntimeException
131
     */
132
    private function getLastOrder()
133
    {
134
        $customer = $this->sharedStorage->get('user')->getCustomer();
135
        $orders = $this->orderRepository->findByCustomer($customer);
136
        $lastOrder = end($orders);
137
138
        if (false === $lastOrder) {
139
            throw new \RuntimeException(sprintf('There is no last order for %s', $customer->getFullName()));
140
        }
141
142
        return $lastOrder;
143
    }
144
}
145