Skip to content

Commit

Permalink
Tests: new MANTIS_TESTSUITE_XDEBUG_SESSION setting
Browse files Browse the repository at this point in the history
Allows debugging of SOAP and REST API endpoints when running PHPUnit.

If not empty, sets the XDEBUG_SESSION cookie with the config's value to
enable Xdebug.

Fixes #33755
  • Loading branch information
dregad committed Feb 17, 2024
1 parent d46c6a9 commit e082b0c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions tests/bootstrap.php.sample
Expand Up @@ -83,3 +83,8 @@ $GLOBALS['MANTIS_TESTSUITE_REST_HOST'] = 'http://localhost/mantisbt/api/rest';
* The API token to use
*/
$GLOBALS['MANTIS_TESTSUITE_API_TOKEN'] = '';

/**
* If not empty, enables debugging of API endpoints.
*/
$GLOBALS['MANTIS_TESTSUITE_XDEBUG_SESSION'] = '';
19 changes: 17 additions & 2 deletions tests/core/RequestBuilder.php
Expand Up @@ -23,6 +23,7 @@
* @link http://www.mantisbt.org
*/

use GuzzleHttp\Cookie\CookieJar;

/**
* A builder class for test case requests.
Expand Down Expand Up @@ -58,20 +59,34 @@ class RequestBuilder {
*/
private $headers;

/**
* @var CookieJar|false Cookies for the Guzzle Client.
*/
private $cookieJar = false;

/**
* Constructor
*
* @param string $p_base_url The API base URL
* @param string $p_token The authentication API token
* @param string $p_xdebug Xdebug session value to enable, empty to disable.
*/
public function __construct( $p_base_url, $p_token ) {
public function __construct( $p_base_url, $p_token, $p_xdebug ) {
$this->base_url = $p_base_url;
$this->token = $p_token;

$this->method = 'GET';
$this->relative_path = '';
$this->body = '';
$this->headers = array();

# Set Xdebug session cookie
if( $p_xdebug ) {
$this->cookieJar = CookieJar::fromArray(
['XDEBUG_SESSION' => $p_xdebug],
parse_url( $p_base_url, PHP_URL_HOST )
);
}
}

/**
Expand Down Expand Up @@ -241,7 +256,7 @@ public function send() {

$t_url = rtrim( $this->base_url, '/' ) . '/' . ltrim( $this->relative_path, '/' );

$t_client = new GuzzleHttp\Client();
$t_client = new GuzzleHttp\Client( ['cookies' => $this->cookieJar] );
/** @noinspection PhpUnhandledExceptionInspection */
return $t_client->request( $this->method, $t_url, $t_options );
}
Expand Down
9 changes: 8 additions & 1 deletion tests/rest/RestBase.php
Expand Up @@ -49,6 +49,11 @@ abstract class RestBase extends TestCase {
*/
protected $base_path = '';

/**
* @var string Xdebug session value to enable, empty to disable debugging.
*/
protected $xdebug_session;

/**
* @var string Username
*/
Expand Down Expand Up @@ -123,6 +128,8 @@ protected function setUp(): void {

$this->token = $GLOBALS['MANTIS_TESTSUITE_API_TOKEN'];

$this->xdebug_session = $GLOBALS['MANTIS_TESTSUITE_XDEBUG_SESSION'] ?? '';

if( array_key_exists( 'MANTIS_TESTSUITE_PROJECT_ID', $GLOBALS ) ) {
$this->projectId = $GLOBALS['MANTIS_TESTSUITE_PROJECT_ID'];
} else {
Expand Down Expand Up @@ -161,7 +168,7 @@ protected function tearDown(): void {
* @return RequestBuilder
*/
public function builder() {
return new RequestBuilder( $this->base_path, $this->token );
return new RequestBuilder( $this->base_path, $this->token, $this->xdebug_session );
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/soap/SoapBase.php
Expand Up @@ -125,6 +125,11 @@ protected function setUp(): void {
array_merge( $this->defaultSoapClientOptions, $this->extraSoapClientFlags() )
);

# Set Xdebug session cookie
if( !empty( $GLOBALS['MANTIS_TESTSUITE_XDEBUG_SESSION'] ) ) {
$this->client->__setCookie('XDEBUG_SESSION', $GLOBALS['MANTIS_TESTSUITE_XDEBUG_SESSION']);
}

$this->mantisPath = substr( $t_wsdl, 0, -strlen( 'api/soap/mantisconnect.php?wsdl' ) );

$this->userName = $GLOBALS['MANTIS_TESTSUITE_USERNAME'] ?? 'administrator';
Expand Down

0 comments on commit e082b0c

Please sign in to comment.