XdebugRequestTrait.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Drupal\Tests;
  3. use Symfony\Component\HttpFoundation\Request;
  4. trait XdebugRequestTrait {
  5. /**
  6. * Adds xdebug cookies, from request setup.
  7. *
  8. * In order to debug web tests you need to either set a cookie, have the
  9. * Xdebug session in the URL or set an environment variable in case of CLI
  10. * requests. If the developer listens to connection on the parent site, by
  11. * default the cookie is not forwarded to the client side, so you cannot
  12. * debug the code running on the child site. In order to make debuggers work
  13. * this bit of information is forwarded. Make sure that the debugger listens
  14. * to at least three external connections.
  15. *
  16. * @param \Symfony\Component\HttpFoundation\Request $request
  17. * The request.
  18. *
  19. * @return array
  20. * The extracted cookies.
  21. */
  22. protected function extractCookiesFromRequest(Request $request) {
  23. $cookie_params = $request->cookies;
  24. $cookies = [];
  25. if ($cookie_params->has('XDEBUG_SESSION')) {
  26. $cookies['XDEBUG_SESSION'][] = $cookie_params->get('XDEBUG_SESSION');
  27. }
  28. // For CLI requests, the information is stored in $_SERVER.
  29. $server = $request->server;
  30. if ($server->has('XDEBUG_CONFIG')) {
  31. // $_SERVER['XDEBUG_CONFIG'] has the form "key1=value1 key2=value2 ...".
  32. $pairs = explode(' ', $server->get('XDEBUG_CONFIG'));
  33. foreach ($pairs as $pair) {
  34. list($key, $value) = explode('=', $pair);
  35. // Account for key-value pairs being separated by multiple spaces.
  36. if (trim($key, ' ') == 'idekey') {
  37. $cookies['XDEBUG_SESSION'][] = trim($value, ' ');
  38. }
  39. }
  40. }
  41. return $cookies;
  42. }
  43. }