PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/functional/tests/app/Magento/Catalog/Test/Handler/CatalogAttributeSet/Curl.php

https://gitlab.com/axeltizon/magento-demopoweraccess
PHP | 196 lines | 105 code | 24 blank | 67 comment | 3 complexity | 2875c852921b4133cb258ffdde2d4bea MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Test\Handler\CatalogAttributeSet;
  7. use Magento\Catalog\Test\Fixture\CatalogAttributeSet;
  8. use Magento\Mtf\Fixture\FixtureInterface;
  9. use Magento\Mtf\Handler\Curl as AbstractCurl;
  10. use Magento\Mtf\Util\Protocol\CurlInterface;
  11. use Magento\Mtf\Util\Protocol\CurlTransport;
  12. use Magento\Mtf\Util\Protocol\CurlTransport\BackendDecorator;
  13. /**
  14. * Class Curl
  15. * Create new Attribute Set via curl
  16. */
  17. class Curl extends AbstractCurl implements CatalogAttributeSetInterface
  18. {
  19. /**
  20. * Regex for finding attribute set id
  21. *
  22. * @var string
  23. */
  24. protected $attributeSetId = '`http.*?product_set\/delete\/id\/(\d*?)\/`';
  25. /**
  26. * Regex for finding attributes
  27. *
  28. * @var string
  29. */
  30. protected $attributes = '/buildCategoryTree\(this.root, ([^;]+\}\])\);/s';
  31. /**
  32. * Regex for finding attribute set name
  33. *
  34. * @var string
  35. */
  36. protected $attributeSetName = '#id="attribute_set_name".*?value="([\w\d]+)"#s';
  37. /**
  38. * Post request for creating Attribute Set
  39. *
  40. * @param FixtureInterface|null $fixture
  41. * @return array
  42. *
  43. * @SuppressWarnings(PHPMD.NPathComplexity)
  44. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  45. */
  46. public function persist(FixtureInterface $fixture = null)
  47. {
  48. /** @var CatalogAttributeSet $fixture */
  49. $response = $fixture->hasData('attribute_set_id')
  50. ? $this->getDefaultAttributeSet($fixture)
  51. : $this->createAttributeSet($fixture);
  52. $attributeSetId = ($fixture->hasData('attribute_set_id'))
  53. ? $fixture->getAttributeSetId()
  54. : $this->getData($this->attributeSetId, $response);
  55. $assignedAttributes = $fixture->hasData('assigned_attributes')
  56. ? $fixture->getDataFieldConfig('assigned_attributes')['source']->getAttributes()
  57. : [];
  58. $dataAttribute = $this->getDataAttributes($response);
  59. $lastAttribute = array_pop($dataAttribute['attributes']);
  60. foreach ($assignedAttributes as $key => $assignedAttribute) {
  61. $dataAttribute['attributes'][] = [
  62. $assignedAttribute->getAttributeId(),
  63. $dataAttribute['groups'][0][0],
  64. ($lastAttribute[2] + ($key + 1)),
  65. null,
  66. ];
  67. }
  68. $this->updateAttributeSet($attributeSetId, $dataAttribute);
  69. return ['attribute_set_id' => $attributeSetId];
  70. }
  71. /**
  72. * Create Attribute Set
  73. *
  74. * @param CatalogAttributeSet $fixture
  75. * @return string
  76. */
  77. protected function createAttributeSet(CatalogAttributeSet $fixture)
  78. {
  79. $data = $fixture->getData();
  80. if (!isset($data['gotoEdit'])) {
  81. $data['gotoEdit'] = 1;
  82. }
  83. $data['skeleton_set'] = $fixture->getDataFieldConfig('skeleton_set')['source']->getAttributeSet()
  84. ->getAttributeSetId();
  85. $url = $_ENV['app_backend_url'] . 'catalog/product_set/save/';
  86. $curl = new BackendDecorator(new CurlTransport(), $this->_configuration);
  87. $curl->addOption(CURLOPT_HEADER, 1);
  88. $curl->write($url, $data);
  89. $response = $curl->read();
  90. $curl->close();
  91. return $response;
  92. }
  93. /**
  94. * Get Default Attribute Set page with curl
  95. *
  96. * @param CatalogAttributeSet $fixture
  97. * @return string
  98. */
  99. protected function getDefaultAttributeSet(CatalogAttributeSet $fixture)
  100. {
  101. $url = $_ENV['app_backend_url'] . 'catalog/product_set/edit/id/' . $fixture->getAttributeSetId() . '/';
  102. $curl = new BackendDecorator(new CurlTransport(), $this->_configuration);
  103. $curl->write($url, [], CurlInterface::GET);
  104. $response = $curl->read();
  105. $curl->close();
  106. return $response;
  107. }
  108. /**
  109. * Update Attribute Set
  110. *
  111. * @param int $attributeSetId
  112. * @param array $dataAttribute
  113. * @return void
  114. */
  115. protected function updateAttributeSet($attributeSetId, array $dataAttribute)
  116. {
  117. $data = ['data' => json_encode($dataAttribute)];
  118. $url = $_ENV['app_backend_url'] . 'catalog/product_set/save/id/' . $attributeSetId . '/';
  119. $curl = new BackendDecorator(new CurlTransport(), $this->_configuration);
  120. $curl->write($url, $data);
  121. $curl->read();
  122. $curl->close();
  123. }
  124. /**
  125. * Get data attributes for curl
  126. *
  127. * @param string $response
  128. * @return array
  129. */
  130. protected function getDataAttributes($response)
  131. {
  132. $attributes = $this->getData($this->attributes, $response, true);
  133. $dataAttribute = [];
  134. $index = 1;
  135. foreach ($attributes as $key => $parentAttributes) {
  136. $dataAttribute['groups'][$key][] = $parentAttributes['id'];
  137. $dataAttribute['groups'][$key][] = $parentAttributes['text'];
  138. $dataAttribute['groups'][$key][] = $key + 1;
  139. if (isset($parentAttributes['children'])) {
  140. foreach ($parentAttributes['children'] as $attribute) {
  141. $dataAttribute['attributes'][$index][] = $attribute['id'];
  142. $dataAttribute['attributes'][$index][] = $parentAttributes['id'];
  143. $dataAttribute['attributes'][$index][] = $index;
  144. $dataAttribute['attributes'][$index][] = $attribute['entity_id'];
  145. $index++;
  146. }
  147. }
  148. }
  149. $dataAttribute['not_attributes'] = [];
  150. $dataAttribute['removeGroups'] = [];
  151. $dataAttribute['attribute_set_name'] = $this->getData($this->attributeSetName, $response);
  152. return $dataAttribute;
  153. }
  154. /**
  155. * Select data from response by regular expression
  156. *
  157. * @param string $regularExpression
  158. * @param string $response
  159. * @param bool $isJson
  160. * @return mixed
  161. * @throws \Exception
  162. */
  163. protected function getData($regularExpression, $response, $isJson = false)
  164. {
  165. preg_match($regularExpression, $response, $matches);
  166. if (!isset($matches[1])) {
  167. throw new \Exception("Can't find data in response by regular expression \"{$regularExpression}\".");
  168. }
  169. return $isJson ? json_decode($matches[1], true) : $matches[1];
  170. }
  171. }