Skip to content

Commit 1e16bc8

Browse files
author
Serhiy Shkolyarenko
committed
MAGETWO-53457: [COMMUNITY] [FEEDBACK] Improve error reporting for failed order placement (checkout) #4682
updated unit tests
1 parent 07a797f commit 1e16bc8

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

app/code/Magento/Authorizenet/Test/Unit/Controller/Directpost/Payment/PlaceTest.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Magento\Payment\Model\IframeConfigProvider;
2121
use Magento\Quote\Api\CartManagementInterface;
2222
use Magento\Quote\Model\Quote;
23+
use Magento\Framework\Exception\LocalizedException;
2324

2425
/**
2526
* Class PlaceTest
@@ -94,6 +95,11 @@ class PlaceTest extends \PHPUnit_Framework_TestCase
9495
*/
9596
protected $quoteMock;
9697

98+
/**
99+
* @var \PHPUnit_Framework_MockObject_MockObject
100+
*/
101+
private $loggerMock;
102+
97103
/**
98104
* @var CheckoutSession|\PHPUnit_Framework_MockObject_MockObject
99105
*/
@@ -152,6 +158,9 @@ protected function setUp()
152158
->getMockBuilder(\Magento\Framework\App\Response\Http::class)
153159
->disableOriginalConstructor()
154160
->getMockForAbstractClass();
161+
$this->loggerMock = $this
162+
->getMockBuilder(\Psr\Log\LoggerInterface::class)
163+
->getMock();
155164

156165
$this->objectManager = new ObjectManager($this);
157166
$this->placeOrderController = $this->objectManager->getObject(
@@ -165,6 +174,7 @@ protected function setUp()
165174
'cartManagement' => $this->cartManagementMock,
166175
'onepageCheckout' => $this->onepageCheckout,
167176
'jsonHelper' => $this->jsonHelperMock,
177+
'logger' => $this->loggerMock,
168178
]
169179
);
170180
}
@@ -214,13 +224,15 @@ public function testExecute(
214224
* @param $controller
215225
* @param $quoteId
216226
* @param $result
227+
* @param \Exception $exception Exception to check
217228
* @dataProvider textExecuteFailedPlaceOrderDataProvider
218229
*/
219230
public function testExecuteFailedPlaceOrder(
220231
$paymentMethod,
221232
$controller,
222233
$quoteId,
223-
$result
234+
$result,
235+
$exception
224236
) {
225237
$this->requestMock->expects($this->at(0))
226238
->method('getParam')
@@ -238,7 +250,11 @@ public function testExecuteFailedPlaceOrder(
238250

239251
$this->cartManagementMock->expects($this->once())
240252
->method('placeOrder')
241-
->willThrowException(new \Exception());
253+
->willThrowException($exception);
254+
255+
$this->loggerMock->expects($this->once())
256+
->method('critical')
257+
->with($exception);
242258

243259
$this->jsonHelperMock->expects($this->any())
244260
->method('jsonEncode')
@@ -278,19 +294,35 @@ public function textExecuteDataProvider()
278294
*/
279295
public function textExecuteFailedPlaceOrderDataProvider()
280296
{
281-
$objectFailed = new \Magento\Framework\DataObject();
282-
$objectFailed->setData('error', true);
283-
$objectFailed->setData(
284-
'error_messages',
285-
__('An error occurred on the server. Please try to place the order again.')
297+
$objectFailed1 = new \Magento\Framework\DataObject(
298+
[
299+
'error' => true,
300+
'error_messages' => __('An error occurred on the server. Please try to place the order again.')
301+
]
302+
);
303+
$generalException = new \Exception('Exception logging will save the world!');
304+
$localizedException = new LocalizedException(__('Electronic payments save the trees.'));
305+
$objectFailed2 = new \Magento\Framework\DataObject(
306+
[
307+
'error' => true,
308+
'error_messages' => $localizedException->getMessage()
309+
]
286310
);
287311

288312
return [
289313
[
290314
['method' => 'authorizenet_directpost'],
291315
IframeConfigProvider::CHECKOUT_IDENTIFIER,
292316
1,
293-
$objectFailed
317+
$objectFailed1,
318+
$generalException,
319+
],
320+
[
321+
['method' => 'authorizenet_directpost'],
322+
IframeConfigProvider::CHECKOUT_IDENTIFIER,
323+
1,
324+
$objectFailed2,
325+
$localizedException,
294326
],
295327
];
296328
}

app/code/Magento/Payment/Test/Unit/Model/Method/AdapterTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class AdapterTest extends \PHPUnit_Framework_TestCase
5050
*/
5151
private $paymentDataObjectFactory;
5252

53+
/**
54+
* @var MockObject
55+
*/
56+
private $logger;
57+
5358
/**
5459
* @var string
5560
*/
@@ -84,6 +89,8 @@ protected function setUp()
8489
$this->formBlockType = '\FormBlock';
8590
$this->infoBlockType = '\InfoBlock';
8691

92+
$this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
93+
->getMock();
8794
$this->adapter = new Adapter(
8895
$this->eventManager,
8996
$this->valueHandlerPool,
@@ -92,7 +99,9 @@ protected function setUp()
9299
$this->formBlockType,
93100
$this->infoBlockType,
94101
$this->commandPool,
95-
$this->validatorPool
102+
$this->validatorPool,
103+
null,
104+
$this->logger
96105
);
97106
}
98107

@@ -221,7 +230,8 @@ public function testExecuteCommandWithCommandExecutor()
221230
'\InfoBlock',
222231
null,
223232
null,
224-
$commandManager
233+
$commandManager,
234+
$this->logger
225235
);
226236

227237
$valueHandler = $this->getMock(ValueHandlerInterface::class);
@@ -274,7 +284,10 @@ public function testExecuteCommandWithCommandPool()
274284
'CODE',
275285
'\FormBlock',
276286
'\InfoBlock',
277-
$commandPool
287+
$commandPool,
288+
null,
289+
null,
290+
$this->logger
278291
);
279292

280293
$valueHandler = $this->getMock(ValueHandlerInterface::class);
@@ -305,4 +318,18 @@ public function testExecuteCommandWithCommandPool()
305318

306319
$adapter->authorize($paymentInfo, 10);
307320
}
321+
322+
public function testValidationExceptionLogged()
323+
{
324+
$exception = new \Exception('We can test exception logging!');
325+
326+
$this->validatorPool->expects(static::once())
327+
->method('get')
328+
->with('global')
329+
->willThrowException($exception);
330+
$this->logger->expects(static::once())
331+
->method('critical')
332+
->with($exception);
333+
$this->adapter->validate();
334+
}
308335
}

0 commit comments

Comments
 (0)