Skip to content

Commit 9a027d5

Browse files
author
Oleksii Korshenko
authored
MAGETWO-85890: Update checkout controller json usage #12806
2 parents a107dbf + cf20e56 commit 9a027d5

File tree

2 files changed

+49
-36
lines changed

2 files changed

+49
-36
lines changed

app/code/Magento/Checkout/Controller/Account/Create.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Checkout\Controller\Account;
77

8+
use Magento\Framework\Controller\ResultFactory;
89
use Magento\Framework\Exception\AlreadyExistsException;
910
use Magento\Framework\Exception\NoSuchEntityException;
1011

@@ -55,7 +56,7 @@ public function __construct(
5556
public function execute()
5657
{
5758
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
58-
$resultJson = $this->_objectManager->get(\Magento\Framework\Controller\Result\JsonFactory::class)->create();
59+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
5960

6061
if ($this->customerSession->isLoggedIn()) {
6162
return $resultJson->setData(
@@ -83,7 +84,7 @@ public function execute()
8384
]
8485
);
8586
} catch (\Exception $e) {
86-
$this->messageManager->addException($e, $e->getMessage());
87+
$this->messageManager->addExceptionMessage($e, $e->getMessage());
8788
throw $e;
8889
}
8990
}

app/code/Magento/Checkout/Test/Unit/Controller/Account/CreateTest.php

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Checkout\Test\Unit\Controller\Account;
77

8+
use Magento\Framework\Controller\ResultFactory;
9+
810
/**
911
* Shopping cart edit tests
1012
*/
@@ -36,9 +38,14 @@ class CreateTest extends \PHPUnit\Framework\TestCase
3638
protected $orderCustomerService;
3739

3840
/**
39-
* @var \PHPUnit_Framework_MockObject_MockObject
41+
* @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $resultFactory;
44+
45+
/**
46+
* @var \Magento\Framework\Controller\ResultInterface|\PHPUnit_Framework_MockObject_MockObject
4047
*/
41-
protected $objectManagerMock;
48+
private $resultPage;
4249

4350
protected function setUp()
4451
{
@@ -48,9 +55,19 @@ protected function setUp()
4855
$this->orderCustomerService = $this->createMock(\Magento\Sales\Api\OrderCustomerManagementInterface::class);
4956
$this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
5057

51-
$this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
52-
$contextMock = $this->createPartialMock(\Magento\Framework\App\Action\Context::class, ['getObjectManager']);
53-
$contextMock->expects($this->once())->method('getObjectManager')->willReturn($this->objectManagerMock);
58+
$contextMock = $this->createPartialMock(
59+
\Magento\Framework\App\Action\Context::class,
60+
['getObjectManager', 'getResultFactory']
61+
);
62+
$this->resultFactory = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
63+
->disableOriginalConstructor()
64+
->getMock();
65+
$contextMock->expects($this->once())
66+
->method('getResultFactory')
67+
->willReturn($this->resultFactory);
68+
$this->resultPage = $this->getMockBuilder(\Magento\Framework\Controller\ResultInterface::class)
69+
->setMethods(['setData'])
70+
->getMockForAbstractClass();
5471

5572
$this->action = $objectManagerHelper->getObject(
5673
\Magento\Checkout\Controller\Account\Create::class,
@@ -66,53 +83,48 @@ protected function setUp()
6683

6784
public function testExecuteAddsSessionMessageIfCustomerIsLoggedIn()
6885
{
69-
$jsonFactoryMock = $this->createMock(\Magento\Framework\Controller\Result\JsonFactory::class);
70-
$this->objectManagerMock->expects($this->once())
71-
->method('get')
72-
->with(\Magento\Framework\Controller\Result\JsonFactory::class)
73-
->willReturn($jsonFactoryMock);
74-
$jsonMock = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
75-
$jsonFactoryMock->expects($this->once())->method('create')->willReturn($jsonMock);
76-
77-
$this->customerSession->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
78-
79-
$jsonMock->expects($this->once())
86+
$resultJson = '{"errors": "true", "message": "Customer is already registered"}';
87+
$this->customerSession->expects($this->once())
88+
->method('isLoggedIn')
89+
->will($this->returnValue(true));
90+
$this->resultFactory->expects($this->once())
91+
->method('create')
92+
->with(ResultFactory::TYPE_JSON)
93+
->willReturn($this->resultPage);
94+
$this->resultPage->expects($this->once())
8095
->method('setData')
8196
->with(
8297
[
8398
'errors' => true,
8499
'message' => __('Customer is already registered')
85100
]
86-
)->willReturnSelf();
87-
$this->action->execute();
101+
)->willReturn($resultJson);
102+
$this->assertEquals($resultJson, $this->action->execute());
88103
}
89104

90105
public function testExecute()
91106
{
92-
$jsonFactoryMock = $this->createMock(\Magento\Framework\Controller\Result\JsonFactory::class);
93-
$this->objectManagerMock->expects($this->once())
94-
->method('get')
95-
->with(\Magento\Framework\Controller\Result\JsonFactory::class)
96-
->willReturn($jsonFactoryMock);
97-
$jsonMock = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
98-
$jsonFactoryMock->expects($this->once())->method('create')->willReturn($jsonMock);
99-
100107
$this->customerSession->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
101108
$this->checkoutSession->expects($this->once())->method('getLastOrderId')->will($this->returnValue(100));
102109
$customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
103-
$this->orderCustomerService->expects($this->once())->method('create')->with(100)->will(
104-
$this->returnValue($customer)
105-
);
106-
107-
$jsonMock->expects($this->once())
110+
$this->orderCustomerService->expects($this->once())
111+
->method('create')
112+
->with(100)
113+
->will($this->returnValue($customer));
114+
115+
$resultJson = '{"errors":"false", "message":"A letter with further instructions will be sent to your email."}';
116+
$this->resultFactory->expects($this->once())
117+
->method('create')
118+
->with(ResultFactory::TYPE_JSON)
119+
->willReturn($this->resultPage);
120+
$this->resultPage->expects($this->once())
108121
->method('setData')
109122
->with(
110123
[
111124
'errors' => false,
112125
'message' => __('A letter with further instructions will be sent to your email.')
113126
]
114-
)->willReturnSelf();
115-
116-
$this->action->execute();
127+
)->willReturn($resultJson);
128+
$this->assertEquals($resultJson, $this->action->execute());
117129
}
118130
}

0 commit comments

Comments
 (0)