Skip to content

Commit 3cefcae

Browse files
committed
Merge pull request #69 from j4m3s/ISSUE-58
Message Events
2 parents f531e0d + 4ee945d commit 3cefcae

10 files changed

+187
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
test/output/
66
examples/example-config.json
77
.idea
8+
/composer.phar

AUTHORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ php-sparkpost is maintained by Message Systems.
55
* Jordan Nornhold <jordan.nornhold@messagesystems.com>, @beardyman
66
* Rich Leland <rich.leland@messagesystems.com>, @richleland
77
* Matthew April <matthew.japril@gmail.com>
8+
* James Fellows <james.fellows@8-w.co.uk>

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased][unreleased]
6-
- All content has been released to date.
6+
- Message Events API added.
7+
- Transmission API now accepts a DateTime object for startDate
78

89
## [1.0.3] - 2016-03-25
910
### Added

lib/SparkPost/APIResource.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ public function delete( $resourcePath=null, Array $query=[] ) {
130130
/**
131131
* assembles a URL for a request
132132
* @param string $resourcePath path after the initial endpoint
133-
* @param array $options array with an optional value of query with values to build a querystring from.
133+
* @param array $options array with an optional value of query with values to build a querystring from. Any
134+
* query elements that are themselves arrays will be imploded into a comma separated list.
134135
* @return string the assembled URL
135136
*/
136137
private function buildUrl($resourcePath, $options) {
@@ -140,6 +141,13 @@ private function buildUrl($resourcePath, $options) {
140141
}
141142

142143
if( !empty($options['query'])) {
144+
// check each query element - if it's an array, implode it to match the API-accepted format
145+
foreach($options['query'] as &$element) {
146+
if(is_array($element)) {
147+
$element = implode(",", $element);
148+
}
149+
}
150+
143151
$queryString = http_build_query($options['query']);
144152
$url .= '?'.$queryString;
145153
}

lib/SparkPost/MessageEvents.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
namespace SparkPost;
3+
4+
/**
5+
* SDK class for querying the Message Events API
6+
*
7+
* @see https://developers.sparkpost.com/api/#/reference/message-events
8+
*/
9+
class MessageEvents extends APIResource
10+
{
11+
/**
12+
* @var string
13+
*/
14+
public $endpoint = 'message-events';
15+
16+
/**
17+
* Method for issuing search requests to the Message Events API.
18+
*
19+
* The method passes-through all of the query parameters - the valid ones are listed at
20+
* @link https://developers.sparkpost.com/api/#/reference/message-events/events-documentation/search-for-message-events
21+
*
22+
* @param array $queryParams The query parameters. Note that a query parameter containing an array
23+
* is collapsed into a comma-separated list.
24+
*
25+
* @return array The result of the query.
26+
*/
27+
public function search(Array $queryParams)
28+
{
29+
// check for DateTime objects & replace them with the formatted string equivalent
30+
foreach(["from", "to"] as $dateTimeParam) {
31+
if (isset($queryParams[$dateTimeParam]) && $queryParams[$dateTimeParam] instanceof \DateTime) {
32+
// the message events API doesn't allow the seconds or GMT offset, so strip them
33+
$queryParams[$dateTimeParam] = substr($queryParams[$dateTimeParam]->format(\DateTime::ATOM), 0, 16);
34+
}
35+
}
36+
37+
return $this->get(null, $queryParams);
38+
}
39+
40+
/**
41+
* List descriptions of the event fields that could be included in a response from the MessageEvent::search() method.
42+
*
43+
* @return array The event field descriptions.
44+
*/
45+
public function documentation() {
46+
return $this->get("events/documentation");
47+
}
48+
49+
/**
50+
* List examples of the event data that will be included in a response from the MessageEvent::search() method.
51+
*
52+
* @param array $events (optional) Event types for which to get a sample payload. If not provided, samples
53+
* for all events will be returned.
54+
*
55+
* @return array Sample events.
56+
*/
57+
public function samples(Array $events = []) {
58+
return $this->get("events/samples", ["events"=>$events]);
59+
}
60+
}

lib/SparkPost/SparkPost.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class SparkPost {
77

88
public $transmission;
9+
public $messageEvents;
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->messageEvents = new MessageEvents($this);
4850
}
4951

5052
/**

lib/SparkPost/Transmission.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Transmission extends APIResource {
7575
* 'replyTo': string,
7676
* 'rfc822': string,
7777
* 'sandbox': boolean,
78-
* 'startTime': string,
78+
* 'startTime': string | \DateTime,
7979
* 'subject': string,
8080
* 'substitutionData': array,
8181
* 'template': string,
@@ -89,6 +89,11 @@ class Transmission extends APIResource {
8989
* @return array API repsonse represented as key-value pairs
9090
*/
9191
public function send( $transmissionConfig ) {
92+
if(isset($transmissionConfig["startTime"]) && $transmissionConfig["startTime"] instanceof \DateTime)
93+
{
94+
$transmissionConfig["startTime"] = $transmissionConfig["startTime"]->format(\DateTime::ATOM);
95+
}
96+
9297
return $this->create( $transmissionConfig );
9398
}
9499

test/unit/APIResourceTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ public function testGet() {
8484
$this->assertEquals($testBody, $this->resource->get('test'));
8585
}
8686

87+
public function testGetCommaSeparated() {
88+
$testBody = ['results'=>['my'=>'test']];
89+
$requestArray = [
90+
"param1" => "param1val",
91+
"param2" => ["param2val1", "param2val2"]
92+
];
93+
$expectedGetParams = "param1=param1val&param2=" . urlencode("param2val1,param2val2");
94+
95+
$responseMock = Mockery::mock();
96+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
97+
once()->
98+
with(matchesPattern("/.*\/test\?{$expectedGetParams}/"), 'GET', Mockery::type('array'), null)->
99+
andReturn($responseMock);
100+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
101+
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
102+
103+
$this->assertEquals($testBody, $this->resource->get('test', $requestArray));
104+
}
105+
87106
public function testDelete() {
88107
$responseMock = Mockery::mock();
89108
$this->sparkPostMock->httpAdapter->shouldReceive('send')->

test/unit/MessageEventTest.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 MessageEvents($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+
46+
public function testDocumentation() {
47+
$testBody = ['results' => ['my' => 'test']];
48+
$responseMock = Mockery::mock();
49+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
50+
once()->
51+
with("/message-events/events/documentation", 'GET', Mockery::type('array'), null)->
52+
andReturn($responseMock);
53+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
54+
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
55+
56+
$this->assertEquals($testBody, $this->sut->documentation());
57+
}
58+
59+
public function testSamples() {
60+
$testBody = ['results' => ['my' => 'test']];
61+
$responseMock = Mockery::mock();
62+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
63+
once()->
64+
with("/message-events/events/samples?events=".urlencode("delivery,bounce"), 'GET', Mockery::type('array'), null)->
65+
andReturn($responseMock);
66+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
67+
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
68+
69+
$this->assertEquals($testBody, $this->sut->samples(["delivery", "bounce"]));
70+
}
71+
}

test/unit/TransmissionTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ public function testSend() {
4444
$this->assertEquals($responseBody, $this->resource->send($body));
4545
}
4646

47+
public function testSendDateTimeConversion()
48+
{
49+
$testStartTime = new \DateTime("2016-08-27 13:01:02", new \DateTimeZone("UTC"));
50+
51+
$responseMock = Mockery::mock();
52+
$responseBody = ['results'=>'yay'];
53+
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
54+
once()->
55+
with('/.*\/transmissions/', 'POST', Mockery::type('array'), matchesPattern('/"start_time":"2016-08-27T13:01:02\+00:00"/'))->
56+
andReturn($responseMock);
57+
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
58+
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($responseBody));
59+
60+
$this->assertEquals($responseBody, $this->resource->send(['startTime'=>$testStartTime]));
61+
}
62+
4763
public function testAllWithFilter() {
4864
$responseMock = Mockery::mock();
4965
$responseBody = ['results'=>'yay'];

0 commit comments

Comments
 (0)