Skip to content

Commit

Permalink
Merge pull request #32 from mfn/mfn-tostring
Browse files Browse the repository at this point in the history
[GUZZLE] Use __toString instead of getContent
  • Loading branch information
vladsandu authored Sep 5, 2020
2 parents f3932a6 + f7e1bf0 commit a8743e5
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Postmark/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
123 changes: 123 additions & 0 deletions tests/TransportGuzzleStreamConsumptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Postmark\ThrowExceptionOnFailurePlugin;
use Postmark\Transport;

require_once __DIR__ . '/../vendor/autoload.php';

class TransportGuzzleStreamConsumptionTest extends TestCase
{

public function testSendWithoutMiddleware()
{
$message = new Swift_Message();

$transport = new TransportGuzzleStreamConsumptionTestPostmarkTransportStubNoMiddleware([
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->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]);
}
}

0 comments on commit a8743e5

Please # to comment.