Skip to content

Commit

Permalink
(REF) BaseRestTest - Change to Guzzle
Browse files Browse the repository at this point in the history
This provides more detailed debug information, esp when using the environment variable (DEBUG).
  • Loading branch information
totten committed Apr 9, 2021
1 parent 88c55e7 commit 43e4c62
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions tests/phpunit/E2E/Extern/BaseRestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -208,9 +213,11 @@ public function apiTestCases() {
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(
Expand All @@ -228,7 +235,7 @@ public function testAPICalls($query, $is_error) {
* 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";
Expand All @@ -248,9 +255,10 @@ public function testNotCMSUser_entityAction() {
"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);
}
Expand All @@ -261,7 +269,7 @@ public function testNotCMSUser_entityAction() {
*/
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
Expand All @@ -273,9 +281,9 @@ public function testGetCorrectUserBack() {
"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);
}
Expand All @@ -285,7 +293,7 @@ public function testGetCorrectUserBack() {
* 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";
Expand All @@ -304,9 +312,10 @@ public function testNotCMSUser_q() {
"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);
}
Expand Down

0 comments on commit 43e4c62

Please sign in to comment.