From 3f38b434a43582d55d6dc7508ce77382d2826bfb Mon Sep 17 00:00:00 2001
From: ionutcalara
+ * An offset to check for.
+ *
+ * The return value will be casted to boolean if non-boolean was returned. + */ + public function offsetExists($offset) + { + $exists = isset($this->collection[$offset]); + if ($exists) { + return $exists; + } + if ($this->couldHaveMoreItems()) { + $this->fetchNext(); + return $this->offsetExists($offset); + } else { + return $exists; + } + } + + /** + * Offset to retrieve + * @link http://php.net/manual/en/arrayaccess.offsetget.php + * @param mixed $offset
+ * The offset to retrieve. + *
+ * @return mixed Can return all value types. + */ + public function offsetGet($offset) + { + $value = isset($this->collection[$offset]) ? $this->collection[$offset] : null; + if ($value) { + return $value; + } + if ($this->couldHaveMoreItems()) { + $this->fetchNext(); + return $this->offsetGet($offset); + } else { + return $value; + } + } + + /** + * Offset to set + * @link http://php.net/manual/en/arrayaccess.offsetset.php + * @param mixed $offset+ * The offset to assign the value to. + *
+ * @param mixed $value+ * The value to set. + *
+ * @return void + */ + public function offsetSet($offset, $value) + { + if ($offset === null) { + $this->collection[] = $value; + } else { + $this->collection[$offset] = $value; + } + } + + /** + * Offset to unset + * @link http://php.net/manual/en/arrayaccess.offsetunset.php + * @param mixed $offset+ * The offset to unset. + *
+ * @return void + */ + public function offsetUnset($offset) + { + unset($this->collection[$offset]); + } + + /** + * @return $this + */ + private function fetchNext() + { + $this->updateOffset()->fetch(); + return $this; + + } + + /** + * @return $this + */ + private function fetch() + { + $api_response = $this->paylike->client->request('GET', $this->endpoint, $this->params); + $data = $api_response->json; + if (count($data)) { + $this->collection = array_merge($this->collection, $data); + $this->total_count += count($data); + } + return $this; + } + + /** + * If after is set, then we increment it, otherwise we increment before + */ + private function updateOffset() + { + if ($this->after()) { + $this->params['after'] = $this->collection[$this->total_count - 1]['id']; + } else { + $this->params['before'] = $this->collection[$this->total_count - 1]['id']; + } + return $this; + } + + /** + * @return mixed + */ + private function after() + { + return $this->params['after']; + } + + /** + * @return mixed + */ + private function before() + { + return $this->params['before']; + } + + /** + * @return mixed + */ + private function limit() + { + return $this->params['limit']; + } + + /** + * @param $params + * @return array + * @throws \Exception + */ + private function setParams($params) + { + $return = array( + 'after' => null, + 'before' => null, + 'limit' => 10, + 'filter' => array() + ); + + if (isset($params['limit'])) { + $limit = $params['limit']; + if (!is_numeric($limit) || $limit <= 0) { + throw new \Exception('Limit is not valid. It has to be a numerical value (> 0)'); + } + $return['limit'] = $limit; + } + + if (isset($params['after'])) { + $after = $params['after']; + if (!is_string($after)) { + throw new \Exception('After is not valid. It has to be a string'); + } + $return['after'] = $after; + } + + if (isset($params['before'])) { + $before = $params['before']; + if (!is_string($before)) { + throw new \Exception('Before is not valid. It has to be a string'); + } + $return['before'] = $before; + } + + if (isset($params['filter'])) { + $filter = $params['filter']; + if (!is_array($filter)) { + throw new \Exception('Filter is not valid. It has to be an array'); + } + if (isset($filter['merchantId'])) { + if (!is_string($filter['merchantId'])) { + throw new \Exception('Merchant filter is not valid. It has to be an string'); + } + $return['filter']['merchantId'] = $filter['merchantId']; + } + if (isset($filter['transactionId'])) { + if (!is_string($filter['transactionId'])) { + throw new \Exception('Transaction filter is not valid. It has to be an string'); + } + $return['filter']['transactionId'] = $filter['transactionId']; + } + } + return $return; + } + + private function couldHaveMoreItems() + { + return ($this->count() % $this->limit() == 0); + } + +} diff --git a/tests/MerchantsTest.php b/tests/MerchantsTest.php index 58481d5..24e07ec 100644 --- a/tests/MerchantsTest.php +++ b/tests/MerchantsTest.php @@ -11,6 +11,9 @@ class MerchantsTest extends BaseTest */ protected $merchants; + /** + * + */ public function setUp() { parent::setUp(); @@ -18,6 +21,9 @@ public function setUp() } + /** + * + */ public function testCreate() { $merchant_id = $this->merchants->create(array( @@ -35,6 +41,9 @@ public function testCreate() $this->assertInternalType('string', $merchant_id, 'primary key type'); } + /** + * + */ public function testFetch() { $merchant_id = $this->merchant_id; @@ -44,26 +53,9 @@ public function testFetch() $this->assertEquals($merchant['id'], $merchant_id, 'primary key'); } - public function testGetAll() - { - $app_id = $this->app_id; - $merchants = array(); - $limit = 10; - $before = null; - - do { - $api_merchants = $this->merchants->get($app_id, $limit, $before); - if (count($api_merchants) < $limit) { - $before = null; - } else { - $before = $api_merchants[$limit - 1]['id']; - } - $merchants = array_merge($merchants,$api_merchants); - } while ($before); - - $this->assertGreaterThan(0, count($merchants), 'number of merchants'); - } - + /** + * + */ public function testUpdate() { $merchant_id = $this->merchant_id; @@ -72,4 +64,76 @@ public function testUpdate() 'name' => 'Updated Merchant Name' )); } + + /** + * @throws \Exception + */ + public function testGetAllMerchantsCursor() + { + $app_id = $this->app_id; + $api_merchants = $this->merchants->find($app_id); + $ids = array(); + foreach ($api_merchants as $merchant) { + // the merchants array grows as needed + $ids[] = $merchant['id']; + } + + $this->assertGreaterThan(0, count($ids), 'number of merchants'); + } + + + /** + * @throws \Exception + */ + public function testGetAllMerchantsCursorOptions() + { + $app_id = $this->app_id; + $after = '5952889e764d2754c974fe94'; + $before = '5b8e5b8cd294fa04eb4cfbeb'; + $api_merchants = $this->merchants->find($app_id, array( + 'after' => $after, + 'before' => $before + )); + $ids = array(); + foreach ($api_merchants as $merchant) { + // the merchants array grows as needed + $ids[] = $merchant['id']; + } + + $this->assertGreaterThan(0, count($api_merchants), 'number of merchants'); + } + + /** + * @throws \Exception + */ + public function testGetAllMerchantsCursorBefore() + { + $app_id = $this->app_id; + $before = '5b8e5b8cd294fa04eb4cfbeb'; + $api_merchants = $this->merchants->before($app_id, $before); + $ids = array(); + foreach ($api_merchants as $merchant) { + // the merchants array grows as needed + $ids[] = $merchant['id']; + } + + $this->assertGreaterThan(0, count($api_merchants), 'number of merchants'); + } + + /** + * @throws \Exception + */ + public function testGetAllMerchantsCursorAfter() + { + $app_id = $this->app_id; + $after = '5952889e764d2754c974fe94'; + $api_merchants = $this->merchants->after($app_id, $after); + $ids = array(); + foreach ($api_merchants as $merchant) { + // the merchants array grows as needed + $ids[] = $merchant['id']; + } + + $this->assertGreaterThan(0, count($api_merchants), 'number of merchants'); + } } diff --git a/tests/TransactionsTest.php b/tests/TransactionsTest.php index 58377dd..ae4ecc3 100644 --- a/tests/TransactionsTest.php +++ b/tests/TransactionsTest.php @@ -13,12 +13,18 @@ class TransactionsTest extends BaseTest */ protected $transactions; + /** + * + */ public function setUp() { parent::setUp(); $this->transactions = $this->paylike->transactions(); } + /** + * + */ public function testCreate() { $merchant_id = $this->merchant_id; @@ -37,6 +43,9 @@ public function testCreate() $this->assertInternalType('string', $new_transaction_id, 'primary key type'); } + /** + * + */ public function testFetch() { $transaction_id = $this->transaction_id; @@ -46,12 +55,18 @@ public function testFetch() $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(); @@ -72,6 +87,9 @@ public function testCapture() $this->assertEquals($trail[0]['amount'], 100, 'amount in capture trail'); } + /** + * + */ public function testCaptureBiggerAmount() { $this->setExpectedException(InvalidRequest::class); @@ -83,6 +101,9 @@ public function testCaptureBiggerAmount() )); } + /** + * + */ public function testRefund() { $new_transaction_id = $this->createNewTransactionForTest(); @@ -111,6 +132,9 @@ public function testRefund() $this->assertEquals($trail[1]['amount'], 120, 'amount in refund trail'); } + /** + * + */ public function testVoid() { $new_transaction_id = $this->createNewTransactionForTest(); @@ -150,25 +174,76 @@ private function createNewTransactionForTest() } /** - * + * @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 testGetAllTransactions() + public function testGetAllTransactionsCursorOptions() { $merchant_id = $this->merchant_id; - $transactions = array(); $limit = 10; - $before = null; - - do { - $api_transactions = $this->transactions->get($merchant_id, $limit, $before); - if (count($api_transactions) < $limit) { - $before = null; - } else { - $before = $api_transactions[$limit - 1]['id']; - } - $transactions = array_merge($transactions, $api_transactions); - } while ($before); - - $this->assertGreaterThan(0, count($transactions), 'number of transactions'); + $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'); } }