Close resources on message send http exception

This commit is contained in:
Vitaliy Chesnokov 2019-11-26 17:42:57 +03:00
parent 5975310f0e
commit b27bab7b9f
No known key found for this signature in database
GPG Key ID: FD23DF1B48ECC3EB
2 changed files with 56 additions and 4 deletions

View File

@ -57,8 +57,11 @@ class Message extends HttpApi
} }
$postDataMultipart = array_merge($this->prepareMultipartParameters($params), $postDataMultipart); $postDataMultipart = array_merge($this->prepareMultipartParameters($params), $postDataMultipart);
try {
$response = $this->httpPostRaw(sprintf('/v3/%s/messages', $domain), $postDataMultipart); $response = $this->httpPostRaw(sprintf('/v3/%s/messages', $domain), $postDataMultipart);
} finally {
$this->closeResources($postDataMultipart); $this->closeResources($postDataMultipart);
}
return $this->hydrateResponse($response, SendResponse::class); return $this->hydrateResponse($response, SendResponse::class);
} }
@ -91,8 +94,11 @@ class Message extends HttpApi
]; ];
} }
$postDataMultipart[] = $this->prepareFile('message', $fileData); $postDataMultipart[] = $this->prepareFile('message', $fileData);
try {
$response = $this->httpPostRaw(sprintf('/v3/%s/messages.mime', $domain), $postDataMultipart); $response = $this->httpPostRaw(sprintf('/v3/%s/messages.mime', $domain), $postDataMultipart);
} finally {
$this->closeResources($postDataMultipart); $this->closeResources($postDataMultipart);
}
return $this->hydrateResponse($response, SendResponse::class); return $this->hydrateResponse($response, SendResponse::class);
} }

View File

@ -124,6 +124,52 @@ class MessageTest extends TestCase
$api->sendMime('foo', ['mailbox@myapp.com'], $message, []); $api->sendMime('foo', ['mailbox@myapp.com'], $message, []);
} }
public function testCloseResourcesOnSendRequestException()
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('httpPostRaw')
->willThrowException(new \Exception('Something went wrong'));
$streamsCount = count(get_resources('stream'));
try {
$api->send('example.com', [
'from' => 'bob@example.com',
'to' => 'sally@example.com',
'subject' => 'Test file path attachments',
'text' => 'Test',
'attachment' => [
['filePath' => __DIR__.'/../TestAssets/mailgun_icon1.png', 'filename' => 'mailgun_icon1.png'],
],
]);
} catch (\Exception $e) {
$this->assertEquals('Something went wrong', $e->getMessage());
}
$this->assertCount($streamsCount, get_resources('stream'));
}
public function testCloseResourcesOnSendMimeRequestException()
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('httpPostRaw')
->willThrowException(new \Exception('Something went wrong'));
$streamsCount = count(get_resources('stream'));
try {
$api->sendMime('foo', ['mailbox@myapp.com'], 'mime message', ['o:Foo' => 'bar']);
} catch (\Exception $e) {
$this->assertEquals('Something went wrong', $e->getMessage());
}
$this->assertCount($streamsCount, get_resources('stream'));
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */