Skip to content

Commit 57c509f

Browse files
committed
Add tests for new MessageEvent DateTime handling
1 parent b678bfc commit 57c509f

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

lib/SparkPost/MessageEvent.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
namespace SparkPost;
33

44
/**
5-
* SDK class for querying message events API
6-
* @package SparkPost
5+
* SDK class for querying the Message Events API
6+
*
7+
* @see https://developers.sparkpost.com/api/#/reference/message-events
78
*/
89
class MessageEvent extends APIResource
910
{
@@ -12,7 +13,7 @@ class MessageEvent extends APIResource
1213
/**
1314
* Method for issuing search requests to the Message Events API.
1415
*
15-
* The method passes-through all of the query parameters listed at
16+
* The method passes-through all of the query parameters - the valid ones are listed at
1617
* @link https://developers.sparkpost.com/api/#/reference/message-events/events-documentation/search-for-message-events
1718
*
1819
* @param array $queryParams The query parameters. Note that a query parameter containing an array
@@ -22,6 +23,30 @@ class MessageEvent extends APIResource
2223
*/
2324
public function search(Array $queryParams)
2425
{
26+
// check for DateTime objects & replace them with the formatted string equivalent
27+
foreach(["from", "to"] as $dateTimeParam) {
28+
if (isset($queryParams[$dateTimeParam]) && $queryParams[$dateTimeParam] instanceof \DateTime) {
29+
// the message events API doesn't allow the seconds or GMT offset, so strip them
30+
$queryParams[$dateTimeParam] = substr($queryParams[$dateTimeParam]->format(\DateTime::ATOM), 0, 16);
31+
}
32+
}
33+
2534
return $this->get(null, $queryParams);
2635
}
36+
37+
/**
38+
* List descriptions of the event fields that could be included in a response from the MessageEvent::search() method.
39+
*
40+
* @return array The event field descriptions.
41+
*/
42+
public function documentation() {
43+
return $this->get("events/documentation");
44+
}
45+
46+
/**
47+
* List an example of the event data that will be included in a response from the MessageEvent::search() method.
48+
*/
49+
public function samples() {
50+
return $this->get("events/samples", ["events" => "bounce"]);
51+
}
2752
}

lib/SparkPost/SparkPost.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class SparkPost {
77

88
public $transmission;
9+
public $messageEvent;
910

1011
/**
1112
* Connection config for making requests.
@@ -45,6 +46,7 @@ public function __construct($httpAdapter, $settingsConfig) {
4546
$this->setHttpAdapter($httpAdapter);
4647

4748
$this->transmission = new Transmission($this);
49+
$this->messageEvent = new MessageEvent($this);
4850
}
4951

5052
/**

test/unit/MessageEventTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace SparkPost;
4+
5+
use \Mockery;
6+
7+
8+
class MessageEventTest extends \PHPUnit_Framework_TestCase
9+
{
10+
private $sparkPostMock;
11+
private $sut;
12+
13+
/**
14+
* (non-PHPdoc)
15+
* @before
16+
* @see PHPUnit_Framework_TestCase::setUp()
17+
*/
18+
public function setUp()
19+
{
20+
$this->sparkPostMock = Mockery::mock('SparkPost\SparkPost', function ($mock) {
21+
$mock->shouldReceive('getHttpHeaders')->andReturn([]);
22+
});
23+
$this->sparkPostMock->httpAdapter = Mockery::mock();
24+
$this->sut = new MessageEvent($this->sparkPostMock);
25+
}
26+
27+
public function testDateTimeConversion()
28+
{
29+
$testBody = ['results' => ['my' => 'test']];
30+
$testFrom = new \DateTime("1978-08-27 04:05:02");
31+
$testFromStr = urlencode("1978-08-27T04:05");
32+
$testTo = new \DateTime("2016-04-04 19:00");
33+
$testToStr = urlencode("2016-04-04T19:00");
34+
35+
$responseMock = Mockery::mock();
36+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
37+
once()->
38+
with("/message-events/?from={$testFromStr}&to={$testToStr}", 'GET', Mockery::type('array'), null)->
39+
andReturn($responseMock);
40+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
41+
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
42+
43+
$this->assertEquals($testBody, $this->sut->search(["from" => $testFrom, "to" => $testTo]));
44+
}
45+
}

0 commit comments

Comments
 (0)