-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from mfn/mfn-tostring
[GUZZLE] Use __toString instead of getContent
- Loading branch information
Showing
2 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
|