2016-10-24 20:01:32 +03:00
|
|
|
<?php
|
|
|
|
|
2019-01-09 22:32:09 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-11-24 01:02:12 +03:00
|
|
|
/*
|
2017-11-22 11:37:04 +03:00
|
|
|
* Copyright (C) 2013 Mailgun
|
2016-11-24 01:02:12 +03:00
|
|
|
*
|
|
|
|
* 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.
|
2016-11-24 01:02:12 +03:00
|
|
|
*/
|
|
|
|
|
2016-10-24 20:01:32 +03:00
|
|
|
namespace Mailgun\Tests\Api;
|
|
|
|
|
2021-01-30 09:39:30 +03:00
|
|
|
use Mailgun\Exception\InvalidArgumentException;
|
2017-12-01 23:10:21 +03:00
|
|
|
use Mailgun\Hydrator\ModelHydrator;
|
|
|
|
use Mailgun\Model\Stats\TotalResponse;
|
|
|
|
use Mailgun\Model\Stats\TotalResponseItem;
|
2021-08-18 22:48:47 +03:00
|
|
|
use Nyholm\Psr7\Response;
|
2016-10-24 20:01:32 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
|
|
|
*/
|
|
|
|
class StatsTest extends TestCase
|
|
|
|
{
|
|
|
|
protected function getApiClass()
|
|
|
|
{
|
|
|
|
return 'Mailgun\Api\Stats';
|
|
|
|
}
|
|
|
|
|
2017-12-01 23:10:21 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider totalProvider
|
|
|
|
*/
|
|
|
|
public function testTotal($queryParameters, $responseData)
|
2016-10-24 20:01:32 +03:00
|
|
|
{
|
2017-12-01 23:10:21 +03:00
|
|
|
$api = $this->getApiMock(null, null, new ModelHydrator());
|
2016-10-24 20:01:32 +03:00
|
|
|
$api->expects($this->once())
|
2016-11-11 23:53:26 +03:00
|
|
|
->method('httpGet')
|
2017-12-01 23:10:21 +03:00
|
|
|
->with('/v3/domain/stats/total', $queryParameters)
|
|
|
|
->willReturn(new Response(200, ['Content-Type' => 'application/json'], \json_encode($responseData)));
|
2016-10-24 20:01:32 +03:00
|
|
|
|
2017-12-01 23:10:21 +03:00
|
|
|
$total = $api->total('domain', $queryParameters);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(TotalResponse::class, $total);
|
|
|
|
$this->assertCount(count($responseData['stats']), $total->getStats());
|
|
|
|
$this->assertContainsOnlyInstancesOf(TotalResponseItem::class, $total->getStats());
|
2021-01-30 09:40:40 +03:00
|
|
|
|
|
|
|
$event = $queryParameters['event'];
|
|
|
|
$responseStat = $total->getStats()[0];
|
|
|
|
$statGetter = 'get'.ucwords($event);
|
|
|
|
|
|
|
|
if ('failed' !== $event) {
|
|
|
|
$expectedTotal = $responseData['stats'][0][$event]['total'];
|
|
|
|
$actualTotal = $responseStat->$statGetter()['total'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('failed' === $event) {
|
|
|
|
$expectedTotal = $responseData['stats'][0][$event]['permanent']['total'];
|
|
|
|
$actualTotal = $responseStat->$statGetter()['permanent']['total'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($expectedTotal, $actualTotal);
|
2016-10-24 20:01:32 +03:00
|
|
|
}
|
|
|
|
|
2016-11-24 01:01:26 +03:00
|
|
|
public function testTotalInvalidArgument()
|
|
|
|
{
|
2021-01-30 09:39:30 +03:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
|
2016-11-24 01:01:26 +03:00
|
|
|
$api = $this->getApiMock();
|
|
|
|
$api->total('');
|
|
|
|
}
|
2016-10-24 20:01:32 +03:00
|
|
|
|
2017-12-01 23:10:21 +03:00
|
|
|
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,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]),
|
|
|
|
],
|
2021-01-30 09:40:40 +03:00
|
|
|
'clicked events' => [
|
|
|
|
'queryParameters' => [
|
|
|
|
'event' => 'clicked',
|
|
|
|
],
|
|
|
|
'responseData' => $this->generateTotalResponsePayload([
|
|
|
|
[
|
|
|
|
'time' => $this->formatDate('-7 days'),
|
|
|
|
'clicked' => [
|
|
|
|
'total' => 7,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
'opened events' => [
|
|
|
|
'queryParameters' => [
|
|
|
|
'event' => 'opened',
|
|
|
|
],
|
|
|
|
'responseData' => $this->generateTotalResponsePayload([
|
|
|
|
[
|
|
|
|
'time' => $this->formatDate('-7 days'),
|
|
|
|
'opened' => [
|
|
|
|
'total' => 19,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
'unsubscribed events' => [
|
|
|
|
'queryParameters' => [
|
|
|
|
'event' => 'unsubscribed',
|
|
|
|
],
|
|
|
|
'responseData' => $this->generateTotalResponsePayload([
|
|
|
|
[
|
|
|
|
'time' => $this->formatDate('-7 days'),
|
|
|
|
'unsubscribed' => [
|
|
|
|
'total' => 10,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
'stored events' => [
|
|
|
|
'queryParameters' => [
|
|
|
|
'event' => 'stored',
|
|
|
|
],
|
|
|
|
'responseData' => $this->generateTotalResponsePayload([
|
|
|
|
[
|
|
|
|
'time' => $this->formatDate('-7 days'),
|
|
|
|
'stored' => [
|
|
|
|
'total' => 12,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]),
|
|
|
|
],
|
2017-12-01 23:10:21 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
2016-10-24 20:01:32 +03:00
|
|
|
}
|