Completed
Push — api ( cecea0...116a36 )
by Kamil
29:57 queued 30s
created

thePaymentOfTheOrderShouldBeFor()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.7
cc 4
nc 3
nop 3
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Api\Admin;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use Behat\Behat\Context\Context;
18
use Sylius\Behat\Client\ApiClientInterface;
19
use Sylius\Component\Core\Formatter\StringInflector;
20
use Sylius\Component\Core\Model\ChannelInterface;
21
use Sylius\Component\Core\Model\CustomerInterface;
22
use Sylius\Component\Core\Model\OrderInterface;
23
use Sylius\Component\Payment\PaymentTransitions;
24
use Webmozart\Assert\Assert;
25
26
final class ManagingPaymentsContext implements Context
27
{
28
    /** @var ApiClientInterface */
29
    private $client;
30
31
    /** @var IriConverterInterface */
32
    private $iriConverter;
33
34
    public function __construct(ApiClientInterface $client, IriConverterInterface $iriConverter)
35
    {
36
        $this->client = $client;
37
        $this->iriConverter = $iriConverter;
38
    }
39
40
    /**
41
     * @Given I am browsing payments
42
     * @When I browse payments
43
     */
44
    public function iAmBrowsingPayments(): void
45
    {
46
        $this->client->index('payments');
47
    }
48
49
    /**
50
     * @When I complete the payment of order :order
51
     */
52
    public function iCompleteThePaymentOfOrder(OrderInterface $order): void
53
    {
54
        $payment = $order->getLastPayment();
55
        Assert::notNull($payment);
56
57
        $this->client->applyTransition(
58
            'payments',
59
            (string) $payment->getId(),
60
            PaymentTransitions::TRANSITION_COMPLETE
61
        );
62
    }
63
64
    /**
65
     * @When I choose :state as a payment state
66
     */
67
    public function iChooseAsAPaymentState(string $state): void
68
    {
69
        $this->client->buildFilter(['state' => $state]);
70
    }
71
72
    /**
73
     * @When I filter
74
     */
75
    public function iFilter(): void
76
    {
77
        $this->client->filter('payments');
78
    }
79
80
    /**
81
     * @Then I should see a single payment in the list
82
     * @Then I should see :count payments in the list
83
     */
84
    public function iShouldSeePaymentsInTheList(int $count = 1): void
85
    {
86
        Assert::same($this->client->countCollectionItems(), $count);
87
    }
88
89
    /**
90
     * @Then the payment of the :orderNumber order should be :paymentState for :customer
91
     */
92
    public function thePaymentOfTheOrderShouldBeFor(
93
        string $orderNumber,
94
        string $paymentState,
95
        CustomerInterface $customer
96
    ): void {
97
        foreach ($this->client->getCollectionItemsWithValue('state', StringInflector::nameToLowercaseCode($paymentState)) as $payment) {
98
            $this->client->showByIri($payment['order']);
99
            if (
100
                $this->client->responseHasValue('number', $orderNumber) &&
101
                $this->client->relatedResourceHasValue('customer', 'email', $customer->getEmail())
102
            ) {
103
                return;
104
            }
105
        }
106
107
        throw new \InvalidArgumentException('There is no payment with given data.');
108
    }
109
110
    /**
111
     * @Then /^I should see payment for (the "[^"]+" order) as (\d+)(?:|st|nd|rd|th) in the list$/
112
     */
113
    public function iShouldSeePaymentForTheOrderInTheList(string $orderNumber, int $position): void
114
    {
115
        Assert::true(
116
            $this->client->hasItemOnPositionWithValue($position - 1, 'order', sprintf('/new-api/orders/%s', $orderNumber))
117
        );
118
    }
119
120
    /**
121
     * @Then I should be notified that the payment has been completed
122
     */
123
    public function iShouldBeNotifiedThatThePaymentHasBeenCompleted(): void
124
    {
125
        Assert::true($this->client->isUpdateSuccessful());
126
    }
127
128
    /**
129
     * @Then I should see the payment of order :order as :paymentState
130
     */
131
    public function iShouldSeeThePaymentOfOrderAs(OrderInterface $order, string $paymentState): void
132
    {
133
        $payment = $order->getLastPayment();
134
        Assert::notNull($payment);
135
136
        $this->client->show('payments', (string) $payment->getId());
137
        Assert::true($this->client->responseHasValue('state', StringInflector::nameToLowercaseCode($paymentState)));
138
    }
139
140
    /**
141
     * @Then I should see (also) the payment of the :order order
142
     */
143
    public function iShouldSeeThePaymentOfTheOrder(OrderInterface $order): void
144
    {
145
        Assert::true($this->client->hasItemWithValue('order', $this->iriConverter->getIriFromItem($order)));
146
    }
147
148
    /**
149
     * @Then I should not see the payment of the :order order
150
     */
151
    public function iShouldNotSeeThePaymentOfTheOrder(OrderInterface $order): void
152
    {
153
        Assert::false($this->client->hasItemWithValue('order', $this->iriConverter->getIriFromItem($order)));
154
    }
155
}
156