SaveTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Test\Unit\Controller\Manage;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class SaveTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Newsletter\Controller\Manage
  15. */
  16. private $action;
  17. /**
  18. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $requestMock;
  21. /**
  22. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $responseMock;
  25. /**
  26. * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $messageManagerMock;
  29. /**
  30. * @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $redirectMock;
  33. /**
  34. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $customerSessionMock;
  37. /**
  38. * @var \Magento\Framework\Data\Form\FormKey\Validator|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $formKeyValidatorMock;
  41. /**
  42. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $customerRepositoryMock;
  45. protected function setUp()
  46. {
  47. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->redirectMock = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->customerSessionMock->expects($this->any())
  63. ->method('isLoggedIn')
  64. ->will($this->returnValue(true));
  65. $this->formKeyValidatorMock = $this->getMockBuilder(\Magento\Framework\Data\Form\FormKey\Validator::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->customerRepositoryMock =
  69. $this->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  73. $this->action = $objectManager->getObject(
  74. \Magento\Newsletter\Controller\Manage\Save::class,
  75. [
  76. 'request' => $this->requestMock,
  77. 'response' => $this->responseMock,
  78. 'messageManager' => $this->messageManagerMock,
  79. 'redirect' => $this->redirectMock,
  80. 'customerSession' => $this->customerSessionMock,
  81. 'formKeyValidator' => $this->formKeyValidatorMock,
  82. 'customerRepository' => $this->customerRepositoryMock
  83. ]
  84. );
  85. }
  86. public function testSaveActionInvalidFormKey()
  87. {
  88. $this->formKeyValidatorMock->expects($this->once())
  89. ->method('validate')
  90. ->will($this->returnValue(false));
  91. $this->redirectMock->expects($this->once())
  92. ->method('redirect')
  93. ->with($this->responseMock, 'customer/account/', []);
  94. $this->messageManagerMock->expects($this->never())
  95. ->method('addSuccess');
  96. $this->messageManagerMock->expects($this->never())
  97. ->method('addError');
  98. $this->action->execute();
  99. }
  100. public function testSaveActionNoCustomerInSession()
  101. {
  102. $this->formKeyValidatorMock->expects($this->once())
  103. ->method('validate')
  104. ->will($this->returnValue(true));
  105. $this->customerSessionMock->expects($this->any())
  106. ->method('getCustomerId')
  107. ->will($this->returnValue(null));
  108. $this->redirectMock->expects($this->once())
  109. ->method('redirect')
  110. ->with($this->responseMock, 'customer/account/', []);
  111. $this->messageManagerMock->expects($this->never())
  112. ->method('addSuccess');
  113. $this->messageManagerMock->expects($this->once())
  114. ->method('addError')
  115. ->with('Something went wrong while saving your subscription.');
  116. $this->action->execute();
  117. }
  118. public function testSaveActionWithException()
  119. {
  120. $this->formKeyValidatorMock->expects($this->once())
  121. ->method('validate')
  122. ->will($this->returnValue(true));
  123. $this->customerSessionMock->expects($this->any())
  124. ->method('getCustomerId')
  125. ->will($this->returnValue(1));
  126. $this->customerRepositoryMock->expects($this->any())
  127. ->method('getById')
  128. ->will(
  129. $this->throwException(
  130. new NoSuchEntityException(
  131. __(
  132. 'No such entity with %fieldName = %fieldValue',
  133. ['fieldName' => 'customerId', 'value' => 'value']
  134. )
  135. )
  136. )
  137. );
  138. $this->redirectMock->expects($this->once())
  139. ->method('redirect')
  140. ->with($this->responseMock, 'customer/account/', []);
  141. $this->messageManagerMock->expects($this->never())
  142. ->method('addSuccess');
  143. $this->messageManagerMock->expects($this->once())
  144. ->method('addError')
  145. ->with('Something went wrong while saving your subscription.');
  146. $this->action->execute();
  147. }
  148. }