Commit daf9ec0d by Camoo Sarl

Unittests added

parent 9a062f6b
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
backupGlobals="true"
>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
...@@ -15,6 +15,8 @@ namespace CAMOO\Balance; ...@@ -15,6 +15,8 @@ namespace CAMOO\Balance;
*/ */
use CAMOO\Client; use CAMOO\Client;
use CAMOO\Balance\Validate;
use CAMOO\Exceptions\CamooException;
class Balance extends Client{ class Balance extends Client{
...@@ -40,6 +42,10 @@ class Balance extends Client{ ...@@ -40,6 +42,10 @@ class Balance extends Client{
*/ */
public function add($hData) { public function add($hData) {
$this->oHttpClient->setResourceName('topup'); $this->oHttpClient->setResourceName('topup');
$oValidator = (new Validate())->addBalance($hData);
if ( $oValidator->validate() === false ) {
throw new CamooException($oValidator->errors());
}
return $this->post($hData); return $this->post($hData);
} }
} }
<?php
namespace CAMOO\Balance;
/**
*
* CAMOO SARL: http://www.camoo.cm
* @copyright (c) camoo.cm
* @license: You are not allowed to sell or distribute this software without permission
* Copyright reserved
* File: src/CAMOO/Balance/Validate.php
* updated: Mai 2017
* Created by: Epiphane Tchabom (e.tchabom@camoo.cm)
* Description: CAMOO Balance API Validate
*
* @link http://www.camoo.cm
*/
require_once(CAMOO_ROOT_SRC_DIR .'vendor/autoload.php');
use Valitron\Validator;
class Validate {
public function addBalance($hData) {
$oValidator = new Validator($hData);
$oValidator
->rule('required', ['phonenumber', 'amount']);
$oValidator
->rule('integer','amount', 'amount');
$this->notEmptyRule($oValidator, 'phonenumber');
$this->notEmptyRule($oValidator, 'amount');
$this->isCMMTN($oValidator, 'phonenumber');
return $oValidator;
}
/**
* @guide https://en.wikipedia.org/wiki/Telephone_numbers_in_Cameroon
*/
private function isCMMTN(&$oValidator, $sParam) {
$oValidator
->rule(function($field, $value, $params, $fields) {
return (boolean) preg_match('/^(67|650|651|652|653|654)\s*/', $value);
}, $sParam)->message("{field} is not a MTN Cameroon phonenumber...");
}
private function notEmptyRule(&$oValidator, $sParam) {
$oValidator
->rule(function($field, $value, $params, $fields) {
if (is_null($value) || empty($value) ) {
return false;
} elseif (is_string($value) && trim($value) === '') {
return false;
}
return true;
}, $sParam)->message("{field} can not be blank/empty...");
}
}
<?php
use PHPUnit\Framework\TestCase;
use CAMOO\Balance\Balance;
use CAMOO\Exceptions\CamooException;
/**
* @covers CAMOO\Balance\Balance
*/
class BalanceTest extends TestCase {
/**
* @covers Balance::current
*/
public function testCurrent() {
$oBalance = new Balance('api_key', 'secret_key');
$this->assertInstanceOf('stdClass',$oBalance->current());
}
/**
* @covers Balance::add
* @dataProvider addProviderSuccess
*/
public function testAddSuccess($hData) {
$oBalance = new Balance('api_key', 'secret_key');
$this->assertInstanceOf('stdClass',$oBalance->add($hData));
}
public function addProviderSuccess()
{
return [
[ ['phonenumber' => 671234567,'amount' => '1000']],
[ ['phonenumber' => 650234567,'amount' => 10000 ]],
[ ['phonenumber' => 651234567,'amount' => 1000 ]],
[ ['phonenumber' => 652234567,'amount' => '1500']],
[ ['phonenumber' => 653234567,'amount' => '3000']],
[ ['phonenumber' => 654234567,'amount' => '500']],
];
}
/**
* @covers Balance::add
* @dataProvider addProviderFailure
*/
public function testAddFailure($hData) {
$oBalance = new Balance('api_key', 'secret_key');
$this->expectException(CamooException::class);
$oBalance->add($hData);
}
public function addProviderFailure()
{
return [
[ ['phonenumbe' => 671234567,'amount' => '1000']],
[ ['phonenumbr' => 650234567,'amount' => 10000 ]],
[ ['phonenmber' => 651234567,'amount' => '35o' ]],
[ ['phonnumber' => 652234567,'amount' => '1500']],
[ ['ponenumber' => 653234567,'amount' => '3000']],
[ ['phonenumber' => 694234567,'amount' => '500']],
];
}
}
<?php
if (is_file('src/autoload.php')) {
require_once 'src/autoload.php';
} else {
require_once dirname(__DIR__) . '/src/autoload.php';
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment