Completed
Push — master ( a818cb...b1c6ca )
by Kamil
07:13 queued 10s
created

anEmailWithTheConfirmationOfTheOrderShouldBeSentTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
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\Ui;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Behat\Service\SharedStorageInterface;
18
use Sylius\Component\Core\Model\OrderInterface;
19
use Sylius\Component\Core\Model\ShipmentInterface;
20
use Sylius\Component\Core\Test\Services\EmailCheckerInterface;
21
use Symfony\Contracts\Translation\TranslatorInterface;
22
use Webmozart\Assert\Assert;
23
24
final class EmailContext implements Context
25
{
26
    /** @var SharedStorageInterface */
27
    private $sharedStorage;
28
29
    /** @var EmailCheckerInterface */
30
    private $emailChecker;
31
32
    /** @var TranslatorInterface */
33
    private $translator;
34
35
    public function __construct(
36
        SharedStorageInterface $sharedStorage,
37
        EmailCheckerInterface $emailChecker,
38
        TranslatorInterface $translator
39
    ) {
40
        $this->sharedStorage = $sharedStorage;
41
        $this->emailChecker = $emailChecker;
42
        $this->translator = $translator;
43
    }
44
45
    /**
46
     * @Then it should be sent to :recipient
47
     * @Then the email with contact request should be sent to :recipient
48
     */
49
    public function anEmailShouldBeSentTo(string $recipient): void
50
    {
51
        Assert::true($this->emailChecker->hasRecipient($recipient));
52
    }
53
54
    /**
55
     * @Then an email with reset token should be sent to :recipient
56
     * @Then an email with reset token should be sent to :recipient in :localeCode locale
57
     */
58
    public function anEmailWithResetTokenShouldBeSentTo(string $recipient, string $localeCode = 'en_US'): void
59
    {
60
        $this->assertEmailContainsMessageTo(
61
            $this->translator->trans('sylius.email.password_reset.reset_your_password', [], null, $localeCode),
62
            $recipient
63
        );
64
    }
65
66
    /**
67
     * @Then an email with the :method shipment's confirmation for the :orderNumber order should be sent to :email
68
     */
69
    public function anEmailWithShipmentsConfirmationForTheOrderShouldBeSentTo(string $method, string $orderNumber, string $recipient): void
70
    {
71
        Assert::true($this->emailChecker->hasMessageTo(
72
            sprintf(
73
                'Your order with number %s has been sent using %s.',
74
                $orderNumber,
75
                $method
76
            ),
77
            $recipient
78
        ));
79
    }
80
81
    /**
82
     * @Then :count email(s) should be sent to :recipient
83
     */
84
    public function numberOfEmailsShouldBeSentTo(int $count, string $recipient): void
85
    {
86
        Assert::same($this->emailChecker->countMessagesTo($recipient), $count);
87
    }
88
89
    /**
90
     * @Then a welcoming email should have been sent to :recipient
91
     * @Then a welcoming email should have been sent to :recipient in :localeCode locale
92
     */
93
    public function aWelcomingEmailShouldHaveBeenSentTo(string $recipient, string $localeCode = 'en_US'): void
94
    {
95
        $this->assertEmailContainsMessageTo(
96
            $this->translator->trans('sylius.email.user_registration.welcome_to_our_store', [], null, $localeCode),
97
            $recipient
98
        );
99
    }
100
101
    /**
102
     * @Then an email with the confirmation of the order :order should be sent to :email
103
     * @Then an email with the confirmation of the order :order should be sent to :email in :localeCode locale
104
     */
105
    public function anEmailWithTheConfirmationOfTheOrderShouldBeSentTo(
106
        OrderInterface $order,
107
        string $recipient,
108
        string $localeCode = 'en_US'
109
    ): void {
110
        $this->assertEmailContainsMessageTo(
111
            sprintf(
112
                '%s %s %s',
113
                $this->translator->trans('sylius.email.order_confirmation.your_order_number', [], null, $localeCode),
114
                $order->getNumber(),
115
                $this->translator->trans('sylius.email.order_confirmation.has_been_successfully_placed', [], null, $localeCode)
116
            ),
117
            $recipient
118
        );
119
    }
120
121
    /**
122
     * @Then /^an email with the summary of (order placed by "[^"]+") should be sent to him$/
123
     * @Then /^an email with the summary of (order placed by "[^"]+") should be sent to him in ("([^"]+)" locale)$/
124
     */
125
    public function anEmailWithSummaryOfOrderPlacedByShouldBeSentTo(OrderInterface $order, string $localeCode = 'en_US'): void
126
    {
127
        $this->anEmailWithTheConfirmationOfTheOrderShouldBeSentTo($order, $order->getCustomer()->getEmailCanonical(), $localeCode);
128
    }
129
130
    /**
131
     * @Then /^an email with shipment's details of (this order) should be sent to "([^"]+)"$/
132
     * @Then /^an email with shipment's details of (this order) should be sent to "([^"]+)" in ("([^"]+)" locale)$/
133
     * @Then an email with the shipment's confirmation of the order :order should be sent to :recipient
134
     * @Then an email with the shipment's confirmation of the order :order should be sent to :recipient in :localeCode locale
135
     */
136
    public function anEmailWithShipmentDetailsOfOrderShouldBeSentTo(
137
        OrderInterface $order,
138
        string $recipient,
139
        string $localeCode = 'en_US'
140
    ): void {
141
        $this->assertEmailContainsMessageTo(
142
            sprintf(
143
                '%s %s %s %s.',
144
                $this->translator->trans('sylius.email.shipment_confirmation.your_order_with_number', [], null, $localeCode),
145
                $order->getNumber(),
146
                $this->translator->trans('sylius.email.shipment_confirmation.has_been_sent_using', [], null, $localeCode),
147
                $this->getShippingMethodName($order)
148
149
            ),
150
            $recipient
151
        );
152
153
        if ($this->sharedStorage->has('tracking_code')) {
154
            $this->assertEmailContainsMessageTo(
155
                sprintf(
156
                    '%s %s %s',
157
                    $this->sharedStorage->get('tracking_code'),
158
                    $this->translator->trans('sylius.email.shipment_confirmation.tracking_code', [], null, $localeCode),
159
                    $this->translator->trans('sylius.email.shipment_confirmation.thank_you_for_transaction', [], null, $localeCode)
160
161
                ),
162
                $recipient
163
            );
164
        }
165
    }
166
167
    private function assertEmailContainsMessageTo(string $message, string $recipient): void
168
    {
169
        Assert::true($this->emailChecker->hasMessageTo($message, $recipient));
170
    }
171
172
    private function getShippingMethodName(OrderInterface $order): string
173
    {
174
        /** @var ShipmentInterface $shipment */
175
        $shipment = $order->getShipments()->first();
176
        if (false === $shipment) {
177
            throw new \LogicException('Order should have at least one shipment.');
178
        }
179
180
        return $shipment->getMethod()->getName();
181
    }
182
}
183