AttributeMergerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Checkout\Test\Unit\Block\Checkout;
  8. use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
  9. use Magento\Customer\Helper\Address as AddressHelper;
  10. use Magento\Customer\Model\Session as CustomerSession;
  11. use Magento\Directory\Helper\Data as DirectoryHelper;
  12. use Magento\Checkout\Block\Checkout\AttributeMerger;
  13. use PHPUnit\Framework\TestCase;
  14. class AttributeMergerTest extends TestCase
  15. {
  16. /**
  17. * @var CustomerRepository
  18. */
  19. private $customerRepository;
  20. /**
  21. * @var CustomerSession
  22. */
  23. private $customerSession;
  24. /**
  25. * @var AddressHelper
  26. */
  27. private $addressHelper;
  28. /**
  29. * @var DirectoryHelper
  30. */
  31. private $directoryHelper;
  32. /**
  33. * @var AttributeMerger
  34. */
  35. private $attributeMerger;
  36. /**
  37. * @inheritdoc
  38. */
  39. protected function setUp()
  40. {
  41. $this->customerRepository = $this->createMock(CustomerRepository::class);
  42. $this->customerSession = $this->createMock(CustomerSession::class);
  43. $this->addressHelper = $this->createMock(AddressHelper::class);
  44. $this->directoryHelper = $this->createMock(DirectoryHelper::class);
  45. $this->attributeMerger = new AttributeMerger(
  46. $this->addressHelper,
  47. $this->customerSession,
  48. $this->customerRepository,
  49. $this->directoryHelper
  50. );
  51. }
  52. /**
  53. * Tests of element attributes merging.
  54. *
  55. * @param String $validationRule - validation rule.
  56. * @param String $expectedValidation - expected mapped validation.
  57. * @dataProvider validationRulesDataProvider
  58. */
  59. public function testMerge(String $validationRule, String $expectedValidation): void
  60. {
  61. $elements = [
  62. 'field' => [
  63. 'visible' => true,
  64. 'formElement' => 'input',
  65. 'label' => __('City'),
  66. 'value' => null,
  67. 'sortOrder' => 1,
  68. 'validation' => [
  69. 'input_validation' => $validationRule
  70. ],
  71. ]
  72. ];
  73. $actualResult = $this->attributeMerger->merge(
  74. $elements,
  75. 'provider',
  76. 'dataScope',
  77. ['field' =>
  78. [
  79. 'validation' => ['length' => true]
  80. ]
  81. ]
  82. );
  83. $expectedResult = [
  84. $expectedValidation => true,
  85. 'length' => true
  86. ];
  87. self::assertEquals($expectedResult, $actualResult['field']['validation']);
  88. }
  89. /**
  90. * Provides possible validation types.
  91. *
  92. * @return array
  93. */
  94. public function validationRulesDataProvider(): array
  95. {
  96. return [
  97. ['alpha', 'validate-alpha'],
  98. ['numeric', 'validate-number'],
  99. ['alphanumeric', 'validate-alphanum'],
  100. ['alphanum-with-spaces', 'validate-alphanum-with-spaces'],
  101. ['url', 'validate-url'],
  102. ['email', 'email2'],
  103. ['length', 'validate-length']
  104. ];
  105. }
  106. }