RegionTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\Renderer;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class RegionTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @param array $regionCollection
  12. * @dataProvider renderDataProvider
  13. */
  14. public function testRender($regionCollection)
  15. {
  16. $countryFactoryMock = $this->createMock(
  17. \Magento\Directory\Model\CountryFactory::class
  18. );
  19. $directoryHelperMock = $this->createPartialMock(
  20. \Magento\Directory\Helper\Data::class,
  21. ['isRegionRequired']
  22. );
  23. $escaperMock = $this->createMock(\Magento\Framework\Escaper::class);
  24. $elementMock = $this->createPartialMock(
  25. \Magento\Framework\Data\Form\Element\AbstractElement::class,
  26. ['getForm', 'getHtmlAttributes']
  27. );
  28. $countryMock = $this->createPartialMock(
  29. \Magento\Framework\Data\Form\Element\AbstractElement::class,
  30. ['getValue']
  31. );
  32. $regionMock = $this->createMock(
  33. \Magento\Framework\Data\Form\Element\AbstractElement::class
  34. );
  35. $countryModelMock = $this->createPartialMock(
  36. \Magento\Directory\Model\Country::class,
  37. ['setId', 'getLoadedRegionCollection', 'toOptionArray', '__wakeup']
  38. );
  39. $formMock = $this->createPartialMock(\Magento\Framework\Data\Form::class, ['getElement']);
  40. $elementMock->expects($this->any())->method('getForm')->will($this->returnValue($formMock));
  41. $elementMock->expects(
  42. $this->any()
  43. )->method(
  44. 'getHtmlAttributes'
  45. )->will(
  46. $this->returnValue(
  47. [
  48. 'title',
  49. 'class',
  50. 'style',
  51. 'onclick',
  52. 'onchange',
  53. 'disabled',
  54. 'readonly',
  55. 'tabindex',
  56. 'placeholder',
  57. ]
  58. )
  59. );
  60. $objectManager = new ObjectManager($this);
  61. $escaper = $objectManager->getObject(\Magento\Framework\Escaper::class);
  62. $reflection = new \ReflectionClass($elementMock);
  63. $reflection_property = $reflection->getProperty('_escaper');
  64. $reflection_property->setAccessible(true);
  65. $reflection_property->setValue($elementMock, $escaper);
  66. $formMock->expects(
  67. $this->any()
  68. )->method(
  69. 'getElement'
  70. )->will(
  71. $this->returnValueMap([['country_id', $countryMock], ['region_id', $regionMock]])
  72. );
  73. $countryMock->expects($this->any())->method('getValue')->will($this->returnValue('GE'));
  74. $directoryHelperMock->expects(
  75. $this->any()
  76. )->method(
  77. 'isRegionRequired'
  78. )->will(
  79. $this->returnValueMap([['GE', true]])
  80. );
  81. $countryFactoryMock->expects($this->once())->method('create')->will($this->returnValue($countryModelMock));
  82. $countryModelMock->expects($this->any())->method('setId')->will($this->returnSelf());
  83. $countryModelMock->expects($this->any())->method('getLoadedRegionCollection')->will($this->returnSelf());
  84. $countryModelMock->expects($this->any())->method('toOptionArray')->will($this->returnValue($regionCollection));
  85. $model = new \Magento\Customer\Model\Renderer\Region($countryFactoryMock, $directoryHelperMock, $escaperMock);
  86. $static = new \ReflectionProperty(\Magento\Customer\Model\Renderer\Region::class, '_regionCollections');
  87. $static->setAccessible(true);
  88. $static->setValue([]);
  89. $html = $model->render($elementMock);
  90. $this->assertContains('required', $html);
  91. $this->assertContains('required-entry', $html);
  92. }
  93. /**
  94. * @return array
  95. */
  96. public function renderDataProvider()
  97. {
  98. return [
  99. 'with no defined regions' => [[]],
  100. 'with defined regions' => [
  101. [
  102. new \Magento\Framework\DataObject(['value' => 'Bavaria']),
  103. new \Magento\Framework\DataObject(['value' => 'Saxony']),
  104. ],
  105. ]
  106. ];
  107. }
  108. }