Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FrontendExecutor.php
Go to the documentation of this file.
1 <?php
8 
12 
17 {
23  private $transport;
24 
30  private $formKey = null;
31 
37  private $response;
38 
44  private $cookies = '';
45 
51  private $customerEmail;
52 
58  private $customerPassword;
59 
68  public function __construct($customerEmail, $customerPassWord)
69  {
70  if (!isset(parent::$baseUrl)) {
71  parent::resolveBaseUrl();
72  }
73  $this->transport = new CurlTransport();
74  $this->customerEmail = $customerEmail;
75  $this->customerPassword = $customerPassWord;
76  $this->authorize();
77  }
78 
85  private function authorize()
86  {
87  $url = parent::$baseUrl . 'customer/account/login/';
88  $this->transport->write($url);
89  $this->read();
90 
91  $url = parent::$baseUrl . 'customer/account/loginPost/';
92  $data = [
93  'login[username]' => $this->customerEmail,
94  'login[password]' => $this->customerPassword,
95  'form_key' => $this->formKey,
96  ];
97  $this->transport->write($url, $data, CurlInterface::POST, ['Set-Cookie:' . $this->cookies]);
98  $response = $this->read();
99  if (strpos($response, 'customer/account/login')) {
100  throw new TestFrameworkException($this->customerEmail . ', cannot be logged in by curl handler!');
101  }
102  }
103 
109  private function setFormKey()
110  {
111  $str = substr($this->response, strpos($this->response, 'form_key'));
112  preg_match('/value="(.*)" \/>/', $str, $matches);
113  if (!empty($matches[1])) {
114  $this->formKey = $matches[1];
115  }
116  }
117 
123  protected function setCookies()
124  {
125  preg_match_all('|Set-Cookie: (.*);|U', $this->response, $matches);
126  if (!empty($matches[1])) {
127  $this->cookies = implode('; ', $matches[1]);
128  }
129  }
130 
141  public function write($url, $data = [], $method = CurlInterface::POST, $headers = [])
142  {
143  if (isset($data['customer_email'])) {
144  unset($data['customer_email']);
145  }
146  if (isset($data['customer_password'])) {
147  unset($data['customer_password']);
148  }
149  $apiUrl = parent::$baseUrl . $url;
150  if ($this->formKey) {
151  $data['form_key'] = $this->formKey;
152  } else {
153  throw new TestFrameworkException(
154  sprintf('Form key is absent! Url: "%s" Response: "%s"', $apiUrl, $this->response)
155  );
156  }
157  $headers = ['Set-Cookie:' . $this->cookies];
158  $this->transport->write($apiUrl, str_replace('null', '', http_build_query($data)), $method, $headers);
159  }
160 
169  public function read($successRegex = null, $returnRegex = null)
170  {
171  $this->response = $this->transport->read();
172  $this->setCookies();
173  $this->setFormKey();
174 
175  if (!empty($successRegex)) {
176  preg_match($successRegex, $this->response, $successMatches);
177  if (empty($successMatches)) {
178  throw new TestFrameworkException("Entity creation was not successful! Response: $this->response");
179  }
180  }
181 
182  if (!empty($returnRegex)) {
183  preg_match($returnRegex, $this->response, $returnMatches);
184  if (!empty($returnMatches)) {
185  return $returnMatches;
186  }
187  }
188  return $this->response;
189  }
190 
198  public function addOption($option, $value)
199  {
200  $this->transport->addOption($option, $value);
201  }
202 
208  public function close()
209  {
210  $this->transport->close();
211  }
212 }
$value
Definition: gender.phtml:16
write($url, $data=[], $method=CurlInterface::POST, $headers=[])
$method
Definition: info.phtml:13