TaxvatTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Framework\Exception\NoSuchEntityException;
  8. use Magento\Customer\Block\Widget\Taxvat;
  9. class TaxvatTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** Constants used in the unit tests */
  12. const CUSTOMER_ENTITY_TYPE = 'customer';
  13. const TAXVAT_ATTRIBUTE_CODE = 'taxvat';
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Api\CustomerMetadataInterface
  16. */
  17. private $customerMetadata;
  18. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Api\Data\AttributeMetadataInterface */
  19. private $attribute;
  20. /** @var Taxvat */
  21. private $_block;
  22. protected function setUp()
  23. {
  24. $this->attribute = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  25. ->getMockForAbstractClass();
  26. $this->customerMetadata = $this->getMockBuilder(\Magento\Customer\Api\CustomerMetadataInterface::class)
  27. ->getMockForAbstractClass();
  28. $this->customerMetadata->expects(
  29. $this->any()
  30. )->method(
  31. 'getAttributeMetadata'
  32. )->with(
  33. self::TAXVAT_ATTRIBUTE_CODE
  34. )->will(
  35. $this->returnValue($this->attribute)
  36. );
  37. $this->_block = new \Magento\Customer\Block\Widget\Taxvat(
  38. $this->createMock(\Magento\Framework\View\Element\Template\Context::class),
  39. $this->createMock(\Magento\Customer\Helper\Address::class),
  40. $this->customerMetadata
  41. );
  42. }
  43. /**
  44. * @param bool $isVisible Determines whether the 'taxvat' attribute is visible or enabled
  45. * @param bool $expectedValue The value we expect from Taxvat::isEnabled()
  46. * @return void
  47. *
  48. * @dataProvider isEnabledDataProvider
  49. */
  50. public function testIsEnabled($isVisible, $expectedValue)
  51. {
  52. $this->attribute->expects($this->once())->method('isVisible')->will($this->returnValue($isVisible));
  53. $this->assertSame($expectedValue, $this->_block->isEnabled());
  54. }
  55. /**
  56. * @return array
  57. */
  58. public function isEnabledDataProvider()
  59. {
  60. return [[true, true], [false, false]];
  61. }
  62. public function testIsEnabledWithException()
  63. {
  64. $this->customerMetadata->expects(
  65. $this->any()
  66. )->method(
  67. 'getAttributeMetadata'
  68. )->will(
  69. $this->throwException(new NoSuchEntityException(
  70. __(
  71. 'No such entity with %fieldName = %fieldValue',
  72. ['fieldName' => 'field', 'fieldValue' => 'value']
  73. )
  74. ))
  75. );
  76. $this->assertSame(false, $this->_block->isEnabled());
  77. }
  78. /**
  79. * @param bool $isRequired Determines whether the 'taxvat' attribute is required
  80. * @param bool $expectedValue The value we expect from Taxvat::isRequired()
  81. * @return void
  82. *
  83. * @dataProvider isRequiredDataProvider
  84. */
  85. public function testIsRequired($isRequired, $expectedValue)
  86. {
  87. $this->attribute->expects($this->once())->method('isRequired')->will($this->returnValue($isRequired));
  88. $this->assertSame($expectedValue, $this->_block->isRequired());
  89. }
  90. /**
  91. * @return array
  92. */
  93. public function isRequiredDataProvider()
  94. {
  95. return [[true, true], [false, false]];
  96. }
  97. public function testIsRequiredWithException()
  98. {
  99. $this->customerMetadata->expects(
  100. $this->any()
  101. )->method(
  102. 'getAttributeMetadata'
  103. )->will(
  104. $this->throwException(new NoSuchEntityException(
  105. __(
  106. 'No such entity with %fieldName = %fieldValue',
  107. ['fieldName' => 'field', 'fieldValue' => 'value']
  108. )
  109. ))
  110. );
  111. $this->assertSame(false, $this->_block->isRequired());
  112. }
  113. }