allow vendord
All checks were successful
Build, Push and Deploy / build-and-push (push) Successful in 3m3s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 16s
Build, Push and Deploy / deploy-staging (push) Successful in 32s
Build, Push and Deploy / deploy-production (push) Has been skipped
Build, Push and Deploy / cleanup (push) Successful in 1s

This commit is contained in:
2026-03-30 14:54:57 +07:00
parent 66aed7c4e8
commit b5e3a778ce
21316 changed files with 2777892 additions and 13 deletions

View File

@@ -0,0 +1,225 @@
<?php
namespace Tests;
class AccountTest extends BaseTestCase
{
/**
* Should return array of FedaPay\Account
*/
public function testShouldReturnAccounts()
{
$body = [
'v1/accounts' => [[
'country' => 'BJ',
'created_at' => '2018-03-12T09:09:03.969Z',
'id' => 1,
'klass' => 'v1/account',
'name' => 'Test account',
'timezone' => 'UTC',
'updated_at' => '2018-03-12T09:09:03.969Z'
]]
];
$this->mockRequest('get', '/v1/accounts', [], $body);
$object = \FedaPay\Account::all();
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertTrue(is_array($object->accounts));
$this->assertInstanceOf(\FedaPay\Account::class, $object->accounts[0]);
$this->assertEquals('Test account', $object->accounts[0]->name);
$this->assertEquals(1, $object->accounts[0]->id);
$this->assertEquals('UTC', $object->accounts[0]->timezone);
}
/**
* Should return array of FedaPay\Account
*/
public function testShouldCreateAccount()
{
$data = [
'name' => 'My account'
];
$body = [
'v1/account' => [
'country' => 'BJ',
'created_at' => '2018-03-12T09:09:03.969Z',
'id' => 1,
'klass' => 'v1/account',
'name' => $data['name'],
'timezone' => 'UTC',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/accounts', $data, $body);
$account = \FedaPay\Account::create($data);
$this->assertInstanceOf(\FedaPay\Account::class, $account);
$this->assertEquals($account->name, $data['name']);
}
/**
* Should retrieve a Account
*/
public function testShouldRetrievedAccount()
{
$body = [
'v1/account' => [
'country' => 'BJ',
'created_at' => '2018-03-12T09:09:03.969Z',
'id' => 1,
'klass' => 'v1/account',
'name' => 'My account',
'timezone' => 'UTC',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('get', '/v1/accounts/1', [], $body);
$account = \FedaPay\Account::retrieve(1);
$this->assertInstanceOf(\FedaPay\Account::class, $account);
$this->assertEquals($account->id, 1);
$this->assertEquals($account->name, 'My account');
$this->assertEquals($account->country, 'BJ');
$this->assertEquals($account->timezone, 'UTC');
}
/**
* Should update a account
*/
public function testShouldUpdateAccount()
{
$data = [
'name' => 'Updated Name',
];
$body = [
'v1/account' => [
'country' => 'BJ',
'created_at' => '2018-03-12T09:09:03.969Z',
'id' => 1,
'klass' => 'v1/account',
'name' => $data['name'],
'timezone' => 'UTC',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('put', '/v1/accounts/1', $data, $body);
$account = \FedaPay\Account::update(1, $data);
$this->assertInstanceOf(\FedaPay\Account::class, $account);
$this->assertEquals($account->name, $data['name']);
$this->assertEquals($account->id, 1);
$this->assertEquals($account->country, 'BJ');
$this->assertEquals($account->timezone, 'UTC');
}
/**
* Should update a account with save
*/
public function testShouldUpdateAccountWithSave()
{
$data = [
'name' => 'Updated Name',
];
$body = [
'v1/account' => [
'country' => 'BJ',
'created_at' => '2018-03-12T09:09:03.969Z',
'id' => 1,
'klass' => 'v1/account',
'name' => 'Name',
'timezone' => 'UTC',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/accounts', $data, $body);
$account = \FedaPay\Account::create($data);
$account->name = 'Updated Name';
$updateData = [
'country' => 'BJ',
'created_at' => '2018-03-12T09:09:03.969Z',
'klass' => 'v1/account',
'name' => 'Updated Name',
'timezone' => 'UTC',
'updated_at' => '2018-03-12T09:09:03.969Z'
];
$this->mockRequest('put', '/v1/accounts/1', $updateData, $body);
$account->save();
}
/**
* Should delete a account
*/
public function testShouldDeleteAccount()
{
$data = [
'name' => 'My account',
];
$body = [
'v1/account' => [
'country' => 'BJ',
'created_at' => '2018-03-12T09:09:03.969Z',
'id' => 1,
'klass' => 'v1/account',
'name' => $data['name'],
'timezone' => 'UTC',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/accounts', $data, $body);
$account = \FedaPay\Account::create($data);
$this->mockRequest('delete', '/v1/accounts/1');
$account->delete();
}
/**
* Should get account light info
*/
public function testShouldGetAccountLightInfo()
{
$data = [
'name' => 'My account',
];
$body = [
'v1/account' => [
'country' => 'BJ',
'created_at' => '2018-03-12T09:09:03.969Z',
'id' => 1,
'klass' => 'v1/account',
'name' => $data['name'],
'timezone' => 'UTC',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('get', '/v1/accounts/light', [], $body, 200, ['FedaPay-Pos-Code' => '0001']);
$account = \FedaPay\Account::light([], ['FedaPay-Pos-Code' => '0001']);
$this->assertInstanceOf(\FedaPay\Account::class, $account);
$this->assertEquals($account->name, $data['name']);
$this->assertEquals($account->id, 1);
$this->assertEquals($account->country, 'BJ');
$this->assertEquals($account->timezone, 'UTC');
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Tests;
class BalanceTest extends BaseTestCase
{
/**
* Should return FedaPay\Balance
*/
public function testShouldReturnCurrencies()
{
$body = [
'v1/balances' => [[
'id' => 1,
'klass' => 'v1/balance',
'currency_id' => 1,
'account_id' => 1,
'amount' => 952,
'mode' => 'mtn',
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]]
];
$this->mockRequest('get', '/v1/balances', [], $body);
$object = \FedaPay\Balance::all();
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertTrue(is_array($object->balances));
$this->assertInstanceOf(\FedaPay\Balance::class, $object->balances[0]);
$this->assertEquals(1, $object->balances[0]->currency_id);
$this->assertEquals(1, $object->balances[0]->account_id);
$this->assertEquals(952, $object->balances[0]->amount);
$this->assertEquals('mtn', $object->balances[0]->mode);
}
/**
* Should retrieve a Customer
*/
public function testShouldRetrievedABalance()
{
$body = [
'v1/balance' => [
'id' => 1,
'klass' => 'v1/balance',
'currency_id' => 1,
'account_id' => 1,
'amount' => 952,
'mode' => 'mtn',
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('get', '/v1/balances/1', [], $body);
$balance = \FedaPay\Balance::retrieve(1);
$this->assertInstanceOf(\FedaPay\Balance::class, $balance);
$this->assertEquals(1, $balance->currency_id);
$this->assertEquals(1, $balance->account_id);
$this->assertEquals(952, $balance->amount);
$this->assertEquals('mtn', $balance->mode);
}
}

View File

@@ -0,0 +1,126 @@
<?php
namespace Tests;
use PHPUnit\Framework\TestCase;
use FedaPay\HttpClient\CurlClient;
abstract class BaseTestCase extends TestCase
{
protected $defaultHeaders = [];
const API_KEY = 'sk_local_123';
const OAUTH_TOKEN = 'oauth_test_token_123';
const API_BASE = 'https://dev-api.fedapay.com';
/** @before */
protected function setUpConfig()
{
\FedaPay\FedaPay::setApiKey(self::API_KEY);
\FedaPay\FedaPay::setApiBase(self::API_BASE);
\FedaPay\Requestor::setHttpClient(\FedaPay\HttpClient\CurlClient::instance());
$this->defaultHeaders = [
'X-Version' => \FedaPay\FedaPay::VERSION,
'X-Api-Version' => \FedaPay\FedaPay::getApiVersion(),
'X-Source' => 'FedaPay PhpLib',
'Authorization' => 'Bearer '. (self::API_KEY ?: self::OAUTH_TOKEN)
];
}
/** @after */
protected function tearDownConfig()
{
// Back to default
\FedaPay\FedaPay::setApiKey(null);
\FedaPay\FedaPay::setApiVersion('v1');
\FedaPay\FedaPay::setEnvironment('sandbox');
\FedaPay\FedaPay::setToken(null);
\FedaPay\FedaPay::setAccountId(null);
\FedaPay\FedaPay::setVerifySslCerts(true);
\FedaPay\FedaPay::setLocale(null);
\FedaPay\Requestor::setHttpClient(null);
}
protected function mockRequest(
$method,
$path,
$params = [],
$response = [],
$rcode = 200,
$headers = []
) {
$mock = $this->setUpMockRequest();
$base = \FedaPay\FedaPay::getApiBase();
$absUrl = $base . $path;
$headers = array_merge($this->defaultHeaders, $headers);
$rawHeaders = [];
foreach ($headers as $k => $v) {
$rawHeaders[] = $k . ': ' . $v;
}
if (is_array($response)) {
$response = json_encode($response);
}
$mock->expects($this->once())
->method('request')
->with(
strtolower($method),
$absUrl,
$params,
$rawHeaders
)
->willReturn([$response, $rcode, []]);
}
protected function mockMultipleRequests($requests)
{
$mock = $this->setUpMockRequest();
$base = \FedaPay\FedaPay::getApiBase();
$withs = [];
$returns = [];
foreach ($requests as $req) {
$req = array_merge(['params' => [], 'response' => [], 'rcode' => 200, 'headers' => []], $req);
$absUrl = $base . $req['path'];
$headers = array_merge($this->defaultHeaders, $req['headers']);
$rawHeaders = [];
foreach ($headers as $k => $v) {
$rawHeaders[] = $k . ': ' . $v;
}
if (is_array($req['response'])) {
$response = json_encode($req['response']);
}
$withs[] = [
strtolower($req['method']),
$absUrl,
$req['params'],
$rawHeaders,
[$response, $req['rcode'], []]
];
}
$mock->expects($this->exactly(count($requests)))
->method('request')
->will($this->returnValueMap($withs));
}
protected function setUpMockRequest()
{
$mock = $this->getMockBuilder('\FedaPay\HttpClient\ClientInterface')
->setMethods(['request'])
->getMock();
\FedaPay\Requestor::setHttpClient($mock);
return $mock;
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Tests;
class CurrencyTest extends BaseTestCase
{
/**
* Should return FedaPay\Currency
*/
public function testShouldReturnCurrencies()
{
$body = [
'v1/currencies' => [[
'id' => 1,
'klass' => 'v1/currency',
'name' => 'FCFA',
'iso' => 'XOF',
'code' => 952,
'prefix' => null,
'suffix' => 'CFA',
'div' => 1,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]]
];
$this->mockRequest('get', '/v1/currencies', [], $body);
$object = \FedaPay\Currency::all();
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertTrue(is_array($object->currencies));
$this->assertInstanceOf(\FedaPay\Currency::class, $object->currencies[0]);
$this->assertEquals('FCFA', $object->currencies[0]->name);
$this->assertEquals('XOF', $object->currencies[0]->iso);
$this->assertEquals(952, $object->currencies[0]->code);
$this->assertEquals(null, $object->currencies[0]->prefix);
$this->assertEquals('CFA', $object->currencies[0]->suffix);
$this->assertEquals(1, $object->currencies[0]->div);
}
/**
* Should retrieve a Customer
*/
public function testShouldRetrievedACurrency()
{
$body = [
'v1/currency' => [
'id' => 1,
'klass' => 'v1/currency',
'name' => 'FCFA',
'iso' => 'XOF',
'code' => 952,
'prefix' => null,
'suffix' => 'CFA',
'div' => 1,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('get', '/v1/currencies/1', [], $body);
$currency = \FedaPay\Currency::retrieve(1);
$this->assertInstanceOf(\FedaPay\Currency::class, $currency);
$this->assertEquals('FCFA', $currency->name);
$this->assertEquals('XOF', $currency->iso);
$this->assertEquals(952, $currency->code);
$this->assertEquals(null, $currency->prefix);
$this->assertEquals('CFA', $currency->suffix);
$this->assertEquals(1, $currency->div);
}
}

View File

@@ -0,0 +1,248 @@
<?php
namespace Tests;
class CustomerTest extends BaseTestCase
{
/**
* Should return array of FedaPay\Customer
*/
public function testShouldReturnCustomers()
{
$body = [
'v1/customers' => [[
'id' => 1,
'klass' => 'v1/customer',
'firstname' => 'John',
'lastname' => 'Doe',
'email' => 'john.doe@example.com',
'phone' => '22967666776',
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]],
'meta' => ['page' => 1]
];
$this->mockRequest('get', '/v1/customers', [], $body);
$object = \FedaPay\Customer::all();
$this->assertTrue(is_array($object->customers));
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object->meta);
$this->assertInstanceOf(\FedaPay\Customer::class, $object->customers[0]);
$this->assertEquals('John', $object->customers[0]->firstname);
$this->assertEquals('Doe', $object->customers[0]->lastname);
$this->assertEquals('john.doe@example.com', $object->customers[0]->email);
$this->assertEquals('22967666776', $object->customers[0]->phone);
}
/**
* Should return array of FedaPay\Customer
*/
public function testCustomerCreationShouldFailed()
{
$data = ['firstname' => 'Myfirstname'];
$body = [
'message' => 'Account creation failed',
'errors' => [
'lastname' => ['lastname field required']
]
];
$this->mockRequest('post', '/v1/customers', $data, $body, 500);
try {
\FedaPay\Customer::create($data);
} catch (\FedaPay\Error\ApiConnection $e) {
$this->assertTrue($e->hasErrors());
$this->assertNotNull($e->getErrorMessage());
$errors = $e->getErrors();
$this->assertArrayHasKey('lastname', $errors);
}
}
/**
* Should return array of FedaPay\Customer
*/
public function testShouldCreateACustomer()
{
$data = [
'firstname' => 'John',
'lastname' => 'Doe',
'email' => 'john.doe@example.com',
'phone' => '+22966000001'
];
$body = [
'v1/customer' => [
'id' => 1,
'klass' => 'v1/customer',
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'phone' => $data['phone'],
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/customers', $data, $body);
$customer = \FedaPay\Customer::create($data);
$this->assertInstanceOf(\FedaPay\Customer::class, $customer);
$this->assertEquals($customer->firstname, $data['firstname']);
$this->assertEquals($customer->lastname, $data['lastname']);
$this->assertEquals($customer->email, $data['email']);
$this->assertEquals($customer->phone, $data['phone']);
$this->assertEquals($customer->id, 1);
}
/**
* Should retrieve a Customer
*/
public function testShouldRetrievedACustomer()
{
$data = [
'firstname' => 'John',
'lastname' => 'Doe',
'email' => 'john.doe@exemple.com',
'phone' => '+2296600000001'
];
$body = [
'v1/customer' => [
'id' => 1,
'klass' => 'v1/customer',
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'phone' => $data['phone'],
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('get', '/v1/customers/1', [], $body);
$customer = \FedaPay\Customer::retrieve(1);
$this->assertInstanceOf(\FedaPay\Customer::class, $customer);
$this->assertEquals($customer->firstname, $data['firstname']);
$this->assertEquals($customer->lastname, $data['lastname']);
$this->assertEquals($customer->email, $data['email']);
$this->assertEquals($customer->phone, $data['phone']);
$this->assertEquals($customer->id, 1);
}
/**
* Should update a customer
*/
public function testShouldUpdateACustomer()
{
$data = [
'firstname' => 'John',
'lastname' => 'Doe',
'email' => 'john.doe@exemple.com',
'phone' => '+2296600000001'
];
$body = [
'v1/customer' => [
'id' => 1,
'klass' => 'v1/customer',
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'phone' => $data['phone'],
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('put', '/v1/customers/1', $data, $body);
$customer = \FedaPay\Customer::update(1, $data);
$this->assertInstanceOf(\FedaPay\Customer::class, $customer);
$this->assertEquals($customer->firstname, $data['firstname']);
$this->assertEquals($customer->lastname, $data['lastname']);
$this->assertEquals($customer->email, $data['email']);
$this->assertEquals($customer->phone, $data['phone']);
$this->assertEquals($customer->id, 1);
}
/**
* Should update a customer with save
*/
public function testShouldUpdateACustomerWithSave()
{
$data = [
'firstname' => 'John',
'lastname' => 'Doe',
'email' => 'john.doe@exemple.com',
'phone' => '+2296600000001'
];
$body = [
'v1/customer' => [
'id' => 1,
'klass' => 'v1/customer',
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'phone' => $data['phone'],
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/customers', $data, $body);
$customer = \FedaPay\Customer::create($data);
$customer->firstname = 'First name';
$updateData = [
'klass' => 'v1/customer',
'firstname' => 'First name',
'lastname' => $data['lastname'],
'email' => $data['email'],
'phone' => $data['phone'],
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
];
$this->mockRequest('put', '/v1/customers/1', $updateData, $body);
$customer->save();
}
/**
* Should delete a customer
*/
public function testShouldDeleteACustomer()
{
$data = [
'firstname' => 'John',
'lastname' => 'Doe',
'email' => 'john.doe@exemple.com',
'phone' => '+2296600000001'
];
$body = [
'v1/customer' => [
'id' => 1,
'klass' => 'v1/customer',
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'phone' => $data['phone'],
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/customers', $data, $body);
$customer = \FedaPay\Customer::create($data);
$this->mockRequest('delete', '/v1/customers/1');
$customer->delete();
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Tests;
class EventTest extends BaseTestCase
{
/**
* Should return array of FedaPay\Event
*/
public function testShouldReturnEvents()
{
$body = [
'v1/events' => [[
'id' => 1,
'klass' => 'v1/event',
'type' => 'transaction.update',
'entity' => [],
'object_id' => 1,
'account_id' => 1,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]],
'meta' => ['page' => 1]
];
$this->mockRequest('get', '/v1/events', [], $body);
$object = \FedaPay\Event::all();
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object->meta);
$this->assertTrue(is_array($object->events));
$this->assertInstanceOf(\FedaPay\Event::class, $object->events[0]);
$this->assertEquals(1, $object->events[0]->id);
$this->assertEquals('transaction.update', $object->events[0]->type);
$this->assertEquals(1, $object->events[0]->object_id);
$this->assertEquals(1, $object->events[0]->account_id);
}
/**
* Should retrieve a Event
*/
public function testShouldRetrievedAEvent()
{
$body = [
'v1/event' => [
'id' => 1,
'klass' => 'v1/event',
'type' => 'transaction.update',
'entity' => [],
'object_id' => 1,
'account_id' => 1,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('get', '/v1/events/1', [], $body);
$event = \FedaPay\Event::retrieve(1);
$this->assertInstanceOf(\FedaPay\Event::class, $event);
$this->assertEquals(1, $event->id);
$this->assertEquals('transaction.update', $event->type);
$this->assertEquals(1, $event->object_id);
$this->assertEquals(1, $event->account_id);
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace Tests;
class FedaPayObjectTest extends BaseTestCase
{
/**
* Should set and get object attributes
*/
public function testShouldSetObjectAttribute()
{
$object = new \FedaPay\FedaPayObject;
$object->id = 1;
$object->name = 'name';
$this->assertEquals($object->id, 1);
$this->assertEquals($object['id'], 1);
$this->assertEquals($object->name, 'name');
$this->assertEquals($object['name'], 'name');
$object = new \FedaPay\FedaPayObject(2);
$this->assertEquals($object->id, 2);
$object = new \FedaPay\FedaPayObject(['foo' => 'value']);
$this->assertEquals($object->foo, 'value');
$this->assertTrue(isset($object->foo));
$this->assertTrue(isset($object['foo']));
$this->assertFalse(isset($object['doe']));
$this->assertFalse(isset($object->doe));
}
public function testShouldSerializeObject()
{
$object = new \FedaPay\FedaPayObject(['foo' => 'value']);
$json = json_encode($object);
$this->assertEquals($json, '{"foo":"value"}');
}
public function testShouldRefreshObject()
{
$object = new \FedaPay\FedaPayObject;
$object->refreshFrom(['foo' => 'value'], null);
$this->assertEquals($object->foo, 'value');
}
public function testShouldSerializeObjectParams()
{
$object = new \FedaPay\FedaPayObject;
$object->refreshFrom(['foo' => 'value', 'id' => 2], null);
$params = $object->serializeParameters();
$this->assertTrue(is_array($params));
$this->assertEquals($params['foo'], 'value');
$this->assertArrayNotHasKey('id', $params);
}
}

View File

@@ -0,0 +1,6 @@
<?php
namespace Tests\Fixtures;
class Foo extends \FedaPay\Resource
{
}

View File

@@ -0,0 +1,6 @@
<?php
namespace Tests\Fixtures;
class FooCurrency extends \FedaPay\Resource
{
}

View File

@@ -0,0 +1,6 @@
<?php
namespace Tests\Fixtures;
class FooPerson extends \FedaPay\Resource
{
}

View File

@@ -0,0 +1,6 @@
<?php
namespace Tests\Fixtures;
class FooTest extends \FedaPay\Resource
{
}

View File

@@ -0,0 +1,223 @@
<?php
namespace Tests;
use FedaPay\HttpClient\CurlClient;
use FedaPay\FedaPay;
class CurlClientTest extends BaseTestCase
{
/**
* @before
*/
public function saveOriginalNetworkValues()
{
$this->origMaxNetworkRetries = FedaPay::getMaxNetworkRetries();
$this->origMaxNetworkRetryDelay = FedaPay::getMaxNetworkRetryDelay();
$this->origInitialNetworkRetryDelay = FedaPay::getInitialNetworkRetryDelay();
}
/**
* @before
*/
public function setUpReflectors()
{
$fedaPayReflector = new \ReflectionClass('\FedaPay\FedaPay');
$this->maxNetworkRetryDelayProperty = $fedaPayReflector->getProperty('maxNetworkRetryDelay');
$this->maxNetworkRetryDelayProperty->setAccessible(true);
$this->initialNetworkRetryDelayProperty = $fedaPayReflector->getProperty('initialNetworkRetryDelay');
$this->initialNetworkRetryDelayProperty->setAccessible(true);
$curlClientReflector = new \ReflectionClass('FedaPay\HttpClient\CurlClient');
$this->shouldRetryMethod = $curlClientReflector->getMethod('shouldRetry');
$this->shouldRetryMethod->setAccessible(true);
$this->sleepTimeMethod = $curlClientReflector->getMethod('sleepTime');
$this->sleepTimeMethod->setAccessible(true);
}
/**
* @after
*/
public function restoreOriginalNetworkValues()
{
FedaPay::setMaxNetworkRetries($this->origMaxNetworkRetries);
$this->setMaxNetworkRetryDelay($this->origMaxNetworkRetryDelay);
$this->setInitialNetworkRetryDelay($this->origInitialNetworkRetryDelay);
}
private function setMaxNetworkRetryDelay($maxNetworkRetryDelay)
{
$this->maxNetworkRetryDelayProperty->setValue(null, $maxNetworkRetryDelay);
}
private function setInitialNetworkRetryDelay($initialNetworkRetryDelay)
{
$this->initialNetworkRetryDelayProperty->setValue(null, $initialNetworkRetryDelay);
}
private function createFakeRandomGenerator($returnValue = 1.0)
{
$fakeRandomGenerator = $this->createMock('\FedaPay\Util\RandomGenerator');
$fakeRandomGenerator->method('randFloat')->willReturn($returnValue);
return $fakeRandomGenerator;
}
public function testTimeout()
{
$curl = new CurlClient();
$this->assertSame(CurlClient::DEFAULT_TIMEOUT, $curl->getTimeout());
$this->assertSame(CurlClient::DEFAULT_CONNECT_TIMEOUT, $curl->getConnectTimeout());
// implicitly tests whether we're returning the CurlClient instance
$curl = $curl->setConnectTimeout(1)->setTimeout(10);
$this->assertSame(1, $curl->getConnectTimeout());
$this->assertSame(10, $curl->getTimeout());
$curl->setTimeout(-1);
$curl->setConnectTimeout(-999);
$this->assertSame(0, $curl->getTimeout());
$this->assertSame(0, $curl->getConnectTimeout());
}
public function testUserAgentInfo()
{
$curl = new CurlClient();
$uaInfo = $curl->getUserAgentInfo();
$this->assertNotNull($uaInfo);
$this->assertNotNull($uaInfo['httplib']);
$this->assertNotNull($uaInfo['ssllib']);
}
public function testDefaultOptions()
{
$withClosure = new CurlClient(function ($method, $absUrl, $headers, $params, $hasFile) use (&$ref) {
$ref = func_get_args();
return [];
});
$withClosure->request('get', 'https://httpbin.org/status/200', [], [], false);
$this->assertSame($ref, ['get', 'https://httpbin.org/status/200', [], [], false]);
// this is the last test case that will run, since it'll throw an exception at the end
$withBadClosure = new CurlClient(function () {
return 'thisShouldNotWork';
});
$this->expectException('FedaPay\Error\ApiConnection');
$this->expectExceptionMessage('Non-array value returned by defaultOptions CurlClient callback');
$withBadClosure->request('get', 'https://httpbin.org/status/200', [], [], false);
}
public function testSslOption()
{
// make sure options array loads/saves properly
$optionsArray = [CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1];
$withOptionsArray = new CurlClient($optionsArray);
$this->assertSame($withOptionsArray->getDefaultOptions(), $optionsArray);
}
public function testShouldRetryOnTimeout()
{
FedaPay::setMaxNetworkRetries(2);
$curlClient = new CurlClient();
$this->assertTrue($this->shouldRetryMethod->invoke($curlClient, CURLE_OPERATION_TIMEOUTED, 0, 0));
}
public function testShouldRetryOnConnectionFailure()
{
FedaPay::setMaxNetworkRetries(2);
$curlClient = new CurlClient();
$this->assertTrue($this->shouldRetryMethod->invoke($curlClient, CURLE_COULDNT_CONNECT, 0, 0));
}
public function testShouldRetryOnConflict()
{
FedaPay::setMaxNetworkRetries(2);
$curlClient = new CurlClient();
$this->assertTrue($this->shouldRetryMethod->invoke($curlClient, 0, 409, 0));
}
public function testShouldNotRetryAtMaximumCount()
{
FedaPay::setMaxNetworkRetries(2);
$curlClient = new CurlClient();
$this->assertFalse($this->shouldRetryMethod->invoke($curlClient, 0, 0, FedaPay::getMaxNetworkRetries()));
}
public function testShouldNotRetryOnCertValidationError()
{
FedaPay::setMaxNetworkRetries(2);
$curlClient = new CurlClient();
$this->assertFalse($this->shouldRetryMethod->invoke($curlClient, CURLE_SSL_PEER_CERTIFICATE, -1, 0));
}
public function testSleepTimeShouldGrowExponentially()
{
$this->setMaxNetworkRetryDelay(999);
$curlClient = new CurlClient(null, $this->createFakeRandomGenerator());
$this->assertEquals(
FedaPay::getInitialNetworkRetryDelay() * 1,
$this->sleepTimeMethod->invoke($curlClient, 1)
);
$this->assertEquals(
FedaPay::getInitialNetworkRetryDelay() * 2,
$this->sleepTimeMethod->invoke($curlClient, 2)
);
$this->assertEquals(
FedaPay::getInitialNetworkRetryDelay() * 4,
$this->sleepTimeMethod->invoke($curlClient, 3)
);
$this->assertEquals(
FedaPay::getInitialNetworkRetryDelay() * 8,
$this->sleepTimeMethod->invoke($curlClient, 4)
);
}
public function testSleepTimeShouldEnforceMaxNetworkRetryDelay()
{
$this->setInitialNetworkRetryDelay(1);
$this->setMaxNetworkRetryDelay(2);
$curlClient = new CurlClient(null, $this->createFakeRandomGenerator());
$this->assertEquals(1, $this->sleepTimeMethod->invoke($curlClient, 1));
$this->assertEquals(2, $this->sleepTimeMethod->invoke($curlClient, 2));
$this->assertEquals(2, $this->sleepTimeMethod->invoke($curlClient, 3));
$this->assertEquals(2, $this->sleepTimeMethod->invoke($curlClient, 4));
}
public function testSleepTimeShouldAddSomeRandomness()
{
$randomValue = 0.8;
$this->setInitialNetworkRetryDelay(1);
$this->setMaxNetworkRetryDelay(8);
$curlClient = new CurlClient(null, $this->createFakeRandomGenerator($randomValue));
$baseValue = FedaPay::getInitialNetworkRetryDelay() * (0.5 * (1 + $randomValue));
// the initial value cannot be smaller than the base,
// so the randomness is ignored
$this->assertEquals(FedaPay::getInitialNetworkRetryDelay(), $this->sleepTimeMethod->invoke($curlClient, 1));
// after the first one, the randomness is applied
$this->assertEquals($baseValue * 2, $this->sleepTimeMethod->invoke($curlClient, 2));
$this->assertEquals($baseValue * 4, $this->sleepTimeMethod->invoke($curlClient, 3));
$this->assertEquals($baseValue * 8, $this->sleepTimeMethod->invoke($curlClient, 4));
}
}

View File

@@ -0,0 +1,334 @@
<?php
namespace Tests;
use Faker\Factory;
class InvoiceTest extends BaseTestCase
{
/**
* Should return array of FedaPay\Invoice
*/
public function testShouldReturnInvoices()
{
$body = [
'v1/invoices' => [[
'id' => 2,
'klass' => 'v1/invoice',
'number' => '2',
'reference' => 'v_hZUvFT',
'status' => 'sent',
'tax' => 0,
'discount_type' => 'percentage',
'discount_amount' => 0,
'ttc' => 2500,
'sub_total' => 0,
'discount' => 0,
'before_tax' => 0,
'tax_amount' => 0,
'total_amount_paid' => 2500,
'notes' => 'hi just a test',
'invoice_products_count' => 1,
'due_at' => '2018-03-12T09:09:03.969Z',
'currency' => [
'klass' => 'v1/currency',
'id' => 1,
'klass' => 'v1/currency',
'name' => 'FCFA',
'iso' => 'XOF',
'code' => 952,
'prefix' => null,
'suffix' => 'CFA',
'div' => 1,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
],
]],
'meta' => [
'current_page' => 1,
'next_page' => null,
'prev_page' => null,
'total_pages' => 1,
'total_count' => 1,
'per_page' => 25,
]
];
$this->mockRequest('get', '/v1/invoices', [], $body);
$object = \FedaPay\Invoice::all();
$this->assertTrue(is_array($object->invoices));
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object->meta);
$this->assertInstanceOf(\FedaPay\Invoice::class, $object->invoices[0]);
$this->assertEquals(2, $object->invoices[0]->number);
$this->assertEquals('v_hZUvFT', $object->invoices[0]->reference);
$this->assertEquals('hi just a test', $object->invoices[0]->notes);
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object->invoices[0]->currency);
}
/**
* Should faild creating the invoice
*/
public function testInvoiceCreationShouldFailed()
{
$data = [
'number' => 1,
'notes' => 'My note'
];
$body = [
'message' => 'Invoice creation failed',
'errors' => [
'notes' => ['notes field required']
]
];
$this->mockRequest('post', '/v1/invoices', $data, $body, 500);
try {
\FedaPay\Invoice::create($data);
} catch (\FedaPay\Error\ApiConnection $e) {
$this->assertTrue($e->hasErrors());
$this->assertNotNull($e->getErrorMessage());
$errors = $e->getErrors();
$this->assertArrayHasKey('notes', $errors);
}
}
/**
* Should create an invoice
*/
public function testShouldCreateAnInvoice()
{
$data = [
'number' => 4,
'reference' => 'reference-invoice',
'notes' => 'Invoice content',
'currency_id' => 1,
'account_id' => 1
];
$body = [
'v1/invoice' => [
'id' => 1,
'klass' => 'v1/invoice',
'number' => $data['number'],
'reference' => $data['reference'],
'notes' => $data['notes'],
'due_at' => '2018-03-12T09:09:03.969Z',
'created_at' => '2019-11-19T10:19:03.969Z',
'updated_at' => '2019-11-19T10:19:03.969Z'
]
];
$this->mockRequest('post', '/v1/invoices', $data, $body);
$invoice = \FedaPay\Invoice::create($data);
$this->assertInstanceOf(\FedaPay\Invoice::class, $invoice);
$this->assertEquals($invoice->number, $data['number']);
$this->assertEquals($invoice->reference, $data['reference']);
$this->assertEquals($invoice->notes, $data['notes']);
$this->assertEquals($invoice->id, 1);
}
/**
* Should retrieve an Invoice
*/
public function testShouldRetrievedAnInvoice()
{
$data = [
'number' => 4,
'reference' => 'reference-invoice',
'notes' => 'Invoice content',
'currency_id' => 1,
'account_id' => 1
];
$body = [
'v1/invoice' => [
'id' => 1,
'klass' => 'v1/invoice',
'number' => $data['number'],
'reference' => $data['reference'],
'notes' => $data['notes'],
'due_at' => '2018-03-12T09:09:03.969Z',
'created_at' => '2019-11-19T10:19:03.969Z',
'updated_at' => '2019-11-19T10:19:03.969Z'
]
];
$this->mockRequest('get', '/v1/invoices/1', [], $body);
$invoice = \FedaPay\Invoice::retrieve(1);
$this->assertInstanceOf(\FedaPay\Invoice::class, $invoice);
$this->assertEquals($invoice->number, $data['number']);
$this->assertEquals($invoice->reference, $data['reference']);
$this->assertEquals($invoice->notes, $data['notes']);
$this->assertEquals($invoice->id, 1);
}
/**
* Should update an invoice
*/
public function testShouldUpdateAnInvoice()
{
$data = [
'number' => 4,
'reference' => 'reference-invoice',
'notes' => 'Invoice content',
'currency_id' => 1,
'account_id' => 1
];
$body = [
'v1/invoice' => [
'id' => 1,
'klass' => 'v1/invoice',
'number' => $data['number'],
'reference' => $data['reference'],
'notes' => $data['notes'],
'due_at' => '2018-03-12T09:09:03.969Z',
'created_at' => '2019-11-19T10:19:03.969Z',
'updated_at' => '2019-11-19T10:19:03.969Z'
]
];
$this->mockRequest('put', '/v1/invoices/1', $data, $body);
$invoice = \FedaPay\Invoice::update(1, $data);
$this->assertInstanceOf(\FedaPay\Invoice::class, $invoice);
$this->assertEquals($invoice->number, $data['number']);
$this->assertEquals($invoice->reference, $data['reference']);
$this->assertEquals($invoice->notes, $data['notes']);
$this->assertEquals($invoice->id, 1);
}
/**
* Should update an invoice with save
*/
public function testShouldUpdateAnInvoiceWithSave()
{
$data = [
'number' => 1,
'reference' => 'reference-invoice',
'notes' => 'Invoice content',
'currency_id' => 1,
'account_id' => 1
];
$body = [
'v1/invoice' => [
'id' => 1,
'klass' => 'v1/invoice',
'number' => $data['number'],
'reference' => $data['reference'],
'notes' => $data['notes'],
'due_at' => '2018-03-12T09:09:03.969Z',
'created_at' => '2019-11-19T10:19:03.969Z',
'updated_at' => '2019-11-19T10:19:03.969Z'
]
];
$this->mockRequest('post', '/v1/invoices', $data, $body);
$invoice = \FedaPay\Invoice::create($data);
$invoice->number = 1;
$updateData = [
'klass' => 'v1/invoice',
'number' => $data['number'],
'notes' => $data['notes'],
'reference' => $data['reference'],
'due_at' => '2018-03-12T09:09:03.969Z',
'created_at' => '2019-11-19T10:19:03.969Z',
'updated_at' => '2019-11-19T10:19:03.969Z'
];
$this->mockRequest('put', '/v1/invoices/1', $updateData, $body);
$invoice->save();
}
/**
* Should delete an invoice
*/
public function testShouldDeleteAInvoice()
{
$data = [
'number' => 4,
'reference' => 'reference-invoice',
'notes' => 'Invoice content',
'currency_id' => 1,
'account_id' => 1
];
$body = [
'v1/invoice' => [
'id' => 1,
'klass' => 'v1/invoice',
'number' => $data['number'],
'reference' => $data['reference'],
'notes' => $data['notes'],
'due_at' => '2018-03-12T09:09:03.969Z',
'created_at' => '2019-11-19T10:19:03.969Z',
'updated_at' => '2019-11-19T10:19:03.969Z'
]
];
$this->mockRequest('post', '/v1/invoices', $data, $body);
$invoice = \FedaPay\Invoice::create($data);
$this->mockRequest('delete', '/v1/invoices/1');
$invoice->delete();
}
public function testShouldVerifyInvoice()
{
$data = [
'number' => 4,
'reference' => 'reference-invoice',
'notes' => 'Invoice content',
'currency_id' => 1,
'account_id' => 1
];
$body = [
'v1/invoice' => [
'id' => 1,
'klass' => 'v1/invoice',
'number' => $data['number'],
'reference' => $data['reference'],
'notes' => $data['notes'],
'due_at' => '2018-03-12T09:09:03.969Z',
'created_at' => '2019-11-19T10:19:03.969Z',
'updated_at' => '2019-11-19T10:19:03.969Z'
]
];
$this->mockRequest('post', '/v1/invoices', $data, $body);
$invoice = \FedaPay\Invoice::create($data);
$body = [
'v1/invoice_verify' => [
'invoice' => [
'id' => 1,
'klass' => 'v1/invoice',
'number' => $data['number'],
'reference' => $data['reference'],
'notes' => $data['notes'],
'due_at' => '2018-03-12T09:09:03.969Z',
'created_at' => '2019-11-19T10:19:03.969Z',
'updated_at' => '2019-11-19T10:19:03.969Z'
],
'sesstings' => []
]
];
$this->mockRequest('get', '/v1/invoices/' . $data['reference'] . '/verify', [], $body);
$object = $invoice->verify($data['reference']);
$this->assertInstanceOf(\FedaPay\Invoice::class, $object->invoice);
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace Tests;
class LogTest extends BaseTestCase
{
/**
* Should return array of FedaPay\Log
*/
public function testShouldReturnLogs()
{
$body = [
'v1/logs' => [[
'id' => 1,
'klass' => 'v1/log',
'method' => 'GET',
'url' => '/url',
'status' => 200,
'ip_address' => '189.2.33.9',
'version' => '0.1.1',
'source' => 'FedaPay PhpLib',
'query' => '{"q":"search"}',
'body' => '{}',
'response' => '{}',
'account_id' => 1,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]],
'meta' => ['page' => 1]
];
$this->mockRequest('get', '/v1/logs', [], $body);
$object = \FedaPay\Log::all();
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object->meta);
$this->assertTrue(is_array($object->logs));
$this->assertInstanceOf(\FedaPay\Log::class, $object->logs[0]);
$this->assertEquals(1, $object->logs[0]->id);
$this->assertEquals('GET', $object->logs[0]->method);
$this->assertEquals('/url', $object->logs[0]->url);
$this->assertEquals(200, $object->logs[0]->status);
$this->assertEquals('189.2.33.9', $object->logs[0]->ip_address);
$this->assertEquals('0.1.1', $object->logs[0]->version);
$this->assertEquals('FedaPay PhpLib', $object->logs[0]->source);
$this->assertEquals('{"q":"search"}', $object->logs[0]->query);
$this->assertEquals('{}', $object->logs[0]->body);
$this->assertEquals('{}', $object->logs[0]->response);
$this->assertEquals(1, $object->logs[0]->account_id);
}
/**
* Should retrieve a Log
*/
public function testShouldRetrievedALog()
{
$body = [
'v1/log' => [
'id' => 1,
'klass' => 'v1/log',
'method' => 'GET',
'url' => '/url',
'status' => 200,
'ip_address' => '189.2.33.9',
'version' => '0.1.1',
'source' => 'FedaPay PhpLib',
'query' => '{"q":"search"}',
'body' => '{}',
'response' => '{}',
'account_id' => 1,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('get', '/v1/logs/1', [], $body);
$log = \FedaPay\Log::retrieve(1);
$this->assertInstanceOf(\FedaPay\Log::class, $log);
$this->assertEquals(1, $log->id);
$this->assertEquals('GET', $log->method);
$this->assertEquals('/url', $log->url);
$this->assertEquals(200, $log->status);
$this->assertEquals('189.2.33.9', $log->ip_address);
$this->assertEquals('0.1.1', $log->version);
$this->assertEquals('FedaPay PhpLib', $log->source);
$this->assertEquals('{"q":"search"}', $log->query);
$this->assertEquals('{}', $log->body);
$this->assertEquals('{}', $log->response);
$this->assertEquals(1, $log->account_id);
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Tests\OAuth;
use Tests\BaseTestCase;
use FedaPay\FedaPay;
class ClientTest extends BaseTestCase
{
/**
* Should return array of FedaPay\Account
*/
public function testShouldSendTokenRequest()
{
$body = [
'access_token' => 'oioio990',
'token_type' => 'Bearer',
'scope' => 'read_write',
'created_at' => '1747346992'
];
FedaPay::setOauthClientId('client id');
FedaPay::setOauthClientSecret('client secret');
$data = [
'grant_type' => 'client_credentials',
'client_id' => 'client id',
'client_secret' => 'client secret'
];
$this->mockRequest('post', '/v1/oauth/token', $data, $body);
$object = \FedaPay\OAuth\Client::grantClientCredentials();
$this->assertEquals('oioio990', $object->access_token);
$this->assertEquals('Bearer', $object->token_type);
$this->assertEquals('read_write', $object->scope);
$this->assertEquals('1747346992', $object->created_at);
}
}

View File

@@ -0,0 +1,335 @@
<?php
namespace Tests;
use Faker\Factory;
class PageTest extends BaseTestCase
{
/**
* Should return array of FedaPay\Page
*/
// public function testShouldReturnPages()
// {
// $body = [
// 'v1/pages' => [[
// 'id' => 2,
// 'klass' => 'v1/page',
// 'name' => 'super Admin',
// 'reference' => 'v_hZUvFT',
// 'description' => 'fdfd',
// 'amount' => 0,
// 'published' => true,
// 'enable_phone_number' => false,
// 'callback_url' => null,
// 'image_url' => null,
// 'custom_fields' => [],
// 'currency' => [
// 'klass' => 'v1/currency',
// 'id' => 1,
// 'klass' => 'v1/currency',
// 'name' => 'FCFA',
// 'iso' => 'XOF',
// 'code' => 952,
// 'prefix' => null,
// 'suffix' => 'CFA',
// 'div' => 1,
// 'created_at' => '2018-03-12T09:09:03.969Z',
// 'updated_at' => '2018-03-12T09:09:03.969Z'
// ]
// ]],
// 'meta' => [
// 'current_page' => 1,
// 'next_page' => null,
// 'prev_page' => null,
// 'total_pages' => 1,
// 'total_count' => 1,
// 'per_page' => 25,
// ]
// ];
// $this->mockRequest('get', '/v1/pages', [], $body);
// $object = \FedaPay\Page::all();
// $this->assertTrue(is_array($object->pages));
// $this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
// $this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object->meta);
// $this->assertInstanceOf(\FedaPay\Page::class, $object->pages[0]);
// $this->assertEquals('super Admin', $object->pages[0]->name);
// $this->assertEquals('v_hZUvFT', $object->pages[0]->reference);
// $this->assertEquals('fdfd', $object->pages[0]->description);
// $this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object->pages[0]->currency);
// }
// /**
// * Should faild creating the page
// */
// public function testPageCreationShouldFailed()
// {
// $data = [
// 'name' => 'Myname',
// 'description' => 'My description'
// ];
// $body = [
// 'message' => 'Page creation failed',
// 'errors' => [
// 'description' => ['description field required']
// ]
// ];
// $this->mockRequest('post', '/v1/pages', $data, $body, 500);
// try {
// \FedaPay\Page::create($data);
// } catch (\FedaPay\Error\ApiConnection $e) {
// $this->assertTrue($e->hasErrors());
// $this->assertNotNull($e->getErrorMessage());
// $errors = $e->getErrors();
// $this->assertArrayHasKey('description', $errors);
// }
// }
// /**
// * Should create a page
// */
// public function testShouldCreateAPage()
// {
// $data = [
// 'name' => 'Page name',
// 'reference' => 'page-reference',
// 'description' => 'Page description',
// 'currency_id' => 1
// ];
// $body = [
// 'v1/page' => [
// 'id' => 1,
// 'klass' => 'v1/page',
// 'name' => $data['name'],
// 'reference' => $data['reference'],
// 'description' => $data['description'],
// 'created_at' => '2019-11-19T10:19:03.969Z',
// 'updated_at' => '2019-11-19T10:19:03.969Z'
// ]
// ];
// $this->mockRequest('post', '/v1/pages', $data, $body);
// $page = \FedaPay\Page::create($data);
// $this->assertInstanceOf(\FedaPay\Page::class, $page);
// $this->assertEquals($page->name, $data['name']);
// $this->assertEquals($page->reference, $data['reference']);
// $this->assertEquals($page->description, $data['description']);
// $this->assertEquals($page->id, 1);
// }
// /**
// * Should retrieve a Page
// */
// public function testShouldRetrievedAPage()
// {
// $data = [
// 'name' => 'Page name',
// 'reference' => 'page-reference',
// 'description' => 'Page description',
// 'currency_id' => 1
// ];
// $body = [
// 'v1/page' => [
// 'id' => 1,
// 'klass' => 'v1/page',
// 'name' => $data['name'],
// 'reference' => $data['reference'],
// 'description' => $data['description'],
// 'created_at' => '2019-11-19T10:19:03.969Z',
// 'updated_at' => '2019-11-19T10:19:03.969Z'
// ]
// ];
// $this->mockRequest('get', '/v1/pages/1', [], $body);
// $page = \FedaPay\Page::retrieve(1);
// $this->assertInstanceOf(\FedaPay\Page::class, $page);
// $this->assertEquals($page->name, $data['name']);
// $this->assertEquals($page->reference, $data['reference']);
// $this->assertEquals($page->description, $data['description']);
// $this->assertEquals($page->id, 1);
// }
// /**
// * Should update a page
// */
// public function testShouldUpdateAPage()
// {
// $data = [
// 'name' => 'Page name',
// 'reference' => 'page-reference',
// 'description' => 'Page description',
// 'currency_id' => 1
// ];
// $body = [
// 'v1/page' => [
// 'id' => 1,
// 'klass' => 'v1/page',
// 'name' => $data['name'],
// 'reference' => $data['reference'],
// 'description' => $data['description'],
// 'created_at' => '2019-11-19T10:19:03.969Z',
// 'updated_at' => '2019-11-19T10:19:03.969Z'
// ]
// ];
// $this->mockRequest('put', '/v1/pages/1', $data, $body);
// $page = \FedaPay\Page::update(1, $data);
// $this->assertInstanceOf(\FedaPay\Page::class, $page);
// $this->assertEquals($page->name, $data['name']);
// $this->assertEquals($page->reference, $data['reference']);
// $this->assertEquals($page->description, $data['description']);
// $this->assertEquals($page->id, 1);
// }
// /**
// * Should update a page with save
// */
// public function testShouldUpdateAPageWithSave()
// {
// $data = [
// 'name' => 'Page name',
// 'reference' => 'page-reference',
// 'description' => 'Page description',
// 'currency_id' => 1
// ];
// $body = [
// 'v1/page' => [
// 'id' => 1,
// 'klass' => 'v1/page',
// 'name' => $data['name'],
// 'reference' => $data['reference'],
// 'description' => $data['description'],
// 'created_at' => '2019-11-19T10:19:03.969Z',
// 'updated_at' => '2019-11-19T10:19:03.969Z'
// ]
// ];
// $this->mockRequest('post', '/v1/pages', $data, $body);
// $page = \FedaPay\Page::create($data);
// $page->name = 'New name';
// $updateData = [
// 'klass' => 'v1/page',
// 'name' => 'New name',
// 'reference' => $data['reference'],
// 'description' => $data['description'],
// 'created_at' => '2019-11-19T10:19:03.969Z',
// 'updated_at' => '2019-11-19T10:19:03.969Z'
// ];
// $this->mockRequest('put', '/v1/pages/1', $updateData, $body);
// $page->save();
// }
// /**
// * Should delete a page
// */
// public function testShouldDeleteAPage()
// {
// $data = [
// 'name' => 'Page name',
// 'reference' => 'page-reference',
// 'description' => 'Page description',
// 'currency_id' => 1
// ];
// $body = [
// 'v1/page' => [
// 'id' => 1,
// 'klass' => 'v1/page',
// 'name' => $data['name'],
// 'reference' => $data['reference'],
// 'description' => $data['description'],
// 'created_at' => '2019-11-19T10:19:03.969Z',
// 'updated_at' => '2019-11-19T10:19:03.969Z'
// ]
// ];
// $this->mockRequest('post', '/v1/pages', $data, $body);
// $page = \FedaPay\Page::create($data);
// $this->mockRequest('delete', '/v1/pages/1');
// $page->delete();
// }
public function testShouldVerifyPage()
{
$data = [
'name' => 'Page name',
'reference' => 'page-reference',
'description' => 'Page description',
'currency_id' => 1
];
$body = [
'v1/page' => [
'id' => 1,
'klass' => 'v1/page',
'name' => $data['name'],
'reference' => $data['reference'],
'description' => $data['description'],
'created_at' => '2019-11-19T10:19:03.969Z',
'updated_at' => '2019-11-19T10:19:03.969Z'
]
];
$this->mockRequest('post', '/v1/pages', $data, $body);
$page = \FedaPay\Page::create($data);
$body = [
'v1/page_verify' => [
'page' => [
'id' => 1,
'klass' => 'v1/page',
'name' => $data['name'],
'reference' => $data['reference'],
'description' => $data['description'],
'created_at' => '2019-11-19T10:19:03.969Z',
'updated_at' => '2019-11-19T10:19:03.969Z'
],
'sesstings' => []
]
];
$this->mockRequest('get', '/v1/pages/' . $data['reference'] . '/verify', [], $body);
$object = $page->verify($data['reference']);
$this->assertInstanceOf(\FedaPay\Page::class, $object->page);
}
public function testShouldGetLightPage()
{
$body = [
'v1/page' => [
'id' => 1,
'klass' => 'v1/page',
'name' => 'Page name',
'reference' => 'XOOO',
'description' => 'Page description',
'created_at' => '2019-11-19T10:19:03.969Z',
'updated_at' => '2019-11-19T10:19:03.969Z'
]
];
$this->mockRequest('get', '/v1/pages/XOOO/light', [], $body);
$page = \FedaPay\Page::light('XOOO');
$this->assertEquals($page->name, 'Page name');
$this->assertEquals($page->reference, 'XOOO');
$this->assertEquals($page->description, 'Page description');
$this->assertEquals($page->id, 1);
}
}

View File

@@ -0,0 +1,712 @@
<?php
namespace Tests;
use Faker\Factory;
class PayoutTest extends BaseTestCase
{
private function createPayout()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'amount' => 1000,
];
$body = [
'v1/payout' => [
'id' => 1,
'klass' => 'v1/payout',
'reference' => '109329828',
'amount' => 1000,
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'balance_id' => 1,
'mode' => 'mtn',
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/payouts', $data, $body);
return \FedaPay\Payout::create($data);
}
/**
* Should return array of FedaPay\Payout
*/
public function testShouldReturnPayouts()
{
$body = [
'v1/payouts' => [
[
'klass' => 'v1/payout',
'id' => 1,
'reference' => '1539796844261',
'amount' => 1000,
'status' => 'pending',
'customer_id' => 1,
'mode' => 'mtn',
'last_error_code' => null,
'last_error_message' => null,
'created_at' => '2018-10-17T17:20:44.261Z',
'updated_at' => '2018-10-17T17:22:47.816Z',
'currency_id' => 1,
'scheduled_at' => '2018-10-17T17:22:06.626Z',
'sent_at' => '2018-10-17T17:22:47.803Z',
'failed_at' => null,
'deleted_at' => null
]
],
'meta' => [
"current_page" => 1,
"next_page" => null,
"prev_page" => null,
"total_pages" => 1,
"total_count" => 11,
"per_page" => 25
]
];
$this->mockRequest('get', '/v1/payouts', [], $body);
$object = \FedaPay\Payout::all();
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object->meta);
$this->assertInstanceOf(\FedaPay\Payout::class, $object->payouts[0]);
$this->assertEquals(1, $object->payouts[0]->id);
$this->assertEquals('1539796844261', $object->payouts[0]->reference);
$this->assertEquals(1000, $object->payouts[0]->amount);
$this->assertEquals('pending', $object->payouts[0]->status);
$this->assertEquals(1, $object->payouts[0]->customer_id);
$this->assertEquals(1, $object->payouts[0]->currency_id);
$this->assertEquals('mtn', $object->payouts[0]->mode);
}
/**
* Should return array of FedaPay\Payout
*/
public function testShouldCreateAPayout()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'amount' => 1000,
'mode' => 'mtn',
'scheduled_at' => '2018-03-12T09:09:03.969Z',
'include' => 'customer,currency'
];
$body = [
'v1/payout' => [
'amount' => 1000,
'currency_id' => 1,
'created_at' => '2018-10-23T15:21:50.434Z',
'customer_id' => 1,
'deleted_at' => null,
'failed_at' => null,
'id' => 13,
'klass' => 'v1/payout',
'last_error_code' => null,
'last_error_message' => null,
'mode' => 'mtn',
'reference' => '1540308110435',
'scheduled_at' => '2018-11-12T09:09:03.969Z',
'sent_at' => null,
'status' => 'pending',
'updated_at' => '2018-10-23T15:21:50.434Z',
'customer' => [
'klass' => 'v1/customer',
'id' => 1,
'firstname' => 'SOHOU',
'lastname' => 'Zidial',
'email' => 'zinsou@test.com',
'account_id' => 1,
'created_at' => '2018-10-17T16:03:24.061Z',
'updated_at' => '2018-10-17T16:03:24.061Z'
]
]
];
$this->mockRequest('post', '/v1/payouts', $data, $body);
$payout = \FedaPay\Payout::create($data);
$this->assertInstanceOf(\FedaPay\Payout::class, $payout);
$this->assertEquals(13, $payout->id);
$this->assertEquals('1540308110435', $payout->reference);
$this->assertEquals(1000, $payout->amount);
$this->assertEquals('pending', $payout->status);
$this->assertInstanceOf(\FedaPay\Customer::class, $payout->customer);
$this->assertEquals(1, $payout->customer->id);
$this->assertEquals('mtn', $payout->mode);
}
/**
* Should return array of FedaPay\Payout
*/
public function testShouldCreatePayoutInBatch()
{
$data = [
'payouts' => [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'amount' => 1000,
'mode' => 'mtn',
'scheduled_at' => '2018-03-12T09:09:03.969Z'
],
'include' => 'customer,currency'
];
$body = [
'v1/payout_batch' => [
'klass' => 'v1/payout_batch',
'payouts' => [
[
'amount' => 1000,
'currency_id' => 1,
'created_at' => '2018-10-23T15:21:50.434Z',
'customer_id' => 1,
'deleted_at' => null,
'failed_at' => null,
'id' => 13,
'klass' => 'v1/payout',
'last_error_code' => null,
'last_error_message' => null,
'mode' => 'mtn',
'reference' => '1540308110435',
'scheduled_at' => '2018-11-12T09:09:03.969Z',
'sent_at' => null,
'status' => 'pending',
'updated_at' => '2018-10-23T15:21:50.434Z',
'customer' => [
'klass' => 'v1/customer',
'id' => 1,
'firstname' => 'SOHOU',
'lastname' => 'Zidial',
'email' => 'zinsou@test.com',
'account_id' => 1,
'created_at' => '2018-10-17T16:03:24.061Z',
'updated_at' => '2018-10-17T16:03:24.061Z'
]
]
],
'errors' => []
]
];
$this->mockRequest('post', '/v1/payouts/batch', $data, $body);
$object = \FedaPay\Payout::createInBatch($data);
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertEquals(13, $object->payouts[0]->id);
$this->assertEquals('1540308110435', $object->payouts[0]->reference);
$this->assertEquals(1000, $object->payouts[0]->amount);
$this->assertEquals('pending', $object->payouts[0]->status);
$this->assertInstanceOf(\FedaPay\Customer::class, $object->payouts[0]->customer);
$this->assertEquals(1, $object->payouts[0]->customer->id);
$this->assertEquals('mtn', $object->payouts[0]->mode);
}
/**
* Should retrieve a Payout
*/
public function testShouldRetrievedAPayout()
{
$body = [
'v1/payout' => [
'amount' => 1000,
'currency_id' => 1,
'created_at' => '2018-10-23T15:21:50.434Z',
'customer_id' => 1,
'deleted_at' => null,
'failed_at' => null,
'id' => 13,
'klass' => 'v1/payout',
'last_error_code' => null,
'last_error_message' => null,
'mode' => 'mtn',
'reference' => '1540308110435',
'scheduled_at' => '2018-11-12T09:09:03.969Z',
'sent_at' => null,
'status' => 'pending',
'updated_at' => '2018-10-23T15:21:50.434Z'
]
];
$this->mockRequest('get', '/v1/payouts/13', [], $body);
$payout = \FedaPay\Payout::retrieve(13);
$this->assertInstanceOf(\FedaPay\Payout::class, $payout);
$this->assertEquals(13, $payout->id);
$this->assertEquals('1540308110435', $payout->reference);
$this->assertEquals(1000, $payout->amount);
$this->assertEquals('pending', $payout->status);
$this->assertEquals('mtn', $payout->mode);
}
/**
* Should start a Payout
*/
public function testShouldScheduleAPayout()
{
$payout = $this->createPayout();
$body = [
'v1/payouts' => [
[
'klass' => 'v1/payout',
'id' => 1,
'reference' => '1540316134325',
'amount' => 1000,
'status' => 'started',
'customer_id' => 1,
'currency_id' => 1,
'mode' => 'mtn',
'last_error_code' => null,
'last_error_message' => null,
'created_at' => '2018-10-23T17:35:34.325Z',
'updated_at' => '2018-10-23T17:36:40.086Z',
'scheduled_at' => '2018-11-01 18:30:22',
'sent_at' => null,
'started_at' => '2018-11-01 18:30:22',
'failed_at' => null,
'deleted_at' => null
]
]
];
$data = [
'payouts' => [[
'id' => 1,
'scheduled_at' => '2018-11-01 18:30:22'
]]
];
$this->mockRequest('put', '/v1/payouts/start', $data, $body);
$payout->schedule('2018-11-01 18:30:22');
$this->assertEquals('2018-11-01 18:30:22', $payout->scheduled_at);
$this->assertEquals('2018-11-01 18:30:22', $payout->started_at);
$this->assertEquals('started', $payout->status);
}
/**
* Should start a Payout with phone number
*/
public function testShouldScheduleAPayoutWithPhoneNumber()
{
$payout = $this->createPayout();
$body = [
'v1/payouts' => [
[
'klass' => 'v1/payout',
'id' => 1,
'reference' => '1540316134325',
'amount' => 1000,
'status' => 'started',
'customer_id' => 1,
'currency_id' => 1,
'mode' => 'mtn',
'last_error_code' => null,
'last_error_message' => null,
'created_at' => '2018-10-23T17:35:34.325Z',
'updated_at' => '2018-10-23T17:36:40.086Z',
'scheduled_at' => '2018-11-01 18:30:22',
'sent_at' => null,
'started_at' => '2018-11-01 18:30:22',
'failed_at' => null,
'deleted_at' => null
]
]
];
$data = [
'payouts' => [[
'id' => 1,
'scheduled_at' => '2018-11-01 18:30:22',
'phone_number' => [
'number' => '66000001',
'country' => 'BJ'
]
]]
];
$this->mockRequest('put', '/v1/payouts/start', $data, $body);
$payout->schedule('2018-11-01 18:30:22', [
'phone_number' => [
'number' => '66000001',
'country' => 'BJ'
]
]);
$this->assertEquals('2018-11-01 18:30:22', $payout->scheduled_at);
$this->assertEquals('2018-11-01 18:30:22', $payout->started_at);
$this->assertEquals('started', $payout->status);
}
/**
* Should fail schedule all payouts
*/
public function testShouldFailScheduleAllPayouts()
{
$data = [[
'scheduled_at' => '2018-11-01 18:30:22'
]];
$this->expectException('\InvalidArgumentException');
$this->expectExceptionMessage('Invalid id argument. You must specify payout id.');
\FedaPay\Payout::scheduleAll($data);
}
/**
* Should schedule all Payouts
*/
public function testShouldScheduleAllPayouts()
{
$body = [
'v1/payouts' => [
[
'klass' => 'v1/payout',
'id' => 1,
'reference' => '1540316134325',
'amount' => 1000,
'status' => 'started',
'customer_id' => 1,
'currency_id' => 1,
'mode' => 'mtn',
'last_error_code' => null,
'last_error_message' => null,
'created_at' => '2018-10-23T17:35:34.325Z',
'updated_at' => '2018-10-23T17:36:40.086Z',
'scheduled_at' => '2018-11-01 18:30:22',
'sent_at' => null,
'started_at' => '2018-11-01 18:30:22',
'failed_at' => null,
'deleted_at' => null
]
]
];
$data = [[
'id' => 1,
'scheduled_at' => '2018-11-01 18:30:22'
]];
$this->mockRequest('put', '/v1/payouts/start', ['payouts' => $data], $body);
$object = \FedaPay\Payout::scheduleAll($data);
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertInstanceOf(\FedaPay\Payout::class, $object->payouts[0]);
$this->assertEquals(1, $object->payouts[0]->id);
$this->assertEquals('1540316134325', $object->payouts[0]->reference);
$this->assertEquals(1000, $object->payouts[0]->amount);
$this->assertEquals('started', $object->payouts[0]->status);
$this->assertEquals('mtn', $object->payouts[0]->mode);
}
/**
* Should send a Payout now
*/
public function testShouldSendAPayoutNow()
{
$payout = $this->createPayout();
$body = [
'v1/payouts' => [
[
'klass' => 'v1/payout',
'id' => 1,
'reference' => '1540316134325',
'amount' => 1000,
'status' => 'sent',
'customer_id' => 1,
'currency_id' => 1,
'mode' => 'mtn',
'last_error_code' => null,
'last_error_message' => null,
'created_at' => '2018-10-23T17:35:34.325Z',
'updated_at' => '2018-10-23T17:36:40.086Z',
'scheduled_at' => '2018-11-01 18:30:22',
'sent_at' => '2018-11-01 18:30:22',
'started_at' => '2018-11-01 18:30:22',
'failed_at' => null,
'deleted_at' => null
]
]
];
$data = [
'payouts' => [[
'id' => 1
]]
];
$this->mockRequest('put', '/v1/payouts/start', $data, $body);
$payout->sendNow();
$this->assertEquals('2018-11-01 18:30:22', $payout->sent_at);
$this->assertEquals('sent', $payout->status);
}
/**
* Should send a Payout now with phone number
*/
public function testShouldSendAPayoutNowWithPhoneNumber()
{
$payout = $this->createPayout();
$body = [
'v1/payouts' => [
[
'klass' => 'v1/payout',
'id' => 1,
'reference' => '1540316134325',
'amount' => 1000,
'status' => 'sent',
'customer_id' => 1,
'currency_id' => 1,
'mode' => 'mtn',
'last_error_code' => null,
'last_error_message' => null,
'created_at' => '2018-10-23T17:35:34.325Z',
'updated_at' => '2018-10-23T17:36:40.086Z',
'scheduled_at' => '2018-11-01 18:30:22',
'sent_at' => '2018-11-01 18:30:22',
'started_at' => '2018-11-01 18:30:22',
'failed_at' => null,
'deleted_at' => null
]
]
];
$data = [
'payouts' => [[
'id' => 1,
'phone_number' => [
'number' => '66000001',
'country' => 'BJ'
]
]]
];
$this->mockRequest('put', '/v1/payouts/start', $data, $body);
$payout->sendNow([
'phone_number' => [
'number' => '66000001',
'country' => 'BJ'
]
]);
$this->assertEquals('2018-11-01 18:30:22', $payout->sent_at);
$this->assertEquals('sent', $payout->status);
}
/**
* Should send all payouts now
*/
public function testShouldSendAllPayoutsNow()
{
$body = [
'v1/payouts' => [
[
'klass' => 'v1/payout',
'id' => 1,
'reference' => '1540316134325',
'amount' => 1000,
'status' => 'sent',
'customer_id' => 1,
'currency_id' => 1,
'mode' => 'mtn',
'last_error_code' => null,
'last_error_message' => null,
'created_at' => '2018-10-23T17:35:34.325Z',
'updated_at' => '2018-10-23T17:36:40.086Z',
'scheduled_at' => '2018-11-01 18:30:22',
'sent_at' => null,
'started_at' => '2018-11-01 18:30:22',
'failed_at' => null,
'deleted_at' => null
]
]
];
$data = [[
'id' => 1
]];
$this->mockRequest('put', '/v1/payouts/start', ['payouts' => $data], $body);
$object = \FedaPay\Payout::sendAllNow([['id' => 1]]);
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertInstanceOf(\FedaPay\Payout::class, $object->payouts[0]);
$this->assertEquals(1, $object->payouts[0]->id);
$this->assertEquals('1540316134325', $object->payouts[0]->reference);
$this->assertEquals(1000, $object->payouts[0]->amount);
$this->assertEquals('sent', $object->payouts[0]->status);
$this->assertEquals('mtn', $object->payouts[0]->mode);
}
/**
* Should update a payout
*/
public function testShouldUpdateAPayout()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'amount' => 1000
];
$body = [
'v1/payout' => [
'id' => 1,
'klass' => 'v1/payout',
'reference' => '109329828',
'amount' => 1000,
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => null,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
]
];
$this->mockRequest('put', '/v1/payouts/1', $data, $body);
$payout = \FedaPay\Payout::update(1, $data);
$this->assertInstanceOf(\FedaPay\Payout::class, $payout);
$this->assertEquals(1, $payout->id);
$this->assertEquals('109329828', $payout->reference);
$this->assertEquals(1000, $payout->amount);
$this->assertEquals('pending', $payout->status);
$this->assertInstanceOf(\FedaPay\Customer::class, $payout->customer);
$this->assertEquals(1, $payout->customer->id);
$this->assertInstanceOf(\FedaPay\Currency::class, $payout->currency);
$this->assertEquals(1, $payout->currency->id);
$this->assertEquals(null, $payout->mode);
}
/**
* Should update a payout with save
*/
public function testShouldUpdateAPayoutWithSave()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'amount' => 1000,
];
$body = [
'v1/payout' => [
'id' => 1,
'klass' => 'v1/payout',
'reference' => '109329828',
'amount' => 1000,
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => 'mtn',
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
]
];
$this->mockRequest('post', '/v1/payouts', $data, $body);
$payout = \FedaPay\Payout::create($data);
$payout->amount = 5000;
$updateData = [
'klass' => 'v1/payout',
'reference' => '109329828',
'amount' => 5000,
'status' => 'pending',
'customer' => [
'klass' => 'v1/customer',
],
'currency' => [
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => 'mtn',
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
];
$this->mockRequest('put', '/v1/payouts/1', $updateData, $body);
$payout->save();
}
/**
* Should delete a payout
*/
public function testShouldDeleteAPayout()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'amount' => 1000,
];
$body = [
'v1/payout' => [
'id' => 1,
'klass' => 'v1/payout',
'reference' => '109329828',
'amount' => 1000,
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => 'mtn',
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
]
];
$this->mockRequest('post', '/v1/payouts', $data, $body);
$payout = \FedaPay\Payout::create($data);
$this->mockRequest('delete', '/v1/payouts/1');
$payout->delete();
}
}

View File

@@ -0,0 +1,101 @@
<?php
namespace Tests;
/**
* Class RequestorTest
*
* @package Tests
*/
class RequestorTest extends BaseTestCase
{
public function testHttpClientInjection()
{
$reflector = new \ReflectionClass('FedaPay\\Requestor');
$method = $reflector->getMethod('httpClient');
$method->setAccessible(true);
$curl = new \FedaPay\HttpClient\CurlClient();
$curl->setTimeout(10);
\FedaPay\Requestor::setHttpClient($curl);
$injectedCurl = $method->invoke(new \FedaPay\Requestor());
$this->assertSame($injectedCurl, $curl);
}
public function testRequestParams()
{
$this->mockRequest(
'get',
'/v1/path',
['foo' => '2'],
[],
500,
[
'X-Custom' => 'foo'
]
);
$requestor = new \FedaPay\Requestor;
$this->expectException('\FedaPay\Error\ApiConnection');
$requestor->request('get', '/path', ['foo' => '2'], ['X-Custom' => 'foo']);
}
public function testRequestSetParams()
{
\FedaPay\FedaPay::setApiKey(null);
\FedaPay\FedaPay::setApiVersion('v3');
\FedaPay\FedaPay::setEnvironment('production');
\FedaPay\FedaPay::setToken('mytoken');
\FedaPay\FedaPay::setAccountId(898);
\FedaPay\FedaPay::setLocale('en');
$this->mockRequest(
'get',
'/v3/path',
['foo' => '2', 'locale' => 'en'],
[],
500,
[
'Authorization' => 'Bearer mytoken',
'FedaPay-Account' => 898,
'X-Api-Version' => 'v3',
'X-Custom' => 'foo'
]
);
$requestor = new \FedaPay\Requestor;
$this->expectException('\FedaPay\Error\ApiConnection');
$requestor->request('get', '/path', ['foo' => '2'], [
'X-Custom' => 'foo'
]);
}
public function testShouldFaildParsingResponse()
{
$this->mockRequest(
'get',
'/v1/path',
[],
'unable to parse',
200
);
$requestor = new \FedaPay\Requestor;
$this->expectException('\FedaPay\Error\ApiConnection', 'unable to parse');
$requestor->request('get', '/path');
}
public function testShouldParseApiErrors()
{
$this->mockRequest(
'get',
'/v1/path',
[],
['message' => 'Error Message'],
400
);
$requestor = new \FedaPay\Requestor;
$this->expectException('\FedaPay\Error\ApiConnection', 'Error Message');
$requestor->request('get', '/path');
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Tests;
class ResourceTest extends BaseTestCase
{
/**
* Should return the right class name
* @return void
*/
public function testReturnClassName()
{
$this->assertEquals(Fixtures\Foo::className(), 'foo');
$this->assertEquals(Fixtures\FooTest::className(), 'footest');
}
/**
* Should return the right class url
* @return void
*/
public function testShouldReturnClassUrl()
{
$this->assertEquals(Fixtures\Foo::classPath(), '/foos');
$this->assertEquals(Fixtures\FooTest::classPath(), '/footests');
$this->assertEquals(Fixtures\FooPerson::classPath(), '/foopeople');
$this->assertEquals(Fixtures\FooCurrency::classPath(), '/foocurrencies');
}
/**
* Should return throw InvalidRequest exception if id is null
* @return void
*/
public function testShouldThrowInvalidRequest()
{
$this->expectException(\FedaPay\Error\InvalidRequest::class);
$this->expectExceptionMessage(
'Could not determine which URL to request: Tests\Fixtures\Foo instance has invalid ID: '
);
Fixtures\Foo::resourcePath(null);
}
/**
* Should return return resource url
* @return void
*/
public function testReturnResourceUrl()
{
$this->assertEquals(Fixtures\Foo::resourcePath(1), '/foos/1');
}
/**
* Should return return resource url
* @return void
*/
public function testReturnInstanceUrl()
{
$object = new Fixtures\Foo;
$object->id = 1;
$this->assertEquals($object->instanceUrl(), '/foos/1');
}
}

View File

@@ -0,0 +1,653 @@
<?php
namespace Tests;
use Faker\Factory;
class TransactionTest extends BaseTestCase
{
/**
* Should return array of FedaPay\Transaction
*/
public function testShouldReturnTransactions()
{
$body = [
'v1/transactions' => [[
'id' => 1,
'klass' => 'v1/transaction',
'transaction_key' => '0KJAU01',
'reference' => '109329828',
'amount' => 100,
'description' => 'Description',
'callback_url' => 'http://e-shop.com',
'status' => 'pending',
'customer_id' => 1,
'currency_id' => 1,
'mode' => 'mtn',
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
'paid_at' => '2018-03-12T09:09:03.969Z'
]],
'meta' => ['page' => 1]
];
$this->mockRequest('get', '/v1/transactions', [], $body);
$object = \FedaPay\Transaction::all();
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object->meta);
$this->assertInstanceOf(\FedaPay\Transaction::class, $object->transactions[0]);
$this->assertEquals(1, $object->transactions[0]->id);
$this->assertEquals('0KJAU01', $object->transactions[0]->transaction_key);
$this->assertEquals('109329828', $object->transactions[0]->reference);
$this->assertEquals(100, $object->transactions[0]->amount);
$this->assertEquals('Description', $object->transactions[0]->description);
$this->assertEquals('http://e-shop.com', $object->transactions[0]->callback_url);
$this->assertEquals('pending', $object->transactions[0]->status);
$this->assertEquals(1, $object->transactions[0]->customer_id);
$this->assertEquals(1, $object->transactions[0]->currency_id);
$this->assertEquals('mtn', $object->transactions[0]->mode);
}
/**
* Should return array of FedaPay\Transaction
*/
public function testShouldCreateATransaction()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'description' => 'Description',
'callback_url' => 'http://localhost/callback',
'amount' => 1000,
'include' => 'customer,currency'
];
$body = [
'v1/transaction' => [
'id' => 1,
'klass' => 'v1/transaction',
'transaction_key' => '0KJAU01',
'reference' => '109329828',
'amount' => 100,
'description' => 'Description',
'callback_url' => 'http://e-shop.com',
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => null,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
'paid_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/transactions', $data, $body);
$transaction = \FedaPay\Transaction::create($data);
$this->assertInstanceOf(\FedaPay\Transaction::class, $transaction);
$this->assertEquals(1, $transaction->id);
$this->assertEquals('0KJAU01', $transaction->transaction_key);
$this->assertEquals('109329828', $transaction->reference);
$this->assertEquals(100, $transaction->amount);
$this->assertEquals('Description', $transaction->description);
$this->assertEquals('http://e-shop.com', $transaction->callback_url);
$this->assertEquals('pending', $transaction->status);
$this->assertInstanceOf(\FedaPay\Customer::class, $transaction->customer);
$this->assertEquals(1, $transaction->customer->id);
$this->assertInstanceOf(\FedaPay\Currency::class, $transaction->currency);
$this->assertEquals(1, $transaction->currency->id);
$this->assertEquals(null, $transaction->mode);
}
/**
* Should return array of FedaPay\Transaction
*/
public function testShouldCreateTransactionInBatch()
{
$data = [
'transactions' => [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'description' => 'Description',
'callback_url' => 'http://localhost/callback',
'amount' => 1000
],
'include' => 'customer,currency'
];
$body = [
'v1/transaction_batch' => [
'klass' => 'v1/transaction_batch',
'transactions' => [
[
'id' => 1,
'klass' => 'v1/transaction',
'transaction_key' => '0KJAU01',
'reference' => '109329828',
'amount' => 100,
'description' => 'Description',
'callback_url' => 'http://e-shop.com',
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => null,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
'paid_at' => '2018-03-12T09:09:03.969Z'
]
],
'errors' => []
]
];
$this->mockRequest('post', '/v1/transactions/batch', $data, $body);
$object = \FedaPay\Transaction::createInBatch($data);
$this->assertEquals(1, $object->transactions[0]->id);
$this->assertEquals('0KJAU01', $object->transactions[0]->transaction_key);
$this->assertEquals('109329828', $object->transactions[0]->reference);
$this->assertEquals(100, $object->transactions[0]->amount);
$this->assertEquals('Description', $object->transactions[0]->description);
$this->assertEquals('http://e-shop.com', $object->transactions[0]->callback_url);
$this->assertEquals('pending', $object->transactions[0]->status);
$this->assertInstanceOf(\FedaPay\Customer::class, $object->transactions[0]->customer);
$this->assertEquals(1, $object->transactions[0]->customer->id);
$this->assertInstanceOf(\FedaPay\Currency::class, $object->transactions[0]->currency);
$this->assertEquals(1, $object->transactions[0]->currency->id);
$this->assertEquals(null, $object->transactions[0]->mode);
}
/**
* Should retrieve a Transaction
*/
public function testShouldRetrievedATransaction()
{
$body = [
'v1/transaction' => [
'id' => 1,
'klass' => 'v1/transaction',
'transaction_key' => '0KJAU01',
'reference' => '109329828',
'amount' => 100,
'description' => 'Description',
'callback_url' => 'http://e-shop.com',
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => null,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
'paid_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('get', '/v1/transactions/1', [], $body);
$transaction = \FedaPay\Transaction::retrieve(1);
$this->assertInstanceOf(\FedaPay\Transaction::class, $transaction);
$this->assertEquals(1, $transaction->id);
$this->assertEquals('0KJAU01', $transaction->transaction_key);
$this->assertEquals('109329828', $transaction->reference);
$this->assertEquals(100, $transaction->amount);
$this->assertEquals('Description', $transaction->description);
$this->assertEquals('http://e-shop.com', $transaction->callback_url);
$this->assertEquals('pending', $transaction->status);
$this->assertInstanceOf(\FedaPay\Customer::class, $transaction->customer);
$this->assertEquals(1, $transaction->customer->id);
$this->assertInstanceOf(\FedaPay\Currency::class, $transaction->currency);
$this->assertEquals(1, $transaction->currency->id);
$this->assertEquals(null, $transaction->mode);
}
/**
* Should update a transaction
*/
public function testShouldUpdateATransaction()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'description' => 'Description',
'callback_url' => 'http://localhost/callback',
'amount' => 1000,
'include' => 'customer,currency'
];
$body = [
'v1/transaction' => [
'id' => 1,
'klass' => 'v1/transaction',
'transaction_key' => '0KJAU01',
'reference' => '109329828',
'amount' => 100,
'description' => 'Description',
'callback_url' => 'http://e-shop.com',
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => null,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
'paid_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('put', '/v1/transactions/1', $data, $body);
$transaction = \FedaPay\Transaction::update(1, $data);
$this->assertInstanceOf(\FedaPay\Transaction::class, $transaction);
$this->assertEquals(1, $transaction->id);
$this->assertEquals('0KJAU01', $transaction->transaction_key);
$this->assertEquals('109329828', $transaction->reference);
$this->assertEquals(100, $transaction->amount);
$this->assertEquals('Description', $transaction->description);
$this->assertEquals('http://e-shop.com', $transaction->callback_url);
$this->assertEquals('pending', $transaction->status);
$this->assertInstanceOf(\FedaPay\Customer::class, $transaction->customer);
$this->assertEquals(1, $transaction->customer->id);
$this->assertInstanceOf(\FedaPay\Currency::class, $transaction->currency);
$this->assertEquals(1, $transaction->currency->id);
$this->assertEquals(null, $transaction->mode);
}
/**
* Should update a transaction with save
*/
public function testShouldUpdateATransactionWithSave()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'description' => 'Description',
'callback_url' => 'http://localhost/callback',
'amount' => 1000,
'include' => 'customer,currency'
];
$body = [
'v1/transaction' => [
'id' => 1,
'klass' => 'v1/transaction',
'transaction_key' => '0KJAU01',
'reference' => '109329828',
'amount' => 100,
'description' => 'Description',
'callback_url' => 'http://e-shop.com',
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => null,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
'paid_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/transactions', $data, $body);
$transaction = \FedaPay\Transaction::create($data);
$transaction->description = 'Update description';
$updateData = [
'klass' => 'v1/transaction',
'transaction_key' => '0KJAU01',
'reference' => '109329828',
'amount' => 100,
'description' => 'Update description',
'callback_url' => 'http://e-shop.com',
'status' => 'pending',
'customer' => [
'klass' => 'v1/customer',
],
'currency' => [
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => null,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
'paid_at' => '2018-03-12T09:09:03.969Z'
];
$this->mockRequest('put', '/v1/transactions/1', $updateData, $body);
$transaction->save();
}
/**
* Should delete a transaction
*/
public function testShouldDeleteATransaction()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'description' => 'Description',
'callback_url' => 'http://localhost/callback',
'amount' => 1000,
'include' => 'customer,currency'
];
$body = [
'v1/transaction' => [
'id' => 1,
'klass' => 'v1/transaction',
'transaction_key' => '0KJAU01',
'reference' => '109329828',
'amount' => 100,
'description' => 'Description',
'callback_url' => 'http://e-shop.com',
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => null,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
'paid_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/transactions', $data, $body);
$transaction = \FedaPay\Transaction::create($data);
$this->mockRequest('delete', '/v1/transactions/1');
$transaction->delete();
}
/**
* Should update a transaction with save
*/
public function testShouldGenerateTransactionToken()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'description' => 'Description',
'callback_url' => 'http://localhost/callback',
'amount' => 1000,
'include' => 'customer,currency'
];
$body = [
'v1/transaction' => [
'id' => 1,
'klass' => 'v1/transaction',
'transaction_key' => '0KJAU01',
'reference' => '109329828',
'amount' => 100,
'description' => 'Description',
'callback_url' => 'http://e-shop.com',
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => null,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
'paid_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/transactions', $data, $body);
$transaction = \FedaPay\Transaction::create($data);
$body = [
'token' => 'PAYEMENT_TOKEN',
'url' => 'https://process.fedapay.com/PAYEMENT_TOKEN',
];
$this->mockRequest('post', '/v1/transactions/1/token', [], $body);
$tokenObject = $transaction->generateToken();
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $tokenObject);
$this->assertEquals('PAYEMENT_TOKEN', $tokenObject->token);
$this->assertEquals('https://process.fedapay.com/PAYEMENT_TOKEN', $tokenObject->url);
}
/**
* Should update a transaction with save
*/
public function testShouldGenerateTransactionTokenFromId()
{
$body = [
'token' => 'PAYEMENT_TOKEN',
'url' => 'https://process.fedapay.com/PAYEMENT_TOKEN',
];
$this->mockRequest('post', '/v1/transactions/1/token', [], $body);
$tokenObject = \FedaPay\Transaction::generateTokenFromId(1);
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $tokenObject);
$this->assertEquals('PAYEMENT_TOKEN', $tokenObject->token);
$this->assertEquals('https://process.fedapay.com/PAYEMENT_TOKEN', $tokenObject->url);
}
/**
* Should update a transaction with save
*/
public function testShouldSendMtnRequestWithToken()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'description' => 'Description',
'callback_url' => 'http://localhost/callback',
'amount' => 1000,
'include' => 'customer,currency'
];
$body = [
'v1/transaction' => [
'id' => 1,
'klass' => 'v1/transaction',
'transaction_key' => '0KJAU01',
'reference' => '109329828',
'amount' => 100,
'description' => 'Description',
'callback_url' => 'http://e-shop.com',
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => null,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
'paid_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/transactions', $data, $body);
$transaction = \FedaPay\Transaction::create($data);
$this->mockRequest('post', '/v1/mtn', ['token' => 'PAYEMENT_TOKEN'], ['message' => 'success']);
$object = $transaction->sendNowWithToken('mtn', 'PAYEMENT_TOKEN');
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertEquals('success', $object->message);
}
/**
* Should send mtn request
*/
public function testShouldSendMtnRequest()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'description' => 'Description',
'callback_url' => 'http://localhost/callback',
'amount' => 1000,
'include' => 'customer,currency'
];
$body = [
'v1/transaction' => [
'id' => 1,
'klass' => 'v1/transaction',
'transaction_key' => '0KJAU01',
'reference' => '109329828',
'amount' => 100,
'description' => 'Description',
'callback_url' => 'http://e-shop.com',
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => null,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
'paid_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/transactions', $data, $body);
$transaction = \FedaPay\Transaction::create($data);
$body = [
'token' => 'PAYEMENT_TOKEN',
'url' => 'https://process.fedapay.com/PAYEMENT_TOKEN',
];
$this->mockMultipleRequests([
['method' => 'post', 'path' => '/v1/transactions/1/token', 'params' => [], 'response' => $body],
['method' => 'post', 'path' => '/v1/mtn', 'params' => ['token' => 'PAYEMENT_TOKEN'], 'response' => ['message' => 'success']]
]);
$object = $transaction->sendNow('mtn');
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertEquals('success', $object->message);
}
/**
* Should send fees request
*/
public function testShouldSendFeesRequest()
{
$data = [
'customer' => ['id' => 1],
'currency' => ['iso' => 'XOF'],
'description' => 'Description',
'callback_url' => 'http://localhost/callback',
'amount' => 1000,
'include' => 'customer,currency'
];
$body = [
'v1/transaction' => [
'id' => 1,
'klass' => 'v1/transaction',
'transaction_key' => '0KJAU01',
'reference' => '109329828',
'amount' => 100,
'description' => 'Description',
'callback_url' => 'http://e-shop.com',
'status' => 'pending',
'customer' => [
'id' => 1,
'klass' => 'v1/customer',
],
'currency' => [
'id' => 1,
'klass' => 'v1/currency',
'iso' => 'XOF'
],
'mode' => null,
'created_at' => '2018-03-12T09:09:03.969Z',
'updated_at' => '2018-03-12T09:09:03.969Z',
'paid_at' => '2018-03-12T09:09:03.969Z'
]
];
$this->mockRequest('post', '/v1/transactions', $data, $body);
$transaction = \FedaPay\Transaction::create($data);
$body = [
'amount_debited' => 51020,
'amount_transferred' => 50000,
'apply_fees_to_merchant' => false,
'commission' => 0.02,
'fees' => 1020,
'fixed_commission' => 0,
'message' => '{fees} de frais supplémentaires sont appliqués sur votre paiement.',
];
$this->mockRequest('get', '/v1/transactions/fees', ['token' => 'TOKEN', 'mode' => 'mtn'], $body);
$object = $transaction->getFees('TOKEN', 'mtn');
$this->assertInstanceOf(\FedaPay\FedaPayObject::class, $object);
$this->assertEquals(51020, $object->amount_debited);
$this->assertEquals(50000, $object->amount_transferred);
$this->assertEquals(false, $object->apply_fees_to_merchant);
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace Tests;
use FedaPay\Util;
class UtilTest extends BaseTestCase
{
public function testIsList()
{
$list = [5, 'nstaoush', []];
$this->assertTrue(Util\Util::isList($list));
$notlist = [5, 'nstaoush', [], 'bar' => 'baz'];
$this->assertFalse(Util\Util::isList($notlist));
}
public function testConvertFedaPayObjectToArrayIncludesId()
{
$customer = Util\Util::convertToFedaPayObject(
[
'id' => '123',
'object' => 'v1/customer',
'value' => ['a', 'b']
],
[]
);
$array = $customer->__toArray(true);
$this->assertTrue(array_key_exists('id', $array));
$this->assertEquals($array['id'], '123');
$this->assertEquals(['a', 'b'], $array['value']);
}
public function testEncodeParameters()
{
$params = [
'a' => 3,
'b' => '+foo?',
'c' => 'bar&baz',
'd' => ['a' => 'a', 'b' => 'b'],
'e' => [0, 1],
'f' => '',
// note the empty hash won't even show up in the request
'g' => [],
];
$this->assertSame(
"a=3&b=%2Bfoo%3F&c=bar%26baz&d[a]=a&d[b]=b&e[]=0&e[]=1&f=",
Util\Util::encodeParameters($params)
);
}
public function testUrlEncode()
{
$this->assertSame("foo", Util\Util::urlEncode("foo"));
$this->assertSame("foo%2B", Util\Util::urlEncode("foo+"));
$this->assertSame("foo%26", Util\Util::urlEncode("foo&"));
$this->assertSame("foo[bar]", Util\Util::urlEncode("foo[bar]"));
}
public function testFlattenParams()
{
$params = [
'a' => 3,
'b' => '+foo?',
'c' => 'bar&baz',
'd' => ['a' => 'a', 'b' => 'b'],
'e' => [0, 1],
'f' => [
['foo' => '1', 'ghi' => '2'],
['foo' => '3', 'bar' => '4'],
],
];
$encoded = [];
Util\Util::flattenParams($params, $encoded);
$this->assertSame(
[
['a', 3],
['b', '+foo?'],
['c', 'bar&baz'],
['d[a]', 'a'],
['d[b]', 'b'],
['e[]', 0],
['e[]', 1],
['f[][foo]', '1'],
['f[][ghi]', '2'],
['f[][foo]', '3'],
['f[][bar]', '4'],
],
$encoded
);
}
}

View File

@@ -0,0 +1,115 @@
<?php
namespace Tests;
use FedaPay\Webhook;
use FedaPay\WebhookSignature;
class WebhookTest extends BaseTestCase
{
const EVENT_PAYLOAD = "{
\"id\": \"evt_test_webhook\",
\"object\": \"event\"
}";
const SECRET = "whsec_test_secret";
private function generateHeader($opts = [])
{
$timestamp = array_key_exists('timestamp', $opts) ? $opts['timestamp'] : time();
$payload = array_key_exists('payload', $opts) ? $opts['payload'] : self::EVENT_PAYLOAD;
$secret = array_key_exists('secret', $opts) ? $opts['secret'] : self::SECRET;
$scheme = array_key_exists('scheme', $opts) ? $opts['scheme'] : WebhookSignature::EXPECTED_SCHEME;
$signature = array_key_exists('signature', $opts) ? $opts['signature'] : null;
if ($signature === null) {
$signedPayload = "$timestamp.$payload";
$signature = hash_hmac("sha256", $signedPayload, $secret);
}
return "t=$timestamp,$scheme=$signature";
}
public function testValidJsonAndHeader()
{
$sigHeader = $this->generateHeader();
$event = Webhook::constructEvent(self::EVENT_PAYLOAD, $sigHeader, self::SECRET);
$this->assertInstanceOf(\FedaPay\Event::class, $event);
$this->assertEquals("evt_test_webhook", $event->id);
}
public function testInvalidJson()
{
$this->expectException('\UnexpectedValueException');
$payload = "this is not valid JSON";
$sigHeader = $this->generateHeader(["payload" => $payload]);
Webhook::constructEvent($payload, $sigHeader, self::SECRET);
}
public function testValidJsonAndInvalidHeader()
{
$this->expectException('\FedaPay\Error\SignatureVerification');
$sigHeader = "bad_header";
Webhook::constructEvent(self::EVENT_PAYLOAD, $sigHeader, self::SECRET);
}
public function testMalformedHeader()
{
$this->expectException('\FedaPay\Error\SignatureVerification');
$this->expectExceptionMessage('Unable to extract timestamp and signatures from header');
$sigHeader = "i'm not even a real signature header";
WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, $sigHeader, self::SECRET);
}
/**
* @expectedException \FedaPay\Error\SignatureVerification
* @expectedExceptionMessage No signatures found with expected scheme
*/
public function testNoSignaturesWithExpectedScheme()
{
$this->expectException('\FedaPay\Error\SignatureVerification');
$this->expectExceptionMessage('No signatures found with expected scheme');
$sigHeader = $this->generateHeader(["scheme" => "v0"]);
WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, $sigHeader, self::SECRET);
}
public function testNoValidSignatureForPayload()
{
$this->expectException('\FedaPay\Error\SignatureVerification');
$this->expectExceptionMessage('No signatures found matching the expected signature for payload');
$sigHeader = $this->generateHeader(["signature" => "bad_signature"]);
WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, $sigHeader, self::SECRET);
}
public function testTimestampTooOld()
{
$this->expectException('\FedaPay\Error\SignatureVerification');
$this->expectExceptionMessage('Timestamp outside the tolerance zone');
$sigHeader = $this->generateHeader(["timestamp" => time() - 15]);
WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, $sigHeader, self::SECRET, 10);
}
public function testTimestampTooRecent()
{
$this->expectException('\FedaPay\Error\SignatureVerification');
$this->expectExceptionMessage('Timestamp outside the tolerance zone');
$sigHeader = $this->generateHeader(["timestamp" => time() + 15]);
WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, $sigHeader, self::SECRET, 10);
}
public function testValidHeaderAndSignature()
{
$sigHeader = $this->generateHeader();
$this->assertTrue(WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, $sigHeader, self::SECRET, 10));
}
public function testHeaderContainsValidSignature()
{
$sigHeader = $this->generateHeader() . ",v1=bad_signature";
$this->assertTrue(WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, $sigHeader, self::SECRET, 10));
}
public function testTimestampOffButNoTolerance()
{
$sigHeader = $this->generateHeader(["timestamp" => 12345]);
$this->assertTrue(WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, $sigHeader, self::SECRET));
}
}

View File

@@ -0,0 +1,5 @@
<?php
require_once __DIR__ . '/../init.php';
require_once __DIR__ . '/bootstrap.php';

View File

@@ -0,0 +1,7 @@
<?php
require_once __DIR__ . '/Fixtures/Foo.php';
require_once __DIR__ . '/Fixtures/FooCurrency.php';
require_once __DIR__ . '/Fixtures/FooPerson.php';
require_once __DIR__ . '/Fixtures/FooTest.php';
require_once __DIR__ . '/BaseTestCase.php';