Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Nov 23, 2013
1 parent ad8f228 commit 8653b5f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 27 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ZfrPaymill/Client/Listener/ErrorHandlerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function handleError(Event $event)
$exception->setRequest($command->getRequest());
$exception->setResponse($command->getResponse());

throw new $exception;
throw $exception;
}
}
}
78 changes: 78 additions & 0 deletions tests/ZfrPaymillTest/Client/Listener/ErrorHandlerListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace ZfrPaymillTest\Client\Listener;

use Guzzle\Common\Event;
use PHPUnit_Framework_TestCase;
use ZfrPaymill\Listener\ErrorHandlerListener;

class ErrorHandlerListenerTest extends PHPUnit_Framework_TestCase
{
public function provider()
{
return array(
array(
10000,
null,
null
),
array(
40000,
'ZfrPaymill\Exception\TransactionErrorException',
'General problem with data.'
),
array(
50300,
'ZfrPaymill\Exception\TransactionErrorException',
'Technical error with 3D secure.'
)
);
}

/**
* @dataProvider provider
*/
public function testUserAgentIsIncluded($responseCode, $exception, $message)
{
if ($exception) {
$this->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);
}
}
31 changes: 5 additions & 26 deletions tests/ZfrPaymillTest/Client/PaymillClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 8653b5f

Please sign in to comment.