Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
CustomerExtractorTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class CustomerExtractorTest extends \PHPUnit\Framework\TestCase
11 {
13  protected $customerExtractor;
14 
16  protected $formFactory;
17 
19  protected $customerFactory;
20 
22  protected $storeManager;
23 
26 
28  protected $dataObjectHelper;
29 
31  protected $request;
32 
34  protected $customerForm;
35 
37  protected $customerData;
38 
40  protected $store;
41 
43  protected $customerGroup;
44 
45  protected function setUp()
46  {
47  $this->formFactory = $this->getMockForAbstractClass(
48  \Magento\Customer\Model\Metadata\FormFactory::class,
49  [],
50  '',
51  false,
52  false,
53  true,
54  ['create']
55  );
56  $this->customerFactory = $this->getMockForAbstractClass(
57  \Magento\Customer\Api\Data\CustomerInterfaceFactory::class,
58  [],
59  '',
60  false,
61  false,
62  true,
63  ['create']
64  );
65  $this->storeManager = $this->getMockForAbstractClass(
66  \Magento\Store\Model\StoreManagerInterface::class,
67  [],
68  '',
69  false
70  );
71  $this->customerGroupManagement = $this->getMockForAbstractClass(
72  \Magento\Customer\Api\GroupManagementInterface::class,
73  [],
74  '',
75  false
76  );
77  $this->dataObjectHelper = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
78  $this->request = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class, [], '', false);
79  $this->customerForm = $this->createMock(\Magento\Customer\Model\Metadata\Form::class);
80  $this->customerData = $this->getMockForAbstractClass(
81  \Magento\Customer\Api\Data\CustomerInterface::class,
82  [],
83  '',
84  false
85  );
86  $this->store = $this->getMockForAbstractClass(
87  \Magento\Store\Api\Data\StoreInterface::class,
88  [],
89  '',
90  false
91  );
92  $this->customerGroup = $this->getMockForAbstractClass(
93  \Magento\Customer\Api\Data\GroupInterface::class,
94  [],
95  '',
96  false
97  );
98  $this->customerExtractor = new CustomerExtractor(
99  $this->formFactory,
100  $this->customerFactory,
101  $this->storeManager,
102  $this->customerGroupManagement,
103  $this->dataObjectHelper
104  );
105  }
106 
107  public function testExtract()
108  {
109  $customerData = [
110  'firstname' => 'firstname',
111  'lastname' => 'firstname',
112  'email' => 'email.example.com',
113  ];
114 
115  $this->formFactory->expects($this->once())
116  ->method('create')
117  ->with('customer', 'form-code')
118  ->willReturn($this->customerForm);
119  $this->customerForm->expects($this->once())
120  ->method('extractData')
121  ->with($this->request)
122  ->willReturn($customerData);
123  $this->customerForm->expects($this->once())
124  ->method('compactData')
125  ->with($customerData)
126  ->willReturn($customerData);
127  $this->customerForm->expects($this->once())
128  ->method('getAllowedAttributes')
129  ->willReturn(['group_id' => 'attribute object']);
130  $this->customerFactory->expects($this->once())
131  ->method('create')
132  ->willReturn($this->customerData);
133  $this->dataObjectHelper->expects($this->once())
134  ->method('populateWithArray')
135  ->with($this->customerData, $customerData, \Magento\Customer\Api\Data\CustomerInterface::class)
136  ->willReturn($this->customerData);
137  $this->storeManager->expects($this->once())
138  ->method('getStore')
139  ->willReturn($this->store);
140  $this->store->expects($this->exactly(2))
141  ->method('getId')
142  ->willReturn(1);
143  $this->customerGroupManagement->expects($this->once())
144  ->method('getDefaultGroup')
145  ->with(1)
146  ->willReturn($this->customerGroup);
147  $this->customerGroup->expects($this->once())
148  ->method('getId')
149  ->willReturn(1);
150  $this->customerData->expects($this->once())
151  ->method('setGroupId')
152  ->with(1);
153  $this->store->expects($this->once())
154  ->method('getWebsiteId')
155  ->willReturn(1);
156  $this->customerData->expects($this->once())
157  ->method('setWebsiteId')
158  ->with(1);
159  $this->customerData->expects($this->once())
160  ->method('setStoreId')
161  ->with(1);
162 
163  $this->assertSame($this->customerData, $this->customerExtractor->extract('form-code', $this->request));
164  }
165 }