| 1 |
<?php |
| 2 |
|
| 3 |
namespace PostHog\Test; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use PostHog\HttpResponse; |
| 7 |
use PostHog\Test\Assets\MockedResponses; |
| 8 |
|
| 9 |
class MockedHttpClient extends \PostHog\HttpClient |
| 10 |
{ |
| 11 |
public $calls; |
| 12 |
|
| 13 |
private $flagEndpointResponse; |
| 14 |
private $flagsEndpointResponse; |
| 15 |
|
| 16 |
public function __construct( |
| 17 |
string $host, |
| 18 |
bool $useSsl = true, |
| 19 |
int $maximumBackoffDuration = 10000, |
| 20 |
bool $compressRequests = false, |
| 21 |
bool $debug = false, |
| 22 |
?Closure $errorHandler = null, |
| 23 |
int $curlTimeoutMilliseconds = 750, |
| 24 |
array $flagEndpointResponse = [], |
| 25 |
array $flagsEndpointResponse = [] |
| 26 |
) { |
| 27 |
parent::__construct( |
| 28 |
$host, |
| 29 |
$useSsl, |
| 30 |
$maximumBackoffDuration, |
| 31 |
$compressRequests, |
| 32 |
$debug, |
| 33 |
$errorHandler, |
| 34 |
$curlTimeoutMilliseconds |
| 35 |
); |
| 36 |
$this->flagEndpointResponse = $flagEndpointResponse; |
| 37 |
$this->flagsEndpointResponse = !empty($flagsEndpointResponse) ? $flagsEndpointResponse : MockedResponses::FLAGS_REQUEST; |
| 38 |
} |
| 39 |
|
| 40 |
public function sendRequest(string $path, ?string $payload, array $extraHeaders = [], array $requestOptions = []): HttpResponse |
| 41 |
{ |
| 42 |
if (!isset($this->calls)) { |
| 43 |
$this->calls = []; |
| 44 |
} |
| 45 |
array_push($this->calls, array("path" => $path, "payload" => $payload, "extraHeaders" => $extraHeaders, "requestOptions" => $requestOptions)); |
| 46 |
|
| 47 |
if (str_starts_with($path, "/flags/")) { |
| 48 |
return new HttpResponse(json_encode($this->flagsEndpointResponse), 200); |
| 49 |
} |
| 50 |
|
| 51 |
if (str_starts_with($path, "/api/feature_flag/local_evaluation")) { |
| 52 |
return new HttpResponse(json_encode($this->flagEndpointResponse), 200); |
| 53 |
} |
| 54 |
|
| 55 |
return parent::sendRequest($path, $payload, $extraHeaders, $requestOptions); |
| 56 |
} |
| 57 |
} |
| 58 |
|