PaymentSectionModifierTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Model\Config\Structure;
  7. use Magento\Paypal\Model\Config\Structure\PaymentSectionModifier;
  8. class PaymentSectionModifierTest extends \PHPUnit\Framework\TestCase
  9. {
  10. private static $specialGroups = [
  11. 'account',
  12. 'recommended_solutions',
  13. 'other_paypal_payment_solutions',
  14. 'other_payment_methods',
  15. ];
  16. /**
  17. * @param string $case
  18. * @param array $structure
  19. * @dataProvider caseProvider
  20. */
  21. public function testSpecialGroupsPresent($case, $structure)
  22. {
  23. $modifier = new PaymentSectionModifier();
  24. $modifiedStructure = $modifier->modify($structure);
  25. $presentSpecialGroups = array_intersect(
  26. self::$specialGroups,
  27. array_keys($modifiedStructure)
  28. );
  29. $this->assertEquals(
  30. self::$specialGroups,
  31. $presentSpecialGroups,
  32. sprintf('All special groups must be present in %s case', $case)
  33. );
  34. }
  35. /**
  36. * @param string $case
  37. * @param array $structure
  38. * @dataProvider caseProvider
  39. */
  40. public function testOnlySpecialGroupsPresent($case, $structure)
  41. {
  42. $modifier = new PaymentSectionModifier();
  43. $modifiedStructure = $modifier->modify($structure);
  44. $presentNotSpecialGroups = array_diff(
  45. array_keys($modifiedStructure),
  46. self::$specialGroups
  47. );
  48. $this->assertEquals(
  49. [],
  50. $presentNotSpecialGroups,
  51. sprintf('Only special groups should be present at top level in "%s" case', $case)
  52. );
  53. }
  54. /**
  55. * @param string $case
  56. * @param array $structure
  57. * @dataProvider caseProvider
  58. */
  59. public function testGroupsNotRemovedAfterModification($case, $structure)
  60. {
  61. $modifier = new PaymentSectionModifier();
  62. $modifiedStructure = $modifier->modify($structure);
  63. $removedGroups = array_diff(
  64. $this->fetchAllAvailableGroups($structure),
  65. $this->fetchAllAvailableGroups($modifiedStructure)
  66. );
  67. $this->assertEquals(
  68. [],
  69. $removedGroups,
  70. sprintf('Groups should not be removed after modification in "%s" case', $case)
  71. );
  72. }
  73. public function testMovedToTargetSpecialGroup()
  74. {
  75. $structure = [
  76. 'some_payment_method1' => [
  77. 'id' => 'some_payment_method1',
  78. 'displayIn' => 'recommended_solutions',
  79. ],
  80. 'some_group' => [
  81. 'id' => 'some_group',
  82. 'children' => [
  83. 'some_payment_method2' => [
  84. 'id' => 'some_payment_method2',
  85. 'displayIn' => 'recommended_solutions'
  86. ],
  87. 'some_payment_method3' => [
  88. 'id' => 'some_payment_method3',
  89. 'displayIn' => 'other_payment_methods'
  90. ],
  91. 'some_payment_method4' => [
  92. 'id' => 'some_payment_method4',
  93. 'displayIn' => 'recommended_solutions'
  94. ],
  95. 'some_payment_method5' => [
  96. 'id' => 'some_payment_method5',
  97. ],
  98. ]
  99. ],
  100. ];
  101. $modifier = new PaymentSectionModifier();
  102. $modifiedStructure = $modifier->modify($structure);
  103. $this->assertEquals(
  104. [
  105. 'account' => [],
  106. 'recommended_solutions' => [
  107. 'children' => [
  108. 'some_payment_method1' => [
  109. 'id' => 'some_payment_method1',
  110. ],
  111. 'some_payment_method2' => [
  112. 'id' => 'some_payment_method2',
  113. ],
  114. 'some_payment_method4' => [
  115. 'id' => 'some_payment_method4',
  116. ],
  117. ],
  118. ],
  119. 'other_paypal_payment_solutions' => [],
  120. 'other_payment_methods' => [
  121. 'children' => [
  122. 'some_payment_method3' => [
  123. 'id' => 'some_payment_method3',
  124. ],
  125. 'some_group' => [
  126. 'id' => 'some_group',
  127. 'children' => [
  128. 'some_payment_method5' => [
  129. 'id' => 'some_payment_method5',
  130. ],
  131. ],
  132. ],
  133. ],
  134. ],
  135. ],
  136. $modifiedStructure,
  137. 'Some group is not moved correctly'
  138. );
  139. }
  140. /**
  141. * This helper method walks recursively through configuration structure and
  142. * collect available configuration groups
  143. *
  144. * @param array $structure
  145. * @return array Sorted list of group identifiers
  146. */
  147. private function fetchAllAvailableGroups($structure)
  148. {
  149. $availableGroups = [];
  150. foreach ($structure as $group => $data) {
  151. $availableGroups[] = $group;
  152. if (isset($data['children'])) {
  153. $availableGroups = array_merge(
  154. $availableGroups,
  155. $this->fetchAllAvailableGroups($data['children'])
  156. );
  157. }
  158. }
  159. $availableGroups = array_values(array_unique($availableGroups));
  160. sort($availableGroups);
  161. return $availableGroups;
  162. }
  163. /**
  164. * @return mixed
  165. */
  166. public function caseProvider()
  167. {
  168. return include __DIR__ . '/_files/payment_section_structure_variations.php';
  169. }
  170. }