diff options
author | James Fellows <james.fellows@finanscapes.com> | 2016-04-03 21:32:25 +0100 |
---|---|---|
committer | James Fellows <james.fellows@finanscapes.com> | 2016-04-03 21:32:25 +0100 |
commit | b678bfce586d89077d3cacd5ba8a936667fe96e6 (patch) | |
tree | 5dd6ca6eec4febc1a4cfcd38e4571137cd44a8be | |
parent | a49a08ca3cb893e4589c7b925e04a938dfe18c84 (diff) | |
download | php-sparkpost-b678bfce586d89077d3cacd5ba8a936667fe96e6.zip php-sparkpost-b678bfce586d89077d3cacd5ba8a936667fe96e6.tar.gz php-sparkpost-b678bfce586d89077d3cacd5ba8a936667fe96e6.tar.bz2 |
Message Event mocked tests pass
-rw-r--r-- | lib/SparkPost/APIResource.php | 14 | ||||
-rw-r--r-- | lib/SparkPost/MessageEvent.php | 28 | ||||
-rw-r--r-- | test/unit/APIResourceTest.php | 15 |
3 files changed, 40 insertions, 17 deletions
diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php index d0b8a5b..71c6a26 100644 --- a/lib/SparkPost/APIResource.php +++ b/lib/SparkPost/APIResource.php @@ -112,11 +112,6 @@ class APIResource { * @return array Result of the request */ public function get( $resourcePath=null, Array $query=[] ) { - foreach($query as $element) { - if(is_array($element)) { - - } - } return $this->callResource( 'get', $resourcePath, ['query'=>$query] ); } @@ -135,7 +130,8 @@ class APIResource { /** * assembles a URL for a request * @param string $resourcePath path after the initial endpoint - * @param array $options array with an optional value of query with values to build a querystring from. + * @param array $options array with an optional value of query with values to build a querystring from. Any + * query elements that are themselves arrays will be imploded into a comma separated list. * @return string the assembled URL */ private function buildUrl($resourcePath, $options) { @@ -145,6 +141,12 @@ class APIResource { } if( !empty($options['query'])) { + foreach($options['query'] as &$element) { + if(is_array($element)) { + $element = implode(",", $element); + } + } + $queryString = http_build_query($options['query']); $url .= '?'.$queryString; } diff --git a/lib/SparkPost/MessageEvent.php b/lib/SparkPost/MessageEvent.php index a0ab16e..a72fb49 100644 --- a/lib/SparkPost/MessageEvent.php +++ b/lib/SparkPost/MessageEvent.php @@ -1,11 +1,27 @@ <?php namespace SparkPost; +/** + * SDK class for querying message events API + * @package SparkPost + */ +class MessageEvent extends APIResource +{ + public $endpoint = 'message-events'; -class MessageEvent extends APIResource { - public $endpoint = 'message-events'; - - public function search(Array $queryParams) { - return $this->get(null, $queryParams); - } + /** + * Method for issuing search requests to the Message Events API. + * + * The method passes-through all of the query parameters 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 + * is collapsed into a comma-separated list. + * + * @return array The result of the query. + */ + public function search(Array $queryParams) + { + return $this->get(null, $queryParams); + } }
\ No newline at end of file diff --git a/test/unit/APIResourceTest.php b/test/unit/APIResourceTest.php index 7163255..0c4fb16 100644 --- a/test/unit/APIResourceTest.php +++ b/test/unit/APIResourceTest.php @@ -86,16 +86,21 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase { public function testGetCommaSeparated() { $testBody = ['results'=>['my'=>'test']]; + $requestArray = [ + "param1" => "param1val", + "param2" => ["param2val1", "param2val2"] + ]; + $expectedGetParams = "param1=param1val¶m2=" . urlencode("param2val1,param2val2"); + $responseMock = Mockery::mock(); $this->sparkPostMock->httpAdapter->shouldReceive('send')-> - once()-> - with('/.*\/test/', 'GET', Mockery::type('array'), null)-> - andReturn($responseMock); + once()-> + with(matchesPattern("/.*\/test\?{$expectedGetParams}/"), 'GET', Mockery::type('array'), null)-> + andReturn($responseMock); $responseMock->shouldReceive('getStatusCode')->andReturn(200); $responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody)); - $this->assertEquals($testBody, $this->resource->get( - 'test', [ "param1" => "param1val", "param2" => ["param2val1", "param2val2"] ])); + $this->assertEquals($testBody, $this->resource->get('test', $requestArray)); } public function testDelete() { |