CreateTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Test\Unit\Controller\Account;
  7. use Magento\Framework\Controller\ResultFactory;
  8. /**
  9. * Shopping cart edit tests
  10. */
  11. class CreateTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $customerSession;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $checkoutSession;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $messageManager;
  25. /**
  26. * @var \Magento\Checkout\Controller\Account\Create
  27. */
  28. protected $action;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $orderCustomerService;
  33. /**
  34. * @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $resultFactory;
  37. /**
  38. * @var \Magento\Framework\Controller\ResultInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $resultPage;
  41. protected function setUp()
  42. {
  43. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  44. $this->checkoutSession = $this->createPartialMock(\Magento\Checkout\Model\Session::class, ['getLastOrderId']);
  45. $this->customerSession = $this->createMock(\Magento\Customer\Model\Session::class);
  46. $this->orderCustomerService = $this->createMock(\Magento\Sales\Api\OrderCustomerManagementInterface::class);
  47. $this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  48. $contextMock = $this->createPartialMock(
  49. \Magento\Framework\App\Action\Context::class,
  50. ['getObjectManager', 'getResultFactory']
  51. );
  52. $this->resultFactory = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $contextMock->expects($this->once())
  56. ->method('getResultFactory')
  57. ->willReturn($this->resultFactory);
  58. $this->resultPage = $this->getMockBuilder(\Magento\Framework\Controller\ResultInterface::class)
  59. ->setMethods(['setData'])
  60. ->getMockForAbstractClass();
  61. $this->action = $objectManagerHelper->getObject(
  62. \Magento\Checkout\Controller\Account\Create::class,
  63. [
  64. 'checkoutSession' => $this->checkoutSession,
  65. 'customerSession' => $this->customerSession,
  66. 'orderCustomerService' => $this->orderCustomerService,
  67. 'messageManager' => $this->messageManager,
  68. 'context' => $contextMock
  69. ]
  70. );
  71. }
  72. public function testExecuteAddsSessionMessageIfCustomerIsLoggedIn()
  73. {
  74. $resultJson = '{"errors": "true", "message": "Customer is already registered"}';
  75. $this->customerSession->expects($this->once())
  76. ->method('isLoggedIn')
  77. ->will($this->returnValue(true));
  78. $this->resultFactory->expects($this->once())
  79. ->method('create')
  80. ->with(ResultFactory::TYPE_JSON)
  81. ->willReturn($this->resultPage);
  82. $this->resultPage->expects($this->once())
  83. ->method('setData')
  84. ->with(
  85. [
  86. 'errors' => true,
  87. 'message' => __('Customer is already registered')
  88. ]
  89. )->willReturn($resultJson);
  90. $this->assertEquals($resultJson, $this->action->execute());
  91. }
  92. public function testExecute()
  93. {
  94. $this->customerSession->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  95. $this->checkoutSession->expects($this->once())->method('getLastOrderId')->will($this->returnValue(100));
  96. $customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  97. $this->orderCustomerService->expects($this->once())
  98. ->method('create')
  99. ->with(100)
  100. ->will($this->returnValue($customer));
  101. $resultJson = '{"errors":"false", "message":"A letter with further instructions will be sent to your email."}';
  102. $this->resultFactory->expects($this->once())
  103. ->method('create')
  104. ->with(ResultFactory::TYPE_JSON)
  105. ->willReturn($this->resultPage);
  106. $this->resultPage->expects($this->once())
  107. ->method('setData')
  108. ->with(
  109. [
  110. 'errors' => false,
  111. 'message' => __('A letter with further instructions will be sent to your email.')
  112. ]
  113. )->willReturn($resultJson);
  114. $this->assertEquals($resultJson, $this->action->execute());
  115. }
  116. }