Files
FOSSBilling/tests/APIHelper.php
T
Belle Aerni 45db4910a7 PHP 8.4 prep (#2464)
* Update CI for PHP 8.4

* Update composer deps for PHP 8.4

* Updates

* This is the default mode and this constant causes test failutes

* Explicily install the PDO extensions

* Temporarily downgrade the live tests to PHP 8.3

It would seem that there is some kind of problem with PHP 8.4 in regards to these tests. I believe it is outside of our control however as it installs fine for me with PHP 8.4 under DirectAdmin

* Update to address deprecations

* Make that nullable

* Fixed my typo

* Bump PHPunit to v11

* Also take care of two minor things

* Formatting fix
2024-11-21 23:16:29 -08:00

113 lines
3.3 KiB
PHP

<?php
namespace APIHelper;
class Request
{
/**
* Handles making a response to the FOSSBilling server.
*
* @param string $endpoint the API endpoint to call (Example: `guest/system/company`)
* @param string $method set to POST to have cURL make a POST request to the server
* @param array $payload when using POST, this array will be sent as the POST fields
* @param string|null $apiKey (optional) the API key to authenticate with
* @param string|null $baseUrl (optional) the base instance URL to make requests against (Example: `http://localhost/`)
*/
public static function makeRequest(string $endpoint, array $payload = [], ?string $role = null, ?string $apiKey = null, string $method = 'POST', ?string $baseUrl = null): Response
{
$cookie = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'cookie.txt';
if (!$role) {
$role = str_starts_with($endpoint, 'admin') ? 'admin' : 'client';
}
if (!$apiKey) {
$apiKey = getenv('TEST_API_KEY');
}
if (!$baseUrl) {
$baseUrl = getenv('APP_URL');
}
$ch = curl_init($baseUrl . 'api/' . $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_USERPWD, "$role:$apiKey");
if (strcasecmp($method, 'POST') === 0) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
}
$output = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return new Response($httpCode, $output);
}
public static function resetCookies()
{
unlink(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'cookie.txt');
}
}
class Response
{
private array $decodedResponse = [];
public function __construct(private readonly int $code, private readonly string $rawResponse)
{
if (json_validate($this->rawResponse)) {
$this->decodedResponse = json_decode($this->rawResponse, true);
}
}
public function getHttpCode(): int
{
return $this->code;
}
public function getResponse(): array
{
return $this->decodedResponse;
}
public function getRawResponse(): string
{
return $this->rawResponse;
}
public function wasSuccessful(): bool
{
return $this->decodedResponse && !$this->decodedResponse['error'];
}
public function getErrorMessage(): string
{
return $this->decodedResponse['error']['message'] ?? 'None';
}
public function getErrorCode(): int
{
return intval($this->decodedResponse['error']['code'] ?? 0);
}
public function getError(): string
{
return $this->getErrorMessage() . ' (Code ' . $this->getErrorCode() . ')';
}
public function generatePHPUnitMessage(): string
{
return 'The API request failed with the following message: ' . $this->getError();
}
public function getResult(): mixed
{
return $this->decodedResponse['result'] ?? '';
}
}