mailgun-php/tests/Mock/Connection/TestBroker.php

77 lines
2.5 KiB
PHP
Raw Normal View History

2013-07-25 03:25:24 +04:00
<?php
2016-07-24 14:42:47 +03:00
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
2016-12-06 21:12:52 +03:00
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Mock\Connection;
2013-07-25 03:25:24 +04:00
use Mailgun\Connection\Exceptions\GenericHTTPError;
use Mailgun\Connection\Exceptions\InvalidCredentials;
use Mailgun\Connection\Exceptions\MissingEndpoint;
use Mailgun\Connection\RestClient;
use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters;
2013-07-25 03:25:24 +04:00
2014-05-14 17:06:05 +04:00
class TestBroker extends RestClient
{
private $apiKey;
protected $apiEndpoint;
2016-07-24 14:42:47 +03:00
public function __construct($apiKey = null, $apiHost = 'api.mailgun.net', $apiVersion = 'v3')
2014-05-14 17:06:05 +04:00
{
2016-07-24 14:42:47 +03:00
$this->apiKey = $apiKey;
$this->apiEndpoint = $apiHost;
2014-05-14 17:06:05 +04:00
}
2016-07-24 14:42:47 +03:00
public function post($endpointUrl, array $postData = [], $files = [])
2014-05-14 17:06:05 +04:00
{
return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200);
2014-05-14 17:06:05 +04:00
}
2016-07-24 14:42:47 +03:00
public function get($endpointUrl, $queryString = [])
2014-05-14 17:06:05 +04:00
{
return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200);
2014-05-14 17:06:05 +04:00
}
public function delete($endpointUrl)
{
return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200);
2014-05-14 17:06:05 +04:00
}
public function put($endpointUrl, $queryString)
{
return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200);
2014-05-14 17:06:05 +04:00
}
public function testResponseHandler($endpointUrl, $httpResponseCode = 200)
2014-05-14 17:06:05 +04:00
{
if ($httpResponseCode === 200) {
2016-07-24 14:42:47 +03:00
$result = new \stdClass();
2014-05-14 17:06:05 +04:00
$result->http_response_body = new \stdClass();
2016-07-24 14:42:47 +03:00
$jsonResponseData = json_decode('{"message": "Some JSON Response Data", "id": "1234"}');
2014-05-14 17:06:05 +04:00
foreach ($jsonResponseData as $key => $value) {
$result->http_response_body->$key = $value;
}
} elseif ($httpResponseCode == 400) {
2014-05-14 17:06:05 +04:00
throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
} elseif ($httpResponseCode == 401) {
2014-05-14 17:06:05 +04:00
throw new InvalidCredentials(EXCEPTION_INVALID_CREDENTIALS);
} elseif ($httpResponseCode == 401) {
2014-05-14 17:06:05 +04:00
throw new GenericHTTPError(EXCEPTION_INVALID_CREDENTIALS);
} elseif ($httpResponseCode == 404) {
2014-05-14 17:06:05 +04:00
throw new MissingEndpoint(EXCEPTION_MISSING_ENDPOINT);
} else {
throw new GenericHTTPError(EXCEPTION_GENERIC_HTTP_ERROR);
return false;
}
$result->http_response_code = $httpResponseCode;
2016-07-24 14:42:47 +03:00
$result->http_endpoint_url = $endpointUrl;
2014-05-14 17:06:05 +04:00
return $result;
}
2013-07-25 03:25:24 +04:00
}