ValidateTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Controller\Adminhtml\Index;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class ValidateTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\RequestInterface
  14. */
  15. protected $request;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ResponseInterface
  18. */
  19. protected $response;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Api\Data\CustomerInterface
  22. */
  23. protected $customer;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Api\Data\CustomerInterfaceFactory
  26. */
  27. protected $customerDataFactory;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\Metadata\FormFactory
  30. */
  31. protected $formFactory;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\Metadata\Form
  34. */
  35. protected $form;
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\ExtensibleDataObjectConverter
  38. */
  39. protected $extensibleDataObjectConverter;
  40. /**
  41. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\DataObjectHelper
  42. */
  43. protected $dataObjectHelper;
  44. /**
  45. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Api\AccountManagementInterface
  46. */
  47. protected $customerAccountManagement;
  48. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Controller\Result\JsonFactory */
  49. protected $resultJsonFactory;
  50. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Controller\Result\Json */
  51. protected $resultJson;
  52. /** @var \Magento\Customer\Controller\Adminhtml\Index\Validate */
  53. protected $controller;
  54. protected function setUp()
  55. {
  56. if (!function_exists('libxml_set_external_entity_loader')) {
  57. $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
  58. }
  59. $this->customer = $this->getMockForAbstractClass(
  60. \Magento\Customer\Api\Data\CustomerInterface::class,
  61. [],
  62. '',
  63. false,
  64. true,
  65. true
  66. );
  67. $this->customer->expects($this->once())->method('getWebsiteId')->willReturn(2);
  68. $this->customerDataFactory = $this->createPartialMock(
  69. \Magento\Customer\Api\Data\CustomerInterfaceFactory::class,
  70. ['create']
  71. );
  72. $this->customerDataFactory->expects($this->once())->method('create')->willReturn($this->customer);
  73. $this->form = $this->createMock(\Magento\Customer\Model\Metadata\Form::class);
  74. $this->request = $this->getMockForAbstractClass(
  75. \Magento\Framework\App\RequestInterface::class,
  76. [],
  77. '',
  78. false,
  79. true,
  80. true,
  81. ['getPost', 'getParam']
  82. );
  83. $this->response = $this->getMockForAbstractClass(
  84. \Magento\Framework\App\ResponseInterface::class,
  85. [],
  86. '',
  87. false
  88. );
  89. $this->formFactory = $this->createPartialMock(\Magento\Customer\Model\Metadata\FormFactory::class, ['create']);
  90. $this->formFactory->expects($this->atLeastOnce())->method('create')->willReturn($this->form);
  91. $this->extensibleDataObjectConverter = $this->createMock(
  92. \Magento\Framework\Api\ExtensibleDataObjectConverter::class
  93. );
  94. $this->dataObjectHelper = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
  95. $this->dataObjectHelper->expects($this->once())->method('populateWithArray');
  96. $this->customerAccountManagement = $this->getMockForAbstractClass(
  97. \Magento\Customer\Api\AccountManagementInterface::class,
  98. [],
  99. '',
  100. false,
  101. true,
  102. true
  103. );
  104. $this->resultJson = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
  105. $this->resultJson->expects($this->once())->method('setData');
  106. $this->resultJsonFactory = $this->createPartialMock(
  107. \Magento\Framework\Controller\Result\JsonFactory::class,
  108. ['create']
  109. );
  110. $this->resultJsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson);
  111. $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  112. $this->controller = $objectHelper->getObject(
  113. \Magento\Customer\Controller\Adminhtml\Index\Validate::class,
  114. [
  115. 'request' => $this->request,
  116. 'response' => $this->response,
  117. 'customerDataFactory' => $this->customerDataFactory,
  118. 'formFactory' => $this->formFactory,
  119. 'extensibleDataObjectConverter' => $this->extensibleDataObjectConverter,
  120. 'customerAccountManagement' => $this->customerAccountManagement,
  121. 'resultJsonFactory' => $this->resultJsonFactory,
  122. 'dataObjectHelper' => $this->dataObjectHelper,
  123. ]
  124. );
  125. }
  126. public function testExecute()
  127. {
  128. $customerEntityId = 2;
  129. $this->request->expects($this->once())
  130. ->method('getParam')
  131. ->with('customer')
  132. ->willReturn([
  133. 'entity_id' => $customerEntityId
  134. ]);
  135. $this->customer->expects($this->once())
  136. ->method('setId')
  137. ->with($customerEntityId);
  138. $this->form->expects($this->once())->method('setInvisibleIgnored');
  139. $this->form->expects($this->atLeastOnce())->method('extractData')->willReturn([]);
  140. $validationResult = $this->getMockForAbstractClass(
  141. \Magento\Customer\Api\Data\ValidationResultsInterface::class,
  142. [],
  143. '',
  144. false,
  145. true,
  146. true
  147. );
  148. $validationResult->expects($this->once())
  149. ->method('getMessages')
  150. ->willReturn(['Error message']);
  151. $this->customerAccountManagement->expects($this->once())
  152. ->method('validate')
  153. ->willReturn($validationResult);
  154. $this->controller->execute();
  155. }
  156. public function testExecuteWithoutAddresses()
  157. {
  158. $this->form->expects($this->once())
  159. ->method('setInvisibleIgnored');
  160. $this->form->expects($this->atLeastOnce())
  161. ->method('extractData')
  162. ->willReturn([]);
  163. $error = $this->createMock(\Magento\Framework\Message\Error::class);
  164. $this->form->expects($this->never())
  165. ->method('validateData')
  166. ->willReturn([$error]);
  167. $validationResult = $this->getMockForAbstractClass(
  168. \Magento\Customer\Api\Data\ValidationResultsInterface::class,
  169. [],
  170. '',
  171. false,
  172. true,
  173. true
  174. );
  175. $validationResult->expects($this->once())
  176. ->method('getMessages')
  177. ->willReturn(['Error message']);
  178. $this->customerAccountManagement->expects($this->once())
  179. ->method('validate')
  180. ->willReturn($validationResult);
  181. $this->controller->execute();
  182. }
  183. public function testExecuteWithException()
  184. {
  185. $this->form->expects($this->once())
  186. ->method('setInvisibleIgnored');
  187. $this->form->expects($this->atLeastOnce())
  188. ->method('extractData')
  189. ->willReturn([]);
  190. $this->form->expects($this->never())
  191. ->method('validateData');
  192. $validationResult = $this->getMockForAbstractClass(
  193. \Magento\Customer\Api\Data\ValidationResultsInterface::class,
  194. [],
  195. '',
  196. false,
  197. true,
  198. true
  199. );
  200. $error = $this->createMock(\Magento\Framework\Message\Error::class);
  201. $error->expects($this->once())
  202. ->method('getText')
  203. ->willReturn('Error text');
  204. $exception = $this->createMock(\Magento\Framework\Validator\Exception::class);
  205. $exception->expects($this->once())
  206. ->method('getMessages')
  207. ->willReturn([$error]);
  208. $validationResult->expects($this->once())
  209. ->method('getMessages')
  210. ->willThrowException($exception);
  211. $this->customerAccountManagement->expects($this->once())
  212. ->method('validate')
  213. ->willReturn($validationResult);
  214. $this->controller->execute();
  215. }
  216. public function testExecuteWithNewCustomerAndNoEntityId()
  217. {
  218. $this->request->expects($this->once())
  219. ->method('getParam')
  220. ->with('customer')
  221. ->willReturn([]);
  222. $this->customer->expects($this->never())
  223. ->method('setId');
  224. $this->form->expects($this->once())->method('setInvisibleIgnored');
  225. $this->form->expects($this->atLeastOnce())->method('extractData')->willReturn([]);
  226. $validationResult = $this->getMockForAbstractClass(
  227. \Magento\Customer\Api\Data\ValidationResultsInterface::class,
  228. [],
  229. '',
  230. false,
  231. true,
  232. true
  233. );
  234. $validationResult->expects($this->once())
  235. ->method('getMessages')
  236. ->willReturn(['Error message']);
  237. $this->customerAccountManagement->expects($this->once())
  238. ->method('validate')
  239. ->willReturn($validationResult);
  240. $this->controller->execute();
  241. }
  242. }