Skip to content

Commit b678bfc

Browse files
committed
Message Event mocked tests pass
1 parent a49a08c commit b678bfc

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

lib/SparkPost/APIResource.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,6 @@ public function update( $resourcePath, Array $body=[]) {
112112
* @return array Result of the request
113113
*/
114114
public function get( $resourcePath=null, Array $query=[] ) {
115-
foreach($query as $element) {
116-
if(is_array($element)) {
117-
118-
}
119-
}
120115
return $this->callResource( 'get', $resourcePath, ['query'=>$query] );
121116
}
122117

@@ -135,7 +130,8 @@ public function delete( $resourcePath=null, Array $query=[] ) {
135130
/**
136131
* assembles a URL for a request
137132
* @param string $resourcePath path after the initial endpoint
138-
* @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.
139135
* @return string the assembled URL
140136
*/
141137
private function buildUrl($resourcePath, $options) {
@@ -145,6 +141,12 @@ private function buildUrl($resourcePath, $options) {
145141
}
146142

147143
if( !empty($options['query'])) {
144+
foreach($options['query'] as &$element) {
145+
if(is_array($element)) {
146+
$element = implode(",", $element);
147+
}
148+
}
149+
148150
$queryString = http_build_query($options['query']);
149151
$url .= '?'.$queryString;
150152
}

lib/SparkPost/MessageEvent.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
<?php
22
namespace SparkPost;
33

4+
/**
5+
* SDK class for querying message events API
6+
* @package SparkPost
7+
*/
8+
class MessageEvent extends APIResource
9+
{
10+
public $endpoint = 'message-events';
411

5-
class MessageEvent extends APIResource {
6-
public $endpoint = 'message-events';
7-
8-
public function search(Array $queryParams) {
9-
return $this->get(null, $queryParams);
10-
}
12+
/**
13+
* Method for issuing search requests to the Message Events API.
14+
*
15+
* The method passes-through all of the query parameters listed at
16+
* @link https://developers.sparkpost.com/api/#/reference/message-events/events-documentation/search-for-message-events
17+
*
18+
* @param array $queryParams The query parameters. Note that a query parameter containing an array
19+
* is collapsed into a comma-separated list.
20+
*
21+
* @return array The result of the query.
22+
*/
23+
public function search(Array $queryParams)
24+
{
25+
return $this->get(null, $queryParams);
26+
}
1127
}

test/unit/APIResourceTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,21 @@ public function testGet() {
8686

8787
public function testGetCommaSeparated() {
8888
$testBody = ['results'=>['my'=>'test']];
89+
$requestArray = [
90+
"param1" => "param1val",
91+
"param2" => ["param2val1", "param2val2"]
92+
];
93+
$expectedGetParams = "param1=param1val&param2=" . urlencode("param2val1,param2val2");
94+
8995
$responseMock = Mockery::mock();
9096
$this->sparkPostMock->httpAdapter->shouldReceive('send')->
91-
once()->
92-
with('/.*\/test/', 'GET', Mockery::type('array'), null)->
93-
andReturn($responseMock);
97+
once()->
98+
with(matchesPattern("/.*\/test\?{$expectedGetParams}/"), 'GET', Mockery::type('array'), null)->
99+
andReturn($responseMock);
94100
$responseMock->shouldReceive('getStatusCode')->andReturn(200);
95101
$responseMock->shouldReceive('getBody->getContents')->andReturn(json_encode($testBody));
96102

97-
$this->assertEquals($testBody, $this->resource->get(
98-
'test', [ "param1" => "param1val", "param2" => ["param2val1", "param2val2"] ]));
103+
$this->assertEquals($testBody, $this->resource->get('test', $requestArray));
99104
}
100105

101106
public function testDelete() {

0 commit comments

Comments
 (0)