Skip to content

Commit 70c51fb

Browse files
committed
fix
1 parent 5fa52f3 commit 70c51fb

2 files changed

Lines changed: 57 additions & 5 deletions

File tree

src/Endpoints/EmailEndpoint.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,13 @@ public function send(?array $payload = null): SendMailResponse
291291
$headers['Idempotency-Key'] = $this->idempotencyKey;
292292
}
293293

294-
$result = $this->postArray('/v1/send', $payload ?? $this->payload, $headers);
294+
try {
295+
$result = $this->postArray('/v1/send', $payload ?? $this->payload, $headers);
295296

296-
$this->payload = [];
297-
$this->idempotencyKey = null;
298-
299-
return $this->hydrate(SendMailResponse::class, $result);
297+
return $this->hydrate(SendMailResponse::class, $result);
298+
} finally {
299+
$this->payload = [];
300+
$this->idempotencyKey = null;
301+
}
300302
}
301303
}

tests/Endpoints/EmailEndpointTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,56 @@
357357
expect($response->toArray())->toBe(['message_id' => '123', 'status' => 'pending']);
358358
});
359359

360+
test('it resets builder state after failed send', function () {
361+
$this->httpClient
362+
->shouldReceive('post')
363+
->once()
364+
->with('/v1/send', [
365+
'from' => 'sender@example.com',
366+
'to' => ['first@example.com'],
367+
'subject' => 'First',
368+
'attachments' => [[
369+
'filename' => 'secret.pdf',
370+
'content' => 'base64secret',
371+
]],
372+
'metadata' => ['invoice' => '123'],
373+
'headers' => ['X-Secret' => 'keep-out'],
374+
], ['Idempotency-Key' => 'first-key'])
375+
->andThrow(new RuntimeException('API unavailable'));
376+
377+
try {
378+
$this->endpoint
379+
->from('sender@example.com')
380+
->to('first@example.com')
381+
->subject('First')
382+
->attach('secret.pdf', 'base64secret')
383+
->metadata(['invoice' => '123'])
384+
->headers(['X-Secret' => 'keep-out'])
385+
->idempotencyKey('first-key')
386+
->send();
387+
388+
throw new RuntimeException('Expected send to fail.');
389+
} catch (RuntimeException $exception) {
390+
expect($exception->getMessage())->toBe('API unavailable');
391+
}
392+
393+
$this->httpClient
394+
->shouldReceive('post')
395+
->once()
396+
->with('/v1/send', [
397+
'from' => 'sender@example.com',
398+
'to' => ['second@example.com'],
399+
'subject' => 'Second',
400+
], [])
401+
->andReturn(['message_id' => '456', 'status' => 'pending']);
402+
403+
$this->endpoint
404+
->from('sender@example.com')
405+
->to('second@example.com')
406+
->subject('Second')
407+
->send();
408+
});
409+
360410
test('it sends without idempotency key when not set', function () {
361411
$this->httpClient
362412
->shouldReceive('post')

0 commit comments

Comments
 (0)