diff options
author | James Fellows <james.fellows@finanscapes.com> | 2016-04-04 21:28:34 +0100 |
---|---|---|
committer | James Fellows <james.fellows@finanscapes.com> | 2016-04-04 21:28:34 +0100 |
commit | 57c509fd9110dc33f5704020bdb72b82796fb2d9 (patch) | |
tree | f19d276b814c62835d685ad455811bc59ff9fcba | |
parent | b678bfce586d89077d3cacd5ba8a936667fe96e6 (diff) | |
download | php-sparkpost-57c509fd9110dc33f5704020bdb72b82796fb2d9.zip php-sparkpost-57c509fd9110dc33f5704020bdb72b82796fb2d9.tar.gz php-sparkpost-57c509fd9110dc33f5704020bdb72b82796fb2d9.tar.bz2 |
Add tests for new MessageEvent DateTime handling
-rw-r--r-- | lib/SparkPost/MessageEvent.php | 31 | ||||
-rw-r--r-- | lib/SparkPost/SparkPost.php | 2 | ||||
-rw-r--r-- | test/unit/MessageEventTest.php | 45 |
3 files changed, 75 insertions, 3 deletions
diff --git a/lib/SparkPost/MessageEvent.php b/lib/SparkPost/MessageEvent.php index a72fb49..e770642 100644 --- a/lib/SparkPost/MessageEvent.php +++ b/lib/SparkPost/MessageEvent.php @@ -2,8 +2,9 @@ namespace SparkPost; /** - * SDK class for querying message events API - * @package SparkPost + * SDK class for querying the Message Events API + * + * @see https://developers.sparkpost.com/api/#/reference/message-events */ class MessageEvent extends APIResource { @@ -12,7 +13,7 @@ class MessageEvent extends APIResource /** * Method for issuing search requests to the Message Events API. * - * The method passes-through all of the query parameters listed at + * The method passes-through all of the query parameters - the valid ones are listed at * @link https://developers.sparkpost.com/api/#/reference/message-events/events-documentation/search-for-message-events * * @param array $queryParams The query parameters. Note that a query parameter containing an array @@ -22,6 +23,30 @@ class MessageEvent extends APIResource */ public function search(Array $queryParams) { + // check for DateTime objects & replace them with the formatted string equivalent + foreach(["from", "to"] as $dateTimeParam) { + if (isset($queryParams[$dateTimeParam]) && $queryParams[$dateTimeParam] instanceof \DateTime) { + // the message events API doesn't allow the seconds or GMT offset, so strip them + $queryParams[$dateTimeParam] = substr($queryParams[$dateTimeParam]->format(\DateTime::ATOM), 0, 16); + } + } + return $this->get(null, $queryParams); } + + /** + * List descriptions of the event fields that could be included in a response from the MessageEvent::search() method. + * + * @return array The event field descriptions. + */ + public function documentation() { + return $this->get("events/documentation"); + } + + /** + * List an example of the event data that will be included in a response from the MessageEvent::search() method. + */ + public function samples() { + return $this->get("events/samples", ["events" => "bounce"]); + } }
\ No newline at end of file diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index 46623c9..0e576fe 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -6,6 +6,7 @@ use Ivory\HttpAdapter\HttpAdapterInterface; class SparkPost { public $transmission; + public $messageEvent; /** * Connection config for making requests. @@ -45,6 +46,7 @@ class SparkPost { $this->setHttpAdapter($httpAdapter); $this->transmission = new Transmission($this); + $this->messageEvent = new MessageEvent($this); } /** diff --git a/test/unit/MessageEventTest.php b/test/unit/MessageEventTest.php new file mode 100644 index 0000000..a91d17a --- /dev/null +++ b/test/unit/MessageEventTest.php @@ -0,0 +1,45 @@ +<?php + +namespace SparkPost; + +use \Mockery; + + +class MessageEventTest extends \PHPUnit_Framework_TestCase +{ + private $sparkPostMock; + private $sut; + + /** + * (non-PHPdoc) + * @before + * @see PHPUnit_Framework_TestCase::setUp() + */ + public function setUp() + { + $this->sparkPostMock = Mockery::mock('SparkPost\SparkPost', function ($mock) { + $mock->shouldReceive('getHttpHeaders')->andReturn([]); + }); + $this->sparkPostMock->httpAdapter = Mockery::mock(); + $this->sut = new MessageEvent($this->sparkPostMock); + } + + public function testDateTimeConversion() + { + $testBody = ['results' => ['my' => 'test']]; + $testFrom = new \DateTime("1978-08-27 04:05:02"); + $testFromStr = urlencode("1978-08-27T04:05"); + $testTo = new \DateTime("2016-04-04 19:00"); + $testToStr = urlencode("2016-04-04T19:00"); + + $responseMock = Mockery::mock(); + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + with("/message-events/?from={$testFromStr}&to={$testToStr}", 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(200); + $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); + + $this->assertEquals($testBody, $this->sut->search(["from" => $testFrom, "to" => $testTo])); + } +}
\ No newline at end of file |