7
7
namespace Magento \Customer \Test \Unit \Helper ;
8
8
9
9
use Magento \Customer \Api \AddressMetadataInterface ;
10
+ use Magento \Customer \Api \AddressMetadataManagementInterface ;
10
11
use Magento \Customer \Api \CustomerMetadataInterface ;
11
12
12
13
/**
@@ -35,6 +36,9 @@ class AddressTest extends \PHPUnit\Framework\TestCase
35
36
/** @var \Magento\Customer\Model\Address\Config|\PHPUnit_Framework_MockObject_MockObject */
36
37
protected $ addressConfig ;
37
38
39
+ /** @var \Magento\Customer\Model\Metadata\AttributeResolver|\PHPUnit_Framework_MockObject_MockObject */
40
+ protected $ attributeResolver ;
41
+
38
42
/** @var \PHPUnit_Framework_MockObject_MockObject|AddressMetadataInterface */
39
43
private $ addressMetadataService ;
40
44
@@ -51,6 +55,7 @@ protected function setUp()
51
55
$ this ->customerMetadataService = $ arguments ['customerMetadataService ' ];
52
56
$ this ->addressConfig = $ arguments ['addressConfig ' ];
53
57
$ this ->addressMetadataService = $ arguments ['addressMetadataService ' ];
58
+ $ this ->attributeResolver = $ arguments ['attributeResolver ' ];
54
59
55
60
$ this ->helper = $ objectManagerHelper ->getObject ($ className , $ arguments );
56
61
}
@@ -322,9 +327,11 @@ public function testGetFormatTypeRenderer($code, $result)
322
327
$ this ->addressConfig ->expects ($ this ->once ())
323
328
->method ('getFormatByCode ' )
324
329
->with ($ code )
325
- ->will ($ this ->returnValue (
326
- new \Magento \Framework \DataObject ($ result !== null ? ['renderer ' => $ result ] : [])
327
- ));
330
+ ->will (
331
+ $ this ->returnValue (
332
+ new \Magento \Framework \DataObject ($ result !== null ? ['renderer ' => $ result ] : [])
333
+ )
334
+ );
328
335
$ this ->assertEquals ($ result , $ this ->helper ->getFormatTypeRenderer ($ code ));
329
336
}
330
337
@@ -334,7 +341,7 @@ public function getFormatTypeRendererDataProvider()
334
341
->disableOriginalConstructor ()->getMock ();
335
342
return [
336
343
['valid_code ' , $ renderer ],
337
- ['invalid_code ' , null ]
344
+ ['invalid_code ' , null ],
338
345
];
339
346
}
340
347
@@ -355,9 +362,11 @@ public function testGetFormat($code, $result)
355
362
$ this ->addressConfig ->expects ($ this ->once ())
356
363
->method ('getFormatByCode ' )
357
364
->with ($ code )
358
- ->will ($ this ->returnValue (
359
- new \Magento \Framework \DataObject (!empty ($ result ) ? ['renderer ' => $ renderer ] : [])
360
- ));
365
+ ->will (
366
+ $ this ->returnValue (
367
+ new \Magento \Framework \DataObject (!empty ($ result ) ? ['renderer ' => $ renderer ] : [])
368
+ )
369
+ );
361
370
362
371
$ this ->assertEquals ($ result , $ this ->helper ->getFormat ($ code ));
363
372
}
@@ -366,7 +375,7 @@ public function getFormatDataProvider()
366
375
{
367
376
return [
368
377
['valid_code ' , ['key ' => 'value ' ]],
369
- ['invalid_code ' , '' ]
378
+ ['invalid_code ' , '' ],
370
379
];
371
380
}
372
381
@@ -396,7 +405,71 @@ public function isAttributeVisibleDataProvider()
396
405
{
397
406
return [
398
407
['fax ' , true ],
399
- ['invalid_code ' , false ]
408
+ ['invalid_code ' , false ],
409
+ ];
410
+ }
411
+
412
+ /**
413
+ * @dataProvider attributeOnFormDataProvider
414
+ * @param bool $isAllowed
415
+ * @param bool $isMetadataExists
416
+ * @param string $attributeCode
417
+ * @param string $formName
418
+ * @param array $attributeFormsList
419
+ */
420
+ public function testIsAttributeAllowedOnForm (
421
+ $ isAllowed ,
422
+ $ isMetadataExists ,
423
+ $ attributeCode ,
424
+ $ formName ,
425
+ array $ attributeFormsList
426
+ ) {
427
+ $ attributeMetadata = null ;
428
+ if ($ isMetadataExists ) {
429
+ $ attributeMetadata = $ this ->getMockBuilder (\Magento \Customer \Api \Data \AttributeMetadataInterface::class)
430
+ ->getMockForAbstractClass ();
431
+ $ attribute = $ this ->getMockBuilder (\Magento \Customer \Model \Attribute::class)
432
+ ->disableOriginalConstructor ()
433
+ ->getMock ();
434
+ $ this ->attributeResolver ->expects ($ this ->once ())
435
+ ->method ('getModelByAttribute ' )
436
+ ->with (AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS , $ attributeMetadata )
437
+ ->willReturn ($ attribute );
438
+ $ attribute ->expects ($ this ->once ())
439
+ ->method ('getUsedInForms ' )
440
+ ->willReturn ($ attributeFormsList );
441
+ }
442
+ $ this ->addressMetadataService ->expects ($ this ->once ())
443
+ ->method ('getAttributeMetadata ' )
444
+ ->with ($ attributeCode )
445
+ ->willReturn ($ attributeMetadata );
446
+ $ this ->assertEquals ($ isAllowed , $ this ->helper ->isAttributeAllowedOnForm ($ attributeCode , $ formName ));
447
+ }
448
+
449
+ public function attributeOnFormDataProvider ()
450
+ {
451
+ return [
452
+ 'metadata not exists ' => [
453
+ 'isAllowed ' => false ,
454
+ 'isMetadataExists ' => false ,
455
+ 'attributeCode ' => 'attribute_code ' ,
456
+ 'formName ' => 'form_name ' ,
457
+ 'attributeFormsList ' => [],
458
+ ],
459
+ 'form not in the list ' => [
460
+ 'isAllowed ' => false ,
461
+ 'isMetadataExists ' => true ,
462
+ 'attributeCode ' => 'attribute_code ' ,
463
+ 'formName ' => 'form_name ' ,
464
+ 'attributeFormsList ' => ['form_1 ' , 'form_2 ' ],
465
+ ],
466
+ 'allowed ' => [
467
+ 'isAllowed ' => true ,
468
+ 'isMetadataExists ' => true ,
469
+ 'attributeCode ' => 'attribute_code ' ,
470
+ 'formName ' => 'form_name ' ,
471
+ 'attributeFormsList ' => ['form_name ' , 'form_1 ' , 'form_2 ' ],
472
+ ],
400
473
];
401
474
}
402
475
}
0 commit comments