|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Nexmo Client Library for PHP |
| 4 | + * |
| 5 | + * @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com) |
| 6 | + * @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License |
| 7 | + */ |
| 8 | + |
| 9 | +namespace NexmoTest\Redact; |
| 10 | + |
| 11 | +use Nexmo\Redact\Client; |
| 12 | +use Zend\Diactoros\Response; |
| 13 | +use NexmoTest\Psr7AssertionTrait; |
| 14 | +use Prophecy\Argument; |
| 15 | +use Psr\Http\Message\RequestInterface; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Nexmo\Client\Exception; |
| 18 | + |
| 19 | +class ClientTest extends TestCase |
| 20 | +{ |
| 21 | + use Psr7AssertionTrait; |
| 22 | + |
| 23 | + protected $nexmoClient; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Client |
| 27 | + */ |
| 28 | + protected $redact; |
| 29 | + |
| 30 | + public function setUp() |
| 31 | + { |
| 32 | + $this->nexmoClient = $this->prophesize('Nexmo\Client'); |
| 33 | + $this->redact = new Client(); |
| 34 | + $this->redact->setClient($this->nexmoClient->reveal()); |
| 35 | + } |
| 36 | + |
| 37 | + public function testUrlAndMethod() |
| 38 | + { |
| 39 | + $this->nexmoClient->send(Argument::that(function (RequestInterface $request) { |
| 40 | + $this->assertEquals('/v1/redact/transaction', $request->getUri()->getPath()); |
| 41 | + $this->assertEquals('api.nexmo.com', $request->getUri()->getHost()); |
| 42 | + $this->assertEquals('POST', $request->getMethod()); |
| 43 | + return true; |
| 44 | + }))->shouldBeCalledTimes(1)->willReturn($this->getResponse('success', 204)); |
| 45 | + |
| 46 | + $this->redact->transaction('ABC123', 'sms'); |
| 47 | + } |
| 48 | + |
| 49 | + public function testNoOptions() |
| 50 | + { |
| 51 | + $this->nexmoClient->send(Argument::that(function (RequestInterface $request) { |
| 52 | + $this->assertRequestJsonBodyContains('id', 'ABC123', $request); |
| 53 | + $this->assertRequestJsonBodyContains('product', 'sms', $request); |
| 54 | + |
| 55 | + return true; |
| 56 | + }))->shouldBeCalledTimes(1)->willReturn($this->getResponse('success', 204)); |
| 57 | + |
| 58 | + $this->redact->transaction('ABC123', 'sms'); |
| 59 | + } |
| 60 | + |
| 61 | + public function testWithOptions() |
| 62 | + { |
| 63 | + $this->nexmoClient->send(Argument::that(function (RequestInterface $request) { |
| 64 | + $this->assertRequestJsonBodyContains('id', 'ABC123', $request); |
| 65 | + $this->assertRequestJsonBodyContains('product', 'sms', $request); |
| 66 | + $this->assertRequestJsonBodyContains('type', 'inbound', $request); |
| 67 | + return true; |
| 68 | + }))->shouldBeCalledTimes(1)->willReturn($this->getResponse('success', 204)); |
| 69 | + |
| 70 | + $this->redact->transaction('ABC123', 'sms', ['type' => 'inbound']); |
| 71 | + } |
| 72 | + |
| 73 | + public function testOptionsDoNotOverwriteParams() |
| 74 | + { |
| 75 | + $this->nexmoClient->send(Argument::that(function (RequestInterface $request) { |
| 76 | + $this->assertRequestJsonBodyContains('id', 'ABC123', $request); |
| 77 | + $this->assertRequestJsonBodyContains('product', 'sms', $request); |
| 78 | + $this->assertRequestJsonBodyContains('type', 'inbound', $request); |
| 79 | + return true; |
| 80 | + }))->shouldBeCalledTimes(1)->willReturn($this->getResponse('success', 204)); |
| 81 | + |
| 82 | + $this->redact->transaction('ABC123', 'sms', ['id' => 'ZZZ', 'type' => 'inbound']); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @dataProvider exceptionsProvider |
| 87 | + */ |
| 88 | + public function testExceptions($response, $code, $expectedException, $expectedMessage) |
| 89 | + { |
| 90 | + $this->setExpectedException($expectedException, $expectedMessage); |
| 91 | + |
| 92 | + $this->nexmoClient->send(Argument::that(function (RequestInterface $request) { |
| 93 | + return true; |
| 94 | + }))->shouldBeCalledTimes(1)->willReturn($this->getResponse($response, $code)); |
| 95 | + |
| 96 | + $this->redact->transaction('ABC123', 'sms'); |
| 97 | + } |
| 98 | + |
| 99 | + public function exceptionsProvider() { |
| 100 | + return [ |
| 101 | + 'unauthorized' => ['unauthorized', 401, Exception\Request::class, "Unauthorized"], |
| 102 | + 'premature-redaction' => ['premature-redaction', 403, Exception\Request::class, "Premature Redaction - You must wait 60 minutes before redacting ID '0A000000B0C9A1234'. See https://developer.nexmo.com/api-errors/redact#premature-redaction"], |
| 103 | + 'unprovisioned' => ['unprovisioned', 403, Exception\Request::class, "Authorisation error - User=ABC123 is not provisioned to redact product=SMS. See https://developer.nexmo.com/api-errors#unprovisioned"], |
| 104 | + 'invalid-id' => ['invalid-id', 404, Exception\Request::class, "Invalid ID - ID '0A000000B0C9A1234' could not be found (type=MT). See https://developer.nexmo.com/api-errors#invalid-id"], |
| 105 | + 'invalid-json' => ['invalid-json', 422, Exception\Request::class, "Invalid JSON - Unexpected character ('\"' (code 34)): was expecting comma to separate Object entries. See https://developer.nexmo.com/api-errors#invalid-json"], |
| 106 | + 'unsupported-product' => ['unsupported-product', 422, Exception\Request::class, "Invalid Product - No product corresponding to supplied string sms2!. See https://developer.nexmo.com/api-errors/redact#invalid-product"], |
| 107 | + 'unknown-error' => ['error', 500, Exception\Server::class, "Unexpected error"], |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Get the API response we'd expect for a call to the API. |
| 113 | + * |
| 114 | + * @param string $type |
| 115 | + * @return Response |
| 116 | + */ |
| 117 | + protected function getResponse($type = 'success', $status = 200) |
| 118 | + { |
| 119 | + return new Response(fopen(__DIR__ . '/responses/' . $type . '.json', 'r'), $status); |
| 120 | + } |
| 121 | +} |
0 commit comments