diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..6ae72673 --- /dev/null +++ b/.travis.yml @@ -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/ diff --git a/README.md b/README.md index 5726545c..2da405c9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/composer.json b/composer.json index cd74305c..8fa1f8de 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,9 @@ "require": { "php-curl-class/php-curl-class": "^4.8" }, + "require-dev": { + "phpunit/phpunit": "^4.8" + }, "authors": [ { "name": "Andy Pieters", diff --git a/src/Api/Api.php b/src/Api/Api.php index 8be251aa..0790b8c3 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -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 diff --git a/src/Config.php b/src/Config.php index e31b433d..6b4f603e 100644 --- a/src/Config.php +++ b/src/Config.php @@ -19,6 +19,8 @@ namespace Paynl; +use Curl\Curl; + /** * Description of Pay * @@ -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 */ @@ -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; + } } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php new file mode 100644 index 00000000..c617018a --- /dev/null +++ b/tests/ConfigTest.php @@ -0,0 +1,41 @@ +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) + ); + } +} diff --git a/tests/HelperTest.php b/tests/HelperTest.php new file mode 100644 index 00000000..4d9c878e --- /dev/null +++ b/tests/HelperTest.php @@ -0,0 +1,37 @@ +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); + } +}