diff --git a/composer.json b/composer.json index 124b52e..cd50b94 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "paylike/php-api", "description": "PHP SDK to communicate with the Paylike HTTP API", - "version": "1.0.8", + "version": "2.0.0", "license": "MIT", "authors": [ { @@ -21,10 +21,10 @@ } }, "require": { - "php": ">=5.3.3", + "php": ">=5.6.0", "ext-curl": "*" }, "require-dev": { - "phpunit/phpunit": "^4.8" + "phpunit/phpunit": "^9.6.3" } } diff --git a/src/Endpoint/Merchants.php b/src/Endpoint/Merchants.php index 4327cdc..ac41471 100644 --- a/src/Endpoint/Merchants.php +++ b/src/Endpoint/Merchants.php @@ -55,7 +55,7 @@ public function update($merchant_id, $args) { $url = 'merchants/' . $merchant_id; - $this->paylike->client->request('PUT', $url, $args); + return $this->paylike->client->request('PUT', $url, $args); } /** diff --git a/src/Utils/Cursor.php b/src/Utils/Cursor.php index 9f59026..0aeedb5 100644 --- a/src/Utils/Cursor.php +++ b/src/Utils/Cursor.php @@ -55,6 +55,7 @@ public function __construct($endpoint, array $params, array $data, Paylike $payl * @link http://php.net/manual/en/iterator.rewind.php * @return void Any returned value is ignored. */ + #[\ReturnTypeWillChange] public function rewind() { $this->current_index = 0; @@ -65,6 +66,7 @@ public function rewind() * @link http://php.net/manual/en/iterator.current.php * @return mixed Can return any type. */ + #[\ReturnTypeWillChange] public function current() { return $this->collection[$this->current_index]; @@ -75,6 +77,7 @@ public function current() * @link http://php.net/manual/en/iterator.key.php * @return mixed scalar on success, or null on failure. */ + #[\ReturnTypeWillChange] public function key() { return $this->current_index; @@ -85,6 +88,7 @@ public function key() * @link http://php.net/manual/en/iterator.next.php * @return null any returned value is ignored. */ + #[\ReturnTypeWillChange] public function next() { ++$this->current_index; @@ -99,6 +103,7 @@ public function next() * @return boolean The return value will be casted to boolean and then evaluated. * Returns true on success or false on failure. */ + #[\ReturnTypeWillChange] public function valid() { return isset($this->collection[$this->current_index]); @@ -107,6 +112,7 @@ public function valid() /** * @return integer */ + #[\ReturnTypeWillChange] public function count() { return max($this->total_count, count($this->collection)); @@ -123,6 +129,7 @@ public function count() *
* The return value will be casted to boolean if non-boolean was returned. */ + #[\ReturnTypeWillChange] public function offsetExists($offset) { $exists = isset($this->collection[$offset]); @@ -145,6 +152,7 @@ public function offsetExists($offset) *
* @return mixed Can return all value types. */ + #[\ReturnTypeWillChange] public function offsetGet($offset) { $value = isset($this->collection[$offset]) ? $this->collection[$offset] : null; @@ -170,6 +178,7 @@ public function offsetGet($offset) * * @return void */ + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { if ($offset === null) { @@ -187,6 +196,7 @@ public function offsetSet($offset, $value) * * @return void */ + #[\ReturnTypeWillChange] public function offsetUnset($offset) { unset($this->collection[$offset]); @@ -195,6 +205,7 @@ public function offsetUnset($offset) /** * @return $this */ + #[\ReturnTypeWillChange] private function fetchNext() { $this->updateOffset()->fetch(); @@ -205,6 +216,7 @@ private function fetchNext() /** * @return $this */ + #[\ReturnTypeWillChange] private function fetch() { $api_response = $this->paylike->client->request('GET', $this->endpoint, $this->params); @@ -219,6 +231,7 @@ private function fetch() /** * If after is set, then we increment it, otherwise we increment before */ + #[\ReturnTypeWillChange] private function updateOffset() { if ($this->after()) { @@ -232,6 +245,7 @@ private function updateOffset() /** * @return mixed */ + #[\ReturnTypeWillChange] private function after() { return $this->params['after']; @@ -240,6 +254,7 @@ private function after() /** * @return mixed */ + #[\ReturnTypeWillChange] private function before() { return $this->params['before']; @@ -248,6 +263,7 @@ private function before() /** * @return mixed */ + #[\ReturnTypeWillChange] private function limit() { return $this->params['limit']; diff --git a/tests/AppsTest.php b/tests/AppsTest.php index 2a04705..f404c88 100644 --- a/tests/AppsTest.php +++ b/tests/AppsTest.php @@ -11,7 +11,7 @@ class AppsTest extends BaseTest */ protected $apps; - public function setUp() + public function setUp():void { parent::setUp(); $this->apps = $this->paylike->apps(); @@ -31,6 +31,6 @@ public function testFetch() { $app = $this->apps->fetch(); - $this->assertEquals($app['id'], $this->app_id, 'app id'); + $this->assertEquals($this->app_id, $app['id'], 'app id'); } } diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 071aef3..bb6e397 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -3,8 +3,9 @@ namespace Paylike\Tests; use Paylike\Paylike; +use PHPUnit\Framework\TestCase; -abstract class BaseTest extends \PHPUnit_Framework_TestCase +abstract class BaseTest extends TestCase { /** * @var Paylike @@ -15,12 +16,12 @@ abstract class BaseTest extends \PHPUnit_Framework_TestCase protected $transaction_id; protected $merchant_id; - public function setUp() + public function setUp(): void { - $this->paylike = new Paylike("dbcf01af-8667-4967-9791-56101ca87ac8"); - $this->app_id = "594d3cde5be12d547cbe2ec2"; - $this->transaction_id = "5da8272132aad22568a511b7"; - $this->merchant_id = "594d3c455be12d547cbe2ebe"; + $this->paylike = new Paylike("a61437c5-1043-443b-ac3a-fe49c2b58481"); + $this->app_id = "601268435b1f7e3d1ebf8271"; + $this->transaction_id = "63ca94a11ec6fb693b1da497"; + $this->merchant_id = "601267ebf700a44f17ee4fbf"; } } diff --git a/tests/CardsTest.php b/tests/CardsTest.php index f1889be..d103c92 100644 --- a/tests/CardsTest.php +++ b/tests/CardsTest.php @@ -12,7 +12,7 @@ class CardsTest extends BaseTest */ protected $cards; - public function setUp() + public function setUp():void { parent::setUp(); $this->cards = $this->paylike->cards(); @@ -29,7 +29,7 @@ public function testCreate() )); $this->assertNotEmpty($card_id, 'primary key'); - $this->assertInternalType('string', $card_id, 'primary key type'); + $this->assertIsString($card_id, 'primary key type'); } public function testFetch() @@ -48,7 +48,7 @@ public function testFetch() public function testFailFetch() { - $this->setExpectedException(NotFound::class); - $this->cards->fetch('wrong id'); + $this->expectException(NotFound::class); + $this->cards->fetch('wrong_id'); } } diff --git a/tests/CurrenciesTest.php b/tests/CurrenciesTest.php index 53de1c1..e4b85bc 100644 --- a/tests/CurrenciesTest.php +++ b/tests/CurrenciesTest.php @@ -14,7 +14,7 @@ class CurrenciesTest extends BaseTest { /** * */ - public function setUp() { + public function setUp():void { parent::setUp(); $this->currency = new Currencies(); } diff --git a/tests/MerchantsAppsTest.php b/tests/MerchantsAppsTest.php index da57ecc..705758f 100644 --- a/tests/MerchantsAppsTest.php +++ b/tests/MerchantsAppsTest.php @@ -15,7 +15,7 @@ class MerchantsAppsTest extends BaseTest /** * */ - public function setUp() + public function setUp():void { parent::setUp(); $this->apps = $this->paylike->merchants()->apps(); diff --git a/tests/MerchantsLinesTest.php b/tests/MerchantsLinesTest.php index 0d0ce1a..e70226d 100644 --- a/tests/MerchantsLinesTest.php +++ b/tests/MerchantsLinesTest.php @@ -15,7 +15,7 @@ class MerchantsLinesTest extends BaseTest /** * */ - public function setUp() + public function setUp():void { parent::setUp(); $this->lines = $this->paylike->merchants()->lines(); @@ -44,7 +44,7 @@ public function testGetAllLinesCursor() public function testGetAllLinesCursorBefore() { $merchant_id = $this->merchant_id; - $before = '5da8594efd0c53603c7bb3a5'; + $before = '63ca90ee5c35c032ce597a3e'; $api_lines = $this->lines->before($merchant_id, $before); $ids = array(); foreach ($api_lines as $line) { @@ -71,5 +71,5 @@ public function testGetAllMerchantsCursorAfter() $this->assertGreaterThan(0, count($api_lines), 'number of lines'); } - + } diff --git a/tests/MerchantsTest.php b/tests/MerchantsTest.php index 2020a93..9f9f48a 100644 --- a/tests/MerchantsTest.php +++ b/tests/MerchantsTest.php @@ -14,7 +14,7 @@ class MerchantsTest extends BaseTest /** * */ - public function setUp() + public function setUp():void { parent::setUp(); $this->merchants = $this->paylike->merchants(); @@ -38,7 +38,7 @@ public function testCreate() )); $this->assertNotEmpty($merchant_id, 'primary key'); - $this->assertInternalType('string', $merchant_id, 'primary key type'); + $this->assertIsString( $merchant_id, 'primary key type'); } /** @@ -60,9 +60,12 @@ public function testUpdate() { $merchant_id = $this->merchant_id; - $this->merchants->update($merchant_id, array( + $response = $this->merchants->update($merchant_id, array( 'name' => 'Updated Merchant Name' )); + + $this->assertNotEmpty($response, 'response'); + } /** @@ -88,8 +91,8 @@ public function testGetAllMerchantsCursor() public function testGetAllMerchantsCursorOptions() { $app_id = $this->app_id; - $after = '5952889e764d2754c974fe94'; - $before = '5b8e5b8cd294fa04eb4cfbeb'; + $after = '601267ebf700a44f17ee4fbf'; + $before = '63ea11e29b99b157051e4455'; $api_merchants = $this->merchants->find($app_id, array( 'after' => $after, 'before' => $before @@ -109,7 +112,7 @@ public function testGetAllMerchantsCursorOptions() public function testGetAllMerchantsCursorBefore() { $app_id = $this->app_id; - $before = '5b8e5b8cd294fa04eb4cfbeb'; + $before = '63ea11e29b99b157051e4455'; $api_merchants = $this->merchants->before($app_id, $before); $ids = array(); foreach ($api_merchants as $merchant) { diff --git a/tests/TransactionsTest.php b/tests/TransactionsTest.php index 95e6593..5eb11b6 100644 --- a/tests/TransactionsTest.php +++ b/tests/TransactionsTest.php @@ -6,264 +6,230 @@ use Paylike\Exception\NotFound; use Paylike\Endpoint\Transactions; -class TransactionsTest extends BaseTest -{ - /** - * @var Transactions - */ - protected $transactions; - - /** - * - */ - public function setUp() - { - parent::setUp(); - $this->transactions = $this->paylike->transactions(); - } - - /** - * - */ - public function testCreate() - { - $merchant_id = $this->merchant_id; - $transaction_id = $this->transaction_id; - - $new_transaction_id = $this->transactions->create($merchant_id, array( - 'transactionId' => $transaction_id, - 'currency' => 'EUR', - 'amount' => 200, - 'custom' => array( - 'source' => 'php client test' - ) - )); - - $this->assertNotEmpty($new_transaction_id, 'primary key'); - $this->assertInternalType('string', $new_transaction_id, 'primary key type'); - } - - /** - * - */ - public function testFetch() - { - $transaction_id = $this->transaction_id; - - $transaction = $this->transactions->fetch($transaction_id); - - $this->assertEquals($transaction['id'], $transaction_id, 'primary key'); - } - - /** - * - */ - public function testFailFetch() - { - $this->setExpectedException(NotFound::class); - $this->transactions->fetch('wrong id'); - } - - /** - * - */ - public function testCapture() - { - $new_transaction_id = $this->createNewTransactionForTest(); - - $transaction = $this->transactions->capture($new_transaction_id, array( - 'currency' => 'EUR', - 'amount' => 100 - )); - - $this->assertEquals($transaction['capturedAmount'], 100, - 'captured amount'); - $this->assertEquals($transaction['pendingAmount'], 200, - 'pending amount'); - - $trail = $transaction['trail']; - $this->assertCount(1, $trail, 'length of trail'); - $this->assertEquals($trail[0]['capture'], true, 'type of trail'); - $this->assertEquals($trail[0]['amount'], 100, 'amount in capture trail'); - } - - /** - * - */ - public function testCaptureBiggerAmount() - { - $this->setExpectedException(InvalidRequest::class); - - $new_transaction_id = $this->createNewTransactionForTest(); - $this->transactions->capture($new_transaction_id, array( - 'currency' => 'EUR', - 'amount' => 400 - )); - } - - /** - * - */ - public function testRefund() - { - $new_transaction_id = $this->createNewTransactionForTest(); - - $this->transactions->capture($new_transaction_id, array( - 'currency' => 'EUR', - 'amount' => 200 - )); - - $transaction = $this->transactions->refund($new_transaction_id, array( - 'amount' => 120 - )); - - $this->assertEquals($transaction['capturedAmount'], 200, - 'captured amount'); - $this->assertEquals($transaction['pendingAmount'], 100, - 'pending amount'); - $this->assertEquals($transaction['refundedAmount'], 120, - 'refunded amount'); - - $trail = $transaction['trail']; - $this->assertCount(2, $trail, 'length of trail'); - $this->assertEquals($trail[0]['capture'], true, 'type of trail'); - $this->assertEquals($trail[0]['amount'], 200, 'amount in capture trail'); - $this->assertEquals($trail[1]['refund'], true, 'type of trail'); - $this->assertEquals($trail[1]['amount'], 120, 'amount in refund trail'); - } - - /** - * - */ - public function testVoid() - { - $new_transaction_id = $this->createNewTransactionForTest(); - - $transaction = $this->transactions->void($new_transaction_id, array( - 'amount' => 200 - )); - - $this->assertEquals($transaction['voidedAmount'], 200, 'voided amount'); - $this->assertEquals($transaction['pendingAmount'], 100, - 'pending amount'); - - $trail = $transaction['trail']; - $this->assertCount(1, $trail, 'length of trail'); - $this->assertEquals($trail[0]['void'], true, 'type of trail'); - $this->assertEquals($trail[0]['amount'], 200, 'amount in void trail'); - } - - /** - * @return bool|mixed - */ - private function createNewTransactionForTest() - { - $merchant_id = $this->merchant_id; - $transaction_id = $this->transaction_id; - - $new_transaction_id = $this->transactions->create($merchant_id, array( - 'transactionId' => $transaction_id, - 'currency' => 'EUR', - 'amount' => 300, - 'custom' => array( - 'source' => 'php client test' - ) - )); - - return $new_transaction_id; - } - - /** - * @throws \Exception - */ - public function testGetAllTransactionsCursor() - { - $merchant_id = $this->merchant_id; - $api_transactions = $this->transactions->find($merchant_id); - $ids = array(); - foreach ($api_transactions as $transaction) { - // the transaction array grows as needed - $ids[] = $transaction['id']; - } - - $this->assertGreaterThan(0, count($ids), 'number of transactions'); - } - - - /** - * @throws \Exception - */ - public function testGetAllTransactionsCursorOptions() - { - $merchant_id = $this->merchant_id; - $limit = 10; - $after = '5b8e839d7cc76f04ecd3f733'; - $before = '5b98deef882cf804f6108700'; - $api_transactions = $this->transactions->find($merchant_id, array( - 'limit' => $limit, - 'after' => $after, - 'before' => $before - )); - $ids = array(); - foreach ($api_transactions as $transaction) { - // the transaction array grows as needed - $ids[] = $transaction['id']; - } - - $this->assertGreaterThan(0, count($api_transactions), 'number of transactions'); - } - - /** - * @throws \Exception - */ - public function testGetAllTransactionsCursorBefore() - { - $merchant_id = $this->merchant_id; - $before = '5b98deef882cf804f6108700'; - $api_transactions = $this->transactions->before($merchant_id, $before); - $ids = array(); - foreach ($api_transactions as $transaction) { - // the transaction array grows as needed - $ids[] = $transaction['id']; - } - - $this->assertGreaterThan(0, count($api_transactions), 'number of transactions'); - } - - /** - * @throws \Exception - */ - public function testGetAllTransactionsCursorAfter() - { - $merchant_id = $this->merchant_id; - $after = '5b8e839d7cc76f04ecd3f733'; - $api_transactions = $this->transactions->before($merchant_id, $after); - $ids = array(); - foreach ($api_transactions as $transaction) { - // the transaction array grows as needed - $ids[] = $transaction['id']; - } - - $this->assertGreaterThan(0, count($api_transactions), 'number of transactions'); - } - - /** - * @throws \Exception - */ - public function testGetAllTransactionsFilter() - { - $merchant_id = $this->merchant_id; - $api_transactions = $this->transactions->find($merchant_id, array( - 'filter' => array( - 'test' => true - ), - )); - $ids = array(); - foreach ($api_transactions as $transaction) { - // the transaction array grows as needed - $ids[] = $transaction['id']; - } - - $this->assertGreaterThan(0, count($api_transactions), 'number of transactions'); - } +class TransactionsTest extends BaseTest { + /** + * @var Transactions + */ + protected $transactions; + + /** + * + */ + public function setUp(): void { + parent::setUp(); + $this->transactions = $this->paylike->transactions(); + } + + /** + * + */ + public function testCreate() { + $merchant_id = $this->merchant_id; + $transaction_id = $this->transaction_id; + + $new_transaction_id = $this->transactions->create( $merchant_id, array( + 'transactionId' => $transaction_id, + 'currency' => 'EUR', + 'amount' => 200, + 'custom' => array( + 'source' => 'php client test' + ) + ) ); + + $this->assertNotEmpty( $new_transaction_id, 'primary key' ); + $this->assertIsString( $new_transaction_id, 'primary key type' ); + } + + /** + * + */ + public function testFetch() { + $transaction_id = $this->transaction_id; + + $transaction = $this->transactions->fetch( $transaction_id ); + + $this->assertEquals( $transaction_id, $transaction['id'], 'primary key' ); + } + + /** + * + */ + public function testFailFetch() { + $this->expectException( NotFound::class ); + $this->transactions->fetch( 'wrong_id' ); + } + + /** + * + */ + public function testCapture() { + $new_transaction_id = $this->createNewTransactionForTest(); + + $transaction = $this->transactions->capture( $new_transaction_id, array( + 'currency' => 'EUR', + 'amount' => 100 + ) ); + + $this->assertEquals( $transaction['capturedAmount'], 100, + 'captured amount' ); + $this->assertEquals( $transaction['pendingAmount'], 200, + 'pending amount' ); + + $trail = $transaction['trail']; + $this->assertCount( 1, $trail, 'length of trail' ); + $this->assertEquals( true,$trail[0]['capture'], 'type of trail' ); + $this->assertEquals( 100, $trail[0]['amount'], 'amount in capture trail' ); + } + + /** + * + */ + public function testCaptureBiggerAmount() { + $this->expectException( InvalidRequest::class ); + + $new_transaction_id = $this->createNewTransactionForTest(); + $this->transactions->capture( $new_transaction_id, array( + 'currency' => 'EUR', + 'amount' => 400 + ) ); + } + + /** + * + */ + public function testRefund() { + $new_transaction_id = $this->createNewTransactionForTest(); + + $this->transactions->capture( $new_transaction_id, array( + 'currency' => 'EUR', + 'amount' => 200 + ) ); + + $transaction = $this->transactions->refund( $new_transaction_id, array( + 'amount' => 120 + ) ); + + $this->assertEquals( 200, $transaction['capturedAmount'], + 'captured amount' ); + $this->assertEquals( 100,$transaction['pendingAmount'], + 'pending amount' ); + $this->assertEquals( 120,$transaction['refundedAmount'], + 'refunded amount' ); + + $trail = $transaction['trail']; + $this->assertCount( 2, $trail, 'length of trail' ); + $this->assertEquals( true,$trail[0]['capture'], 'type of trail' ); + $this->assertEquals( 200,$trail[0]['amount'], 'amount in capture trail' ); + $this->assertEquals( true,$trail[1]['refund'], 'type of trail' ); + $this->assertEquals( 120,$trail[1]['amount'], 'amount in refund trail' ); + } + + /** + * + */ + public function testVoid() { + $new_transaction_id = $this->createNewTransactionForTest(); + + $transaction = $this->transactions->void( $new_transaction_id, array( + 'amount' => 200 + ) ); + + $this->assertEquals( 200,$transaction['voidedAmount'], 'voided amount' ); + $this->assertEquals( 100,$transaction['pendingAmount'], + 'pending amount' ); + + $trail = $transaction['trail']; + $this->assertCount( 1, $trail, 'length of trail' ); + $this->assertEquals( true,$trail[0]['void'], 'type of trail' ); + $this->assertEquals( 200,$trail[0]['amount'], 'amount in void trail' ); + } + + /** + * @return bool|mixed + */ + private function createNewTransactionForTest() { + $merchant_id = $this->merchant_id; + $transaction_id = $this->transaction_id; + + $new_transaction_id = $this->transactions->create( $merchant_id, array( + 'transactionId' => $transaction_id, + 'currency' => 'EUR', + 'amount' => 300, + 'custom' => array( + 'source' => 'php client test' + ) + ) ); + + return $new_transaction_id; + } + + /** + * @throws \Exception + */ + public function testGetAllTransactionsCursor() { + $merchant_id = $this->merchant_id; + $api_transactions = $this->transactions->find( $merchant_id ); + $ids = array(); + foreach ( $api_transactions as $transaction ) { + // the transaction array grows as needed + $ids[] = $transaction['id']; + } + + $this->assertGreaterThan( 0, count( $ids ), 'number of transactions' ); + } + + + /** + * @throws \Exception + */ + public function testGetAllTransactionsCursorOptions() { + $merchant_id = $this->merchant_id; + $limit = 10; + $after = '63ecabe99b99b157051e9c31'; + $before = '63ecabeec9586702e2ebd26d'; + $api_transactions = $this->transactions->find( $merchant_id, array( + 'limit' => $limit, + 'after' => $after, + 'before' => $before + ) ); + + $this->assertGreaterThan( 0, count( $api_transactions ), 'number of transactions' ); + } + + /** + * @throws \Exception + */ + public function testGetAllTransactionsCursorBefore() { + $merchant_id = $this->merchant_id; + $before = '63ecabeec9586702e2ebd26d'; + $api_transactions = $this->transactions->before( $merchant_id, $before ); + + $this->assertGreaterThan( 0, count( $api_transactions ), 'number of transactions' ); + } + + /** + * @throws \Exception + */ + public function testGetAllTransactionsCursorAfter() { + $merchant_id = $this->merchant_id; + $after = '63ecabe99b99b157051e9c31'; + $api_transactions = $this->transactions->before( $merchant_id, $after ); + + + $this->assertGreaterThan( 0, count( $api_transactions ), 'number of transactions' ); + } + + /** + * @throws \Exception + */ + public function testGetAllTransactionsFilter() { + $merchant_id = $this->merchant_id; + $api_transactions = $this->transactions->find( $merchant_id, array( + 'filter' => array( + 'test' => true + ), + ) ); + + $this->assertGreaterThan( 0, count( $api_transactions ), 'number of transactions' ); + } }