diff core/tests/Drupal/Tests/XdebugRequestTrait.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/tests/Drupal/Tests/XdebugRequestTrait.php	Thu Jul 05 14:24:15 2018 +0000
@@ -0,0 +1,48 @@
+<?php
+
+namespace Drupal\Tests;
+
+use Symfony\Component\HttpFoundation\Request;
+
+trait XdebugRequestTrait {
+
+  /**
+   * Adds xdebug cookies, from request setup.
+   *
+   * In order to debug web tests you need to either set a cookie, have the
+   * Xdebug session in the URL or set an environment variable in case of CLI
+   * requests. If the developer listens to connection on the parent site, by
+   * default the cookie is not forwarded to the client side, so you cannot
+   * debug the code running on the child site. In order to make debuggers work
+   * this bit of information is forwarded. Make sure that the debugger listens
+   * to at least three external connections.
+   *
+   * @param \Symfony\Component\HttpFoundation\Request $request
+   *   The request.
+   *
+   * @return array
+   *   The extracted cookies.
+   */
+  protected function extractCookiesFromRequest(Request $request) {
+    $cookie_params = $request->cookies;
+    $cookies = [];
+    if ($cookie_params->has('XDEBUG_SESSION')) {
+      $cookies['XDEBUG_SESSION'][] = $cookie_params->get('XDEBUG_SESSION');
+    }
+    // For CLI requests, the information is stored in $_SERVER.
+    $server = $request->server;
+    if ($server->has('XDEBUG_CONFIG')) {
+      // $_SERVER['XDEBUG_CONFIG'] has the form "key1=value1 key2=value2 ...".
+      $pairs = explode(' ', $server->get('XDEBUG_CONFIG'));
+      foreach ($pairs as $pair) {
+        list($key, $value) = explode('=', $pair);
+        // Account for key-value pairs being separated by multiple spaces.
+        if (trim($key, ' ') == 'idekey') {
+          $cookies['XDEBUG_SESSION'][] = trim($value, ' ');
+        }
+      }
+    }
+    return $cookies;
+  }
+
+}