|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SparkPost\Test; |
| 4 | + |
| 5 | +use SparkPost\SparkPost; |
| 6 | +use SparkPost\SparkPostResponse; |
| 7 | +use SparkPost\SparkPostPromise; |
| 8 | +use Psr\Http\Message\ResponseInterface; |
| 9 | +use Http\Promise\Promise; |
| 10 | +use GuzzleHttp\Promise\Promise as GuzzlePromise; |
| 11 | +use GuzzleHttp\Promise\FulfilledPromise as GuzzleFulfilledPromise; |
| 12 | +use GuzzleHttp\Promise\RejectedPromise as GuzzleRejectedPromise; |
| 13 | +use Http\Adapter\Guzzle6\Promise as GuzzleAdapterPromise; |
| 14 | +use Http\Client\Exception\HttpException; |
| 15 | +use Http\Adapter\Guzzle6\Client; |
| 16 | +use Mockery; |
| 17 | +use SparkPost\Test\TestUtils\ClassUtils; |
| 18 | + |
| 19 | +class TransmissionTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + private static $utils; |
| 22 | + private $clientMock; |
| 23 | + /** @var SparkPost */ |
| 24 | + private $resource; |
| 25 | + |
| 26 | + private $postTransmissionPayload = [ |
| 27 | + 'content' => [ |
| 28 | + 'from' => [ 'name' => 'Sparkpost Team', 'email' => 'postmaster@sendmailfor.me' ], |
| 29 | + 'subject' => 'First Mailing From PHP', |
| 30 | + 'text' => 'Congratulations, {{name}}!! You just sent your very first mailing!', |
| 31 | + ], |
| 32 | + 'substitution_data' => ['name' => 'Avi'], |
| 33 | + 'recipients' => [ |
| 34 | + [ |
| 35 | + 'address' => [ |
| 36 | + 'name' => 'Vincent', |
| 37 | + 'email' => 'vincent.song@sparkpost.com' |
| 38 | + ] |
| 39 | + ], |
| 40 | + [ 'address' => 'test@example.com' ] |
| 41 | + ], |
| 42 | + 'cc' => [ |
| 43 | + [ |
| 44 | + 'address' => [ |
| 45 | + 'email' => 'avi.goldman@sparkpost.com' |
| 46 | + ], |
| 47 | + ] |
| 48 | + ], |
| 49 | + 'bcc' => [ |
| 50 | + ['address' => 'Emely Giraldo <emely.giraldo@sparkpost.com>'], |
| 51 | + ] |
| 52 | + |
| 53 | + ]; |
| 54 | + |
| 55 | + private $getTransmissionPayload = [ |
| 56 | + 'campaign_id' => 'thanksgiving' |
| 57 | + ]; |
| 58 | + |
| 59 | + /** |
| 60 | + * (non-PHPdoc). |
| 61 | + * |
| 62 | + * @before |
| 63 | + * |
| 64 | + * @see PHPUnit_Framework_TestCase::setUp() |
| 65 | + */ |
| 66 | + public function setUp() |
| 67 | + { |
| 68 | + //setup mock for the adapter |
| 69 | + $this->clientMock = Mockery::mock('Http\Adapter\Guzzle6\Client'); |
| 70 | + |
| 71 | + $this->resource = new SparkPost($this->clientMock, ['key' => 'SPARKPOST_API_KEY', 'async' => false]); |
| 72 | + self::$utils = new ClassUtils($this->resource); |
| 73 | + } |
| 74 | + |
| 75 | + public function tearDown() |
| 76 | + { |
| 77 | + Mockery::close(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @expectedException Exception |
| 82 | + */ |
| 83 | + public function testInvalidEmailFormat() |
| 84 | + { |
| 85 | + $this->postTransmissionPayload['recipients'][] = [ |
| 86 | + 'address' => 'invalid email format' |
| 87 | + ]; |
| 88 | + |
| 89 | + $response = $this->resource->transmissions->post($this->postTransmissionPayload); |
| 90 | + } |
| 91 | + |
| 92 | + public function testGet() |
| 93 | + { |
| 94 | + $responseMock = Mockery::mock('Psr\Http\Message\ResponseInterface'); |
| 95 | + $responseBodyMock = Mockery::mock(); |
| 96 | + |
| 97 | + $responseBody = ['results' => 'yay']; |
| 98 | + |
| 99 | + $this->resource->httpClient->shouldReceive('sendRequest')-> |
| 100 | + once()-> |
| 101 | + with(Mockery::type('GuzzleHttp\Psr7\Request'))-> |
| 102 | + andReturn($responseMock); |
| 103 | + |
| 104 | + $responseMock->shouldReceive('getStatusCode')->andReturn(200); |
| 105 | + $responseMock->shouldReceive('getBody')->andReturn($responseBodyMock); |
| 106 | + $responseBodyMock->shouldReceive('__toString')->andReturn(json_encode($responseBody)); |
| 107 | + |
| 108 | + $response = $this->resource->transmissions->get($this->getTransmissionPayload); |
| 109 | + |
| 110 | + $this->assertEquals($responseBody, $response->getBody()); |
| 111 | + $this->assertEquals(200, $response->getStatusCode()); |
| 112 | + } |
| 113 | + |
| 114 | + public function testPut() |
| 115 | + { |
| 116 | + $responseMock = Mockery::mock('Psr\Http\Message\ResponseInterface'); |
| 117 | + $responseBodyMock = Mockery::mock(); |
| 118 | + |
| 119 | + $responseBody = ['results' => 'yay']; |
| 120 | + |
| 121 | + $this->resource->httpClient->shouldReceive('sendRequest')-> |
| 122 | + once()-> |
| 123 | + with(Mockery::type('GuzzleHttp\Psr7\Request'))-> |
| 124 | + andReturn($responseMock); |
| 125 | + |
| 126 | + $responseMock->shouldReceive('getStatusCode')->andReturn(200); |
| 127 | + $responseMock->shouldReceive('getBody')->andReturn($responseBodyMock); |
| 128 | + $responseBodyMock->shouldReceive('__toString')->andReturn(json_encode($responseBody)); |
| 129 | + |
| 130 | + $response = $this->resource->transmissions->put($this->getTransmissionPayload); |
| 131 | + |
| 132 | + $this->assertEquals($responseBody, $response->getBody()); |
| 133 | + $this->assertEquals(200, $response->getStatusCode()); |
| 134 | + } |
| 135 | + |
| 136 | + public function testPost() |
| 137 | + { |
| 138 | + $responseMock = Mockery::mock('Psr\Http\Message\ResponseInterface'); |
| 139 | + $responseBodyMock = Mockery::mock(); |
| 140 | + |
| 141 | + $responseBody = ['results' => 'yay']; |
| 142 | + |
| 143 | + $this->resource->httpClient->shouldReceive('sendRequest')-> |
| 144 | + once()-> |
| 145 | + with(Mockery::type('GuzzleHttp\Psr7\Request'))-> |
| 146 | + andReturn($responseMock); |
| 147 | + |
| 148 | + $responseMock->shouldReceive('getStatusCode')->andReturn(200); |
| 149 | + $responseMock->shouldReceive('getBody')->andReturn($responseBodyMock); |
| 150 | + $responseBodyMock->shouldReceive('__toString')->andReturn(json_encode($responseBody)); |
| 151 | + |
| 152 | + $response = $this->resource->transmissions->post($this->postTransmissionPayload); |
| 153 | + |
| 154 | + $this->assertEquals($responseBody, $response->getBody()); |
| 155 | + $this->assertEquals(200, $response->getStatusCode()); |
| 156 | + } |
| 157 | + |
| 158 | + public function testDelete() |
| 159 | + { |
| 160 | + $responseMock = Mockery::mock('Psr\Http\Message\ResponseInterface'); |
| 161 | + $responseBodyMock = Mockery::mock(); |
| 162 | + |
| 163 | + $responseBody = ['results' => 'yay']; |
| 164 | + |
| 165 | + $this->resource->httpClient->shouldReceive('sendRequest')-> |
| 166 | + once()-> |
| 167 | + with(Mockery::type('GuzzleHttp\Psr7\Request'))-> |
| 168 | + andReturn($responseMock); |
| 169 | + |
| 170 | + $responseMock->shouldReceive('getStatusCode')->andReturn(200); |
| 171 | + $responseMock->shouldReceive('getBody')->andReturn($responseBodyMock); |
| 172 | + $responseBodyMock->shouldReceive('__toString')->andReturn(json_encode($responseBody)); |
| 173 | + |
| 174 | + $response = $this->resource->transmissions->delete($this->getTransmissionPayload); |
| 175 | + |
| 176 | + $this->assertEquals($responseBody, $response->getBody()); |
| 177 | + $this->assertEquals(200, $response->getStatusCode()); |
| 178 | + } |
| 179 | + |
| 180 | + public function testFormatPayload() |
| 181 | + { |
| 182 | + $correctFormattedPayload = json_decode('{"content":{"from":{"name":"Sparkpost Team","email":"postmaster@sendmailfor.me"},"subject":"First Mailing From PHP","text":"Congratulations, {{name}}!! You just sent your very first mailing!","headers":{"CC":"avi.goldman@sparkpost.com"}},"substitution_data":{"name":"Avi"},"recipients":[{"address":{"name":"Vincent","email":"vincent.song@sparkpost.com"}},{"address":{"email":"test@example.com"}},{"address":{"email":"emely.giraldo@sparkpost.com","header_to":"\"Vincent\" <vincent.song@sparkpost.com>"}},{"address":{"email":"avi.goldman@sparkpost.com","header_to":"\"Vincent\" <vincent.song@sparkpost.com>"}}]}', true); |
| 183 | + |
| 184 | + $formattedPayload = $this->resource->transmissions->formatPayload($this->postTransmissionPayload); |
| 185 | + $this->assertEquals($correctFormattedPayload, $formattedPayload); |
| 186 | + } |
| 187 | +} |
0 commit comments