diff --git a/src/Postmark/Transport.php b/src/Postmark/Transport.php index 34be046..1c912bd 100644 --- a/src/Postmark/Transport.php +++ b/src/Postmark/Transport.php @@ -96,7 +96,7 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul $success = $response->getStatusCode() === 200; - if ($responseEvent = $this->_eventDispatcher->createResponseEvent($this, $response->getBody()->getContents(), $success)) { + if ($responseEvent = $this->_eventDispatcher->createResponseEvent($this, $response->getBody()->__toString(), $success)) { $this->_eventDispatcher->dispatchEvent($responseEvent, 'responseReceived'); } diff --git a/tests/TransportGuzzleStreamConsumptionTest.php b/tests/TransportGuzzleStreamConsumptionTest.php new file mode 100644 index 0000000..beaf513 --- /dev/null +++ b/tests/TransportGuzzleStreamConsumptionTest.php @@ -0,0 +1,123 @@ +registerPlugin(new ThrowExceptionOnFailurePlugin()); + + $exception = null; + try { + $transport->send($message); + } catch (Swift_TransportException $exception) { + // Deliberately empty + } + + $this->assertNotNull($exception); + $this->assertInstanceOf(Swift_TransportException::class, $exception); + $this->assertSame('Some error from server', $exception->getMessage()); + } + + public function testSendWithMiddleware() + { + $message = new Swift_Message(); + + $transport = new TransportGuzzleStreamConsumptionTestPostmarkTransportStubWithConsumingMiddleware([ + new Response(422, [], 'Some error from server'), + ]); + $transport->registerPlugin(new ThrowExceptionOnFailurePlugin()); + + $exception = null; + try { + $transport->send($message); + } catch (Swift_TransportException $exception) { + // Deliberately empty + } + + $this->assertNotNull($exception); + $this->assertInstanceOf(Swift_TransportException::class, $exception); + + // This would fail if \Postmark\Transport::send would use + // getBody->getContents() instead of getBody->__toString() + $this->assertSame('Some error from server', $exception->getMessage()); + } +} + +class TransportGuzzleStreamConsumptionTestPostmarkTransportStubNoMiddleware extends Transport +{ + protected $client; + + public function __construct(array $responses = []) + { + parent::__construct('TESTING_SERVER'); + + $this->client = $this->mockGuzzle($responses); + } + + protected function getHttpClient() + { + return $this->client; + } + + private function mockGuzzle(array $responses) + { + $stack = HandlerStack::create(new MockHandler($responses)); + + return new Client(['handler' => $stack]); + } +} + +class TransportGuzzleStreamConsumptionTestPostmarkTransportStubWithConsumingMiddleware extends Transport +{ + protected $client; + + public function __construct(array $responses = []) + { + parent::__construct('TESTING_SERVER'); + + $this->client = $this->mockGuzzle($responses); + } + + protected function getHttpClient() + { + return $this->client; + } + + private function mockGuzzle(array $responses) + { + $stack = HandlerStack::create(new MockHandler($responses)); + $stack->push( + function (callable $handler) { + return function ($request, array $options) use ($handler) { + return $handler($request, $options)->then( + function (Response $response) { + // Pretend to do something with $response, like logging + $response->getBody()->__toString(); + + return $response; + } + ); + }; + } + ); + + return new Client(['handler' => $stack]); + } +} +