@@ -19,6 +19,14 @@ class SparkPostTest extends \PHPUnit_Framework_TestCase
19
19
/** @var SparkPost */
20
20
private $ resource ;
21
21
22
+ private $ exceptionMock ;
23
+ private $ exceptionBody ;
24
+
25
+ private $ responseMock ;
26
+ private $ responseBody ;
27
+
28
+ private $ promiseMock ;
29
+
22
30
private $ postTransmissionPayload = [
23
31
'content ' => [
24
32
'from ' => ['name ' => 'Sparkpost Team ' , 'email ' => 'postmaster@sendmailfor.me ' ],
@@ -44,8 +52,30 @@ class SparkPostTest extends \PHPUnit_Framework_TestCase
44
52
*/
45
53
public function setUp ()
46
54
{
55
+ // response mock up
56
+ $ responseBodyMock = Mockery::mock ();
57
+ $ this ->responseBody = ['results ' => 'yay ' ];
58
+ $ this ->responseMock = Mockery::mock ('Psr\Http\Message\ResponseInterface ' );
59
+ $ this ->responseMock ->shouldReceive ('getStatusCode ' )->andReturn (200 );
60
+ $ this ->responseMock ->shouldReceive ('getBody ' )->andReturn ($ responseBodyMock );
61
+ $ responseBodyMock ->shouldReceive ('__toString ' )->andReturn (json_encode ($ this ->responseBody ));
62
+
63
+ // exception mock up
64
+ $ exceptionResponseMock = Mockery::mock ();
65
+ $ this ->exceptionBody = ['results ' => 'failed ' ];
66
+ $ this ->exceptionMock = Mockery::mock ('Http\Client\Exception\HttpException ' );
67
+ $ this ->exceptionMock ->shouldReceive ('getResponse ' )->andReturn ($ exceptionResponseMock );
68
+ $ exceptionResponseMock ->shouldReceive ('getStatusCode ' )->andReturn (500 );
69
+ $ exceptionResponseMock ->shouldReceive ('getBody->__toString ' )->andReturn (json_encode ($ this ->exceptionBody ));
70
+
71
+ // promise mock up
72
+ $ this ->promiseMock = Mockery::mock ('Http\Promise\Promise ' );
73
+
47
74
//setup mock for the adapter
48
75
$ this ->clientMock = Mockery::mock ('Http\Adapter\Guzzle6\Client ' );
76
+ $ this ->clientMock ->shouldReceive ('sendAsyncRequest ' )->
77
+ with (Mockery::type ('GuzzleHttp\Psr7\Request ' ))->
78
+ andReturn ($ this ->promiseMock );
49
79
50
80
$ this ->resource = new SparkPost ($ this ->clientMock , ['key ' => 'SPARKPOST_API_KEY ' ]);
51
81
}
@@ -55,187 +85,142 @@ public function tearDown()
55
85
Mockery::close ();
56
86
}
57
87
58
- public function testRequest ()
88
+ public function testRequestSync ()
59
89
{
60
- $ responseMock = Mockery::mock ('Psr\Http\Message\ResponseInterface ' );
61
90
$ this ->resource ->setOptions (['async ' => false ]);
62
- $ this ->clientMock ->shouldReceive ('sendRequest ' )->andReturn ($ responseMock );
91
+ $ this ->clientMock ->shouldReceive ('sendRequest ' )->andReturn ($ this ->responseMock );
92
+
63
93
$ this ->assertInstanceOf ('SparkPost\SparkPostResponse ' , $ this ->resource ->request ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload ));
94
+ }
64
95
96
+ public function testRequestAsync ()
97
+ {
65
98
$ promiseMock = Mockery::mock ('Http\Promise\Promise ' );
66
99
$ this ->resource ->setOptions (['async ' => true ]);
67
100
$ this ->clientMock ->shouldReceive ('sendAsyncRequest ' )->andReturn ($ promiseMock );
101
+
68
102
$ this ->assertInstanceOf ('SparkPost\SparkPostPromise ' , $ this ->resource ->request ('GET ' , 'transmissions ' , $ this ->getTransmissionPayload ));
69
103
}
70
104
71
105
public function testDebugOptionWhenFalse () {
72
- $ responseMock = Mockery::mock ('Psr\Http\Message\ResponseInterface ' );
73
106
$ this ->resource ->setOptions (['async ' => false , 'debug ' => false ]);
74
- $ this ->clientMock ->shouldReceive ('sendRequest ' )->andReturn ($ responseMock );
107
+ $ this ->clientMock ->shouldReceive ('sendRequest ' )->andReturn ($ this ->responseMock );
108
+
75
109
$ response = $ this ->resource ->request ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
110
+
76
111
$ this ->assertEquals ($ response ->getRequest (), null );
77
112
}
78
113
79
114
public function testDebugOptionWhenTrue () {
80
- $ responseMock = Mockery:: mock ( ' Psr\Http\Message\ResponseInterface ' );
115
+ // setup
81
116
$ this ->resource ->setOptions (['async ' => false , 'debug ' => true ]);
82
- $ this ->clientMock ->shouldReceive ('sendRequest ' )->andReturn ($ responseMock );
117
+
118
+ // successful
119
+ $ this ->clientMock ->shouldReceive ('sendRequest ' )->once ()->andReturn ($ this ->responseMock );
83
120
$ response = $ this ->resource ->request ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
84
121
$ this ->assertEquals (json_decode ($ response ->getRequest ()['body ' ], true ), $ this ->postTransmissionPayload );
122
+
123
+ // unsuccessful
124
+ $ this ->clientMock ->shouldReceive ('sendRequest ' )->once ()->andThrow ($ this ->exceptionMock );
125
+
126
+ try {
127
+ $ response = $ this ->resource ->request ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
128
+ }
129
+ catch (\Exception $ e ) {
130
+ $ this ->assertEquals (json_decode ($ e ->getRequest ()['body ' ], true ), $ this ->postTransmissionPayload );
131
+ }
85
132
}
86
133
87
134
public function testSuccessfulSyncRequest ()
88
135
{
89
- $ responseMock = Mockery::mock ('Psr\Http\Message\ResponseInterface ' );
90
- $ responseBodyMock = Mockery::mock ();
91
-
92
- $ responseBody = ['results ' => 'yay ' ];
93
-
94
136
$ this ->clientMock ->shouldReceive ('sendRequest ' )->
95
137
once ()->
96
138
with (Mockery::type ('GuzzleHttp\Psr7\Request ' ))->
97
- andReturn ($ responseMock );
98
-
99
- $ responseMock ->shouldReceive ('getStatusCode ' )->andReturn (200 );
100
- $ responseMock ->shouldReceive ('getBody ' )->andReturn ($ responseBodyMock );
101
- $ responseBodyMock ->shouldReceive ('__toString ' )->andReturn (json_encode ($ responseBody ));
139
+ andReturn ($ this ->responseMock );
102
140
103
141
$ response = $ this ->resource ->syncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
104
142
105
- $ this ->assertEquals ($ responseBody , $ response ->getBody ());
143
+ $ this ->assertEquals ($ this -> responseBody , $ response ->getBody ());
106
144
$ this ->assertEquals (200 , $ response ->getStatusCode ());
107
145
}
108
146
109
147
public function testUnsuccessfulSyncRequest ()
110
148
{
111
- $ exceptionMock = Mockery::mock ('Http\Client\Exception\HttpException ' );
112
-
113
- $ responseBody = ['results ' => 'failed ' ];
114
-
115
149
$ this ->clientMock ->shouldReceive ('sendRequest ' )->
116
150
once ()->
117
151
with (Mockery::type ('GuzzleHttp\Psr7\Request ' ))->
118
- andThrow ($ exceptionMock );
119
-
120
- $ exceptionMock ->shouldReceive ('getResponse->getStatusCode ' )->andReturn (500 );
121
- $ exceptionMock ->shouldReceive ('getResponse->getBody->__toString ' )->andReturn (json_encode ($ responseBody ));
152
+ andThrow ($ this ->exceptionMock );
122
153
123
154
try {
124
155
$ this ->resource ->syncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
125
156
} catch (\Exception $ e ) {
126
- $ this ->assertEquals ($ responseBody , $ e ->getBody ());
157
+ $ this ->assertEquals ($ this -> exceptionBody , $ e ->getBody ());
127
158
$ this ->assertEquals (500 , $ e ->getCode ());
128
159
}
129
160
}
130
161
131
162
public function testSuccessfulAsyncRequestWithWait ()
132
163
{
133
- $ promiseMock = Mockery::mock ('Http\Promise\Promise ' );
134
- $ responseMock = Mockery::mock ('Psr\Http\Message\ResponseInterface ' );
135
- $ responseBodyMock = Mockery::mock ();
136
-
137
- $ responseBody = ['results ' => 'yay ' ];
138
-
139
- $ this ->clientMock ->shouldReceive ('sendAsyncRequest ' )->
140
- once ()->
141
- with (Mockery::type ('GuzzleHttp\Psr7\Request ' ))->
142
- andReturn ($ promiseMock );
143
-
144
- $ promiseMock ->shouldReceive ('wait ' )->andReturn ($ responseMock );
145
-
146
- $ responseMock ->shouldReceive ('getStatusCode ' )->andReturn (200 );
147
- $ responseMock ->shouldReceive ('getBody ' )->andReturn ($ responseBodyMock );
148
- $ responseBodyMock ->shouldReceive ('__toString ' )->andReturn (json_encode ($ responseBody ));
164
+ $ this ->promiseMock ->shouldReceive ('wait ' )->andReturn ($ this ->responseMock );
149
165
150
166
$ promise = $ this ->resource ->asyncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
151
-
152
167
$ response = $ promise ->wait ();
153
168
154
- $ this ->assertEquals ($ responseBody , $ response ->getBody ());
169
+ $ this ->assertEquals ($ this -> responseBody , $ response ->getBody ());
155
170
$ this ->assertEquals (200 , $ response ->getStatusCode ());
156
171
}
157
172
158
173
public function testUnsuccessfulAsyncRequestWithWait ()
159
174
{
160
- $ promiseMock = Mockery::mock ('Http\Promise\Promise ' );
161
- $ exceptionMock = Mockery::mock ('Http\Client\Exception\HttpException ' );
162
-
163
- $ responseBody = ['results ' => 'failed ' ];
164
-
165
- $ this ->clientMock ->shouldReceive ('sendAsyncRequest ' )->
166
- once ()->
167
- with (Mockery::type ('GuzzleHttp\Psr7\Request ' ))->
168
- andReturn ($ promiseMock );
169
-
170
- $ promiseMock ->shouldReceive ('wait ' )->andThrow ($ exceptionMock );
171
-
172
- $ exceptionMock ->shouldReceive ('getResponse->getStatusCode ' )->andReturn (500 );
173
- $ exceptionMock ->shouldReceive ('getResponse->getBody->__toString ' )->andReturn (json_encode ($ responseBody ));
175
+ $ this ->promiseMock ->shouldReceive ('wait ' )->andThrow ($ this ->exceptionMock );
174
176
175
177
$ promise = $ this ->resource ->asyncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
176
178
177
179
try {
178
180
$ response = $ promise ->wait ();
179
181
} catch (\Exception $ e ) {
180
- $ this ->assertEquals ($ responseBody , $ e ->getBody ());
182
+ $ this ->assertEquals ($ this -> exceptionBody , $ e ->getBody ());
181
183
$ this ->assertEquals (500 , $ e ->getCode ());
182
184
}
183
185
}
184
186
185
187
public function testSuccessfulAsyncRequestWithThen ()
186
188
{
187
- $ responseBody = ['results ' => 'yay ' ];
188
- $ responseMock = Mockery::mock ('Psr\Http\Message\ResponseInterface ' );
189
- $ responseBodyMock = Mockery::mock ();
190
- $ responseMock ->shouldReceive ('getStatusCode ' )->andReturn (200 );
191
- $ responseMock ->shouldReceive ('getBody ' )->andReturn ($ responseBodyMock );
192
- $ responseBodyMock ->shouldReceive ('__toString ' )->andReturn (json_encode ($ responseBody ));
189
+ $ guzzlePromise = new GuzzleFulfilledPromise ($ this ->responseMock );
190
+ $ result = $ this ->resource ->buildRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload , []);
193
191
194
- $ guzzlePromise = new GuzzleFulfilledPromise ( $ responseMock );
192
+ $ promise = new SparkPostPromise ( new GuzzleAdapterPromise ( $ guzzlePromise , $ result ) );
195
193
196
- $ promise = new SparkPostPromise (new GuzzleAdapterPromise ($ guzzlePromise , $ this ->resource ->buildRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload , [])));
197
-
198
- $ promise ->then (function ($ exception ) use ($ responseBody ) {
199
- $ this ->assertEquals (200 , $ exception ->getStatusCode ());
200
- $ this ->assertEquals ($ responseBody , $ exception ->getBody ());
194
+ $ responseBody = $ this ->responseBody ;
195
+ $ promise ->then (function ($ response ) use ($ responseBody ) {
196
+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
197
+ $ this ->assertEquals ($ responseBody , $ response ->getBody ());
201
198
}, null )->wait ();
202
199
}
203
200
204
201
public function testUnsuccessfulAsyncRequestWithThen ()
205
202
{
206
- $ responseBody = ['results ' => 'failed ' ];
207
- $ exceptionMock = Mockery::mock ('Http\Client\Exception\HttpException ' );
208
- $ exceptionMock ->shouldReceive ('getResponse->getStatusCode ' )->andReturn (500 );
209
- $ exceptionMock ->shouldReceive ('getResponse->getBody->__toString ' )->andReturn (json_encode ($ responseBody ));
210
-
211
- $ guzzlePromise = new GuzzleRejectedPromise ($ exceptionMock );
212
-
213
- $ request = $ this ->resource ->buildRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload , []);
203
+ $ guzzlePromise = new GuzzleRejectedPromise ($ this ->exceptionMock );
204
+ $ result = $ this ->resource ->buildRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload , []);
214
205
215
- $ promise = new SparkPostPromise (new GuzzleAdapterPromise ($ guzzlePromise , $ request ));
206
+ $ promise = new SparkPostPromise (new GuzzleAdapterPromise ($ guzzlePromise , $ result ));
216
207
217
- $ promise ->then (null , function ($ exception ) use ($ responseBody ) {
208
+ $ exceptionBody = $ this ->exceptionBody ;
209
+ $ promise ->then (null , function ($ exception ) use ($ exceptionBody ) {
218
210
$ this ->assertEquals (500 , $ exception ->getCode ());
219
- $ this ->assertEquals ($ responseBody , $ exception ->getBody ());
211
+ $ this ->assertEquals ($ exceptionBody , $ exception ->getBody ());
220
212
})->wait ();
221
213
}
222
214
223
215
public function testPromise ()
224
216
{
225
- $ promiseMock = Mockery::mock ('Http\Promise\Promise ' );
226
-
227
- $ this ->clientMock ->shouldReceive ('sendAsyncRequest ' )->
228
- once ()->
229
- with (Mockery::type ('GuzzleHttp\Psr7\Request ' ))->
230
- andReturn ($ promiseMock );
231
-
232
217
$ promise = $ this ->resource ->asyncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
233
218
234
- $ promiseMock ->shouldReceive ('getState ' )->twice ()->andReturn ('pending ' );
235
- $ this ->assertEquals ($ promiseMock ->getState (), $ promise ->getState ());
219
+ $ this -> promiseMock ->shouldReceive ('getState ' )->twice ()->andReturn ('pending ' );
220
+ $ this ->assertEquals ($ this -> promiseMock ->getState (), $ promise ->getState ());
236
221
237
- $ promiseMock ->shouldReceive ('getState ' )->once ()->andReturn ('rejected ' );
238
- $ this ->assertEquals (' rejected ' , $ promise ->getState ());
222
+ $ this -> promiseMock ->shouldReceive ('getState ' )->twice ()->andReturn ('rejected ' );
223
+ $ this ->assertEquals ($ this -> promiseMock -> getState () , $ promise ->getState ());
239
224
}
240
225
241
226
/**
0 commit comments