GuzzleMiddleware, HttpTestTrait - Add 'DEBUG' option
[civicrm-core.git] / Civi / Test / HttpTestTrait.php
index 8b28883a669ce85773dd2d1c4fddca1a2d3ad46a..cdc42c64de6fc0f6aa3d45e3edf05a0fe514421e 100644 (file)
@@ -3,6 +3,7 @@
 namespace Civi\Test;
 
 use GuzzleHttp\HandlerStack;
+use GuzzleHttp\MessageFormatter;
 use GuzzleHttp\Middleware;
 
 /**
@@ -38,9 +39,16 @@ trait HttpTestTrait {
    */
   protected function createGuzzle($options = []) {
     $handler = HandlerStack::create();
-    $handler->push(\CRM_Utils_GuzzleMiddleware::url(), 'civi_url');
+    $handler->unshift(\CRM_Utils_GuzzleMiddleware::url(), 'civi_url');
     $handler->push(Middleware::history($this->httpHistory), 'history');
 
+    if (getenv('DEBUG') >= 2) {
+      $handler->push(Middleware::log(new \CRM_Utils_EchoLogger(), new MessageFormatter(MessageFormatter::DEBUG)), 'log');
+    }
+    elseif (getenv('DEBUG') >= 1) {
+      $handler->push(\CRM_Utils_GuzzleMiddleware::curlLog(new \CRM_Utils_EchoLogger()), 'curl_log');
+    }
+
     $defaults = [
       'handler' => $handler,
       'base_uri' => 'auto:',