From fdea06b2f72f4fbb943111ca5437972ee93352f6 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 16:29:15 +0100 Subject: [PATCH 01/19] Started working on unit tests --- .travis.yml | 26 ++++++++++++++++++++++++++ composer.json | 3 ++- tests/ConfigTest.php | 41 +++++++++++++++++++++++++++++++++++++++++ tests/HelperTest.php | 27 +++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 .travis.yml create mode 100644 tests/ConfigTest.php create mode 100644 tests/HelperTest.php diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..cb27510a --- /dev/null +++ b/.travis.yml @@ -0,0 +1,26 @@ +language: php + +sudo: false + +# whitelist for building branches +branches: + only: + - master + - stable + +matrix: + include: + - php: 5.4 + - php: 5.5 + - php: 5.6 + - php: 7 + fast_finish: true + +before_install: + - composer self-update + +before_script: + - composer install + +script: + - phpunit --bootstrap vendor/autoload.php tests/ diff --git a/composer.json b/composer.json index cd74305c..401bdf2e 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,8 @@ { "name": "paynl/sdk", "require": { - "php-curl-class/php-curl-class": "^4.8" + "php-curl-class/php-curl-class": "^4.8", + "phpunit/phpunit": "^5.2" }, "authors": [ { 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..a02e9645 --- /dev/null +++ b/tests/HelperTest.php @@ -0,0 +1,27 @@ +expectException(\Paynl\Error\Required\ApiToken::class); + + \Paynl\Config::setApiToken(''); + \Paynl\Helper::requireApiToken(); + } + + public function testRequireServiceIdException() + { + $this->expectException(\Paynl\Error\Required\ServiceId::class); + + \Paynl\Config::setServiceId(''); + \Paynl\Helper::requireServiceId(); + } + + public function testCalculateTaxClass() + { + $calculatedTaxClass = \Paynl\Helper::calculateTaxClass(10, 0.6); + + $this->assertEquals('L', $calculatedTaxClass); + } +} From 3dd89a2fb91ef5b971310a55fd59036fce548ca5 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 16:34:56 +0100 Subject: [PATCH 02/19] Also testing for php 5.3 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index cb27510a..19c28087 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ branches: matrix: include: + - php: 5.3 - php: 5.4 - php: 5.5 - php: 5.6 From 2d91615481782a22b36faf1a20b85fa1ff174b30 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 16:35:06 +0100 Subject: [PATCH 03/19] Added unit test doc --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 5726545c..6bf011b4 100644 --- a/README.md +++ b/README.md @@ -176,3 +176,6 @@ echo $transaction->isPaid()?'Paid':'Not paid'; ``` + +### Testing +Please run ```phpunit --bootstrap vendor/autoload.php tests/``` to test the application \ No newline at end of file From f235d3a36c086b5333602ad171973d5cf73b6ec7 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 16:43:37 +0100 Subject: [PATCH 04/19] Testing Helper::splitAddress --- tests/HelperTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/HelperTest.php b/tests/HelperTest.php index a02e9645..82455af9 100644 --- a/tests/HelperTest.php +++ b/tests/HelperTest.php @@ -24,4 +24,14 @@ public function testCalculateTaxClass() $this->assertEquals('L', $calculatedTaxClass); } + + public function testSplitAddress() + { + $splittedAddress = \Paynl\Helper::splitAddress('Voorstraat 2'); + + $this->assertEquals([ + 'Voorstraat', + '2' + ], $splittedAddress); + } } From 50ebd91159d1b36c3224470a65dd58afbbb20133 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 16:49:15 +0100 Subject: [PATCH 05/19] using own phpunit --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 19c28087..6ae72673 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,4 +24,4 @@ before_script: - composer install script: - - phpunit --bootstrap vendor/autoload.php tests/ + - vendor/bin/phpunit --bootstrap vendor/autoload.php tests/ From ed7aa2b7eb2c9a2e2d5fb4545632f98df7f1935c Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 16:51:58 +0100 Subject: [PATCH 06/19] moved phpunit to dev requirements --- composer.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 401bdf2e..d4591516 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,11 @@ { "name": "paynl/sdk", "require": { - "php-curl-class/php-curl-class": "^4.8", - "phpunit/phpunit": "^5.2" + "php-curl-class/php-curl-class": "^4.8" }, + "require-dev": { + "phpunit/phpunit": "^5.2" + } "authors": [ { "name": "Andy Pieters", From 7b835636fc60d59e81f528d6b9eaeea1d19283e6 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 16:53:53 +0100 Subject: [PATCH 07/19] typo fix --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d4591516..3af49d40 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ }, "require-dev": { "phpunit/phpunit": "^5.2" - } + }, "authors": [ { "name": "Andy Pieters", From f84005010685407b441a96fa811d35d3a9c9990f Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 16:57:19 +0100 Subject: [PATCH 08/19] removed own phpunit --- .travis.yml | 2 +- composer.json | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6ae72673..19c28087 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,4 +24,4 @@ before_script: - composer install script: - - vendor/bin/phpunit --bootstrap vendor/autoload.php tests/ + - phpunit --bootstrap vendor/autoload.php tests/ diff --git a/composer.json b/composer.json index 3af49d40..cd74305c 100644 --- a/composer.json +++ b/composer.json @@ -3,9 +3,6 @@ "require": { "php-curl-class/php-curl-class": "^4.8" }, - "require-dev": { - "phpunit/phpunit": "^5.2" - }, "authors": [ { "name": "Andy Pieters", From 76f2702242f4bf8d2a2010314a5d70509b0c9fe4 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 17:04:17 +0100 Subject: [PATCH 09/19] Added PHP 5.3 support --- tests/HelperTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/HelperTest.php b/tests/HelperTest.php index 82455af9..a6e7e092 100644 --- a/tests/HelperTest.php +++ b/tests/HelperTest.php @@ -4,7 +4,7 @@ class HelperTest extends PHPUnit_Framework_TestCase { public function testRequireApiTokenException() { - $this->expectException(\Paynl\Error\Required\ApiToken::class); + $this->expectException('\Paynl\Error\Required\ApiToken'); \Paynl\Config::setApiToken(''); \Paynl\Helper::requireApiToken(); @@ -12,7 +12,7 @@ public function testRequireApiTokenException() public function testRequireServiceIdException() { - $this->expectException(\Paynl\Error\Required\ServiceId::class); + $this->expectException('\Paynl\Error\Required\ServiceId'); \Paynl\Config::setServiceId(''); \Paynl\Helper::requireServiceId(); From b86dd3bffc85cf9694172f6c8d1d40565d614d1b Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 17:08:09 +0100 Subject: [PATCH 10/19] PHP 5.3 support --- tests/HelperTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/HelperTest.php b/tests/HelperTest.php index a6e7e092..2d624c4c 100644 --- a/tests/HelperTest.php +++ b/tests/HelperTest.php @@ -29,9 +29,9 @@ public function testSplitAddress() { $splittedAddress = \Paynl\Helper::splitAddress('Voorstraat 2'); - $this->assertEquals([ + $this->assertEquals(array( 'Voorstraat', '2' - ], $splittedAddress); + ), $splittedAddress); } } From 7809802c0f5fd8dd6e68803798aef62c18f6aeff Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 17:17:33 +0100 Subject: [PATCH 11/19] Using phpunit 4.8 --- .travis.yml | 2 +- composer.json | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 19c28087..6ae72673 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,4 +24,4 @@ before_script: - composer install script: - - phpunit --bootstrap vendor/autoload.php tests/ + - vendor/bin/phpunit --bootstrap vendor/autoload.php tests/ 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", From de79e7ab6c985849fbbb9ddd6129700cd56f3cb6 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 17:26:57 +0100 Subject: [PATCH 12/19] Supporting PHPUnit 4.8 --- tests/HelperTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/HelperTest.php b/tests/HelperTest.php index 2d624c4c..4d9c878e 100644 --- a/tests/HelperTest.php +++ b/tests/HelperTest.php @@ -4,7 +4,7 @@ class HelperTest extends PHPUnit_Framework_TestCase { public function testRequireApiTokenException() { - $this->expectException('\Paynl\Error\Required\ApiToken'); + $this->setExpectedException('\Paynl\Error\Required\ApiToken'); \Paynl\Config::setApiToken(''); \Paynl\Helper::requireApiToken(); @@ -12,7 +12,7 @@ public function testRequireApiTokenException() public function testRequireServiceIdException() { - $this->expectException('\Paynl\Error\Required\ServiceId'); + $this->setExpectedException('\Paynl\Error\Required\ServiceId'); \Paynl\Config::setServiceId(''); \Paynl\Helper::requireServiceId(); From 467c704230ea8afb15477edd38b7bdf1e7493198 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 17:43:47 +0100 Subject: [PATCH 13/19] Can now inject custom curl class --- src/Api/Api.php | 54 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/src/Api/Api.php b/src/Api/Api.php index 8be251aa..fb64e961 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -34,6 +34,8 @@ class Api protected $data = array(); + protected $curl; + /** * @var bool Is the ApiToken required for this API */ @@ -43,6 +45,46 @@ class Api */ protected $serviceIdRequired = false; + /** + * @return int + */ + public function getVersion() + { + return $this->version; + } + + /** + * @param int $version + * + * @return Api + */ + public function setVersion($version) + { + $this->version = $version; + + return $this; + } + + /** + * @return object + */ + public function getCurl() + { + return $this->curl; + } + + /** + * @param object $curl + * + * @return Api + */ + public function setCurl($curl) + { + $this->curl = $curl; + + return $this; + } + public function isApiTokenRequired() { return $this->apiTokenRequired; @@ -93,17 +135,19 @@ public function doRequest($endpoint, $version = null) $uri = Config::getApiUrl($endpoint, $version); - $curl = new Curl(); + if (null === $this->curl) { + $this->curl = new Curl(); + } if(Config::getCAInfoLocation()){ // set a custom CAInfo file - $curl->setOpt(CURLOPT_CAINFO, Config::getCAInfoLocation()); + $this->curl->setOpt(CURLOPT_CAINFO, Config::getCAInfoLocation()); } - $result = $curl->post($uri, $data); + $result = $this->curl->post($uri, $data); - if($curl->error){ - throw new Error\Error($curl->errorMessage); + if($this->curl->error){ + throw new Error\Error($this->curl->errorMessage); } $output = static::processResult($result); From e388df5cfdd86b6a8472ed41a5f49ac1d699f597 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 17:44:07 +0100 Subject: [PATCH 14/19] Removed newline --- src/Api/Api.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Api/Api.php b/src/Api/Api.php index fb64e961..6f230a75 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -132,7 +132,6 @@ public function doRequest($endpoint, $version = null) $data = $this->getData(); - $uri = Config::getApiUrl($endpoint, $version); if (null === $this->curl) { From 6eb3e3dafd3b2e137007daf6627759672eb63761 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 17:45:02 +0100 Subject: [PATCH 15/19] removed version getter and setter --- src/Api/Api.php | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/Api/Api.php b/src/Api/Api.php index 6f230a75..fe34a02d 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -45,26 +45,6 @@ class Api */ protected $serviceIdRequired = false; - /** - * @return int - */ - public function getVersion() - { - return $this->version; - } - - /** - * @param int $version - * - * @return Api - */ - public function setVersion($version) - { - $this->version = $version; - - return $this; - } - /** * @return object */ From 7265aaf4d98f5f5de5c4b14489a87ebe141761b7 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 17:48:44 +0100 Subject: [PATCH 16/19] Reverted curl injectable change --- src/Api/Api.php | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/src/Api/Api.php b/src/Api/Api.php index fe34a02d..8be251aa 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -34,8 +34,6 @@ class Api protected $data = array(); - protected $curl; - /** * @var bool Is the ApiToken required for this API */ @@ -45,26 +43,6 @@ class Api */ protected $serviceIdRequired = false; - /** - * @return object - */ - public function getCurl() - { - return $this->curl; - } - - /** - * @param object $curl - * - * @return Api - */ - public function setCurl($curl) - { - $this->curl = $curl; - - return $this; - } - public function isApiTokenRequired() { return $this->apiTokenRequired; @@ -112,21 +90,20 @@ public function doRequest($endpoint, $version = null) $data = $this->getData(); + $uri = Config::getApiUrl($endpoint, $version); - if (null === $this->curl) { - $this->curl = new Curl(); - } + $curl = new Curl(); if(Config::getCAInfoLocation()){ // set a custom CAInfo file - $this->curl->setOpt(CURLOPT_CAINFO, Config::getCAInfoLocation()); + $curl->setOpt(CURLOPT_CAINFO, Config::getCAInfoLocation()); } - $result = $this->curl->post($uri, $data); + $result = $curl->post($uri, $data); - if($this->curl->error){ - throw new Error\Error($this->curl->errorMessage); + if($curl->error){ + throw new Error\Error($curl->errorMessage); } $output = static::processResult($result); From ef22a35936e6e19a2734b640c0cf365d054b71ba Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 17:57:59 +0100 Subject: [PATCH 17/19] Curl class can now be changed Used for unit testing --- src/Api/Api.php | 2 +- src/Config.php | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) 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; + } } From 44908f4df221b90138c1bceb3ffb33aec7c8fad8 Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 18:10:05 +0100 Subject: [PATCH 18/19] test doc update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6bf011b4..aadb228c 100644 --- a/README.md +++ b/README.md @@ -178,4 +178,4 @@ echo $transaction->isPaid()?'Paid':'Not paid'; ``` ### Testing -Please run ```phpunit --bootstrap vendor/autoload.php tests/``` to test the application \ No newline at end of file +Please run ```vendor/bin/phpunit --bootstrap vendor/autoload.php tests/``` to test the application \ No newline at end of file From 946e9e4f5c180eb50eb7cf7eb6c825a7176dd07e Mon Sep 17 00:00:00 2001 From: ivodvb Date: Thu, 3 Mar 2016 18:13:07 +0100 Subject: [PATCH 19/19] added newline --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index aadb228c..2da405c9 100644 --- a/README.md +++ b/README.md @@ -178,4 +178,4 @@ echo $transaction->isPaid()?'Paid':'Not paid'; ``` ### Testing -Please run ```vendor/bin/phpunit --bootstrap vendor/autoload.php tests/``` to test the application \ No newline at end of file +Please run ```vendor/bin/phpunit --bootstrap vendor/autoload.php tests/``` to test the application