Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/code/Magento/Checkout/Controller/Account/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Checkout\Controller\Account;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\NoSuchEntityException;

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

if ($this->customerSession->isLoggedIn()) {
return $resultJson->setData(
Expand Down Expand Up @@ -83,7 +84,7 @@ public function execute()
]
);
} catch (\Exception $e) {
$this->messageManager->addException($e, $e->getMessage());
$this->messageManager->addExceptionMessage($e, $e->getMessage());
throw $e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Checkout\Test\Unit\Controller\Account;

use Magento\Framework\Controller\ResultFactory;

/**
* Shopping cart edit tests
*/
Expand Down Expand Up @@ -36,9 +38,14 @@ class CreateTest extends \PHPUnit\Framework\TestCase
protected $orderCustomerService;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $resultFactory;

/**
* @var \Magento\Framework\Controller\ResultInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $objectManagerMock;
private $resultPage;

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

$this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
$contextMock = $this->createPartialMock(\Magento\Framework\App\Action\Context::class, ['getObjectManager']);
$contextMock->expects($this->once())->method('getObjectManager')->willReturn($this->objectManagerMock);
$contextMock = $this->createPartialMock(
\Magento\Framework\App\Action\Context::class,
['getObjectManager', 'getResultFactory']
);
$this->resultFactory = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
->disableOriginalConstructor()
->getMock();
$contextMock->expects($this->once())
->method('getResultFactory')
->willReturn($this->resultFactory);
$this->resultPage = $this->getMockBuilder(\Magento\Framework\Controller\ResultInterface::class)
->setMethods(['setData'])
->getMockForAbstractClass();

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

public function testExecuteAddsSessionMessageIfCustomerIsLoggedIn()
{
$jsonFactoryMock = $this->createMock(\Magento\Framework\Controller\Result\JsonFactory::class);
$this->objectManagerMock->expects($this->once())
->method('get')
->with(\Magento\Framework\Controller\Result\JsonFactory::class)
->willReturn($jsonFactoryMock);
$jsonMock = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
$jsonFactoryMock->expects($this->once())->method('create')->willReturn($jsonMock);

$this->customerSession->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));

$jsonMock->expects($this->once())
$resultJson = '{"errors": "true", "message": "Customer is already registered"}';
$this->customerSession->expects($this->once())
->method('isLoggedIn')
->will($this->returnValue(true));
$this->resultFactory->expects($this->once())
->method('create')
->with(ResultFactory::TYPE_JSON)
->willReturn($this->resultPage);
$this->resultPage->expects($this->once())
->method('setData')
->with(
[
'errors' => true,
'message' => __('Customer is already registered')
]
)->willReturnSelf();
$this->action->execute();
)->willReturn($resultJson);
$this->assertEquals($resultJson, $this->action->execute());
}

public function testExecute()
{
$jsonFactoryMock = $this->createMock(\Magento\Framework\Controller\Result\JsonFactory::class);
$this->objectManagerMock->expects($this->once())
->method('get')
->with(\Magento\Framework\Controller\Result\JsonFactory::class)
->willReturn($jsonFactoryMock);
$jsonMock = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
$jsonFactoryMock->expects($this->once())->method('create')->willReturn($jsonMock);

$this->customerSession->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
$this->checkoutSession->expects($this->once())->method('getLastOrderId')->will($this->returnValue(100));
$customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
$this->orderCustomerService->expects($this->once())->method('create')->with(100)->will(
$this->returnValue($customer)
);

$jsonMock->expects($this->once())
$this->orderCustomerService->expects($this->once())
->method('create')
->with(100)
->will($this->returnValue($customer));

$resultJson = '{"errors":"false", "message":"A letter with further instructions will be sent to your email."}';
$this->resultFactory->expects($this->once())
->method('create')
->with(ResultFactory::TYPE_JSON)
->willReturn($this->resultPage);
$this->resultPage->expects($this->once())
->method('setData')
->with(
[
'errors' => false,
'message' => __('A letter with further instructions will be sent to your email.')
]
)->willReturnSelf();

$this->action->execute();
)->willReturn($resultJson);
$this->assertEquals($resultJson, $this->action->execute());
}
}