AbstractWidgetTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\AbstractWidget;
  8. class AbstractWidgetTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** Constants used in the various unit tests. */
  11. const KEY_FIELD_ID_FORMAT = 'field_id_format';
  12. const KEY_FIELD_NAME_FORMAT = 'field_name_format';
  13. const FORMAT_D = '%d';
  14. const FORMAT_S = '%s';
  15. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Helper\Address */
  16. private $_addressHelper;
  17. /** @var AbstractWidget */
  18. private $_block;
  19. protected function setUp()
  20. {
  21. $this->_addressHelper = $this->createMock(\Magento\Customer\Helper\Address::class);
  22. $this->_block = new \Magento\Customer\Block\Widget\AbstractWidget(
  23. $this->createMock(\Magento\Framework\View\Element\Template\Context::class),
  24. $this->_addressHelper,
  25. $this->getMockBuilder(\Magento\Customer\Api\CustomerMetadataInterface::class)->getMockForAbstractClass()
  26. );
  27. }
  28. /**
  29. * @param string $key
  30. * @param string|null $expectedValue
  31. *
  32. * @dataProvider getConfigDataProvider
  33. */
  34. public function testGetConfig($key, $expectedValue)
  35. {
  36. $this->_addressHelper->expects(
  37. $this->once()
  38. )->method(
  39. 'getConfig'
  40. )->with(
  41. $key
  42. )->will(
  43. $this->returnValue($expectedValue)
  44. );
  45. $this->assertEquals($expectedValue, $this->_block->getConfig($key));
  46. }
  47. /**
  48. * @return array
  49. */
  50. public function getConfigDataProvider()
  51. {
  52. return [['key', 'value'], [null, null]];
  53. }
  54. /**
  55. * The default field id format is '%s' so we set it to '%d' to verify that '%d' gets returned when
  56. * AbstractWidget::getFieldIdFormat() is called.
  57. */
  58. public function testGetFieldIdFormatHasData()
  59. {
  60. $this->_block->setData(self::KEY_FIELD_ID_FORMAT, self::FORMAT_D);
  61. $this->assertEquals(self::FORMAT_D, $this->_block->getFieldIdFormat());
  62. }
  63. /**
  64. * Returns the default '%s' field id format when the block has no data for it.
  65. */
  66. public function testGetFieldIdFormatHasNoData()
  67. {
  68. $this->assertEquals(self::FORMAT_S, $this->_block->getFieldIdFormat());
  69. }
  70. /**
  71. * The default field name format is '%s' so we set it to '%d' to verify that '%d' gets returned when
  72. * AbstractWidget::getFieldNameFormat() is called.
  73. */
  74. public function testGetFieldNameFormatHasData()
  75. {
  76. $this->_block->setData(self::KEY_FIELD_NAME_FORMAT, self::FORMAT_D);
  77. $this->assertEquals(self::FORMAT_D, $this->_block->getFieldNameFormat());
  78. }
  79. /**
  80. * Returns the default '%s' field name format when the block has no data for it.
  81. */
  82. public function testGetFieldNameFormatHasNoData()
  83. {
  84. $this->assertEquals(self::FORMAT_S, $this->_block->getFieldNameFormat());
  85. }
  86. /**
  87. * Test '%s' and '%d' formats to verify that '%s' returns a string and '%d' returns a numeric
  88. * string when AbstractWidget::getFieldId() is invoked.
  89. *
  90. * @param string $format Field id format (e.g. '%s' or '%d')
  91. * @param string $fieldId Field id
  92. * @param string $expectedValue The value we expect from AbstractWidget::getFieldId()
  93. * @param string $method The method to invoke on the result from getFieldId() should return true
  94. *
  95. * @dataProvider getFieldIdDataProvider
  96. */
  97. public function testGetFieldId($format, $fieldId, $expectedValue, $method)
  98. {
  99. $this->_block->setData(self::KEY_FIELD_ID_FORMAT, $format);
  100. $this->assertTrue(call_user_func($method, $blockFieldId = $this->_block->getFieldId($fieldId)));
  101. $this->assertSame($expectedValue, $blockFieldId);
  102. }
  103. /**
  104. * @return array
  105. */
  106. public function getFieldIdDataProvider()
  107. {
  108. return [
  109. [self::FORMAT_S, 'Id', 'Id', 'is_string'],
  110. [self::FORMAT_D, '123', '123', 'is_numeric'],
  111. [self::FORMAT_D, 'Id', '0', 'is_numeric']
  112. ];
  113. }
  114. /**
  115. * Test '%s' and '%d' formats to verify that '%s' returns a string and '%d' returns a numeric
  116. * string when AbstractWidget::getFieldName() is invoked.
  117. *
  118. * @param string $format Field name format (e.g. '%s' or '%d')
  119. * @param string $fieldName The field name
  120. * @param string $expectedValue The value we expect from AbstractWidget::getFieldName
  121. * @param string $method The method to invoke on the result from getFieldName() should return true
  122. *
  123. * @dataProvider getFieldNameDataProvider
  124. */
  125. public function testGetFieldName($format, $fieldName, $expectedValue, $method)
  126. {
  127. $this->_block->setData(self::KEY_FIELD_NAME_FORMAT, $format);
  128. $this->assertTrue(call_user_func($method, $blockFieldName = $this->_block->getFieldName($fieldName)));
  129. $this->assertEquals($expectedValue, $blockFieldName);
  130. }
  131. /**
  132. * @return array
  133. */
  134. public function getFieldNameDataProvider()
  135. {
  136. return [
  137. [self::FORMAT_S, 'Name', 'Name', 'is_string'],
  138. [self::FORMAT_D, '123', '123', 'is_numeric'],
  139. [self::FORMAT_D, 'Name', '0', 'is_numeric']
  140. ];
  141. }
  142. }