1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Model\Order;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\ObjectManagerInterface;
use Magento\Quote\Api\GuestCartManagementInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\QuoteIdMask;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Sales\Model\OrderRepository;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
use PHPUnit\Framework\Constraint\StringContains;
/**
* Class verifies order creation.
*
* @magentoDbIsolation enabled
* @magentoAppArea frontend
*/
class CreateTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var TransportBuilderMock
*/
private $transportBuilder;
/**
* @var QuoteIdMaskFactory
*/
private $quoteIdMaskFactory;
/**
* @var FormKey
*/
private $formKey;
/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->transportBuilder = $this->objectManager->get(TransportBuilderMock::class);
$this->quoteIdMaskFactory = $this->objectManager->get(QuoteIdMaskFactory::class);
$this->formKey = $this->objectManager->get(FormKey::class);
}
/**
* @magentoDataFixture Magento/Sales/_files/guest_quote_with_addresses.php
* @return void
*/
public function testSendEmailOnOrderPlace(): void
{
/** @var Quote $quote */
$quote = $this->objectManager->create(Quote::class);
$quote->load('guest_quote', 'reserved_order_id');
$checkoutSession = $this->objectManager->get(CheckoutSession::class);
$checkoutSession->setQuoteId($quote->getId());
/** @var QuoteIdMask $quoteIdMask */
$quoteIdMask = $this->quoteIdMaskFactory->create();
$quoteIdMask->load($quote->getId(), 'quote_id');
$cartId = $quoteIdMask->getMaskedId();
/** @var GuestCartManagementInterface $cartManagement */
$cartManagement = $this->objectManager->get(GuestCartManagementInterface::class);
$orderId = $cartManagement->placeOrder($cartId);
$order = $this->objectManager->get(OrderRepository::class)->get($orderId);
$message = $this->transportBuilder->getSentMessage();
$subject = __('Your %1 order confirmation', $order->getStore()->getFrontendName())->render();
$assert = $this->logicalAnd(
new StringContains($order->getBillingAddress()->getName()),
new StringContains(
'Thank you for your order from ' . $order->getStore()->getFrontendName()
),
new StringContains(
"Your Order <span class=\"no-link\">#{$order->getIncrementId()}</span>"
)
);
$this->assertEquals($message->getSubject(), $subject);
$this->assertThat($message->getRawMessage(), $assert);
}
}