mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2025-02-16 13:03:13 +03:00
Fixed stats retrieval (#384)
* [Stats] Fixed stats always empty * Added tests
This commit is contained in:
parent
6dfd2b5a18
commit
b73adaf60c
@ -58,7 +58,7 @@ final class TotalResponse implements ApiResponse
|
||||
public static function create(array $data)
|
||||
{
|
||||
$stats = [];
|
||||
if (isset($data['status'])) {
|
||||
if (isset($data['stats'])) {
|
||||
foreach ($data['stats'] as $s) {
|
||||
$stats[] = TotalResponseItem::create($s);
|
||||
}
|
||||
|
@ -43,9 +43,9 @@ class TotalResponseItem
|
||||
{
|
||||
return new self(
|
||||
isset($data['time']) ? new \DateTime($data['time']) : null,
|
||||
isset($data['accepted']) ? $data['accepted'] : null,
|
||||
isset($data['delivered']) ? $data['delivered'] : null,
|
||||
isset($data['failed']) ? $data['failed'] : null
|
||||
isset($data['accepted']) ? $data['accepted'] : [],
|
||||
isset($data['delivered']) ? $data['delivered'] : [],
|
||||
isset($data['failed']) ? $data['failed'] : []
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,9 @@
|
||||
namespace Mailgun\Tests\Api;
|
||||
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Mailgun\Hydrator\ModelHydrator;
|
||||
use Mailgun\Model\Stats\TotalResponse;
|
||||
use Mailgun\Model\Stats\TotalResponseItem;
|
||||
|
||||
/**
|
||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||
@ -21,19 +24,22 @@ class StatsTest extends TestCase
|
||||
return 'Mailgun\Api\Stats';
|
||||
}
|
||||
|
||||
public function testTotal()
|
||||
/**
|
||||
* @dataProvider totalProvider
|
||||
*/
|
||||
public function testTotal($queryParameters, $responseData)
|
||||
{
|
||||
$data = [
|
||||
'foo' => 'bar',
|
||||
];
|
||||
|
||||
$api = $this->getApiMock();
|
||||
$api = $this->getApiMock(null, null, new ModelHydrator());
|
||||
$api->expects($this->once())
|
||||
->method('httpGet')
|
||||
->with('/v3/domain/stats/total', $data)
|
||||
->willReturn(new Response());
|
||||
->with('/v3/domain/stats/total', $queryParameters)
|
||||
->willReturn(new Response(200, ['Content-Type' => 'application/json'], \json_encode($responseData)));
|
||||
|
||||
$api->total('domain', $data);
|
||||
$total = $api->total('domain', $queryParameters);
|
||||
|
||||
$this->assertInstanceOf(TotalResponse::class, $total);
|
||||
$this->assertCount(count($responseData['stats']), $total->getStats());
|
||||
$this->assertContainsOnlyInstancesOf(TotalResponseItem::class, $total->getStats());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,4 +75,78 @@ class StatsTest extends TestCase
|
||||
|
||||
$api->all('');
|
||||
}
|
||||
|
||||
public function totalProvider()
|
||||
{
|
||||
return [
|
||||
'accepted events' => [
|
||||
'queryParameters' => [
|
||||
'event' => 'accepted',
|
||||
],
|
||||
'responseData' => $this->generateTotalResponsePayload([
|
||||
[
|
||||
'time' => $this->formatDate('-7 days'),
|
||||
'accepted' => [
|
||||
'outgoing' => 10,
|
||||
'incoming' => 5,
|
||||
'total' => 15,
|
||||
],
|
||||
],
|
||||
]),
|
||||
],
|
||||
'failed events' => [
|
||||
'queryParameters' => [
|
||||
'event' => 'failed',
|
||||
],
|
||||
'responseData' => $this->generateTotalResponsePayload([
|
||||
[
|
||||
'time' => $this->formatDate('-7 days'),
|
||||
'failed' => [
|
||||
'permanent' => [
|
||||
'bounce' => 4,
|
||||
'delayed-bounce' => 1,
|
||||
'suppress-bounce' => 1,
|
||||
'suppress-unsubscribe' => 2,
|
||||
'suppress-complaint' => 3,
|
||||
'total' => 10,
|
||||
],
|
||||
'temporary' => [
|
||||
'espblock' => 1,
|
||||
],
|
||||
],
|
||||
],
|
||||
]),
|
||||
],
|
||||
'delivered events' => [
|
||||
'queryParameters' => [
|
||||
'event' => 'delivered',
|
||||
],
|
||||
'responseData' => $this->generateTotalResponsePayload([
|
||||
[
|
||||
'time' => $this->formatDate('-7 days'),
|
||||
'delivered' => [
|
||||
'smtp' => 15,
|
||||
'http' => 5,
|
||||
'total' => 20,
|
||||
],
|
||||
],
|
||||
]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function generateTotalResponsePayload(array $stats, $start = '-7 days', $end = 'now', $resolution = 'day')
|
||||
{
|
||||
return [
|
||||
'end' => $this->formatDate($end),
|
||||
'resolution' => $resolution,
|
||||
'start' => $this->formatDate($start),
|
||||
'stats' => $stats,
|
||||
];
|
||||
}
|
||||
|
||||
private function formatDate($time = 'now')
|
||||
{
|
||||
return (new \DateTime($time, new \DateTimeZone('UTC')))->format('D, d M Y H:i:s T');
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
protected $testDomain;
|
||||
|
||||
public function __construct()
|
||||
protected function setUp()
|
||||
{
|
||||
$this->apiPrivKey = getenv('MAILGUN_PRIV_KEY');
|
||||
$this->apiPubKey = getenv('MAILGUN_PUB_KEY');
|
||||
@ -47,22 +47,28 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
|
||||
|
||||
abstract protected function getApiClass();
|
||||
|
||||
protected function getApiMock()
|
||||
protected function getApiMock($httpClient = null, $requestClient = null, $hydrator = null)
|
||||
{
|
||||
$httpClient = $this->getMockBuilder('Http\Client\HttpClient')
|
||||
->setMethods(['sendRequest'])
|
||||
->getMock();
|
||||
$httpClient
|
||||
->expects($this->any())
|
||||
->method('sendRequest');
|
||||
if (null === $httpClient) {
|
||||
$httpClient = $this->getMockBuilder('Http\Client\HttpClient')
|
||||
->setMethods(['sendRequest'])
|
||||
->getMock();
|
||||
$httpClient
|
||||
->expects($this->any())
|
||||
->method('sendRequest');
|
||||
}
|
||||
|
||||
$requestClient = $this->getMockBuilder('Mailgun\RequestBuilder')
|
||||
->setMethods(['create'])
|
||||
->getMock();
|
||||
if (null === $requestClient) {
|
||||
$requestClient = $this->getMockBuilder('Mailgun\RequestBuilder')
|
||||
->setMethods(['create'])
|
||||
->getMock();
|
||||
}
|
||||
|
||||
$hydrator = $this->getMockBuilder('Mailgun\Hydrator\Hydrator')
|
||||
->setMethods(['hydrate'])
|
||||
->getMock();
|
||||
if (null === $hydrator) {
|
||||
$hydrator = $this->getMockBuilder('Mailgun\Hydrator\Hydrator')
|
||||
->setMethods(['hydrate'])
|
||||
->getMock();
|
||||
}
|
||||
|
||||
return $this->getMockBuilder($this->getApiClass())
|
||||
->setMethods(['httpGet', 'httpPost', 'httpPostRaw', 'httpDelete', 'httpPut'])
|
||||
|
Loading…
x
Reference in New Issue
Block a user