@@ -19,6 +19,14 @@ class SparkPostTest extends \PHPUnit_Framework_TestCase
1919 /** @var SparkPost */
2020 private $ resource ;
2121
22+ private $ exceptionMock ;
23+ private $ exceptionBody ;
24+
25+ private $ responseMock ;
26+ private $ responseBody ;
27+
28+ private $ promiseMock ;
29+
2230 private $ postTransmissionPayload = [
2331 'content ' => [
2432 'from ' => ['name ' => 'Sparkpost Team ' , 'email ' => 'postmaster@sendmailfor.me ' ],
@@ -44,8 +52,30 @@ class SparkPostTest extends \PHPUnit_Framework_TestCase
4452 */
4553 public function setUp ()
4654 {
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+
4774 //setup mock for the adapter
4875 $ this ->clientMock = Mockery::mock ('Http\Adapter\Guzzle6\Client ' );
76+ $ this ->clientMock ->shouldReceive ('sendAsyncRequest ' )->
77+ with (Mockery::type ('GuzzleHttp\Psr7\Request ' ))->
78+ andReturn ($ this ->promiseMock );
4979
5080 $ this ->resource = new SparkPost ($ this ->clientMock , ['key ' => 'SPARKPOST_API_KEY ' ]);
5181 }
@@ -55,187 +85,142 @@ public function tearDown()
5585 Mockery::close ();
5686 }
5787
58- public function testRequest ()
88+ public function testRequestSync ()
5989 {
60- $ responseMock = Mockery::mock ('Psr\Http\Message\ResponseInterface ' );
6190 $ this ->resource ->setOptions (['async ' => false ]);
62- $ this ->clientMock ->shouldReceive ('sendRequest ' )->andReturn ($ responseMock );
91+ $ this ->clientMock ->shouldReceive ('sendRequest ' )->andReturn ($ this ->responseMock );
92+
6393 $ this ->assertInstanceOf ('SparkPost\SparkPostResponse ' , $ this ->resource ->request ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload ));
94+ }
6495
96+ public function testRequestAsync ()
97+ {
6598 $ promiseMock = Mockery::mock ('Http\Promise\Promise ' );
6699 $ this ->resource ->setOptions (['async ' => true ]);
67100 $ this ->clientMock ->shouldReceive ('sendAsyncRequest ' )->andReturn ($ promiseMock );
101+
68102 $ this ->assertInstanceOf ('SparkPost\SparkPostPromise ' , $ this ->resource ->request ('GET ' , 'transmissions ' , $ this ->getTransmissionPayload ));
69103 }
70104
71105 public function testDebugOptionWhenFalse () {
72- $ responseMock = Mockery::mock ('Psr\Http\Message\ResponseInterface ' );
73106 $ this ->resource ->setOptions (['async ' => false , 'debug ' => false ]);
74- $ this ->clientMock ->shouldReceive ('sendRequest ' )->andReturn ($ responseMock );
107+ $ this ->clientMock ->shouldReceive ('sendRequest ' )->andReturn ($ this ->responseMock );
108+
75109 $ response = $ this ->resource ->request ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
110+
76111 $ this ->assertEquals ($ response ->getRequest (), null );
77112 }
78113
79114 public function testDebugOptionWhenTrue () {
80- $ responseMock = Mockery:: mock ( ' Psr\Http\Message\ResponseInterface ' );
115+ // setup
81116 $ 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 );
83120 $ response = $ this ->resource ->request ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
84121 $ 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+ }
85132 }
86133
87134 public function testSuccessfulSyncRequest ()
88135 {
89- $ responseMock = Mockery::mock ('Psr\Http\Message\ResponseInterface ' );
90- $ responseBodyMock = Mockery::mock ();
91-
92- $ responseBody = ['results ' => 'yay ' ];
93-
94136 $ this ->clientMock ->shouldReceive ('sendRequest ' )->
95137 once ()->
96138 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 );
102140
103141 $ response = $ this ->resource ->syncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
104142
105- $ this ->assertEquals ($ responseBody , $ response ->getBody ());
143+ $ this ->assertEquals ($ this -> responseBody , $ response ->getBody ());
106144 $ this ->assertEquals (200 , $ response ->getStatusCode ());
107145 }
108146
109147 public function testUnsuccessfulSyncRequest ()
110148 {
111- $ exceptionMock = Mockery::mock ('Http\Client\Exception\HttpException ' );
112-
113- $ responseBody = ['results ' => 'failed ' ];
114-
115149 $ this ->clientMock ->shouldReceive ('sendRequest ' )->
116150 once ()->
117151 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 );
122153
123154 try {
124155 $ this ->resource ->syncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
125156 } catch (\Exception $ e ) {
126- $ this ->assertEquals ($ responseBody , $ e ->getBody ());
157+ $ this ->assertEquals ($ this -> exceptionBody , $ e ->getBody ());
127158 $ this ->assertEquals (500 , $ e ->getCode ());
128159 }
129160 }
130161
131162 public function testSuccessfulAsyncRequestWithWait ()
132163 {
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 );
149165
150166 $ promise = $ this ->resource ->asyncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
151-
152167 $ response = $ promise ->wait ();
153168
154- $ this ->assertEquals ($ responseBody , $ response ->getBody ());
169+ $ this ->assertEquals ($ this -> responseBody , $ response ->getBody ());
155170 $ this ->assertEquals (200 , $ response ->getStatusCode ());
156171 }
157172
158173 public function testUnsuccessfulAsyncRequestWithWait ()
159174 {
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 );
174176
175177 $ promise = $ this ->resource ->asyncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
176178
177179 try {
178180 $ response = $ promise ->wait ();
179181 } catch (\Exception $ e ) {
180- $ this ->assertEquals ($ responseBody , $ e ->getBody ());
182+ $ this ->assertEquals ($ this -> exceptionBody , $ e ->getBody ());
181183 $ this ->assertEquals (500 , $ e ->getCode ());
182184 }
183185 }
184186
185187 public function testSuccessfulAsyncRequestWithThen ()
186188 {
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 , []);
193191
194- $ guzzlePromise = new GuzzleFulfilledPromise ( $ responseMock );
192+ $ promise = new SparkPostPromise ( new GuzzleAdapterPromise ( $ guzzlePromise , $ result ) );
195193
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 ());
201198 }, null )->wait ();
202199 }
203200
204201 public function testUnsuccessfulAsyncRequestWithThen ()
205202 {
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 , []);
214205
215- $ promise = new SparkPostPromise (new GuzzleAdapterPromise ($ guzzlePromise , $ request ));
206+ $ promise = new SparkPostPromise (new GuzzleAdapterPromise ($ guzzlePromise , $ result ));
216207
217- $ promise ->then (null , function ($ exception ) use ($ responseBody ) {
208+ $ exceptionBody = $ this ->exceptionBody ;
209+ $ promise ->then (null , function ($ exception ) use ($ exceptionBody ) {
218210 $ this ->assertEquals (500 , $ exception ->getCode ());
219- $ this ->assertEquals ($ responseBody , $ exception ->getBody ());
211+ $ this ->assertEquals ($ exceptionBody , $ exception ->getBody ());
220212 })->wait ();
221213 }
222214
223215 public function testPromise ()
224216 {
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-
232217 $ promise = $ this ->resource ->asyncRequest ('POST ' , 'transmissions ' , $ this ->postTransmissionPayload );
233218
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 ());
236221
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 ());
239224 }
240225
241226 /**
0 commit comments