PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/functional/tests/app/Magento/Backend/Test/Handler/Conditions.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 280 lines | 158 code | 19 blank | 103 comment | 11 complexity | 8f35f3ffc908fded0615db5a71b02a1d MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 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. 'greater than' => '>',
  52. 'equals or greater than' => '>=',
  53. ],
  54. 'value_type' => [
  55. 'same_as' => 'the Same as Matched Product Categories',
  56. ],
  57. 'value' => [
  58. 'California' => '12',
  59. 'United States' => 'US',
  60. '[flatrate] Fixed' => 'flatrate_flatrate',
  61. 'FOUND' => '1',
  62. 'TRUE' => '1',
  63. ],
  64. 'aggregator' => [
  65. 'ALL' => 'all',
  66. 'ANY' => 'any',
  67. ],
  68. 'attribute'=> [
  69. 'total quantity' => 'qty',
  70. ],
  71. ];
  72. /**
  73. * Map encode special chars
  74. *
  75. * @var array
  76. */
  77. protected $encodeChars = [
  78. '\{' => '&lbrace;',
  79. '\}' => '&rbrace;',
  80. '\[' => '&lbracket;',
  81. '\]' => '&rbracket;',
  82. '\:' => '&colon;',
  83. ];
  84. /**
  85. * Map decode special chars
  86. *
  87. * @var array
  88. */
  89. protected $decodeChars = [
  90. '&lbrace;' => '{',
  91. '&rbrace;' => '}',
  92. '&lbracket;' => '[',
  93. '&rbracket;' => ']',
  94. '&colon;' => ':',
  95. ];
  96. /**
  97. * Prepare conditions to array for send by post request
  98. *
  99. * @param string $conditions
  100. * @return array
  101. */
  102. protected function prepareCondition($conditions)
  103. {
  104. $decodeConditions = empty($conditions)
  105. ? $this->decodeValue("[Conditions combination]")
  106. : $this->decodeValue("{Conditions combination:[{$conditions}]}");
  107. return $this->convertMultipleCondition($decodeConditions);
  108. }
  109. /**
  110. * Convert condition combination
  111. *
  112. * @param string $combination
  113. * @param array|string $conditions
  114. * @param int $nesting
  115. * @return array
  116. */
  117. private function convertConditionsCombination($combination, $conditions, $nesting)
  118. {
  119. $combination = [$nesting => $this->convertSingleCondition($combination)];
  120. $conditions = $this->convertMultipleCondition($conditions, $nesting, 1);
  121. return $combination + $conditions;
  122. }
  123. /**
  124. * Convert multiple condition
  125. *
  126. * @param array $conditions
  127. * @param int $nesting
  128. * @param int $count
  129. * @return array
  130. */
  131. private function convertMultipleCondition(array $conditions, $nesting = 1, $count = 0)
  132. {
  133. $result = [];
  134. foreach ($conditions as $key => $condition) {
  135. $curNesting = $nesting . ($count ? ('--' . $count) : '');
  136. if (!is_numeric($key)) {
  137. $result += $this->convertConditionsCombination($key, $condition, $curNesting);
  138. } elseif (is_string($condition)) {
  139. $result[$curNesting] = $this->convertSingleCondition($condition);
  140. } else {
  141. $result += $this->convertMultipleCondition($condition, $nesting, $count);
  142. }
  143. $count++;
  144. }
  145. return $result;
  146. }
  147. /**
  148. * Convert single condition
  149. *
  150. * @param string $condition
  151. * @return array
  152. * @throws \Exception
  153. */
  154. private function convertSingleCondition($condition)
  155. {
  156. $condition = $this->parseCondition($condition);
  157. extract($condition);
  158. if (isset($param)) {
  159. $typeParam = $this->getTypeParam($param);
  160. $typeParam['attribute'] = $type;
  161. } else {
  162. $typeParam = $this->getTypeParam($type);
  163. }
  164. if (empty($typeParam)) {
  165. throw new \Exception("Can't find type param \"{$type}\".");
  166. }
  167. $ruleParam = [];
  168. foreach ($rules as $value) {
  169. $param = $this->getRuleParam($value);
  170. if (empty($param)) {
  171. $ruleParam['value'] = $value;
  172. break;
  173. }
  174. $ruleParam += $param;
  175. }
  176. if (count($ruleParam) != count($rules)) {
  177. throw new \Exception(
  178. "Can't find all params. "
  179. . "\nSearch: " . implode(', ', $rules) . " "
  180. . "\nFind: " . implode(', ', $ruleParam)
  181. );
  182. }
  183. return $ruleParam + $typeParam;
  184. }
  185. /**
  186. * Get type param by name
  187. *
  188. * @param string $name
  189. * @return array
  190. */
  191. private function getTypeParam($name)
  192. {
  193. return isset($this->mapTypeParams[$name]) ? $this->mapTypeParams[$name] : [];
  194. }
  195. /**
  196. * Get rule param by name
  197. *
  198. * @param string $name
  199. * @return array
  200. */
  201. private function getRuleParam($name)
  202. {
  203. foreach ($this->mapRuleParams as $typeParam => &$params) {
  204. if (isset($params[$name])) {
  205. return [$typeParam => $params[$name]];
  206. }
  207. }
  208. return [];
  209. }
  210. /**
  211. * Decode value
  212. *
  213. * @param string $value
  214. * @return array
  215. * @throws \Exception
  216. */
  217. private function decodeValue($value)
  218. {
  219. $value = str_replace(array_keys($this->encodeChars), $this->encodeChars, $value);
  220. $value = preg_replace('/(\]|})({|\[)/', '$1,$2', $value);
  221. $value = preg_replace('/{([^:]+):/', '{"$1":', $value);
  222. $value = preg_replace('/\[([^\[{])/', '"$1', $value);
  223. $value = preg_replace('/([^\]}])\]/', '$1"', $value);
  224. $value = str_replace(array_keys($this->decodeChars), $this->decodeChars, $value);
  225. $value = "[{$value}]";
  226. $value = json_decode($value, true);
  227. if (null === $value) {
  228. throw new \Exception('Bad format value.');
  229. }
  230. return $value;
  231. }
  232. /**
  233. * Parse condition
  234. *
  235. * @param string $condition
  236. * @return array
  237. * @throws \Exception
  238. */
  239. private function parseCondition($condition)
  240. {
  241. if (!preg_match_all('/([^|]+\|?)/', $condition, $match)) {
  242. throw new \Exception('Bad format condition');
  243. }
  244. foreach ($match[1] as $key => $value) {
  245. $match[1][$key] = rtrim($value, '|');
  246. }
  247. $param = $match[1][0];
  248. $type = array_shift($match[1]);
  249. if (count($match[1]) == 3) {
  250. $type = array_shift($match[1]);
  251. } else {
  252. $param = null;
  253. }
  254. return [
  255. 'type' => $type,
  256. 'rules' => array_values($match[1]),
  257. 'param' => $param
  258. ];
  259. }
  260. }