From ef86bfebad511e577550b9dc069d7e96d2c0e18d Mon Sep 17 00:00:00 2001 From: Kadek Jayak Date: Sun, 25 Sep 2022 05:53:53 +0000 Subject: [PATCH] feat: cleanup tests and ip resolver --- composer.json | 15 +- composer.lock | 392 +--------------------------------------- phpunit.xml.dist | 7 +- src/BCAParser.php | 52 ++++-- tests/BCAParserTest.php | 30 ++- 5 files changed, 90 insertions(+), 406 deletions(-) diff --git a/composer.json b/composer.json index e2ce183..00fc329 100755 --- a/composer.json +++ b/composer.json @@ -9,17 +9,22 @@ "email": "kadekjayak@yahoo.co.id" } ], - "require": { - "phpunit/phpunit": "^9.5" - }, "autoload": { "psr-4": { - "BCAParser\\":"src/" + "BCAParser\\": "src/" } }, "autoload-dev": { "psr-4": { "BCAParser\\Tests\\": "tests/" } + }, + "require": { + "ext-curl": "*", + "ext-xml": "*", + "ext-dom": "*" + }, + "require-dev": { + "phpunit/phpunit": "^9.5" } -} +} \ No newline at end of file diff --git a/composer.lock b/composer.lock index 42eab42..717fc98 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,9 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4db323ddd5bf3c98a0b0b7bc0d43715d", - "packages": [ + "content-hash": "3f6d90b920315da795ddc0fb860a37b0", + "packages": [], + "packages-dev": [ { "name": "doctrine/instantiator", "version": "1.4.1", @@ -302,233 +303,6 @@ }, "time": "2022-02-21T01:04:05+00:00" }, - { - "name": "phpdocumentor/reflection-common", - "version": "2.2.0", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/ReflectionCommon.git", - "reference": "1d01c49d4ed62f25aa84a747ad35d5a16924662b" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/1d01c49d4ed62f25aa84a747ad35d5a16924662b", - "reference": "1d01c49d4ed62f25aa84a747ad35d5a16924662b", - "shasum": "" - }, - "require": { - "php": "^7.2 || ^8.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-2.x": "2.x-dev" - } - }, - "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jaap van Otterdijk", - "email": "opensource@ijaap.nl" - } - ], - "description": "Common reflection classes used by phpdocumentor to reflect the code structure", - "homepage": "http://www.phpdoc.org", - "keywords": [ - "FQSEN", - "phpDocumentor", - "phpdoc", - "reflection", - "static analysis" - ], - "support": { - "issues": "https://github.com/phpDocumentor/ReflectionCommon/issues", - "source": "https://github.com/phpDocumentor/ReflectionCommon/tree/2.x" - }, - "time": "2020-06-27T09:03:43+00:00" - }, - { - "name": "phpdocumentor/reflection-docblock", - "version": "5.3.0", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "622548b623e81ca6d78b721c5e029f4ce664f170" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/622548b623e81ca6d78b721c5e029f4ce664f170", - "reference": "622548b623e81ca6d78b721c5e029f4ce664f170", - "shasum": "" - }, - "require": { - "ext-filter": "*", - "php": "^7.2 || ^8.0", - "phpdocumentor/reflection-common": "^2.2", - "phpdocumentor/type-resolver": "^1.3", - "webmozart/assert": "^1.9.1" - }, - "require-dev": { - "mockery/mockery": "~1.3.2", - "psalm/phar": "^4.8" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "5.x-dev" - } - }, - "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Mike van Riel", - "email": "me@mikevanriel.com" - }, - { - "name": "Jaap van Otterdijk", - "email": "account@ijaap.nl" - } - ], - "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "support": { - "issues": "https://github.com/phpDocumentor/ReflectionDocBlock/issues", - "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.3.0" - }, - "time": "2021-10-19T17:43:47+00:00" - }, - { - "name": "phpdocumentor/type-resolver", - "version": "1.6.0", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/93ebd0014cab80c4ea9f5e297ea48672f1b87706", - "reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706", - "shasum": "" - }, - "require": { - "php": "^7.2 || ^8.0", - "phpdocumentor/reflection-common": "^2.0" - }, - "require-dev": { - "ext-tokenizer": "*", - "psalm/phar": "^4.8" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-1.x": "1.x-dev" - } - }, - "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Mike van Riel", - "email": "me@mikevanriel.com" - } - ], - "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", - "support": { - "issues": "https://github.com/phpDocumentor/TypeResolver/issues", - "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.6.0" - }, - "time": "2022-01-04T19:58:01+00:00" - }, - { - "name": "phpspec/prophecy", - "version": "v1.15.0", - "source": { - "type": "git", - "url": "https://github.com/phpspec/prophecy.git", - "reference": "bbcd7380b0ebf3961ee21409db7b38bc31d69a13" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/bbcd7380b0ebf3961ee21409db7b38bc31d69a13", - "reference": "bbcd7380b0ebf3961ee21409db7b38bc31d69a13", - "shasum": "" - }, - "require": { - "doctrine/instantiator": "^1.2", - "php": "^7.2 || ~8.0, <8.2", - "phpdocumentor/reflection-docblock": "^5.2", - "sebastian/comparator": "^3.0 || ^4.0", - "sebastian/recursion-context": "^3.0 || ^4.0" - }, - "require-dev": { - "phpspec/phpspec": "^6.0 || ^7.0", - "phpunit/phpunit": "^8.0 || ^9.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.x-dev" - } - }, - "autoload": { - "psr-4": { - "Prophecy\\": "src/Prophecy" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Konstantin Kudryashov", - "email": "ever.zet@gmail.com", - "homepage": "http://everzet.com" - }, - { - "name": "Marcello Duarte", - "email": "marcello.duarte@gmail.com" - } - ], - "description": "Highly opinionated mocking framework for PHP 5.3+", - "homepage": "https://github.com/phpspec/prophecy", - "keywords": [ - "Double", - "Dummy", - "fake", - "mock", - "spy", - "stub" - ], - "support": { - "issues": "https://github.com/phpspec/prophecy/issues", - "source": "https://github.com/phpspec/prophecy/tree/v1.15.0" - }, - "time": "2021-12-08T12:19:24+00:00" - }, { "name": "phpunit/php-code-coverage", "version": "9.2.15", @@ -849,16 +623,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.19", + "version": "9.5.23", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "35ea4b7f3acabb26f4bb640f8c30866c401da807" + "reference": "888556852e7e9bbeeedb9656afe46118765ade34" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/35ea4b7f3acabb26f4bb640f8c30866c401da807", - "reference": "35ea4b7f3acabb26f4bb640f8c30866c401da807", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/888556852e7e9bbeeedb9656afe46118765ade34", + "reference": "888556852e7e9bbeeedb9656afe46118765ade34", "shasum": "" }, "require": { @@ -873,7 +647,6 @@ "phar-io/manifest": "^2.0.3", "phar-io/version": "^3.0.2", "php": ">=7.3", - "phpspec/prophecy": "^1.12.1", "phpunit/php-code-coverage": "^9.2.13", "phpunit/php-file-iterator": "^3.0.5", "phpunit/php-invoker": "^3.1.1", @@ -891,10 +664,6 @@ "sebastian/type": "^3.0", "sebastian/version": "^3.0.2" }, - "require-dev": { - "ext-pdo": "*", - "phpspec/prophecy-phpunit": "^2.0.1" - }, "suggest": { "ext-soap": "*", "ext-xdebug": "*" @@ -936,7 +705,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.19" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.23" }, "funding": [ { @@ -948,7 +717,7 @@ "type": "github" } ], - "time": "2022-03-15T09:57:31+00:00" + "time": "2022-08-22T14:01:36+00:00" }, { "name": "sebastian/cli-parser", @@ -1914,88 +1683,6 @@ ], "time": "2020-09-28T06:39:44+00:00" }, - { - "name": "symfony/polyfill-ctype", - "version": "v1.25.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "30885182c981ab175d4d034db0f6f469898070ab" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/30885182c981ab175d4d034db0f6f469898070ab", - "reference": "30885182c981ab175d4d034db0f6f469898070ab", - "shasum": "" - }, - "require": { - "php": ">=7.1" - }, - "provide": { - "ext-ctype": "*" - }, - "suggest": { - "ext-ctype": "For best performance" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-main": "1.23-dev" - }, - "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" - } - }, - "autoload": { - "files": [ - "bootstrap.php" - ], - "psr-4": { - "Symfony\\Polyfill\\Ctype\\": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Gert de Pagter", - "email": "BackEndTea@gmail.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for ctype functions", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "ctype", - "polyfill", - "portable" - ], - "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.25.0" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2021-10-20T20:35:02+00:00" - }, { "name": "theseer/tokenizer", "version": "1.2.1", @@ -2045,67 +1732,8 @@ } ], "time": "2021-07-28T10:34:58+00:00" - }, - { - "name": "webmozart/assert", - "version": "1.10.0", - "source": { - "type": "git", - "url": "https://github.com/webmozarts/assert.git", - "reference": "6964c76c7804814a842473e0c8fd15bab0f18e25" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/webmozarts/assert/zipball/6964c76c7804814a842473e0c8fd15bab0f18e25", - "reference": "6964c76c7804814a842473e0c8fd15bab0f18e25", - "shasum": "" - }, - "require": { - "php": "^7.2 || ^8.0", - "symfony/polyfill-ctype": "^1.8" - }, - "conflict": { - "phpstan/phpstan": "<0.12.20", - "vimeo/psalm": "<4.6.1 || 4.6.2" - }, - "require-dev": { - "phpunit/phpunit": "^8.5.13" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.10-dev" - } - }, - "autoload": { - "psr-4": { - "Webmozart\\Assert\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Bernhard Schussek", - "email": "bschussek@gmail.com" - } - ], - "description": "Assertions to validate method input/output with nice error messages.", - "keywords": [ - "assert", - "check", - "validate" - ], - "support": { - "issues": "https://github.com/webmozarts/assert/issues", - "source": "https://github.com/webmozarts/assert/tree/1.10.0" - }, - "time": "2021-03-09T10:59:23+00:00" } ], - "packages-dev": [], "aliases": [], "minimum-stability": "stable", "stability-flags": [], @@ -2113,5 +1741,5 @@ "prefer-lowest": false, "platform": [], "platform-dev": [], - "plugin-api-version": "2.1.0" + "plugin-api-version": "2.3.0" } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b2eb89a..91d4a5b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -13,18 +13,15 @@ verbose="true" > - + ./tests - - - \ No newline at end of file diff --git a/src/BCAParser.php b/src/BCAParser.php index 291f067..93d3693 100755 --- a/src/BCAParser.php +++ b/src/BCAParser.php @@ -9,13 +9,11 @@ * @author kadekjayak * @copyright 2016 kadekjayak * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 - * @version 0.1 + * @version 1.0 */ use DOMDocument; -define('BCA_PARSER_DEBUG', false); - class BCAParser { private $username; @@ -47,31 +45,52 @@ class BCAParser { /** * The Constructor - * this class will make login request to BCA when initialized + * if you have static IP Address, please pass it to the 3rd parameters + * those will skip the ip resolver and makes the function calls faster * * @param string $username * @param string $password */ - public function __construct($username, $password) + public function __construct($username, $password, $ipAddress = null) { - if( BCA_PARSER_DEBUG == true ) error_reporting(E_ALL); + if ($this->isDebug()) error_reporting(E_ALL); + $this->username = $username; $this->password = $password; + $this->ipAddress = $ipAddress; + $this->curlHandle = curl_init(); $this->setupCurl(); - $this->login($this->username, $this->password); + // $this->login($this->username, $this->password); } /** * Get ip address, required on login parameters * + * If something wrong with this 3rd party service, + * cpass your public ip address from the constructor + * OR + * extend this class and override the function with your own + * * @return String; */ - private function getIpAddress() + protected function getIpAddress() { - if($this->ipAddress !== null) $this->ipAddress = json_decode( file_get_contents( 'http://myjsonip.appspot.com/' ) )->ip; + if (!$this->ipAddress) { + $this->ipAddress = json_decode( file_get_contents( 'https://api.ipify.org?format=json' ) )->ip; + } + return $this->ipAddress; + } + /** + * Is Debug Enabled + * + * @return boolean + */ + protected function isDebug() + { + return defined("BCA_PARSER_DEBUG") && BCA_PARSER_DEBUG == true; } /** @@ -82,7 +101,7 @@ private function getIpAddress() public function exec() { $result = curl_exec($this->curlHandle); - if( BCA_PARSER_DEBUG == true ) { + if ($this->isDebug()) { $http_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE); print_r($result); @@ -134,7 +153,7 @@ protected function curlSetPost() /** * Login to BCA */ - private function login($username, $password) + public function login($username, $password) { //Just to Get Cookies curl_setopt( $this->curlHandle, CURLOPT_URL, $this->_defaultTargets['loginUrl'] ); @@ -195,7 +214,7 @@ private function getSaldoRekeningTable($html) { $dom = new DOMDocument(); - if ( BCA_PARSER_DEBUG ) { + if ($this->isDebug()) { $dom->loadHTML($html); } else { @$dom->loadHTML($html); @@ -289,7 +308,7 @@ private function getMutasiRekeningTable($html) { $dom = new DOMDocument(); - if ( BCA_PARSER_DEBUG ) { + if ($this->isDebug()) { $dom->loadHTML($html); } else { @$dom->loadHTML($html); @@ -418,4 +437,11 @@ public function logout() return $this->exec(); } + /** + * Get Last HTTP Code + */ + public function getLastHttpCode(){ + return curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE); + } + } diff --git a/tests/BCAParserTest.php b/tests/BCAParserTest.php index 1feac50..92d250e 100644 --- a/tests/BCAParserTest.php +++ b/tests/BCAParserTest.php @@ -16,10 +16,29 @@ public function setUp(): void { $this->username = getenv("BCA_USERNAME"); $this->password = getenv("BCA_PASSWORD"); - $this->client = new BCAParser($this->username, $this->password); + + /** + * Add Delays before each calls + * I'm affraid they'll block the account if we did it too fast + */ + sleep(5); + } + + /** + * Test Authentication + */ + public function testAuthentication(): void + { + $this->client->login($this->username, $this->password); + $this->client->logout(); + + $this->assertTrue(200 == intval($this->client->getLastHttpCode())); } + /** + * @depends testAuthentication + */ public function testGetBalance(): void { $balance = $this->client->getSaldo(); @@ -32,6 +51,9 @@ public function testGetBalance(): void $this->assertArrayHasKey('saldo', $tx); } + /** + * @depends testGetBalance + */ public function testGetTransactionMutation(): void { $fromDate = date('Y-m-d', strtotime(date('Y-m-d') . ' - 1 week')); @@ -49,6 +71,9 @@ public function testGetTransactionMutation(): void } } + /** + * @depends testGetTransactionMutation + */ public function testGetTransactionDebit(): void { $fromDate = date('Y-m-d', strtotime(date('Y-m-d') . ' - 1 week')); @@ -66,6 +91,9 @@ public function testGetTransactionDebit(): void } } + /** + * @depends testGetTransactionDebit + */ public function testGetTransactionCredit(): void { $fromDate = date('Y-m-d', strtotime(date('Y-m-d') . ' - 1 week'));