|
357 | 357 | expect($response->toArray())->toBe(['message_id' => '123', 'status' => 'pending']); |
358 | 358 | }); |
359 | 359 |
|
| 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 | + |
360 | 410 | test('it sends without idempotency key when not set', function () { |
361 | 411 | $this->httpClient |
362 | 412 | ->shouldReceive('post') |
|
0 commit comments