From 8653b5fa0a24d2766b51656c395f971ebdefe70f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Sat, 23 Nov 2013 22:33:14 +0100 Subject: [PATCH] Better error handling --- README.md | 5 ++ .../Client/Listener/ErrorHandlerListener.php | 2 +- .../Listener/ErrorHandlerListenerTest.php | 78 +++++++++++++++++++ .../Client/PaymillClientTest.php | 31 ++------ 4 files changed, 89 insertions(+), 27 deletions(-) create mode 100644 tests/ZfrPaymillTest/Client/Listener/ErrorHandlerListenerTest.php diff --git a/README.md b/README.md index 736944d..d5a9085 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,11 @@ try { } ``` +> For transaction/refund/preauthorization methods, Paymill may return status code 200 even if an error occured. +Paymill stores this error in a `response_code` property in the reponse. However, ZfrPaymill will automatically +checks if this is set, and throw a `TransactionErrorException`, so that you don't need to check for this yourself, +but only catch the exception. + ### Advanced usage #### Listeners diff --git a/src/ZfrPaymill/Client/Listener/ErrorHandlerListener.php b/src/ZfrPaymill/Client/Listener/ErrorHandlerListener.php index 75da835..a266308 100644 --- a/src/ZfrPaymill/Client/Listener/ErrorHandlerListener.php +++ b/src/ZfrPaymill/Client/Listener/ErrorHandlerListener.php @@ -104,7 +104,7 @@ public function handleError(Event $event) $exception->setRequest($command->getRequest()); $exception->setResponse($command->getResponse()); - throw new $exception; + throw $exception; } } } diff --git a/tests/ZfrPaymillTest/Client/Listener/ErrorHandlerListenerTest.php b/tests/ZfrPaymillTest/Client/Listener/ErrorHandlerListenerTest.php new file mode 100644 index 0000000..af6ce75 --- /dev/null +++ b/tests/ZfrPaymillTest/Client/Listener/ErrorHandlerListenerTest.php @@ -0,0 +1,78 @@ +setExpectedException($exception, $message, $responseCode); + } + + $event = new Event(); + $command = $this->getMock('Guzzle\Service\Command\CommandInterface'); + $event['command'] = $command; + + $request = $this->getMock('Guzzle\Http\Message\Request', array(), array(), '', false); + $response = $this->getMock('Guzzle\Http\Message\Response', array(), array(), '', false); + + $command->expects($this->once()) + ->method('toArray') + ->will($this->returnValue(array( + 'data' => array( + 'response_code' => $responseCode + ) + ))); + + $command->expects($this->any())->method('getRequest')->will($this->returnValue($request)); + $command->expects($this->any())->method('getResponse')->will($this->returnValue($response)); + + $listener = new ErrorHandlerListener(); + $listener->handleError($event); + } +} diff --git a/tests/ZfrPaymillTest/Client/PaymillClientTest.php b/tests/ZfrPaymillTest/Client/PaymillClientTest.php index 1191c83..b954a24 100644 --- a/tests/ZfrPaymillTest/Client/PaymillClientTest.php +++ b/tests/ZfrPaymillTest/Client/PaymillClientTest.php @@ -24,47 +24,26 @@ class PaymillClientTest extends PHPUnit_Framework_TestCase { /** - * @var PusherClient + * @var PaymillClient */ protected $client; - /** - * @var Credentials - */ - protected $credentials; - public function setUp() { - $this->credentials = new Credentials('3', '278d425bdf160c739803', '7ad3773142a6692b25b8'); - $this->client = new PusherClient($this->credentials); - } - - /** - * @covers PusherClient::__construct - */ - public function testAssertApplicationIdIsAlwaysSent() - { - $config = $this->client->getConfig('command.params'); - $this->assertEquals($config['app_id'], $this->credentials->getAppId()); + $this->client = new PaymillClient('abc'); } - /** - * @covers PusherClient::getApiVersion - */ public function testCanRetrieveApiVersion() { - $this->assertEquals('1.0', $this->client->getApiVersion()); + $this->assertEquals('2.0', $this->client->getApiVersion()); } - /** - * @covers PusherClient - */ public function testUserAgentIsIncluded() { // Make sure the user agent contains "pusher-php" - $command = $this->client->getCommand('GetChannelsInfo'); + $command = $this->client->getCommand('GetOffers'); $request = $command->prepare(); $this->client->dispatch('command.before_send', array('command' => $command)); - $this->assertRegExp('/^zfr-pusher-php/', (string)$request->getHeader('User-Agent', true)); + $this->assertRegExp('/^zfr-paymill-php/', (string)$request->getHeader('User-Agent', true)); } }