|
8 | 8 |
|
9 | 9 | namespace NexmoTest\Message; |
10 | 10 |
|
| 11 | +use Nexmo\Client\Exception; |
11 | 12 | use Nexmo\Message\Client; |
12 | 13 | use Nexmo\Message\Message; |
13 | 14 | use Nexmo\Message\Text; |
14 | 15 | use NexmoTest\Psr7AssertionTrait; |
| 16 | +use NexmoTest\MessageAssertionTrait; |
15 | 17 | use Prophecy\Argument; |
16 | 18 | use Psr\Http\Message\RequestInterface; |
17 | 19 | use Zend\Diactoros\Request; |
|
20 | 22 | class ClientTest extends \PHPUnit_Framework_TestCase |
21 | 23 | { |
22 | 24 | use Psr7AssertionTrait; |
| 25 | + use MessageAssertionTrait; |
23 | 26 |
|
24 | 27 | protected $nexmoClient; |
25 | 28 |
|
@@ -202,6 +205,53 @@ public function testRateLimitRetires() |
202 | 205 | $this->assertEquals($success, $message->getResponse()); |
203 | 206 | } |
204 | 207 |
|
| 208 | + /** |
| 209 | + * @dataProvider searchRejectionsProvider |
| 210 | + */ |
| 211 | + public function testCanSearchRejections($date, $to, $responseFile, $expectedResponse, $expectedHttpCode, $expectedException) |
| 212 | + { |
| 213 | + $query = new \Nexmo\Message\Query($date, $to); |
| 214 | + |
| 215 | + $apiResponse = $this->getResponse($responseFile, $expectedHttpCode); |
| 216 | + |
| 217 | + $this->nexmoClient->send(Argument::that(function (Request $request) use ($to, $date) { |
| 218 | + $this->assertRequestQueryContains('to', $to, $request); |
| 219 | + $this->assertRequestQueryContains('date', $date->format('Y-m-d'), $request); |
| 220 | + return true; |
| 221 | + }))->willReturn($apiResponse); |
| 222 | + |
| 223 | + |
| 224 | + // If we're expecting this to throw an exception, listen for it in advance |
| 225 | + if ($expectedException !== null) { |
| 226 | + $this->expectException($expectedException); |
| 227 | + $this->expectExceptionMessage($expectedResponse); |
| 228 | + } |
| 229 | + |
| 230 | + // Make the request and assert that our responses match |
| 231 | + $rejectionsResponse = $this->messageClient->searchRejections($query); |
| 232 | + $this->assertListOfMessagesEqual($expectedResponse, $rejectionsResponse); |
| 233 | + } |
| 234 | + |
| 235 | + public function searchRejectionsProvider() |
| 236 | + { |
| 237 | + $r = []; |
| 238 | + |
| 239 | + $r['no rejections found'] = [new \DateTime(), '123456', 'search-rejections-empty', [], 200, null]; |
| 240 | + |
| 241 | + // Build up our expected message object |
| 242 | + $message = new Message('0C0000005BA0B864'); |
| 243 | + $message->setResponse($this->getResponse('search-rejections')); |
| 244 | + $message->setIndex(0); |
| 245 | + $r['rejection found'] = [new \DateTime(), '123456', 'search-rejections', [$message], 200, null]; |
| 246 | + |
| 247 | + $r['error-code provided (validation)'] = [new \DateTime(), '123456', 'search-rejections-error-provided-validation', 'Validation error: You forgot to do something', 400, Exception\Request::class]; |
| 248 | + $r['error-code provided (server error)'] = [new \DateTime(), '123456', 'search-rejections-error-provided-server-error', 'Gremlins! There are gremlins in the system!', 500, Exception\Request::class]; |
| 249 | + $r['error-code not provided'] = [new \DateTime(), '123456', 'empty', 'error status from API', 500, Exception\Request::class]; |
| 250 | + $r['missing items key in response on 200'] = [new \DateTime(), '123456', 'empty', 'unexpected response from API', 200, Exception\Exception::class]; |
| 251 | + |
| 252 | + return $r; |
| 253 | + } |
| 254 | + |
205 | 255 | /** |
206 | 256 | * Get the API response we'd expect for a call to the API. Message API currently returns 200 all the time, so only |
207 | 257 | * change between success / fail is body of the message. |
|
0 commit comments