8
8
namespace Magento \Customer \Test \Unit \Model \Address ;
9
9
10
10
use Magento \Customer \Api \CustomerRepositoryInterface ;
11
+ use Magento \Customer \Model \Address \DataProvider ;
11
12
use Magento \Customer \Model \AttributeMetadataResolver ;
12
13
use Magento \Customer \Model \FileUploaderDataResolver ;
13
14
use Magento \Customer \Model \ResourceModel \Address \CollectionFactory ;
17
18
use Magento \Customer \Api \Data \CustomerInterface ;
18
19
use Magento \Framework \View \Element \UiComponent \ContextInterface ;
19
20
use Magento \Customer \Model \Address as AddressModel ;
21
+ use Magento \Ui \Component \Form \Element \Multiline ;
22
+ use Magento \Ui \Component \Form \Field ;
23
+ use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
20
24
21
25
/**
22
26
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23
27
*/
24
28
class DataProviderTest extends \PHPUnit \Framework \TestCase
25
29
{
30
+ private const ATTRIBUTE_CODE = 'street ' ;
31
+
26
32
/**
27
33
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
28
34
*/
@@ -53,11 +59,6 @@ class DataProviderTest extends \PHPUnit\Framework\TestCase
53
59
*/
54
60
private $ context ;
55
61
56
- /**
57
- * @var Type|\PHPUnit_Framework_MockObject_MockObject
58
- */
59
- private $ entityType ;
60
-
61
62
/**
62
63
* @var AddressModel|\PHPUnit_Framework_MockObject_MockObject
63
64
*/
@@ -74,7 +75,7 @@ class DataProviderTest extends \PHPUnit\Framework\TestCase
74
75
private $ attributeMetadataResolver ;
75
76
76
77
/**
77
- * @var \Magento\Customer\Model\Address\ DataProvider
78
+ * @var DataProvider
78
79
*/
79
80
private $ model ;
80
81
@@ -102,26 +103,68 @@ protected function setUp()
102
103
$ this ->eavConfig = $ this ->getMockBuilder (Config::class)
103
104
->disableOriginalConstructor ()
104
105
->getMock ();
105
- $ this ->entityType = $ this ->getMockBuilder (Type::class)
106
- ->disableOriginalConstructor ()
107
- ->getMock ();
108
- $ this ->entityType ->expects ($ this ->once ())
109
- ->method ('getAttributeCollection ' )
110
- ->willReturn ([]);
111
106
$ this ->eavConfig ->expects ($ this ->once ())
112
107
->method ('getEntityType ' )
113
- ->willReturn ($ this ->entityType );
108
+ ->with ('customer_address ' )
109
+ ->willReturn ($ this ->getTypeAddressMock ([]));
114
110
$ this ->customer = $ this ->getMockForAbstractClass (CustomerInterface::class);
115
111
$ this ->address = $ this ->getMockBuilder (AddressModel::class)
116
112
->disableOriginalConstructor ()
117
113
->getMock ();
118
-
114
+ $ this ->attributeMetadataResolver ->expects ($ this ->at (0 ))
115
+ ->method ('getAttributesMeta ' )
116
+ ->willReturn (
117
+ [
118
+ 'arguments ' => [
119
+ 'data ' => [
120
+ 'config ' => [
121
+ 'dataType ' => Multiline::NAME ,
122
+ 'formElement ' => 'frontend_input ' ,
123
+ 'options ' => 'test-options ' ,
124
+ 'visible ' => null ,
125
+ 'required ' => 'is_required ' ,
126
+ 'label ' => __ ('Street ' ),
127
+ 'sortOrder ' => 'sort_order ' ,
128
+ 'default ' => 'default_value ' ,
129
+ 'size ' => 'multiline_count ' ,
130
+ 'componentType ' => Field::NAME ,
131
+ ],
132
+ ],
133
+ ],
134
+ ]
135
+ );
136
+ $ this ->attributeMetadataResolver ->expects ($ this ->at (1 ))
137
+ ->method ('getAttributesMeta ' )
138
+ ->willReturn (
139
+ [
140
+ 'arguments ' => [
141
+ 'data ' => [
142
+ 'config ' => [
143
+ 'dataType ' => 'frontend_input ' ,
144
+ 'formElement ' => 'frontend_input ' ,
145
+ 'visible ' => null ,
146
+ 'required ' => 'is_required ' ,
147
+ 'label ' => __ ('frontend_label ' ),
148
+ 'sortOrder ' => 'sort_order ' ,
149
+ 'default ' => 'default_value ' ,
150
+ 'size ' => 'multiline_count ' ,
151
+ 'componentType ' => Field::NAME ,
152
+ 'prefer ' => 'toggle ' ,
153
+ 'valueMap ' => [
154
+ 'true ' => 1 ,
155
+ 'false ' => 0 ,
156
+ ],
157
+ ],
158
+ ],
159
+ ],
160
+ ]
161
+ );
119
162
$ this ->model = $ objectManagerHelper ->getObject (
120
- \ Magento \ Customer \ Model \ Address \ DataProvider::class,
163
+ DataProvider::class,
121
164
[
122
- '' ,
123
- '' ,
124
- '' ,
165
+ 'name ' => ' test-name ' ,
166
+ 'primaryFieldName ' => ' primary-field-name ' ,
167
+ 'requestFieldName ' => ' request-field-name ' ,
125
168
'addressCollectionFactory ' => $ this ->addressCollectionFactory ,
126
169
'customerRepository ' => $ this ->customerRepository ,
127
170
'eavConfig ' => $ this ->eavConfig ,
@@ -168,8 +211,10 @@ public function testGetDefaultData(): void
168
211
public function testGetData (): void
169
212
{
170
213
$ expectedData = [
171
- '3 ' => [
172
- 'parent_id ' => "1 " ,
214
+ '1 ' => [
215
+ 'parent_id ' => '1 ' ,
216
+ 'default_billing ' => '1 ' ,
217
+ 'default_shipping ' => '1 ' ,
173
218
'firstname ' => 'John ' ,
174
219
'lastname ' => 'Doe ' ,
175
220
'street ' => [
@@ -197,16 +242,16 @@ public function testGetData(): void
197
242
198
243
$ this ->address ->expects ($ this ->once ())
199
244
->method ('getEntityId ' )
200
- ->willReturn ('3 ' );
245
+ ->willReturn ('1 ' );
201
246
$ this ->address ->expects ($ this ->once ())
202
247
->method ('load ' )
203
- ->with (" 3 " )
248
+ ->with (' 1 ' )
204
249
->willReturnSelf ();
205
250
$ this ->address ->expects ($ this ->once ())
206
251
->method ('getData ' )
207
252
->willReturn ([
208
- 'parent_id ' => " 1 " ,
209
- 'firstname ' => " John " ,
253
+ 'parent_id ' => ' 1 ' ,
254
+ 'firstname ' => ' John ' ,
210
255
'lastname ' => 'Doe ' ,
211
256
'street ' => "42000 Ave W 55 Cedar City \nApt. 33 "
212
257
]);
@@ -216,4 +261,91 @@ public function testGetData(): void
216
261
217
262
$ this ->assertEquals ($ expectedData , $ this ->model ->getData ());
218
263
}
264
+
265
+ /**
266
+ * Get customer address type mock
267
+ *
268
+ * @param array $customerAttributes
269
+ * @return Type|\PHPUnit_Framework_MockObject_MockObject
270
+ */
271
+ protected function getTypeAddressMock ($ customerAttributes = [])
272
+ {
273
+ $ typeAddressMock = $ this ->getMockBuilder (Type::class)
274
+ ->disableOriginalConstructor ()
275
+ ->getMock ();
276
+ $ attributesCollection = !empty ($ customerAttributes ) ? $ customerAttributes : $ this ->getAttributeMock ();
277
+ foreach ($ attributesCollection as $ attribute ) {
278
+ $ attribute ->expects ($ this ->any ())
279
+ ->method ('getEntityType ' )
280
+ ->willReturn ($ typeAddressMock );
281
+ }
282
+
283
+ $ typeAddressMock ->expects ($ this ->once ())
284
+ ->method ('getAttributeCollection ' )
285
+ ->willReturn ($ attributesCollection );
286
+
287
+ return $ typeAddressMock ;
288
+ }
289
+
290
+ /**
291
+ * Get attribute mock
292
+ *
293
+ * @param array $options
294
+ * @return AbstractAttribute[]|\PHPUnit_Framework_MockObject_MockObject[]
295
+ */
296
+ protected function getAttributeMock ($ options = []): array
297
+ {
298
+ $ attributeMock = $ this ->getMockBuilder (AbstractAttribute::class)
299
+ ->setMethods (
300
+ [
301
+ 'getAttributeCode ' ,
302
+ 'getDataUsingMethod ' ,
303
+ 'getFrontendInput ' ,
304
+ 'getIsVisible ' ,
305
+ 'getSource ' ,
306
+ 'getIsUserDefined ' ,
307
+ 'getUsedInForms ' ,
308
+ 'getEntityType ' ,
309
+ ]
310
+ )
311
+ ->disableOriginalConstructor ()
312
+ ->getMockForAbstractClass ();
313
+
314
+ $ attributeCode = self ::ATTRIBUTE_CODE ;
315
+ if (isset ($ options [self ::ATTRIBUTE_CODE ]['specific_code_prefix ' ])) {
316
+ $ attributeCode .= $ options [self ::ATTRIBUTE_CODE ]['specific_code_prefix ' ];
317
+ }
318
+
319
+ $ attributeMock ->expects ($ this ->exactly (2 ))
320
+ ->method ('getAttributeCode ' )
321
+ ->willReturn ($ attributeCode );
322
+
323
+ $ attributeBooleanMock = $ this ->getMockBuilder (AbstractAttribute::class)
324
+ ->setMethods (
325
+ [
326
+ 'getAttributeCode ' ,
327
+ 'getDataUsingMethod ' ,
328
+ 'getFrontendInput ' ,
329
+ 'getIsVisible ' ,
330
+ 'getIsUserDefined ' ,
331
+ 'getUsedInForms ' ,
332
+ 'getSource ' ,
333
+ 'getEntityType ' ,
334
+ ]
335
+ )
336
+ ->disableOriginalConstructor ()
337
+ ->getMockForAbstractClass ();
338
+
339
+ $ booleanAttributeCode = 'test-code-boolean ' ;
340
+ if (isset ($ options ['test-code-boolean ' ]['specific_code_prefix ' ])) {
341
+ $ booleanAttributeCode .= $ options ['test-code-boolean ' ]['specific_code_prefix ' ];
342
+ }
343
+
344
+ $ attributeBooleanMock ->expects ($ this ->exactly (2 ))
345
+ ->method ('getAttributeCode ' )
346
+ ->willReturn ($ booleanAttributeCode );
347
+
348
+ $ mocks = [$ attributeMock , $ attributeBooleanMock ];
349
+ return $ mocks ;
350
+ }
219
351
}
0 commit comments