GenderTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Block\Widget;
  7. use Magento\Customer\Block\Widget\Gender;
  8. use Magento\Customer\Api\Data\CustomerInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. class GenderTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /** Constants used in the unit tests */
  13. const CUSTOMER_ENTITY_TYPE = 'customer';
  14. const GENDER_ATTRIBUTE_CODE = 'gender';
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Api\CustomerMetadataInterface
  17. */
  18. private $customerMetadata;
  19. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Api\Data\AttributeMetadataInterface */
  20. private $attribute;
  21. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Model\Session */
  22. private $customerSession;
  23. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Api\CustomerRepositoryInterface */
  24. private $customerRepository;
  25. /** @var Gender */
  26. private $block;
  27. protected function setUp()
  28. {
  29. $this->attribute = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  30. ->getMockForAbstractClass();
  31. $this->customerMetadata = $this->getMockBuilder(\Magento\Customer\Api\CustomerMetadataInterface::class)
  32. ->getMockForAbstractClass();
  33. $this->customerMetadata->expects($this->any())
  34. ->method('getAttributeMetadata')
  35. ->with(self::GENDER_ATTRIBUTE_CODE)
  36. ->will($this->returnValue($this->attribute));
  37. $this->customerRepository = $this
  38. ->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)
  39. ->getMockForAbstractClass();
  40. $this->customerSession = $this->createMock(\Magento\Customer\Model\Session::class);
  41. $this->block = new \Magento\Customer\Block\Widget\Gender(
  42. $this->createMock(\Magento\Framework\View\Element\Template\Context::class),
  43. $this->createMock(\Magento\Customer\Helper\Address::class),
  44. $this->customerMetadata,
  45. $this->customerRepository,
  46. $this->customerSession
  47. );
  48. }
  49. /**
  50. * Test the Gender::isEnabled() method.
  51. *
  52. * @param bool $isVisible Determines whether the 'gender' attribute is visible or enabled
  53. * @param bool $expectedValue The value we expect from Gender::isEnabled()
  54. * @return void
  55. *
  56. * @dataProvider isEnabledDataProvider
  57. */
  58. public function testIsEnabled($isVisible, $expectedValue)
  59. {
  60. $this->attribute->expects($this->once())->method('isVisible')->will($this->returnValue($isVisible));
  61. $this->assertSame($expectedValue, $this->block->isEnabled());
  62. }
  63. /**
  64. * The testIsEnabled data provider.
  65. * @return array
  66. */
  67. public function isEnabledDataProvider()
  68. {
  69. return [[true, true], [false, false]];
  70. }
  71. public function testIsEnabledWithException()
  72. {
  73. $this->customerMetadata->expects(
  74. $this->any()
  75. )->method(
  76. 'getAttributeMetadata'
  77. )->will(
  78. $this->throwException(new NoSuchEntityException(
  79. __(
  80. 'No such entity with %fieldName = %fieldValue',
  81. ['fieldName' => 'field', 'fieldValue' => 'value']
  82. )
  83. ))
  84. );
  85. $this->assertSame(false, $this->block->isEnabled());
  86. }
  87. /**
  88. * Test the Gender::isRequired() method.
  89. *
  90. * @param bool $isRequired Determines whether the 'gender' attribute is required
  91. * @param bool $expectedValue The value we expect from Gender::isRequired()
  92. * @return void
  93. *
  94. * @dataProvider isRequiredDataProvider
  95. */
  96. public function testIsRequired($isRequired, $expectedValue)
  97. {
  98. $this->attribute->expects($this->once())->method('isRequired')->will($this->returnValue($isRequired));
  99. $this->assertSame($expectedValue, $this->block->isRequired());
  100. }
  101. /**
  102. * The testIsRequired data provider.
  103. * @return array
  104. */
  105. public function isRequiredDataProvider()
  106. {
  107. return [[true, true], [false, false]];
  108. }
  109. public function testIsRequiredWithException()
  110. {
  111. $this->customerMetadata->expects(
  112. $this->any()
  113. )->method(
  114. 'getAttributeMetadata'
  115. )->will(
  116. $this->throwException(new NoSuchEntityException(
  117. __(
  118. 'No such entity with %fieldName = %fieldValue',
  119. ['fieldName' => 'field', 'fieldValue' => 'value']
  120. )
  121. ))
  122. );
  123. $this->assertSame(false, $this->block->isRequired());
  124. }
  125. /**
  126. * Test the Gender::getCustomer() method.
  127. * @return void
  128. */
  129. public function testGetCustomer()
  130. {
  131. $customerData = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  132. ->getMockForAbstractClass();
  133. $this->customerSession->expects($this->once())->method('getCustomerId')->will($this->returnValue(1));
  134. $this->customerRepository
  135. ->expects($this->once())
  136. ->method('getById')
  137. ->with(1)
  138. ->will($this->returnValue($customerData));
  139. $customer = $this->block->getCustomer();
  140. $this->assertSame($customerData, $customer);
  141. }
  142. /**
  143. * Test the Gender::getGenderOptions() method.
  144. * @return void
  145. */
  146. public function testGetGenderOptions()
  147. {
  148. $options = [
  149. ['label' => __('Male'), 'value' => 'M'],
  150. ['label' => __('Female'), 'value' => 'F'],
  151. ['label' => __('Not Specified'), 'value' => 'NA']
  152. ];
  153. $this->attribute->expects($this->once())->method('getOptions')->will($this->returnValue($options));
  154. $this->assertSame($options, $this->block->getGenderOptions());
  155. }
  156. }