mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2024-11-24 13:36:02 +03:00
parent
398989de61
commit
6954b4dd2a
@ -3,10 +3,10 @@
|
|||||||
"description": "The Mailgun SDK provides methods for all API functions.",
|
"description": "The Mailgun SDK provides methods for all API functions.",
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^7.1",
|
"php": "^7.1",
|
||||||
"php-http/httplug": "^1.0 || ^2.0",
|
"psr/http-client": "^1.0",
|
||||||
"php-http/multipart-stream-builder": "^1.0",
|
"php-http/multipart-stream-builder": "^1.0",
|
||||||
"php-http/client-common": "^1.9",
|
"php-http/client-common": "^1.9 || ^2.0",
|
||||||
"php-http/discovery": "^1.0",
|
"php-http/discovery": "^1.6",
|
||||||
"webmozart/assert": "^1.2"
|
"webmozart/assert": "^1.2"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
parameters:
|
parameters:
|
||||||
level: 5
|
level: 5
|
||||||
paths:
|
paths:
|
||||||
- src
|
- src
|
||||||
|
|
||||||
|
excludes_analyse:
|
||||||
|
- %currentWorkingDirectory%/src/HttpClient/Plugin/HistoryTrait.php
|
||||||
|
@ -11,14 +11,14 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Mailgun\Api;
|
namespace Mailgun\Api;
|
||||||
|
|
||||||
use Http\Client\Exception as HttplugException;
|
|
||||||
use Http\Client\HttpClient;
|
|
||||||
use Mailgun\Exception\UnknownErrorException;
|
use Mailgun\Exception\UnknownErrorException;
|
||||||
use Mailgun\Hydrator\Hydrator;
|
use Mailgun\Hydrator\Hydrator;
|
||||||
use Mailgun\Hydrator\NoopHydrator;
|
use Mailgun\Hydrator\NoopHydrator;
|
||||||
use Mailgun\Exception\HttpClientException;
|
use Mailgun\Exception\HttpClientException;
|
||||||
use Mailgun\Exception\HttpServerException;
|
use Mailgun\Exception\HttpServerException;
|
||||||
use Mailgun\HttpClient\RequestBuilder;
|
use Mailgun\HttpClient\RequestBuilder;
|
||||||
|
use Psr\Http\Client as Psr18;
|
||||||
|
use Psr\Http\Client\ClientInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,7 +29,7 @@ abstract class HttpApi
|
|||||||
/**
|
/**
|
||||||
* The HTTP client.
|
* The HTTP client.
|
||||||
*
|
*
|
||||||
* @var HttpClient
|
* @var ClientInterface
|
||||||
*/
|
*/
|
||||||
protected $httpClient;
|
protected $httpClient;
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ abstract class HttpApi
|
|||||||
*/
|
*/
|
||||||
protected $requestBuilder;
|
protected $requestBuilder;
|
||||||
|
|
||||||
public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator)
|
public function __construct(ClientInterface $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator)
|
||||||
{
|
{
|
||||||
$this->httpClient = $httpClient;
|
$this->httpClient = $httpClient;
|
||||||
$this->requestBuilder = $requestBuilder;
|
$this->requestBuilder = $requestBuilder;
|
||||||
@ -113,7 +113,7 @@ abstract class HttpApi
|
|||||||
$response = $this->httpClient->sendRequest(
|
$response = $this->httpClient->sendRequest(
|
||||||
$this->requestBuilder->create('GET', $path, $requestHeaders)
|
$this->requestBuilder->create('GET', $path, $requestHeaders)
|
||||||
);
|
);
|
||||||
} catch (HttplugException\NetworkException $e) {
|
} catch (Psr18\NetworkExceptionInterface $e) {
|
||||||
throw HttpServerException::networkError($e);
|
throw HttpServerException::networkError($e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,7 +145,7 @@ abstract class HttpApi
|
|||||||
$response = $this->httpClient->sendRequest(
|
$response = $this->httpClient->sendRequest(
|
||||||
$this->requestBuilder->create('POST', $path, $requestHeaders, $body)
|
$this->requestBuilder->create('POST', $path, $requestHeaders, $body)
|
||||||
);
|
);
|
||||||
} catch (HttplugException\NetworkException $e) {
|
} catch (Psr18\NetworkExceptionInterface $e) {
|
||||||
throw HttpServerException::networkError($e);
|
throw HttpServerException::networkError($e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,7 +165,7 @@ abstract class HttpApi
|
|||||||
$response = $this->httpClient->sendRequest(
|
$response = $this->httpClient->sendRequest(
|
||||||
$this->requestBuilder->create('PUT', $path, $requestHeaders, $this->createRequestBody($parameters))
|
$this->requestBuilder->create('PUT', $path, $requestHeaders, $this->createRequestBody($parameters))
|
||||||
);
|
);
|
||||||
} catch (HttplugException\NetworkException $e) {
|
} catch (Psr18\NetworkExceptionInterface $e) {
|
||||||
throw HttpServerException::networkError($e);
|
throw HttpServerException::networkError($e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,7 +185,7 @@ abstract class HttpApi
|
|||||||
$response = $this->httpClient->sendRequest(
|
$response = $this->httpClient->sendRequest(
|
||||||
$this->requestBuilder->create('DELETE', $path, $requestHeaders, $this->createRequestBody($parameters))
|
$this->requestBuilder->create('DELETE', $path, $requestHeaders, $this->createRequestBody($parameters))
|
||||||
);
|
);
|
||||||
} catch (HttplugException\NetworkException $e) {
|
} catch (Psr18\NetworkExceptionInterface $e) {
|
||||||
throw HttpServerException::networkError($e);
|
throw HttpServerException::networkError($e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,11 +11,11 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Mailgun\Api;
|
namespace Mailgun\Api;
|
||||||
|
|
||||||
use Http\Client\HttpClient;
|
|
||||||
use Mailgun\Api\Suppression\Bounce;
|
use Mailgun\Api\Suppression\Bounce;
|
||||||
use Mailgun\Api\Suppression\Complaint;
|
use Mailgun\Api\Suppression\Complaint;
|
||||||
use Mailgun\Api\Suppression\Unsubscribe;
|
use Mailgun\Api\Suppression\Unsubscribe;
|
||||||
use Mailgun\Hydrator\Hydrator;
|
use Mailgun\Hydrator\Hydrator;
|
||||||
|
use Psr\Http\Client\ClientInterface;
|
||||||
use Mailgun\HttpClient\RequestBuilder;
|
use Mailgun\HttpClient\RequestBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,7 +26,7 @@ use Mailgun\HttpClient\RequestBuilder;
|
|||||||
class Suppression
|
class Suppression
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var HttpClient
|
* @var ClientInterface
|
||||||
*/
|
*/
|
||||||
private $httpClient;
|
private $httpClient;
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ class Suppression
|
|||||||
*/
|
*/
|
||||||
private $hydrator;
|
private $hydrator;
|
||||||
|
|
||||||
public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator)
|
public function __construct(ClientInterface $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator)
|
||||||
{
|
{
|
||||||
$this->httpClient = $httpClient;
|
$this->httpClient = $httpClient;
|
||||||
$this->requestBuilder = $requestBuilder;
|
$this->requestBuilder = $requestBuilder;
|
||||||
|
@ -11,7 +11,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Mailgun\Api;
|
namespace Mailgun\Api;
|
||||||
|
|
||||||
use Http\Client\HttpClient;
|
|
||||||
use Mailgun\Assert;
|
use Mailgun\Assert;
|
||||||
use Mailgun\Hydrator\Hydrator;
|
use Mailgun\Hydrator\Hydrator;
|
||||||
use Mailgun\Model\Webhook\CreateResponse;
|
use Mailgun\Model\Webhook\CreateResponse;
|
||||||
@ -21,6 +20,7 @@ use Mailgun\Model\Webhook\ShowResponse;
|
|||||||
use Mailgun\Model\Webhook\UpdateResponse;
|
use Mailgun\Model\Webhook\UpdateResponse;
|
||||||
use Mailgun\HttpClient\RequestBuilder;
|
use Mailgun\HttpClient\RequestBuilder;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Client\ClientInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||||
@ -32,7 +32,7 @@ class Webhook extends HttpApi
|
|||||||
*/
|
*/
|
||||||
private $apiKey;
|
private $apiKey;
|
||||||
|
|
||||||
public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator, string $apiKey)
|
public function __construct(ClientInterface $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator, string $apiKey)
|
||||||
{
|
{
|
||||||
parent::__construct($httpClient, $requestBuilder, $hydrator);
|
parent::__construct($httpClient, $requestBuilder, $hydrator);
|
||||||
$this->apiKey = $apiKey;
|
$this->apiKey = $apiKey;
|
||||||
|
@ -23,7 +23,7 @@ final class HttpServerException extends \RuntimeException implements Exception
|
|||||||
return new self('An unexpected error occurred at Mailgun\'s servers. Try again later and contact support if the error still exists.', $httpStatus);
|
return new self('An unexpected error occurred at Mailgun\'s servers. Try again later and contact support if the error still exists.', $httpStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function networkError(\Exception $previous)
|
public static function networkError(\Throwable $previous)
|
||||||
{
|
{
|
||||||
return new self('Mailgun\'s servers are currently unreachable.', 0, $previous);
|
return new self('Mailgun\'s servers are currently unreachable.', 0, $previous);
|
||||||
}
|
}
|
||||||
|
@ -11,14 +11,14 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Mailgun\HttpClient;
|
namespace Mailgun\HttpClient;
|
||||||
|
|
||||||
use Http\Client\HttpClient;
|
|
||||||
use Http\Client\Common\PluginClient;
|
use Http\Client\Common\PluginClient;
|
||||||
use Http\Discovery\HttpClientDiscovery;
|
use Http\Discovery\Psr18ClientDiscovery;
|
||||||
use Http\Discovery\UriFactoryDiscovery;
|
use Http\Discovery\UriFactoryDiscovery;
|
||||||
use Http\Message\UriFactory;
|
use Http\Message\UriFactory;
|
||||||
use Http\Client\Common\Plugin;
|
use Http\Client\Common\Plugin;
|
||||||
use Mailgun\HttpClient\Plugin\History;
|
use Mailgun\HttpClient\Plugin\History;
|
||||||
use Mailgun\HttpClient\Plugin\ReplaceUriPlugin;
|
use Mailgun\HttpClient\Plugin\ReplaceUriPlugin;
|
||||||
|
use Psr\Http\Client\ClientInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure a HTTP client.
|
* Configure a HTTP client.
|
||||||
@ -50,7 +50,7 @@ final class HttpClientConfigurator
|
|||||||
private $uriFactory;
|
private $uriFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var HttpClient
|
* @var ClientInterface
|
||||||
*/
|
*/
|
||||||
private $httpClient;
|
private $httpClient;
|
||||||
|
|
||||||
@ -124,16 +124,16 @@ final class HttpClientConfigurator
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getHttpClient(): HttpClient
|
private function getHttpClient(): ClientInterface
|
||||||
{
|
{
|
||||||
if (null === $this->httpClient) {
|
if (null === $this->httpClient) {
|
||||||
$this->httpClient = HttpClientDiscovery::find();
|
$this->httpClient = Psr18ClientDiscovery::find();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->httpClient;
|
return $this->httpClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setHttpClient(HttpClient $httpClient): self
|
public function setHttpClient(ClientInterface $httpClient): self
|
||||||
{
|
{
|
||||||
$this->httpClient = $httpClient;
|
$this->httpClient = $httpClient;
|
||||||
|
|
||||||
|
@ -12,7 +12,6 @@ declare(strict_types=1);
|
|||||||
namespace Mailgun\HttpClient\Plugin;
|
namespace Mailgun\HttpClient\Plugin;
|
||||||
|
|
||||||
use Http\Client\Common\Plugin\Journal;
|
use Http\Client\Common\Plugin\Journal;
|
||||||
use Http\Client\Exception;
|
|
||||||
use Psr\Http\Message\RequestInterface;
|
use Psr\Http\Message\RequestInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
@ -23,6 +22,7 @@ use Psr\Http\Message\ResponseInterface;
|
|||||||
*/
|
*/
|
||||||
final class History implements Journal
|
final class History implements Journal
|
||||||
{
|
{
|
||||||
|
use HistoryTrait;
|
||||||
/**
|
/**
|
||||||
* @var ResponseInterface
|
* @var ResponseInterface
|
||||||
*/
|
*/
|
||||||
@ -40,8 +40,4 @@ final class History implements Journal
|
|||||||
{
|
{
|
||||||
$this->lastResponse = $response;
|
$this->lastResponse = $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addFailure(RequestInterface $request, Exception $exception)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
41
src/HttpClient/Plugin/HistoryTrait.php
Normal file
41
src/HttpClient/Plugin/HistoryTrait.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Mailgun
|
||||||
|
*
|
||||||
|
* This software may be modified and distributed under the terms
|
||||||
|
* of the MIT license. See the LICENSE file for details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Mailgun\HttpClient\Plugin;
|
||||||
|
|
||||||
|
use Http\Client\Exception;
|
||||||
|
use Psr\Http\Client\ClientExceptionInterface;
|
||||||
|
use Psr\Http\Message\RequestInterface;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Below is a some code to make the History plugin compatible with both 1.x and 2.x of php-client/client-common
|
||||||
|
*/
|
||||||
|
if (\class_exists(\Http\Client\Common\HttpMethodsClientInterface::class)) {
|
||||||
|
/**
|
||||||
|
* @internal code for php-http/client-common:2.x
|
||||||
|
*/
|
||||||
|
trait HistoryTrait
|
||||||
|
{
|
||||||
|
public function addFailure(RequestInterface $request, ClientExceptionInterface $exception)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/**
|
||||||
|
* @internal code for php-http/client-common:1.x
|
||||||
|
*/
|
||||||
|
trait HistoryTrait
|
||||||
|
{
|
||||||
|
public function addFailure(RequestInterface $request, Exception $exception)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -22,6 +22,8 @@ use Psr\Http\Message\UriInterface;
|
|||||||
*/
|
*/
|
||||||
final class ReplaceUriPlugin implements Plugin
|
final class ReplaceUriPlugin implements Plugin
|
||||||
{
|
{
|
||||||
|
use Plugin\VersionBridgePlugin;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var UriInterface
|
* @var UriInterface
|
||||||
*/
|
*/
|
||||||
@ -32,10 +34,7 @@ final class ReplaceUriPlugin implements Plugin
|
|||||||
$this->uri = $uri;
|
$this->uri = $uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function doHandleRequest(RequestInterface $request, callable $next, callable $first)
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function handleRequest(RequestInterface $request, callable $next, callable $first)
|
|
||||||
{
|
{
|
||||||
$request = $request->withUri($this->uri);
|
$request = $request->withUri($this->uri);
|
||||||
|
|
||||||
|
@ -11,10 +11,12 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Mailgun\HttpClient;
|
namespace Mailgun\HttpClient;
|
||||||
|
|
||||||
use Http\Discovery\MessageFactoryDiscovery;
|
use Http\Discovery\Psr17FactoryDiscovery;
|
||||||
use Http\Message\MultipartStream\MultipartStreamBuilder;
|
use Http\Message\MultipartStream\MultipartStreamBuilder;
|
||||||
use Http\Message\RequestFactory;
|
use Psr\Http\Message\RequestFactoryInterface;
|
||||||
use Psr\Http\Message\RequestInterface;
|
use Psr\Http\Message\RequestInterface;
|
||||||
|
use Psr\Http\Message\StreamFactoryInterface;
|
||||||
|
use Psr\Http\Message\StreamInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||||
@ -22,10 +24,15 @@ use Psr\Http\Message\RequestInterface;
|
|||||||
class RequestBuilder
|
class RequestBuilder
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var RequestFactory
|
* @var RequestFactoryInterface|null
|
||||||
*/
|
*/
|
||||||
private $requestFactory;
|
private $requestFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var StreamFactoryInterface|null
|
||||||
|
*/
|
||||||
|
private $streamFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var MultipartStreamBuilder
|
* @var MultipartStreamBuilder
|
||||||
*/
|
*/
|
||||||
@ -42,13 +49,13 @@ class RequestBuilder
|
|||||||
* 'filename'=> string (optional)
|
* 'filename'=> string (optional)
|
||||||
* 'headers' => array (optinal) ['header-name' => 'header-value']
|
* 'headers' => array (optinal) ['header-name' => 'header-value']
|
||||||
* )
|
* )
|
||||||
*
|
|
||||||
* @return RequestInterface
|
|
||||||
*/
|
*/
|
||||||
public function create(string $method, string $uri, array $headers = [], $body = null)
|
public function create(string $method, string $uri, array $headers = [], $body = null): RequestInterface
|
||||||
{
|
{
|
||||||
if (!is_array($body)) {
|
if (!is_array($body)) {
|
||||||
return $this->getRequestFactory()->createRequest($method, $uri, $headers, $body);
|
$stream = $this->getStreamFactory()->createStream($body);
|
||||||
|
|
||||||
|
return $this->createRequest($method, $uri, $headers, $stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
$builder = $this->getMultipartStreamBuilder();
|
$builder = $this->getMultipartStreamBuilder();
|
||||||
@ -67,37 +74,42 @@ class RequestBuilder
|
|||||||
|
|
||||||
$headers['Content-Type'] = 'multipart/form-data; boundary="'.$boundary.'"';
|
$headers['Content-Type'] = 'multipart/form-data; boundary="'.$boundary.'"';
|
||||||
|
|
||||||
return $this->getRequestFactory()->createRequest($method, $uri, $headers, $multipartStream);
|
return $this->createRequest($method, $uri, $headers, $multipartStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function getRequestFactory(): RequestFactoryInterface
|
||||||
* @return RequestFactory
|
|
||||||
*/
|
|
||||||
private function getRequestFactory()
|
|
||||||
{
|
{
|
||||||
if (null === $this->requestFactory) {
|
if (null === $this->requestFactory) {
|
||||||
$this->requestFactory = MessageFactoryDiscovery::find();
|
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->requestFactory;
|
return $this->requestFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function setRequestFactory(RequestFactoryInterface $requestFactory): self
|
||||||
* @param RequestFactory $requestFactory
|
|
||||||
*
|
|
||||||
* @return RequestBuilder
|
|
||||||
*/
|
|
||||||
public function setRequestFactory($requestFactory)
|
|
||||||
{
|
{
|
||||||
$this->requestFactory = $requestFactory;
|
$this->requestFactory = $requestFactory;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function getStreamFactory(): StreamFactoryInterface
|
||||||
* @return MultipartStreamBuilder
|
{
|
||||||
*/
|
if (null === $this->streamFactory) {
|
||||||
private function getMultipartStreamBuilder()
|
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->streamFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setStreamFactory(StreamFactoryInterface $streamFactory): self
|
||||||
|
{
|
||||||
|
$this->streamFactory = $streamFactory;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getMultipartStreamBuilder(): MultipartStreamBuilder
|
||||||
{
|
{
|
||||||
if (null === $this->multipartStreamBuilder) {
|
if (null === $this->multipartStreamBuilder) {
|
||||||
$this->multipartStreamBuilder = new MultipartStreamBuilder();
|
$this->multipartStreamBuilder = new MultipartStreamBuilder();
|
||||||
@ -106,15 +118,21 @@ class RequestBuilder
|
|||||||
return $this->multipartStreamBuilder;
|
return $this->multipartStreamBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function setMultipartStreamBuilder(MultipartStreamBuilder $multipartStreamBuilder): self
|
||||||
* @param MultipartStreamBuilder $multipartStreamBuilder
|
|
||||||
*
|
|
||||||
* @return RequestBuilder
|
|
||||||
*/
|
|
||||||
public function setMultipartStreamBuilder($multipartStreamBuilder)
|
|
||||||
{
|
{
|
||||||
$this->multipartStreamBuilder = $multipartStreamBuilder;
|
$this->multipartStreamBuilder = $multipartStreamBuilder;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function createRequest(string $method, string $uri, array $headers, StreamInterface $stream)
|
||||||
|
{
|
||||||
|
$request = $this->getRequestFactory()->createRequest($method, $uri);
|
||||||
|
$request = $request->withBody($stream);
|
||||||
|
foreach ($headers as $name => $value) {
|
||||||
|
$request = $request->withAddedHeader($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $request;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,13 +11,14 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Mailgun;
|
namespace Mailgun;
|
||||||
|
|
||||||
use Http\Client\HttpClient;
|
use Http\Client\Common\PluginClient;
|
||||||
use Mailgun\HttpClient\HttpClientConfigurator;
|
use Mailgun\HttpClient\HttpClientConfigurator;
|
||||||
use Mailgun\HttpClient\Plugin\History;
|
use Mailgun\HttpClient\Plugin\History;
|
||||||
use Mailgun\HttpClient\RequestBuilder;
|
use Mailgun\HttpClient\RequestBuilder;
|
||||||
use Mailgun\Hydrator\ModelHydrator;
|
use Mailgun\Hydrator\ModelHydrator;
|
||||||
use Mailgun\Hydrator\Hydrator;
|
use Mailgun\Hydrator\Hydrator;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Client\ClientInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is the base class for the Mailgun SDK.
|
* This class is the base class for the Mailgun SDK.
|
||||||
@ -30,7 +31,7 @@ final class Mailgun
|
|||||||
private $apiKey;
|
private $apiKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var HttpClient
|
* @var ClientInterface|PluginClient
|
||||||
*/
|
*/
|
||||||
private $httpClient;
|
private $httpClient;
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ namespace Mailgun\Tests\Api;
|
|||||||
use GuzzleHttp\Psr7\Request;
|
use GuzzleHttp\Psr7\Request;
|
||||||
use GuzzleHttp\Psr7\Response;
|
use GuzzleHttp\Psr7\Response;
|
||||||
use Mailgun\Hydrator\ModelHydrator;
|
use Mailgun\Hydrator\ModelHydrator;
|
||||||
|
use Psr\Http\Client\ClientInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,7 +50,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
|
|||||||
protected function getApiMock($httpClient = null, $requestClient = null, $hydrator = null)
|
protected function getApiMock($httpClient = null, $requestClient = null, $hydrator = null)
|
||||||
{
|
{
|
||||||
if (null === $httpClient) {
|
if (null === $httpClient) {
|
||||||
$httpClient = $this->getMockBuilder('Http\Client\HttpClient')
|
$httpClient = $this->getMockBuilder(ClientInterface::class)
|
||||||
->setMethods(['sendRequest'])
|
->setMethods(['sendRequest'])
|
||||||
->getMock();
|
->getMock();
|
||||||
$httpClient
|
$httpClient
|
||||||
@ -79,7 +80,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
|
|||||||
*/
|
*/
|
||||||
protected function getApiInstance($apiKey = null)
|
protected function getApiInstance($apiKey = null)
|
||||||
{
|
{
|
||||||
$httpClient = $this->getMockBuilder('Http\Client\HttpClient')
|
$httpClient = $this->getMockBuilder(ClientInterface::class)
|
||||||
->setMethods(['sendRequest'])
|
->setMethods(['sendRequest'])
|
||||||
->getMock();
|
->getMock();
|
||||||
$httpClient
|
$httpClient
|
||||||
|
@ -12,19 +12,25 @@ declare(strict_types=1);
|
|||||||
namespace Mailgun\Tests\HttpClient;
|
namespace Mailgun\Tests\HttpClient;
|
||||||
|
|
||||||
use Http\Message\MultipartStream\MultipartStreamBuilder;
|
use Http\Message\MultipartStream\MultipartStreamBuilder;
|
||||||
use Http\Message\RequestFactory;
|
|
||||||
use Mailgun\HttpClient\RequestBuilder;
|
use Mailgun\HttpClient\RequestBuilder;
|
||||||
use Mailgun\Tests\MailgunTestCase;
|
use Mailgun\Tests\MailgunTestCase;
|
||||||
|
use Nyholm\Psr7\Stream;
|
||||||
use PHPUnit\Framework\MockObject\MockObject;
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
|
use Psr\Http\Message\RequestFactoryInterface;
|
||||||
use Psr\Http\Message\RequestInterface;
|
use Psr\Http\Message\RequestInterface;
|
||||||
|
use Psr\Http\Message\StreamFactoryInterface;
|
||||||
use Psr\Http\Message\StreamInterface;
|
use Psr\Http\Message\StreamInterface;
|
||||||
|
|
||||||
class RequestBuilderTest extends MailgunTestCase
|
class RequestBuilderTest extends MailgunTestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var MockObject|RequestFactory
|
* @var MockObject|RequestFactoryInterface
|
||||||
*/
|
*/
|
||||||
private $requestFactory;
|
private $requestFactory;
|
||||||
|
/**
|
||||||
|
* @var MockObject|StreamFactoryInterface
|
||||||
|
*/
|
||||||
|
private $streamFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var RequestBuilder
|
* @var RequestBuilder
|
||||||
@ -38,13 +44,18 @@ class RequestBuilderTest extends MailgunTestCase
|
|||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->requestFactory = $this->getMockBuilder(RequestFactory::class)
|
$this->requestFactory = $this->getMockBuilder(RequestFactoryInterface::class)
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
|
||||||
|
$this->streamFactory = $this->getMockBuilder(StreamFactoryInterface::class)
|
||||||
->disableOriginalConstructor()
|
->disableOriginalConstructor()
|
||||||
->getMock();
|
->getMock();
|
||||||
|
|
||||||
$this->requestBuilder = new RequestBuilder();
|
$this->requestBuilder = new RequestBuilder();
|
||||||
//Everything but testing class is mock. Otherwise it wouldn't be unit testing
|
//Everything but testing class is mock. Otherwise it wouldn't be unit testing
|
||||||
$this->requestBuilder->setRequestFactory($this->requestFactory);
|
$this->requestBuilder->setRequestFactory($this->requestFactory);
|
||||||
|
$this->requestBuilder->setStreamFactory($this->streamFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,21 +71,34 @@ class RequestBuilderTest extends MailgunTestCase
|
|||||||
|
|
||||||
public function testCreateSimpleStream()
|
public function testCreateSimpleStream()
|
||||||
{
|
{
|
||||||
|
$streamContent = 'content';
|
||||||
|
$stream = Stream::create($streamContent);
|
||||||
|
|
||||||
|
$this->streamFactory
|
||||||
|
->expects($this->once())
|
||||||
|
->method('createStream')
|
||||||
|
->with($streamContent)
|
||||||
|
->willReturn($stream);
|
||||||
|
|
||||||
|
$request = $this->getMockBuilder(RequestInterface::class)->getMock();
|
||||||
|
$request->expects($this->once())
|
||||||
|
->method('withBody')
|
||||||
|
->with($this->equalTo($stream))
|
||||||
|
->willReturn($request);
|
||||||
|
|
||||||
|
$request->expects($this->once())
|
||||||
|
->method('withAddedHeader')
|
||||||
|
->with($this->equalTo('Content-Type'), $this->equalTo('application/json'))
|
||||||
|
->willReturn($request);
|
||||||
|
|
||||||
$this->requestFactory
|
$this->requestFactory
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('createRequest')
|
->method('createRequest')
|
||||||
->with(
|
->with(
|
||||||
$this->equalTo('GET'),
|
$this->equalTo('GET'),
|
||||||
$this->equalTo('http://foo.bar'),
|
$this->equalTo('http://foo.bar')
|
||||||
$this->callback(function (array $headers) {
|
|
||||||
$this->assertArrayHasKey('Content-Type', $headers);
|
|
||||||
$this->assertEquals('application/json', $headers['Content-Type']);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}),
|
|
||||||
$this->equalTo('content')
|
|
||||||
)
|
)
|
||||||
->willReturn($request = $this->getMockBuilder(RequestInterface::class)->getMock());
|
->willReturn($request);
|
||||||
|
|
||||||
$result = $this->requestBuilder
|
$result = $this->requestBuilder
|
||||||
->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], 'content');
|
->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], 'content');
|
||||||
@ -122,21 +146,25 @@ class RequestBuilderTest extends MailgunTestCase
|
|||||||
->method('reset')
|
->method('reset')
|
||||||
->willReturn($multipartStreamBuilder);
|
->willReturn($multipartStreamBuilder);
|
||||||
|
|
||||||
|
$request = $this->getMockBuilder(RequestInterface::class)->getMock();
|
||||||
|
$request->expects($this->once())
|
||||||
|
->method('withBody')
|
||||||
|
->with($this->equalTo($stream))
|
||||||
|
->willReturn($request);
|
||||||
|
|
||||||
|
$request->expects($this->once())
|
||||||
|
->method('withAddedHeader')
|
||||||
|
->with($this->equalTo('Content-Type'), $this->equalTo('multipart/form-data; boundary="some boundary"'))
|
||||||
|
->willReturn($request);
|
||||||
|
|
||||||
$this->requestFactory
|
$this->requestFactory
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('createRequest')
|
->method('createRequest')
|
||||||
->with(
|
->with(
|
||||||
$this->equalTo('GET'),
|
$this->equalTo('GET'),
|
||||||
$this->equalTo('http://foo.bar'),
|
$this->equalTo('http://foo.bar')
|
||||||
$this->callback(function (array $headers) {
|
|
||||||
$this->assertArrayHasKey('Content-Type', $headers);
|
|
||||||
$this->assertEquals('multipart/form-data; boundary="some boundary"', $headers['Content-Type']);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}),
|
|
||||||
$this->equalTo($stream)
|
|
||||||
)
|
)
|
||||||
->willReturn($request = $this->getMockBuilder(RequestInterface::class)->getMock());
|
->willReturn($request);
|
||||||
|
|
||||||
$this->requestBuilder->setMultipartStreamBuilder($multipartStreamBuilder);
|
$this->requestBuilder->setMultipartStreamBuilder($multipartStreamBuilder);
|
||||||
$result = $this->requestBuilder
|
$result = $this->requestBuilder
|
||||||
|
Loading…
Reference in New Issue
Block a user