Skip to content

Commit aee8663

Browse files
author
Spandana Chittimala
committed
Merge branch 'MAGETWO-99383' of https://github.com/magento-mpi/magento2ce into PR-2019-05-13
2 parents 774a17e + 433f9e7 commit aee8663

File tree

7 files changed

+195
-31
lines changed

7 files changed

+195
-31
lines changed

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
3737
protected $eventManager;
3838

3939
/**
40-
* @var QuoteValidator
40+
* @var SubmitQuoteValidator
4141
*/
42-
protected $quoteValidator;
42+
private $submitQuoteValidator;
4343

4444
/**
4545
* @var OrderFactory
@@ -158,7 +158,7 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
158158

159159
/**
160160
* @param EventManager $eventManager
161-
* @param QuoteValidator $quoteValidator
161+
* @param SubmitQuoteValidator $submitQuoteValidator
162162
* @param OrderFactory $orderFactory
163163
* @param OrderManagement $orderManagement
164164
* @param CustomerManagement $customerManagement
@@ -185,7 +185,7 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
185185
*/
186186
public function __construct(
187187
EventManager $eventManager,
188-
QuoteValidator $quoteValidator,
188+
SubmitQuoteValidator $submitQuoteValidator,
189189
OrderFactory $orderFactory,
190190
OrderManagement $orderManagement,
191191
CustomerManagement $customerManagement,
@@ -210,7 +210,7 @@ public function __construct(
210210
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress = null
211211
) {
212212
$this->eventManager = $eventManager;
213-
$this->quoteValidator = $quoteValidator;
213+
$this->submitQuoteValidator = $submitQuoteValidator;
214214
$this->orderFactory = $orderFactory;
215215
$this->orderManagement = $orderManagement;
216216
$this->customerManagement = $customerManagement;
@@ -299,7 +299,7 @@ public function assignCustomer($cartId, $customerId, $storeId)
299299
throw new StateException(
300300
__("The customer can't be assigned to the cart because the customer already has an active cart.")
301301
);
302-
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
302+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
303303
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
304304
}
305305

@@ -484,7 +484,7 @@ protected function resolveItems(QuoteEntity $quote)
484484
protected function submitQuote(QuoteEntity $quote, $orderData = [])
485485
{
486486
$order = $this->orderFactory->create();
487-
$this->quoteValidator->validateBeforeSubmit($quote);
487+
$this->submitQuoteValidator->validateQuote($quote);
488488
if (!$quote->getCustomerIsGuest()) {
489489
if ($quote->getCustomerId()) {
490490
$this->_prepareCustomerQuote($quote);
@@ -539,6 +539,7 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
539539
$order->setCustomerFirstname($quote->getCustomerFirstname());
540540
$order->setCustomerMiddlename($quote->getCustomerMiddlename());
541541
$order->setCustomerLastname($quote->getCustomerLastname());
542+
$this->submitQuoteValidator->validateOrder($order);
542543

543544
$this->eventManager->dispatch(
544545
'sales_model_service_quote_submit_before',
@@ -654,13 +655,12 @@ private function rollbackAddresses(
654655
'exception' => $e,
655656
]
656657
);
657-
// phpcs:ignore Magento2.Exceptions.ThrowCatch
658+
// phpcs:ignore Magento2.Exceptions.ThrowCatch
658659
} catch (\Exception $consecutiveException) {
659660
$message = sprintf(
660661
"An exception occurred on 'sales_model_service_quote_submit_failure' event: %s",
661662
$consecutiveException->getMessage()
662663
);
663-
664664
// phpcs:ignore Magento2.Exceptions.DirectThrow
665665
throw new \Exception($message, 0, $e);
666666
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Sales\Model\Order;
12+
use Magento\Sales\Model\Order\Address\Validator as OrderAddressValidator;
13+
14+
/**
15+
* Validates quote and order before quote submit.
16+
*/
17+
class SubmitQuoteValidator
18+
{
19+
/**
20+
* @var QuoteValidator
21+
*/
22+
private $quoteValidator;
23+
24+
/**
25+
* @var OrderAddressValidator
26+
*/
27+
private $orderAddressValidator;
28+
29+
/**
30+
* @param QuoteValidator $quoteValidator
31+
* @param OrderAddressValidator $orderAddressValidator
32+
*/
33+
public function __construct(
34+
QuoteValidator $quoteValidator,
35+
OrderAddressValidator $orderAddressValidator
36+
) {
37+
$this->quoteValidator = $quoteValidator;
38+
$this->orderAddressValidator = $orderAddressValidator;
39+
}
40+
41+
/**
42+
* Validates quote.
43+
*
44+
* @param Quote $quote
45+
* @return void
46+
* @throws LocalizedException
47+
*/
48+
public function validateQuote(Quote $quote): void
49+
{
50+
$this->quoteValidator->validateBeforeSubmit($quote);
51+
}
52+
53+
/**
54+
* Validates order.
55+
*
56+
* @param Order $order
57+
* @return void
58+
* @throws LocalizedException
59+
*/
60+
public function validateOrder(Order $order): void
61+
{
62+
foreach ($order->getAddresses() as $address) {
63+
$errors = $this->orderAddressValidator->validate($address);
64+
if (!empty($errors)) {
65+
throw new LocalizedException(
66+
__("Failed address validation:\n%1", implode("\n", $errors))
67+
);
68+
}
69+
}
70+
}
71+
}

app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
/**
1818
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1919
* @SuppressWarnings(PHPMD.TooManyFields)
20+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
21+
* @SuppressWarnings(PHPMD.ExcessiveClassLength)
2022
*/
2123
class QuoteManagementTest extends \PHPUnit\Framework\TestCase
2224
{
@@ -26,9 +28,9 @@ class QuoteManagementTest extends \PHPUnit\Framework\TestCase
2628
protected $model;
2729

2830
/**
29-
* @var \Magento\Quote\Model\QuoteValidator|\PHPUnit_Framework_MockObject_MockObject
31+
* @var \Magento\Quote\Model\SubmitQuoteValidator|\PHPUnit_Framework_MockObject_MockObject
3032
*/
31-
protected $quoteValidator;
33+
protected $submitQuoteValidator;
3234

3335
/**
3436
* @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
@@ -162,7 +164,7 @@ protected function setUp()
162164
{
163165
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
164166

165-
$this->quoteValidator = $this->createMock(\Magento\Quote\Model\QuoteValidator::class);
167+
$this->submitQuoteValidator = $this->createMock(\Magento\Quote\Model\SubmitQuoteValidator::class);
166168
$this->eventManager = $this->getMockForAbstractClass(\Magento\Framework\Event\ManagerInterface::class);
167169
$this->orderFactory = $this->createPartialMock(
168170
\Magento\Sales\Api\Data\OrderInterfaceFactory::class,
@@ -232,7 +234,7 @@ protected function setUp()
232234
\Magento\Quote\Model\QuoteManagement::class,
233235
[
234236
'eventManager' => $this->eventManager,
235-
'quoteValidator' => $this->quoteValidator,
237+
'submitQuoteValidator' => $this->submitQuoteValidator,
236238
'orderFactory' => $this->orderFactory,
237239
'orderManagement' => $this->orderManagement,
238240
'customerManagement' => $this->customerManagement,
@@ -587,7 +589,9 @@ public function testSubmit()
587589
$shippingAddress
588590
);
589591

590-
$this->quoteValidator->expects($this->once())->method('validateBeforeSubmit')->with($quote);
592+
$this->submitQuoteValidator->expects($this->once())
593+
->method('validateQuote')
594+
->with($quote);
591595
$this->quoteAddressToOrder->expects($this->once())
592596
->method('convert')
593597
->with($shippingAddress, $orderData)
@@ -681,7 +685,7 @@ public function testPlaceOrderIfCustomerIsGuest()
681685
->setConstructorArgs(
682686
[
683687
'eventManager' => $this->eventManager,
684-
'quoteValidator' => $this->quoteValidator,
688+
'quoteValidator' => $this->submitQuoteValidator,
685689
'orderFactory' => $this->orderFactory,
686690
'orderManagement' => $this->orderManagement,
687691
'customerManagement' => $this->customerManagement,
@@ -744,8 +748,8 @@ public function testPlaceOrder()
744748
->setMethods(['submit'])
745749
->setConstructorArgs(
746750
[
747-
'eventManager' => $this->eventManager,
748-
'quoteValidator' => $this->quoteValidator,
751+
'eventManager' => $this->eventManager,
752+
'quoteValidator' => $this->submitQuoteValidator,
749753
'orderFactory' => $this->orderFactory,
750754
'orderManagement' => $this->orderManagement,
751755
'customerManagement' => $this->customerManagement,
@@ -982,6 +986,9 @@ protected function prepareOrderFactory(
982986
return $order;
983987
}
984988

989+
/**
990+
* @throws NoSuchEntityException
991+
*/
985992
public function testGetCartForCustomer()
986993
{
987994
$customerId = 100;
@@ -1026,6 +1033,9 @@ protected function setPropertyValue(&$object, $property, $value)
10261033
return $object;
10271034
}
10281035

1036+
/**
1037+
* @throws \Magento\Framework\Exception\LocalizedException
1038+
*/
10291039
public function testSubmitForCustomer()
10301040
{
10311041
$orderData = [];
@@ -1058,7 +1068,8 @@ public function testSubmitForCustomer()
10581068
$shippingAddress
10591069
);
10601070

1061-
$this->quoteValidator->expects($this->once())->method('validateBeforeSubmit')->with($quote);
1071+
$this->submitQuoteValidator->method('validateQuote')
1072+
->with($quote);
10621073
$this->quoteAddressToOrder->expects($this->once())
10631074
->method('convert')
10641075
->with($shippingAddress, $orderData)

app/code/Magento/Sales/Model/Service/OrderService.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Sales\Api\OrderManagementInterface;
99
use Magento\Payment\Gateway\Command\CommandException;
10+
use Psr\Log\LoggerInterface;
1011

1112
/**
1213
* Class OrderService
@@ -55,6 +56,11 @@ class OrderService implements OrderManagementInterface
5556
*/
5657
private $paymentFailures;
5758

59+
/**
60+
* @var LoggerInterface
61+
*/
62+
private $logger;
63+
5864
/**
5965
* Constructor
6066
*
@@ -65,7 +71,8 @@ class OrderService implements OrderManagementInterface
6571
* @param \Magento\Sales\Model\OrderNotifier $notifier
6672
* @param \Magento\Framework\Event\ManagerInterface $eventManager
6773
* @param \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender $orderCommentSender
68-
* @param \Magento\Sales\Api\PaymentFailuresInterface|null $paymentFailures
74+
* @param \Magento\Sales\Api\PaymentFailuresInterface $paymentFailures
75+
* @param LoggerInterface $logger
6976
*/
7077
public function __construct(
7178
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
@@ -75,7 +82,8 @@ public function __construct(
7582
\Magento\Sales\Model\OrderNotifier $notifier,
7683
\Magento\Framework\Event\ManagerInterface $eventManager,
7784
\Magento\Sales\Model\Order\Email\Sender\OrderCommentSender $orderCommentSender,
78-
\Magento\Sales\Api\PaymentFailuresInterface $paymentFailures = null
85+
\Magento\Sales\Api\PaymentFailuresInterface $paymentFailures,
86+
LoggerInterface $logger
7987
) {
8088
$this->orderRepository = $orderRepository;
8189
$this->historyRepository = $historyRepository;
@@ -84,8 +92,8 @@ public function __construct(
8492
$this->notifier = $notifier;
8593
$this->eventManager = $eventManager;
8694
$this->orderCommentSender = $orderCommentSender;
87-
$this->paymentFailures = $paymentFailures ? : \Magento\Framework\App\ObjectManager::getInstance()
88-
->get(\Magento\Sales\Api\PaymentFailuresInterface::class);
95+
$this->paymentFailures = $paymentFailures;
96+
$this->logger = $logger;
8997
}
9098

9199
/**
@@ -189,25 +197,31 @@ public function unHold($id)
189197
}
190198

191199
/**
200+
* Perform place order.
201+
*
192202
* @param \Magento\Sales\Api\Data\OrderInterface $order
193203
* @return \Magento\Sales\Api\Data\OrderInterface
194204
* @throws \Exception
195205
*/
196206
public function place(\Magento\Sales\Api\Data\OrderInterface $order)
197207
{
198-
// transaction will be here
199-
//begin transaction
200208
try {
201209
$order->place();
202-
return $this->orderRepository->save($order);
203-
//commit
210+
} catch (CommandException $e) {
211+
$this->paymentFailures->handle((int)$order->getQuoteId(), __($e->getMessage()));
212+
throw $e;
213+
}
214+
215+
try {
216+
$order = $this->orderRepository->save($order);
204217
} catch (\Exception $e) {
205-
if ($e instanceof CommandException) {
206-
$this->paymentFailures->handle((int)$order->getQuoteId(), __($e->getMessage()));
207-
}
218+
$this->logger->critical(
219+
'Saving order ' . $order->getIncrementId() . ' failed: ' . $e->getMessage()
220+
);
208221
throw $e;
209-
//rollback;
210222
}
223+
224+
return $order;
211225
}
212226

213227
/**

app/code/Magento/Sales/Test/Unit/Model/Service/OrderServiceTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Sales\Test\Unit\Model\Service;
77

8+
use Magento\Sales\Api\PaymentFailuresInterface;
9+
use Psr\Log\LoggerInterface;
10+
811
/**
912
* Class OrderUnHoldTest
1013
*
@@ -140,14 +143,22 @@ protected function setUp()
140143
->disableOriginalConstructor()
141144
->getMock();
142145

146+
/** @var PaymentFailuresInterface|\PHPUnit_Framework_MockObject_MockObject $paymentFailures */
147+
$paymentFailures = $this->createMock(PaymentFailuresInterface::class);
148+
149+
/** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject $logger */
150+
$logger = $this->createMock(LoggerInterface::class);
151+
143152
$this->orderService = new \Magento\Sales\Model\Service\OrderService(
144153
$this->orderRepositoryMock,
145154
$this->orderStatusHistoryRepositoryMock,
146155
$this->searchCriteriaBuilderMock,
147156
$this->filterBuilderMock,
148157
$this->orderNotifierMock,
149158
$this->eventManagerMock,
150-
$this->orderCommentSender
159+
$this->orderCommentSender,
160+
$paymentFailures,
161+
$logger
151162
);
152163
}
153164

0 commit comments

Comments
 (0)