ViewTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Helper;
  7. use Magento\Customer\Api\CustomerMetadataInterface;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. class ViewTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** @var \Magento\Customer\Helper\View */
  12. protected $_helper;
  13. /** @var CustomerMetadataInterface|\PHPUnit_Framework_MockObject_MockObject */
  14. protected $_customerMetadataService;
  15. protected function setUp()
  16. {
  17. $this->_customerMetadataService = $this->createMock(\Magento\Customer\Api\CustomerMetadataInterface::class);
  18. $this->_helper = Bootstrap::getObjectManager()->create(
  19. \Magento\Customer\Helper\View::class,
  20. ['customerMetadataService' => $this->_customerMetadataService]
  21. );
  22. parent::setUp();
  23. }
  24. /**
  25. * @param \Magento\Customer\Api\Data\CustomerInterface $customerData
  26. * @param string $expectedCustomerName
  27. * @param bool $isPrefixAllowed
  28. * @param bool $isMiddleNameAllowed
  29. * @param bool $isSuffixAllowed
  30. * @dataProvider getCustomerNameDataProvider
  31. */
  32. public function testGetCustomerName(
  33. $customerData,
  34. $expectedCustomerName,
  35. $isPrefixAllowed = false,
  36. $isMiddleNameAllowed = false,
  37. $isSuffixAllowed = false
  38. ) {
  39. $visibleAttribute = $this->createMock(\Magento\Customer\Api\Data\AttributeMetadataInterface::class);
  40. $visibleAttribute->expects($this->any())->method('isVisible')->will($this->returnValue(true));
  41. $invisibleAttribute = $this->createMock(\Magento\Customer\Api\Data\AttributeMetadataInterface::class);
  42. $invisibleAttribute->expects($this->any())->method('isVisible')->will($this->returnValue(false));
  43. $this->_customerMetadataService->expects(
  44. $this->any()
  45. )->method(
  46. 'getAttributeMetadata'
  47. )->will(
  48. $this->returnValueMap(
  49. [
  50. ['prefix', $isPrefixAllowed ? $visibleAttribute : $invisibleAttribute],
  51. ['middlename', $isMiddleNameAllowed ? $visibleAttribute : $invisibleAttribute],
  52. ['suffix', $isSuffixAllowed ? $visibleAttribute : $invisibleAttribute],
  53. ]
  54. )
  55. );
  56. $this->assertEquals(
  57. $expectedCustomerName,
  58. $this->_helper->getCustomerName($customerData),
  59. 'Full customer name is invalid'
  60. );
  61. }
  62. public function getCustomerNameDataProvider()
  63. {
  64. /** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory */
  65. $customerFactory = Bootstrap::getObjectManager()->create(
  66. \Magento\Customer\Api\Data\CustomerInterfaceFactory::class
  67. );
  68. return [
  69. 'With disabled prefix, middle name, suffix' => [
  70. $customerFactory->create()->setPrefix(
  71. 'prefix'
  72. )->setFirstname(
  73. 'FirstName'
  74. )->setMiddlename(
  75. 'MiddleName'
  76. )->setLastname(
  77. 'LastName'
  78. )->setSuffix(
  79. 'suffix'
  80. ),
  81. 'FirstName LastName',
  82. ],
  83. 'With prefix, middle name, suffix' => [
  84. $customerFactory->create()->setPrefix(
  85. 'prefix'
  86. )->setFirstname(
  87. 'FirstName'
  88. )->setMiddlename(
  89. 'MiddleName'
  90. )->setLastname(
  91. 'LastName'
  92. )->setSuffix(
  93. 'suffix'
  94. ),
  95. 'prefix FirstName MiddleName LastName suffix',
  96. true, // $isPrefixAllowed
  97. true, // $isMiddleNameAllowed
  98. true, //$isSuffixAllowed
  99. ],
  100. 'Empty prefix, middle name, suffix' => [
  101. $customerFactory->create()->setFirstname('FirstName')->setLastname('LastName'),
  102. 'FirstName LastName',
  103. true, // $isPrefixAllowed
  104. true, // $isMiddleNameAllowed
  105. true, //$isSuffixAllowed
  106. ],
  107. 'Empty prefix and suffix, not empty middle name' => [
  108. $customerFactory->create()->setFirstname(
  109. 'FirstName'
  110. )->setMiddlename(
  111. 'MiddleName'
  112. )->setLastname(
  113. 'LastName'
  114. ),
  115. 'FirstName MiddleName LastName',
  116. true, // $isPrefixAllowed
  117. true, // $isMiddleNameAllowed
  118. true, //$isSuffixAllowed
  119. ]
  120. ];
  121. }
  122. }