Completed
Push — symfony-fqcn-sylius-4 ( 896d93...f41f61 )
by Kamil
18:34
created

PaypalContext::assertNotification()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
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\External\PaypalExpressCheckoutPageInterface;
16
use Sylius\Behat\Page\Shop\Checkout\CompletePageInterface;
17
use Sylius\Behat\Page\Shop\Order\ShowPageInterface;
18
use Sylius\Behat\Service\Mocker\PaypalApiMocker;
19
use Sylius\Behat\Service\SharedStorageInterface;
20
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
21
use Webmozart\Assert\Assert;
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 PaypalExpressCheckoutPageInterface
35
     */
36
    private $paypalExpressCheckoutPage;
37
38
    /**
39
     * @var ShowPageInterface
40
     */
41
    private $orderDetails;
42
43
    /**
44
     * @var CompletePageInterface
45
     */
46
    private $summaryPage;
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 PaypalExpressCheckoutPageInterface $paypalExpressCheckoutPage
61
     * @param ShowPageInterface $orderDetails
62
     * @param CompletePageInterface $summaryPage
63
     * @param PaypalApiMocker $paypalApiMocker
64
     * @param OrderRepositoryInterface $orderRepository
65
     */
66
    public function __construct(
67
        SharedStorageInterface $sharedStorage,
68
        PaypalExpressCheckoutPageInterface $paypalExpressCheckoutPage,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $paypalExpressCheckoutPage exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
69
        ShowPageInterface $orderDetails,
70
        CompletePageInterface $summaryPage,
71
        PaypalApiMocker $paypalApiMocker,
72
        OrderRepositoryInterface $orderRepository
73
    ) {
74
        $this->sharedStorage = $sharedStorage;
75
        $this->paypalExpressCheckoutPage = $paypalExpressCheckoutPage;
76
        $this->orderDetails = $orderDetails;
77
        $this->summaryPage = $summaryPage;
78
        $this->paypalApiMocker = $paypalApiMocker;
79
        $this->orderRepository = $orderRepository;
80
    }
81
82
    /**
83
     * @When /^I confirm my order with paypal payment$/
84
     * @Given /^I have confirmed my order with paypal payment$/
85
     */
86
    public function iConfirmMyOrderWithPaypalPayment()
87
    {
88
        $this->paypalApiMocker->performActionInApiInitializeScope(function () {
89
            $this->summaryPage->confirmOrder();
90
        });
91
    }
92
93
    /**
94
     * @Then I should be redirected back to PayPal Express Checkout page
95
     */
96
    public function iShouldBeRedirectedToPaypalExpressCheckoutPage()
97
    {
98
        Assert::true($this->paypalExpressCheckoutPage->isOpen());
99
    }
100
101
    /**
102
     * @When I sign in to PayPal and pay successfully
103
     */
104
    public function iSignInToPaypalAndPaySuccessfully()
105
    {
106
        $this->paypalApiMocker->performActionInApiSuccessfulScope(function () {
107
            $this->paypalExpressCheckoutPage->pay();
108
        });
109
    }
110
111
    /**
112
     * @Given /^I have cancelled (?:|my )PayPal payment$/
113
     * @When /^I cancel (?:|my )PayPal payment$/
114
     */
115
    public function iCancelMyPaypalPayment()
116
    {
117
        $this->paypalExpressCheckoutPage->cancel();
118
    }
119
120
    /**
121
     * @Given /^I tried to pay(?:| again)$/
122
     * @When /^I try to pay(?:| again)$/
123
     */
124
    public function iTryToPayAgain()
125
    {
126
        $this->paypalApiMocker->performActionInApiInitializeScope(function () {
127
            $this->orderDetails->pay();
128
        });
129
    }
130
131
    /**
132
     * @Then I should be notified that my payment has been cancelled
133
     */
134
    public function iShouldBeNotifiedThatMyPaymentHasBeenCancelled()
135
    {
136
        $this->assertNotification('Your payment has been cancelled.');
137
138
    }
139
140
    /**
141
     * @Then I should be notified that my payment has been completed
142
     */
143
    public function iShouldBeNotifiedThatMyPaymentHasBeenCompleted()
144
    {
145
        $this->assertNotification('Your payment has been completed.');
146
    }
147
148
    /**
149
     * @param string $expectedNotification
150
     */
151
    private function assertNotification($expectedNotification)
152
    {
153
        $notifications = $this->orderDetails->getNotifications();
154
        $hasNotifications = '';
155
156
        foreach ($notifications as $notification) {
157
            $hasNotifications .= $notification;
158
            if ($notification === $expectedNotification) {
159
                return;
160
            }
161
        }
162
163
        throw new \RuntimeException(sprintf('There is no notificaiton with "%s". Got "%s"', $expectedNotification, $hasNotifications));
164
    }
165
}
166