/vendor/magento/magento2-base/dev/tests/functional/tests/app/Magento/Backend/Test/Handler/Conditions.php

https://gitlab.com/daigiangaitu91/magento · PHP · 259 lines · 137 code · 19 blank · 103 comment · 6 complexity · 4b6c870769a42a0cff99b3d9f8544952 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2015 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Handler;
  7. use Magento\Mtf\Handler\Curl;
  8. /**
  9. * Class Conditions
  10. * Curl class for fixture with conditions
  11. *
  12. * Format value of conditions.
  13. * Add slash to symbols: "{", "}", "[", "]", ":".
  14. * 1. Single condition:
  15. * [Type|Param|Param|...|Param]
  16. * 2. List conditions:
  17. * [Type|Param|Param|...|Param]
  18. * [Type|Param|Param|...|Param]
  19. * [Type|Param|Param|...|Param]
  20. * 3. Combination condition with single condition
  21. * {Type|Param|Param|...|Param:[Type|Param|Param|...|Param]}
  22. * 4. Combination condition with list conditions
  23. * {Type|Param|Param|...|Param:[[Type|Param|...|Param][Type|Param|...|Param]...[Type|Param|...|Param]]}
  24. *
  25. * Example value:
  26. * {Products subselection|total amount|greater than|135|ANY:[[Price in cart|is|100][Quantity in cart|is|100]]}
  27. * {Conditions combination:[
  28. * [Subtotal|is|100]
  29. * {Product attribute combination|NOT FOUND|ANY:[[Attribute Set|is|Default][Attribute Set|is|Default]]}
  30. * ]}
  31. */
  32. abstract class Conditions extends Curl
  33. {
  34. /**
  35. * Map of type parameter
  36. *
  37. * @var array
  38. */
  39. protected $mapTypeParams = [];
  40. /**
  41. * Map of rule parameters
  42. *
  43. * @var array
  44. */
  45. protected $mapRuleParams = [
  46. 'operator' => [
  47. 'is' => '==',
  48. 'is not' => '!=',
  49. 'equal to' => '==',
  50. 'matches' => '==',
  51. ],
  52. 'value_type' => [
  53. 'same_as' => 'the Same as Matched Product Categories',
  54. ],
  55. 'value' => [
  56. 'California' => '12',
  57. 'United States' => 'US',
  58. '[flatrate] Fixed' => 'flatrate_flatrate',
  59. ],
  60. 'aggregator' => [
  61. 'ALL' => 'all',
  62. ],
  63. ];
  64. /**
  65. * Map encode special chars
  66. *
  67. * @var array
  68. */
  69. protected $encodeChars = [
  70. '\{' => '&lbrace;',
  71. '\}' => '&rbrace;',
  72. '\[' => '&lbracket;',
  73. '\]' => '&rbracket;',
  74. '\:' => '&colon;',
  75. ];
  76. /**
  77. * Map decode special chars
  78. *
  79. * @var array
  80. */
  81. protected $decodeChars = [
  82. '&lbrace;' => '{',
  83. '&rbrace;' => '}',
  84. '&lbracket;' => '[',
  85. '&rbracket;' => ']',
  86. '&colon;' => ':',
  87. ];
  88. /**
  89. * Prepare conditions to array for send by post request
  90. *
  91. * @param string $conditions
  92. * @return array
  93. */
  94. protected function prepareCondition($conditions)
  95. {
  96. $decodeConditions = empty($conditions)
  97. ? $this->decodeValue("[Conditions combination]")
  98. : $this->decodeValue("{Conditions combination:[{$conditions}]}");
  99. return $this->convertMultipleCondition($decodeConditions);
  100. }
  101. /**
  102. * Convert condition combination
  103. *
  104. * @param string $combination
  105. * @param array|string $conditions
  106. * @param int $nesting
  107. * @return array
  108. */
  109. private function convertConditionsCombination($combination, $conditions, $nesting)
  110. {
  111. $combination = [$nesting => $this->convertSingleCondition($combination)];
  112. $conditions = $this->convertMultipleCondition($conditions, $nesting, 1);
  113. return $combination + $conditions;
  114. }
  115. /**
  116. * Convert multiple condition
  117. *
  118. * @param array $conditions
  119. * @param int $nesting
  120. * @param int $count
  121. * @return array
  122. */
  123. private function convertMultipleCondition(array $conditions, $nesting = 1, $count = 0)
  124. {
  125. $result = [];
  126. foreach ($conditions as $key => $condition) {
  127. $curNesting = $nesting . ($count ? ('--' . $count) : '');
  128. if (!is_numeric($key)) {
  129. $result += $this->convertConditionsCombination($key, $condition, $curNesting);
  130. } elseif (is_string($condition)) {
  131. $result[$curNesting] = $this->convertSingleCondition($condition);
  132. } else {
  133. $result += $this->convertMultipleCondition($condition, $nesting, $count);
  134. }
  135. $count++;
  136. }
  137. return $result;
  138. }
  139. /**
  140. * Convert single condition
  141. *
  142. * @param string $condition
  143. * @return array
  144. * @throws \Exception
  145. */
  146. private function convertSingleCondition($condition)
  147. {
  148. $condition = $this->parseCondition($condition);
  149. extract($condition);
  150. $typeParam = $this->getTypeParam($type);
  151. if (empty($typeParam)) {
  152. throw new \Exception("Can't find type param \"{$type}\".");
  153. }
  154. $ruleParam = [];
  155. foreach ($rules as $value) {
  156. $param = $this->getRuleParam($value);
  157. if (empty($param)) {
  158. $ruleParam['value'] = $value;
  159. break;
  160. }
  161. $ruleParam += $param;
  162. }
  163. if (count($ruleParam) != count($rules)) {
  164. throw new \Exception(
  165. "Can't find all params. "
  166. . "\nSearch: " . implode(', ', $rules) . " "
  167. . "\nFind: " . implode(', ', $ruleParam)
  168. );
  169. }
  170. return $typeParam + $ruleParam;
  171. }
  172. /**
  173. * Get type param by name
  174. *
  175. * @param string $name
  176. * @return array
  177. */
  178. private function getTypeParam($name)
  179. {
  180. return isset($this->mapTypeParams[$name]) ? $this->mapTypeParams[$name] : [];
  181. }
  182. /**
  183. * Get rule param by name
  184. *
  185. * @param string $name
  186. * @return array
  187. */
  188. private function getRuleParam($name)
  189. {
  190. foreach ($this->mapRuleParams as $typeParam => &$params) {
  191. if (isset($params[$name])) {
  192. return [$typeParam => $params[$name]];
  193. }
  194. }
  195. return [];
  196. }
  197. /**
  198. * Decode value
  199. *
  200. * @param string $value
  201. * @return array
  202. * @throws \Exception
  203. */
  204. private function decodeValue($value)
  205. {
  206. $value = str_replace(array_keys($this->encodeChars), $this->encodeChars, $value);
  207. $value = preg_replace('/(\]|})({|\[)/', '$1,$2', $value);
  208. $value = preg_replace('/{([^:]+):/', '{"$1":', $value);
  209. $value = preg_replace('/\[([^\[{])/', '"$1', $value);
  210. $value = preg_replace('/([^\]}])\]/', '$1"', $value);
  211. $value = str_replace(array_keys($this->decodeChars), $this->decodeChars, $value);
  212. $value = "[{$value}]";
  213. $value = json_decode($value, true);
  214. if (null === $value) {
  215. throw new \Exception('Bad format value.');
  216. }
  217. return $value;
  218. }
  219. /**
  220. * Parse condition
  221. *
  222. * @param string $condition
  223. * @return array
  224. * @throws \Exception
  225. */
  226. private function parseCondition($condition)
  227. {
  228. if (!preg_match_all('/([^|]+\|?)/', $condition, $match)) {
  229. throw new \Exception('Bad format condition');
  230. }
  231. foreach ($match[1] as $key => $value) {
  232. $match[1][$key] = rtrim($value, '|');
  233. }
  234. return [
  235. 'type' => array_shift($match[1]),
  236. 'rules' => array_values($match[1]),
  237. ];
  238. }
  239. }