From: Tim Otten Date: Thu, 4 Mar 2021 09:31:46 +0000 (-0800) Subject: (REF) BaseRestTest - Change to Guzzle X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=aaae058a0408828c713ddf99394851cb3e480a58;p=civicrm-core.git (REF) BaseRestTest - Change to Guzzle This provides more detailed debug information, esp when using the environment variable (DEBUG). --- diff --git a/tests/phpunit/E2E/Extern/BaseRestTest.php b/tests/phpunit/E2E/Extern/BaseRestTest.php index 626f89598e..931dbfebc6 100644 --- a/tests/phpunit/E2E/Extern/BaseRestTest.php +++ b/tests/phpunit/E2E/Extern/BaseRestTest.php @@ -9,12 +9,17 @@ +--------------------------------------------------------------------+ */ +use Civi\Test\HttpTestTrait; + /** * Verify that the REST API bindings correctly parse and authenticate requests. * * @group e2e */ abstract class E2E_Extern_BaseRestTest extends CiviEndToEndTestCase { + + use HttpTestTrait; + protected $url; protected static $api_key; protected $session_id; @@ -208,9 +213,11 @@ abstract class E2E_Extern_BaseRestTest extends CiviEndToEndTestCase { public function testAPICalls($query, $is_error) { $this->updateAdminApiKey(); - $client = CRM_Utils_HttpClient::singleton(); - list($status, $data) = $client->post($this->getRestUrl(), $query); - $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status); + $http = $this->createGuzzle(['http_errors' => FALSE]); + $response = $http->post($this->getRestUrl(), ['form_params' => $query]); + $this->assertStatusCode(200, $response); + $data = (string) $response->getBody(); + $result = json_decode($data, TRUE); if ($result === NULL) { $msg = print_r(array( @@ -228,7 +235,7 @@ abstract class E2E_Extern_BaseRestTest extends CiviEndToEndTestCase { * a real user. Submit in "?entity=X&action=X" notation */ public function testNotCMSUser_entityAction() { - $client = CRM_Utils_HttpClient::singleton(); + $http = $this->createGuzzle(['http_errors' => FALSE]); //Create contact with api_key $test_key = "testing1234"; @@ -248,9 +255,10 @@ abstract class E2E_Extern_BaseRestTest extends CiviEndToEndTestCase { "json" => "1", "api_key" => $test_key, ); - list($status, $data) = $client->post($this->getRestUrl(), $params); - $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status); - $result = json_decode($data, TRUE); + + $response = $http->post($this->getRestUrl(), ['form_params' => $params]); + $this->assertStatusCode(200, $response); + $result = json_decode((string) $response->getBody(), TRUE); $this->assertNotNull($result); $this->assertAPIErrorCode($result, 1); } @@ -261,7 +269,7 @@ abstract class E2E_Extern_BaseRestTest extends CiviEndToEndTestCase { */ public function testGetCorrectUserBack() { $this->updateAdminApiKey(); - $client = CRM_Utils_HttpClient::singleton(); + $http = $this->createGuzzle(['http_errors' => FALSE]); //Create contact with api_key // The key associates with a real contact but not a real user @@ -273,9 +281,9 @@ abstract class E2E_Extern_BaseRestTest extends CiviEndToEndTestCase { "api_key" => self::getApiKey(), "id" => "user_contact_id", ); - list($status, $data) = $client->post($this->getRestUrl(), $params); - $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status); - $result = json_decode($data, TRUE); + $response = $http->post($this->getRestUrl(), ['form_params' => $params]); + $this->assertStatusCode(200, $response); + $result = json_decode((string) $response->getBody(), TRUE); $this->assertNotNull($result); $this->assertEquals($result['id'], $this->adminContactId); } @@ -285,7 +293,7 @@ abstract class E2E_Extern_BaseRestTest extends CiviEndToEndTestCase { * a real user. Submit in "?q=civicrm/$entity/$action" notation */ public function testNotCMSUser_q() { - $client = CRM_Utils_HttpClient::singleton(); + $http = $this->createGuzzle(['http_errors' => FALSE]); //Create contact with api_key $test_key = "testing1234"; @@ -304,9 +312,10 @@ abstract class E2E_Extern_BaseRestTest extends CiviEndToEndTestCase { "json" => "1", "api_key" => $test_key, ); - list($status, $data) = $client->post($this->getRestUrl(), $params); - $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status); - $result = json_decode($data, TRUE); + $response = $http->post($this->getRestUrl(), ['form_params' => $params]); + + $this->assertStatusCode(200, $response); + $result = json_decode((string) $response->getBody(), TRUE); $this->assertNotNull($result); $this->assertAPIErrorCode($result, 1); }