|
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\Service\SharedStorageInterface; |
|
16
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
17
|
|
|
use Sylius\Component\Core\Model\ShipmentInterface; |
|
18
|
|
|
use Sylius\Component\Core\Test\Services\EmailCheckerInterface; |
|
19
|
|
|
use Webmozart\Assert\Assert; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Jan Góralski <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
final class EmailContext implements Context |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var SharedStorageInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $sharedStorage; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var EmailCheckerInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $emailChecker; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param SharedStorageInterface $sharedStorage |
|
38
|
|
|
* @param EmailCheckerInterface $emailChecker |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(SharedStorageInterface $sharedStorage, EmailCheckerInterface $emailChecker) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->sharedStorage = $sharedStorage; |
|
43
|
|
|
$this->emailChecker = $emailChecker; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @Then it should be sent to :recipient |
|
48
|
|
|
* @Then the email with reset token should be sent to :recipient |
|
49
|
|
|
*/ |
|
50
|
|
|
public function anEmailShouldBeSentTo($recipient) |
|
51
|
|
|
{ |
|
52
|
|
|
Assert::true( |
|
53
|
|
|
$this->emailChecker->hasRecipient($recipient), |
|
54
|
|
|
sprintf('An email should have been sent to %s.', $recipient) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @Then :count email(s) should be sent to :recipient |
|
60
|
|
|
*/ |
|
61
|
|
|
public function numberOfEmailsShouldBeSentTo($count, $recipient) |
|
62
|
|
|
{ |
|
63
|
|
|
$actualMessagesCount = $this->emailChecker->countMessagesTo($recipient); |
|
64
|
|
|
|
|
65
|
|
|
Assert::eq( |
|
66
|
|
|
$actualMessagesCount, |
|
67
|
|
|
$count, |
|
68
|
|
|
sprintf( |
|
69
|
|
|
'%d messages were sent, while there should be %d.', |
|
70
|
|
|
$actualMessagesCount, |
|
71
|
|
|
$count |
|
72
|
|
|
) |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @Then a welcoming email should have been sent to :recipient |
|
78
|
|
|
*/ |
|
79
|
|
|
public function aWelcomingEmailShouldHaveBeenSentTo($recipient) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->assertEmailContainsMessageTo('Welcome to our store', $recipient); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @Then /^an email with the summary of (order placed by "([^"]+)") should be sent to him$/ |
|
86
|
|
|
*/ |
|
87
|
|
|
public function anEmailWithOrderConfirmationShouldBeSentTo(OrderInterface $order) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->assertEmailContainsMessageTo( |
|
90
|
|
|
sprintf( |
|
91
|
|
|
'Your order no. %s has been successfully placed.', |
|
92
|
|
|
$order->getNumber() |
|
93
|
|
|
), |
|
94
|
|
|
$order->getCustomer()->getEmailCanonical() |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @Then /^an email with shipment's details of (this order) should be sent to "([^"]+)"$/ |
|
100
|
|
|
*/ |
|
101
|
|
|
public function anEmailWithShipmentDetailsOfOrderShouldBeSentTo(OrderInterface $order, $recipient) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->assertEmailContainsMessageTo($order->getNumber(), $recipient); |
|
104
|
|
|
$this->assertEmailContainsMessageTo($this->getShippingMethodName($order), $recipient); |
|
105
|
|
|
|
|
106
|
|
|
$tracking = $this->sharedStorage->get('tracking_code'); |
|
107
|
|
|
$this->assertEmailContainsMessageTo($tracking, $recipient); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $message |
|
112
|
|
|
* @param string $recipient |
|
113
|
|
|
*/ |
|
114
|
|
|
private function assertEmailContainsMessageTo($message, $recipient) |
|
115
|
|
|
{ |
|
116
|
|
|
Assert::true( |
|
117
|
|
|
$this->emailChecker->hasMessageTo($message, $recipient), |
|
118
|
|
|
sprintf('Message "%s" was not sent to "%s".', $message, $recipient) |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param OrderInterface $order |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
private function getShippingMethodName(OrderInterface $order) |
|
128
|
|
|
{ |
|
129
|
|
|
/** @var ShipmentInterface $shipment */ |
|
130
|
|
|
$shipment = $order->getShipments()->first(); |
|
131
|
|
|
if (false === $shipment) { |
|
132
|
|
|
throw new \LogicException('Order should have at least one shipment.'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $shipment->getMethod()->getName(); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|