CustomerExtractorTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model;
  7. use Magento\Customer\Model\CustomerExtractor;
  8. class CustomerExtractorTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var CustomerExtractor */
  11. protected $customerExtractor;
  12. /** @var \Magento\Customer\Model\Metadata\FormFactory|\PHPUnit_Framework_MockObject_MockObject */
  13. protected $formFactory;
  14. /** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject */
  15. protected $customerFactory;
  16. /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $storeManager;
  18. /** @var \Magento\Customer\Api\GroupManagementInterface|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $customerGroupManagement;
  20. /** @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $dataObjectHelper;
  22. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $request;
  24. /** @var \Magento\Customer\Model\Metadata\Form|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $customerForm;
  26. /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $customerData;
  28. /** @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $store;
  30. /** @var \Magento\Customer\Api\Data\GroupInterface|\PHPUnit_Framework_MockObject_MockObject */
  31. protected $customerGroup;
  32. protected function setUp()
  33. {
  34. $this->formFactory = $this->getMockForAbstractClass(
  35. \Magento\Customer\Model\Metadata\FormFactory::class,
  36. [],
  37. '',
  38. false,
  39. false,
  40. true,
  41. ['create']
  42. );
  43. $this->customerFactory = $this->getMockForAbstractClass(
  44. \Magento\Customer\Api\Data\CustomerInterfaceFactory::class,
  45. [],
  46. '',
  47. false,
  48. false,
  49. true,
  50. ['create']
  51. );
  52. $this->storeManager = $this->getMockForAbstractClass(
  53. \Magento\Store\Model\StoreManagerInterface::class,
  54. [],
  55. '',
  56. false
  57. );
  58. $this->customerGroupManagement = $this->getMockForAbstractClass(
  59. \Magento\Customer\Api\GroupManagementInterface::class,
  60. [],
  61. '',
  62. false
  63. );
  64. $this->dataObjectHelper = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
  65. $this->request = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class, [], '', false);
  66. $this->customerForm = $this->createMock(\Magento\Customer\Model\Metadata\Form::class);
  67. $this->customerData = $this->getMockForAbstractClass(
  68. \Magento\Customer\Api\Data\CustomerInterface::class,
  69. [],
  70. '',
  71. false
  72. );
  73. $this->store = $this->getMockForAbstractClass(
  74. \Magento\Store\Api\Data\StoreInterface::class,
  75. [],
  76. '',
  77. false
  78. );
  79. $this->customerGroup = $this->getMockForAbstractClass(
  80. \Magento\Customer\Api\Data\GroupInterface::class,
  81. [],
  82. '',
  83. false
  84. );
  85. $this->customerExtractor = new CustomerExtractor(
  86. $this->formFactory,
  87. $this->customerFactory,
  88. $this->storeManager,
  89. $this->customerGroupManagement,
  90. $this->dataObjectHelper
  91. );
  92. }
  93. public function testExtract()
  94. {
  95. $customerData = [
  96. 'firstname' => 'firstname',
  97. 'lastname' => 'firstname',
  98. 'email' => 'email.example.com',
  99. ];
  100. $this->formFactory->expects($this->once())
  101. ->method('create')
  102. ->with('customer', 'form-code')
  103. ->willReturn($this->customerForm);
  104. $this->customerForm->expects($this->once())
  105. ->method('extractData')
  106. ->with($this->request)
  107. ->willReturn($customerData);
  108. $this->customerForm->expects($this->once())
  109. ->method('compactData')
  110. ->with($customerData)
  111. ->willReturn($customerData);
  112. $this->customerForm->expects($this->once())
  113. ->method('getAllowedAttributes')
  114. ->willReturn(['group_id' => 'attribute object']);
  115. $this->customerFactory->expects($this->once())
  116. ->method('create')
  117. ->willReturn($this->customerData);
  118. $this->dataObjectHelper->expects($this->once())
  119. ->method('populateWithArray')
  120. ->with($this->customerData, $customerData, \Magento\Customer\Api\Data\CustomerInterface::class)
  121. ->willReturn($this->customerData);
  122. $this->storeManager->expects($this->once())
  123. ->method('getStore')
  124. ->willReturn($this->store);
  125. $this->store->expects($this->exactly(2))
  126. ->method('getId')
  127. ->willReturn(1);
  128. $this->customerGroupManagement->expects($this->once())
  129. ->method('getDefaultGroup')
  130. ->with(1)
  131. ->willReturn($this->customerGroup);
  132. $this->customerGroup->expects($this->once())
  133. ->method('getId')
  134. ->willReturn(1);
  135. $this->customerData->expects($this->once())
  136. ->method('setGroupId')
  137. ->with(1);
  138. $this->store->expects($this->once())
  139. ->method('getWebsiteId')
  140. ->willReturn(1);
  141. $this->customerData->expects($this->once())
  142. ->method('setWebsiteId')
  143. ->with(1);
  144. $this->customerData->expects($this->once())
  145. ->method('setStoreId')
  146. ->with(1);
  147. $this->assertSame($this->customerData, $this->customerExtractor->extract('form-code', $this->request));
  148. }
  149. }