Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WebapiDecorator.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Integration\Test\Fixture\Integration;
10 use Magento\Mtf\Config\DataInterface;
11 use Magento\Mtf\Fixture\FixtureFactory;
12 use Magento\Mtf\ObjectManager;
13 use Magento\Mtf\Util\Protocol\CurlInterface;
15 
21 class WebapiDecorator implements CurlInterface
22 {
26  const CONFIG_TOKEN_PATH = 'handler/0/webapi/0/token/0/value';
27 
33  protected $objectManager;
34 
40  protected $transport;
41 
47  protected $configuration;
48 
54  protected $fixtureFactory;
55 
61  protected $headers = [
62  'Accept: application/json',
63  'Content-Type:application/json',
64  ];
65 
71  protected $response;
72 
80  public function __construct(
81  ObjectManager $objectManager,
82  CurlTransport $transport,
83  DataInterface $configuration,
84  FixtureFactory $fixtureFactory
85  ) {
86  $this->objectManager = $objectManager;
87  $this->transport = $transport;
88  $this->configuration = $configuration;
89  $this->fixtureFactory = $fixtureFactory;
90 
91  $this->init();
92  }
93 
99  protected function init()
100  {
101  $integrationToken = $this->configuration->get(self::CONFIG_TOKEN_PATH);
102 
103  if (null === $integrationToken || !$this->isValidIntegration()) {
104  $this->disableSecretKey();
106  $integration = $this->fixtureFactory->create(
107  \Magento\Integration\Test\Fixture\Integration::class,
108  ['dataset' => 'default_active']
109  );
110  $integration->persist();
111 
113  }
114  }
115 
121  protected function disableSecretKey()
122  {
123  $config = $this->fixtureFactory->create(
124  \Magento\Config\Test\Fixture\ConfigData::class,
125  ['dataset' => 'secret_key_disable']
126  );
127  $config->persist();
128  }
129 
136  protected function setConfiguration(Integration $integration)
137  {
138  $fileConfig = MTF_BP . '/etc/config.xml';
139  $dom = new \DOMDocument();
140  if (!file_exists($fileConfig)) {
141  copy(MTF_BP . '/etc/config.xml.dist', $fileConfig);
142  }
143  $dom->load($fileConfig);
144 
145  $webapiToken = (new \DOMXPath($dom))->query('//config/handler/webapi/token')->item(0);
146  if ($webapiToken) {
147  $webapiToken->nodeValue = $integration->getToken();
148  } else {
149  $webapi = (new \DOMXPath($dom))->query('//config/handler/webapi')->item(0);
150  $webapi->appendChild($dom->createElement('token', $integration->getToken()));
151  }
152 
153  $dom->save($fileConfig);
154  $this->configuration = $this->objectManager->create(\Magento\Mtf\Config\DataInterface::class);
155  }
156 
162  protected function isValidIntegration()
163  {
164  $this->write($_ENV['app_frontend_url'] . 'rest/V1/modules', [], CurlInterface::GET);
165  $response = json_decode($this->read(), true);
166 
167  return (null !== $response) && !isset($response['message']);
168  }
169 
179  public function write($url, $params = [], $method = CurlInterface::POST, $headers = [])
180  {
181  $headers = array_merge(
182  ['Authorization: Bearer ' . $this->configuration->get(self::CONFIG_TOKEN_PATH)],
184  $headers
185  );
186 
187  $this->transport->write($url, json_encode($params), $method, $headers);
188  }
189 
195  public function read()
196  {
197  $this->response = $this->transport->read();
198  return $this->response;
199  }
200 
208  public function addOption($option, $value)
209  {
210  $this->transport->addOption($option, $value);
211  }
212 
218  public function close()
219  {
220  $this->transport->close();
221  }
222 }
$config
Definition: fraud_order.php:17
$value
Definition: gender.phtml:16
defined('MTF_TESTS_PATH')||define('MTF_TESTS_PATH' MTF_BP
Definition: bootstrap.php:10
__construct(ObjectManager $objectManager, CurlTransport $transport, DataInterface $configuration, FixtureFactory $fixtureFactory)
write($url, $params=[], $method=CurlInterface::POST, $headers=[])
$method
Definition: info.phtml:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18