Skip to content

Commit

Permalink
Merge pull request #22 from paynl/UnitTests
Browse files Browse the repository at this point in the history
More Unit tests
  • Loading branch information
andypieters committed Apr 28, 2016
2 parents d0e0bdf + 68f2618 commit 10460b8
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 14 deletions.
2 changes: 1 addition & 1 deletion samples/transaction/start.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
'initials' => 'T',
'lastName' => 'Test',
'gender' => 'M',
'dob' => '14-05-1999',
'birthDate' => '14-05-1999',
'phoneNumber' => '0612345678',
'emailAddress' => '[email protected]',
),
Expand Down
11 changes: 2 additions & 9 deletions src/Curl/Dummy.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ class Dummy
private $_result;

public $error;

/**
* Set the result, this will be returned when post
*
* @param $result
*/
public function __construct($result)
{

public function setResult($result){
$this->_result = $result;
}


//dummy function to prevent errors
public function __call($name, $arguments)
{
Expand Down
5 changes: 4 additions & 1 deletion src/Refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Refund
* Start a new transaction
*
* @param array|null $options
* @return Result\Start
* @return Result\Add
* @throws Error\Error
*/
public static function add($options = array())
Expand Down Expand Up @@ -89,6 +89,9 @@ public static function add($options = array())
$api->setCurrency($options['currency']);
}
if (isset($options['processDate'])) {
if(is_string($options['processDate'])){
$options['processDate'] = new \DateTime($options['processDate']);
}
$api->setProcessDate($options['processDate']);
}

Expand Down
8 changes: 6 additions & 2 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public function testGetCurlDefault(){
$this->assertInstanceOf('\Curl\Curl', \Paynl\Config::getCurl());
}
public function testGetCurlCustom(){
\Paynl\Config::setCurl(new DateTime());
$this->assertInstanceOf('\DateTime', \Paynl\Config::getCurl());
\Paynl\Config::setCurl(new \Paynl\Curl\Dummy());
$this->assertInstanceOf('\Paynl\Curl\Dummy', \Paynl\Config::getCurl());
}
public function testGetCurlCustomString(){
\Paynl\Config::setCurl('\Paynl\Curl\Dummy');
$this->assertInstanceOf('\Paynl\Curl\Dummy', \Paynl\Config::getCurl());
}
}
5 changes: 4 additions & 1 deletion tests/PaymentmethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class PaymentmethodsTest extends PHPUnit_Framework_TestCase

private function setDummyData(){
$this->testApiResult = file_get_contents(dirname(__FILE__).'/dummyData/getService.json');
\Paynl\Config::setCurl(new \Paynl\Curl\Dummy($this->testApiResult));

$curl = new \Paynl\Curl\Dummy();
$curl->setResult($this->testApiResult);
\Paynl\Config::setCurl($curl);
}

