FormTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Metadata;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. class FormTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var Form
  12. */
  13. protected $_form;
  14. /** @var array */
  15. protected $_attributes;
  16. /** @var \Magento\Framework\App\RequestInterface */
  17. protected $_request;
  18. /** @var array */
  19. protected $_expected;
  20. /** @var array */
  21. protected $_requestData = [];
  22. public function setUp()
  23. {
  24. $objectManager = Bootstrap::getObjectManager();
  25. /** @var FormFactory $formFactory */
  26. $formFactory = $objectManager->create(\Magento\Customer\Model\Metadata\FormFactory::class);
  27. $this->_form = $formFactory->create('customer_address', 'customer_address_edit');
  28. $this->_attributes = [
  29. 'id' => 14,
  30. 'default_shipping' => 1,
  31. 'default_billing' => 0,
  32. 'company' => 'Company Name',
  33. 'middlename' => 'Mid',
  34. 'prefix' => 'Mr.',
  35. 'suffix' => 'Esq.',
  36. 'vat_id' => '',
  37. 'firstname' => 'Jane',
  38. 'lastname' => 'Doe',
  39. 'street' => ['2211 North First Street'],
  40. 'city' => 'San Jose',
  41. 'country_id' => 'US',
  42. 'postcode' => '95131',
  43. 'telephone' => '5125125125',
  44. 'region_id' => 12,
  45. 'region' => 'California',
  46. ];
  47. $requestData = [
  48. 'company' => 'Company Name',
  49. 'middlename' => 'Mid',
  50. 'prefix' => 'Mr.',
  51. 'suffix' => 'Esq.',
  52. 'vat_id' => '',
  53. 'firstname' => 'New Name',
  54. 'lastname' => 'Doe',
  55. 'street' => ['2211 New Street'],
  56. 'city' => 'San Jose',
  57. 'country_id' => 'US',
  58. 'postcode' => '95131',
  59. 'telephone' => '5125125125',
  60. 'region_id' => 12,
  61. 'region' => 'California',
  62. ];
  63. $this->_request = $objectManager->get(\Magento\Framework\App\RequestInterface::class);
  64. $this->_request->setParams($requestData);
  65. $this->_expected = array_merge($this->_attributes, $requestData);
  66. unset($this->_expected['id']);
  67. unset($this->_expected['default_shipping']);
  68. unset($this->_expected['default_billing']);
  69. unset($this->_expected['middlename']);
  70. unset($this->_expected['prefix']);
  71. unset($this->_expected['suffix']);
  72. }
  73. /**
  74. * @magentoAppIsolation enabled
  75. */
  76. public function testCompactData()
  77. {
  78. $attributeValues = $this->_form->compactData($this->_form->extractData($this->_request));
  79. $this->assertEquals($this->_expected, $attributeValues);
  80. }
  81. /**
  82. * @magentoAppIsolation enabled
  83. */
  84. public function testGetAttributes()
  85. {
  86. $expectedAttributes = [
  87. 'prefix',
  88. 'firstname',
  89. 'middlename',
  90. 'lastname',
  91. 'suffix',
  92. 'company',
  93. 'street',
  94. 'city',
  95. 'country_id',
  96. 'region',
  97. 'region_id',
  98. 'postcode',
  99. 'telephone',
  100. 'fax',
  101. 'vat_id',
  102. ];
  103. $this->assertEquals($expectedAttributes, array_keys($this->_form->getAttributes()));
  104. }
  105. /**
  106. * @magentoAppIsolation enabled
  107. */
  108. public function testGetSystemAttributes()
  109. {
  110. $this->assertCount(15, $this->_form->getSystemAttributes());
  111. }
  112. /**
  113. * @magentoAppIsolation enabled
  114. * @magentoDataFixture Magento/Customer/_files/attribute_user_defined_address.php
  115. */
  116. public function testGetUserAttributes()
  117. {
  118. $expectedAttributes = ['address_user_attribute'];
  119. $this->assertEquals($expectedAttributes, array_keys($this->_form->getUserAttributes()));
  120. }
  121. /**
  122. * @magentoAppIsolation enabled
  123. */
  124. public function testRestoreData()
  125. {
  126. $attributeValues = $this->_form->restoreData($this->_form->extractData($this->_request));
  127. $this->assertEquals($this->_expected, $attributeValues);
  128. }
  129. }