Completed
Push — master ( 84981d...080a22 )
by Kamil
05:57
created

anEmailWithShipmentsConfirmationForTheOrderShouldBeSentTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
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 Webmozart\Assert\Assert;
22
23
final class EmailContext implements Context
24
{
25
    /** @var SharedStorageInterface */
26
    private $sharedStorage;
27
28
    /** @var EmailCheckerInterface */
29
    private $emailChecker;
30
31
    public function __construct(SharedStorageInterface $sharedStorage, EmailCheckerInterface $emailChecker)
32
    {
33
        $this->sharedStorage = $sharedStorage;
34
        $this->emailChecker = $emailChecker;
35
    }
36
37
    /**
38
     * @Then it should be sent to :recipient
39
     * @Then the email with reset token should be sent to :recipient
40
     * @Then the email with contact request should be sent to :recipient
41
     */
42
    public function anEmailShouldBeSentTo($recipient)
43
    {
44
        Assert::true($this->emailChecker->hasRecipient($recipient));
45
    }
46
47
    /**
48
     * @Then an email with the :method shipment's confirmation for the :orderNumber order should be sent to :email
49
     */
50
    public function anEmailWithShipmentsConfirmationForTheOrderShouldBeSentTo(string $method, string $orderNumber, string $recipient): void
51
    {
52
        Assert::true($this->emailChecker->hasMessageTo(
53
            sprintf(
54
                'Your order with number %s has been sent using %s.',
55
                $orderNumber,
56
                $method
57
            ),
58
            $recipient
59
        ));
60
    }
61
62
    /**
63
     * @Then :count email(s) should be sent to :recipient
64
     */
65
    public function numberOfEmailsShouldBeSentTo($count, $recipient)
66
    {
67
        Assert::same($this->emailChecker->countMessagesTo($recipient), (int) $count);
68
    }
69
70
    /**
71
     * @Then a welcoming email should have been sent to :recipient
72
     */
73
    public function aWelcomingEmailShouldHaveBeenSentTo($recipient)
74
    {
75
        $this->assertEmailContainsMessageTo('Welcome to our store', $recipient);
76
    }
77
78
    /**
79
     * @Then /^an email with the summary of (order placed by "([^"]+)") should be sent to him$/
80
     */
81
    public function anEmailWithOrderConfirmationShouldBeSentTo(OrderInterface $order)
82
    {
83
        $this->assertEmailContainsMessageTo(
84
            sprintf(
85
                'Your order no. %s has been successfully placed.',
86
                $order->getNumber()
87
            ),
88
            $order->getCustomer()->getEmailCanonical()
89
        );
90
    }
91
92
    /**
93
     * @Then /^an email with shipment's details of (this order) should be sent to "([^"]+)"$/
94
     */
95
    public function anEmailWithShipmentDetailsOfOrderShouldBeSentTo(OrderInterface $order, $recipient)
96
    {
97
        $this->assertEmailContainsMessageTo($order->getNumber(), $recipient);
98
        $this->assertEmailContainsMessageTo($this->getShippingMethodName($order), $recipient);
99
100
        $tracking = $this->sharedStorage->get('tracking_code');
101
        $this->assertEmailContainsMessageTo($tracking, $recipient);
102
    }
103
104
    /**
105
     * @param string $message
106
     * @param string $recipient
107
     */
108
    private function assertEmailContainsMessageTo($message, $recipient)
109
    {
110
        Assert::true($this->emailChecker->hasMessageTo($message, $recipient));
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    private function getShippingMethodName(OrderInterface $order)
117
    {
118
        /** @var ShipmentInterface $shipment */
119
        $shipment = $order->getShipments()->first();
120
        if (false === $shipment) {
121
            throw new \LogicException('Order should have at least one shipment.');
122
        }
123
124
        return $shipment->getMethod()->getName();
125
    }
126
}
127