public function testGetPaymentMethodsNoServiceId(){
Expand Down
246 changes: 246 additions & 0 deletions tests/TransactionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
<?php

/**
* Created by PhpStorm.
* User: andy
* Date: 20-4-2016
* Time: 14:06
*/
class TransactionTest extends PHPUnit_Framework_TestCase
{
private $testApiResult;

private function setDummyData($name)
{
$this->testApiResult = file_get_contents(dirname(__FILE__) . '/dummyData/Transaction/' . $name . '.json');
$curl = new \Paynl\Curl\Dummy();
$curl->setResult($this->testApiResult);
\Paynl\Config::setCurl($curl);
}

/**
* Start a transaction with all options
*
* @return \Paynl\Result\Transaction\Start
*/
private function startTransactionFull()
{
$this->setDummyData('startOk');
$result = \Paynl\Transaction::start(array(
// required
'amount' => 10,
'returnUrl' => '/return.php',

// optional
'exchangeUrl' => '/exchange.php',
'paymentMethod' => 10,
'currency' => 'EUR',
'expireDate' => 'tomorrow',
'bank' => 1,
'description' => '123456',
'testmode' => 1,
'extra1' => 'ext1',
'extra2' => 'ext2',
'extra3' => 'ext3',
'ipaddress' => '123.123.123.123',
'invoiceDate' => 'now',
'deliveryDate' => 'tomorrow', // in case of tickets for an event, use the event date here
'products' => array(
array(
'id' => 1,
'name' => 'een product',
'price' => 5,
'tax' => 0.87,
'qty' => 1,
),
array(
'id' => 2,
'name' => 'ander product',
'price' => 5,
'tax' => 0.87,
'qty' => 1,
)
),
'language' => 'EN',
'enduser' => array(
'initials' => 'T',
'lastName' => 'Test',
'gender' => 'M',
'birthDate' => '14-05-1999',
'phoneNumber' => '0612345678',
'emailAddress' => '[email protected]',
),
'address' => array(
'streetName' => 'Test',
'houseNumber' => '10',
'zipCode' => '1234AB',
'city' => 'Test',
'country' => 'NL',
),
'invoiceAddress' => array(
'initials' => 'IT',
'lastName' => 'ITEST',
'streetName' => 'Istreet',
'houseNumber' => '70',
'zipCode' => '5678CD',
'city' => 'ITest',
'country' => 'NL',
),
));
return $result;
}

public function testStartNoToken()
{
$this->setExpectedException('\Paynl\Error\Required\ApiToken');

\Paynl\Config::setApiToken('');
\Paynl\Config::setServiceId('SL-1234-5678');

$this->startTransactionFull();
}

public function testStartNoServiceId()
{
$this->setExpectedException('\Paynl\Error\Required\ServiceId');

\Paynl\Config::setApiToken('123456789012345678901234567890');
\Paynl\Config::setServiceId('');

$this->startTransactionFull();
}

public function testStartNoAmount()
{
$this->setExpectedException('\Paynl\Error\Required');
$this->setDummyData('startOk');
\Paynl\Config::setApiToken('123456789012345678901234567890');
\Paynl\Config::setServiceId('SL-1234-5678');

\Paynl\Transaction::start(array(
'returnUrl' => '/return.php',
'ipaddress' => '127.0.0.1',
));
}

public function testStartNoReturn()
{
$this->setExpectedException('\Paynl\Error\Required');
$this->setDummyData('startOk');
\Paynl\Config::setApiToken('123456789012345678901234567890');
\Paynl\Config::setServiceId('SL-1234-5678');

\Paynl\Transaction::start(array(
'amount' => 10,
'ipaddress' => '127.0.0.1',
));
}

public function testStartMinumumOk()
{
\Paynl\Config::setApiToken('123456789012345678901234567890');
\Paynl\Config::setServiceId('SL-1234-5678');

$this->setDummyData('startOk');
$result = \Paynl\Transaction::start(array(
'amount' => 10,
'returnUrl' => '/return.php',
'ipaddress' => '127.0.0.1',
));

$this->validateStartResult($result);
}

private function validateStartResult($result)
{
$this->assertInstanceOf('\Paynl\Result\Transaction\Start', $result);

/**
* @var $result \Paynl\Result\Transaction\Start
*/

$this->assertNotEmpty($result->getTransactionId(), 'Could not get the transactionId');
$this->assertNotEmpty($result->getPaymentReference(), 'Could not get the PaymentReference');
$this->assertNotEmpty($result->getRedirectUrl(), 'Could not get the redirectUrl');
}

public function testStartFullOk()
{
\Paynl\Config::setApiToken('123456789012345678901234567890');
\Paynl\Config::setServiceId('SL-1234-5678');

$result = $this->startTransactionFull();

$this->validateStartResult($result);
}

public function testGetTransactionPaid()
{
\Paynl\Config::setApiToken('123456789012345678901234567890');

$this->setDummyData('Result/transactionPaid');

$transaction = Paynl\Transaction::get('645958819Xdd3ea1');

$this->assertInstanceOf('\Paynl\Result\Transaction\Transaction', $transaction);

}

public function testGetForReturn()
{
\Paynl\Config::setApiToken('123456789012345678901234567890');

$this->setDummyData('Result/transactionPaid');

$_GET['orderId'] = "645958819Xdd3ea1";

$transaction = Paynl\Transaction::getForReturn();
$this->assertInstanceOf('\Paynl\Result\Transaction\Transaction', $transaction);
}

public function testGetForExchange()
{
\Paynl\Config::setApiToken('123456789012345678901234567890');

$this->setDummyData('Result/transactionPaid');

$_GET['order_id'] = "645958819Xdd3ea1";

$transaction = Paynl\Transaction::getForExchange();
$this->assertInstanceOf('\Paynl\Result\Transaction\Transaction', $transaction);
}

public function testGetForExchangePost()
{
\Paynl\Config::setApiToken('123456789012345678901234567890');

$this->setDummyData('Result/transactionPaid');

unset($_GET['order_id']);

$_POST['order_id'] = "645958819Xdd3ea1";

$transaction = Paynl\Transaction::getForExchange();
$this->assertInstanceOf('\Paynl\Result\Transaction\Transaction', $transaction);
}

public function testRefund()
{
\Paynl\Config::setApiToken('123456789012345678901234567890');

$this->setDummyData('Result/refund');
$refund = \Paynl\Transaction::refund('645958819Xdd3ea1', 5);

$this->assertInstanceOf('Paynl\Result\Transaction\Refund', $refund);
}

public function testRefundError()
{
\Paynl\Config::setApiToken('123456789012345678901234567890');

$this->setExpectedException('\Paynl\Error\Api');

$this->setDummyData('Result/refundError');
\Paynl\Transaction::refund('645958819Xdd3ea1', 5, 'Description');
}
}
33 changes: 33 additions & 0 deletions tests/ValidateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* Created by PhpStorm.
* User: andy
* Date: 26-4-16
* Time: 19:30
*/
class ValidateTest extends PHPUnit_Framework_TestCase
{
private $testApiResult;

private function setDummyData($name){
$this->testApiResult = file_get_contents(dirname(__FILE__).'/dummyData/Validate/'.$name.'.json');
$curl = new \Paynl\Curl\Dummy();
$curl->setResult($this->testApiResult);
\Paynl\Config::setCurl($curl);
}

public function testIsPayServerIpYes(){
$this->setDummyData('isPayServerIpYes');
$result = \Paynl\Validate::isPayServerIp('37.46.137.137');

$this->assertEquals(true, $result);
}
public function testIsPayServerIpNo(){
$this->setDummyData('isPayServerIpNo');

$result = \Paynl\Validate::isPayServerIp('192.168.20.1');

$this->assertEquals(false, $result);
}
}
1 change: 1 addition & 0 deletions tests/dummyData/Transaction/Result/refund.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"request":{"result":"1"},"refundId":"RF-1234-1234"}
1 change: 1 addition & 0 deletions tests/dummyData/Transaction/Result/refundError.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"request":{"result":"0","errorId":"1","errorMessage":"You do not have enough credit to refund this amount."},"refundId":""}
1 change: 1 addition & 0 deletions tests/dummyData/Transaction/Result/transactionPaid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"request":{"result":"1","errorId":"","errorMessage":""},"connection":{"trust":"10","country":"NL","city":"Zwolle","locationLat":"52.505798339844","locationLon":"6.0858001708984","browserData":"","ipAddress":"213.126.82.230","countryName":"Netherlands","blacklist":"0","host":"D57E52E6.static.ziggozakelijk.nl","orderIpAddress":"213.126.82.230","orderReturnURL":"http:\/\/c80fde59.ngrok.io\/pay_payment\/order\/return\/","merchantCode":"M-3421-2120","merchantName":"Classic Carparts"},"enduser":{"gender":"0","emailAddress":"[email protected]","initials":"A","lastName":"Pieters","phoneNumber":"0612345678","address":{"streetName":"Kopersteden","streetNumber":"10","zipCode":"7547 TK","city":"Enschede","countryCode":"NL"},"invoiceAddress":{"streetName":"Kopersteden","streetNumber":"10","zipCode":"7547 TK","city":"Enschede","countryCode":"NL","initials":"A","lastName":"Pieters","gender":""},"language":"nl","accessCode":"","dob":"","bankAccount":"","iban":"","bic":"","sendConfirmMail":"","confirmMailTemplate":""},"saleData":{"invoiceDate":"26-04-2016","deliveryDate":"27-04-2016","orderData":[{"productId":"608","description":"Tori Tank","price":"6000","quantity":"1","vatCode":"N"},{"productId":"0","description":"Verzendkosten","price":"500","quantity":"1","vatCode":"N"}]},"paymentDetails":{"amount":"6500","currenyAmount":"6500","paidAmount":"6500","paidCurrenyAmount":"6500","paidBase":"6500","paidCosts":"0","paidCostsVat":"0","paidCurrency":"EUR","paidAttemps":"1","paidDuration":"0","description":"145000011","processTime":"10","state":"100","stateName":"PAID","stateDescription":"Paid","exchange":"","storno":"0","paymentOptionId":"613","paymentOptionSubId":"0","secure":"0","secureStatus":"","identifierName":"A Pieters","identifierPublic":"","identifierHash":"","customerKey":"11f2365950119362500e06af693e380b","serviceId":"SL-6712-4510","serviceName":"Andy","serviceDescription":"Test voor andys webshops","created":"2016-04-26 15:34:03","modified":"2016-04-26 15:34:13","paymentMethodId":"4","paymentMethodName":"Transacties ","paymentMethodDescription":"Pay Per Transaction","paymentProfileName":"SandBox"},"statsDetails":{"tool":"","info":"","object":"","extra1":"145000011","extra2":"[email protected]","extra3":"","promotorId":"0","transferData":"","paymentSessionId":"645958819"},"stornoDetails":{"stornoId":"","stornoAmount":"","bankAccount":"","iban":"","bic":"","city":"","datetime":"","reason":"","emailAddress":""}}
1 change: 1 addition & 0 deletions tests/dummyData/Transaction/startOk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"request":{"result":"1","errorId":"","errorMessage":""},"endUser":{"blacklist":"0"},"transaction":{"transactionId":"643872862Xe19e4c","paymentURL":"https:\/\/betalen.rabobank.nl\/ideal-betaling\/landingpage?random=93ea6be3e748ed876158a6ebd5fa8ce1c3950a67f9551c990a5f6f7acd7432c3&trxid=0020001238088229","popupAllowed":"0","paymentReference":"6000 0006 4387 2862"}}
1 change: 1 addition & 0 deletions tests/dummyData/Validate/isPayServerIpNo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"result":"0"}
1 change: 1 addition & 0 deletions tests/dummyData/Validate/isPayServerIpYes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"result":"1"}

0 comments on commit 10460b8

Please sign in to comment.