Skip to content

Commit

Permalink
Merge pull request #16 from ivodvb/unit-tests
Browse files Browse the repository at this point in the history
Added unit tests
  • Loading branch information
andypieters committed Mar 3, 2016
2 parents cef7f32 + 946e9e4 commit 39ff687
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
language: php

sudo: false

# whitelist for building branches
branches:
only:
- master
- stable

matrix:
include:
- php: 5.3
- php: 5.4
- php: 5.5
- php: 5.6
- php: 7
fast_finish: true

before_install:
- composer self-update

before_script:
- composer install

script:
- vendor/bin/phpunit --bootstrap vendor/autoload.php tests/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,6 @@ echo $transaction->isPaid()?'Paid':'Not paid';


```

### Testing
Please run ```vendor/bin/phpunit --bootstrap vendor/autoload.php tests/``` to test the application
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"require": {
"php-curl-class/php-curl-class": "^4.8"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
},
"authors": [
{
"name": "Andy Pieters",
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function doRequest($endpoint, $version = null)

$uri = Config::getApiUrl($endpoint, $version);

$curl = new Curl();
$curl = Config::getCurl();

if(Config::getCAInfoLocation()){
// set a custom CAInfo file
Expand Down
27 changes: 27 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

namespace Paynl;

use Curl\Curl;

/**
* Description of Pay
*
Expand All @@ -36,6 +38,8 @@ class Config
// @var int The version of the Pay.nl API to use for requests.
private static $apiVersion = 5;

private static $curl;

/**
* @var string path tho CAInfo location
*/
Expand Down Expand Up @@ -123,4 +127,27 @@ public static function getApiUrl($endpoint, $version = null)
return self::$apiBase . '/v' . $version . '/' . $endpoint . '/json';
}

/**
* @return object
*/
public static function getCurl()
{
if (null === self::$curl) {
self::$curl = new Curl();
}

return self::$curl;
}

/**
* @param mixed $curl
*/
public static function setCurl($curl)
{
if (is_string($curl)) {
$curl = new $curl;
}

self::$curl = $curl;
}
}
41 changes: 41 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

class ConfigTest extends PHPUnit_Framework_TestCase
{
public function testCAInfoLocation()
{
\Paynl\Config::setCAInfoLocation('test/location');

$this->assertEquals('test/location',
\Paynl\Config::getCAInfoLocation());
}

public function testApiToken()
{
\Paynl\Config::setApiToken('my-api-token');

$this->assertEquals('my-api-token', \Paynl\Config::getApiToken());
}

public function testServiceId()
{
\Paynl\Config::setServiceId('my-service-id');

$this->assertEquals('my-service-id', \Paynl\Config::getServiceId());
}

public function testApiVersion()
{
\Paynl\Config::setServiceId('api-version');

$this->assertEquals('api-version', \Paynl\Config::getServiceId());
}

public function testApiUrl()
{
$this->assertEquals(
'https://rest-api.pay.nl/v5/transaction/json',
\Paynl\Config::getApiUrl('transaction', 5)
);
}
}
37 changes: 37 additions & 0 deletions tests/HelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

class HelperTest extends PHPUnit_Framework_TestCase
{
public function testRequireApiTokenException()
{
$this->setExpectedException('\Paynl\Error\Required\ApiToken');

\Paynl\Config::setApiToken('');
\Paynl\Helper::requireApiToken();
}

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

\Paynl\Config::setServiceId('');
\Paynl\Helper::requireServiceId();
}

public function testCalculateTaxClass()
{
$calculatedTaxClass = \Paynl\Helper::calculateTaxClass(10, 0.6);

$this->assertEquals('L', $calculatedTaxClass);
}

public function testSplitAddress()
{
$splittedAddress = \Paynl\Helper::splitAddress('Voorstraat 2');

$this->assertEquals(array(
'Voorstraat',
'2'
), $splittedAddress);
}
}

0 comments on commit 39ff687

Please sign in to comment.