Revize 11e2d062
Přidáno uživatelem Ondřej Fibich před téměř 7 roky(ů)
application/services/member/ExpirationCalcService.php | ||
---|---|---|
*/
|
||
class ExpirationCalcService extends \AbstractService
|
||
{
|
||
|
||
/**
|
||
* @var Transfer_Model
|
||
*/
|
||
protected $transfer_model;
|
||
|
||
/**
|
||
* @var Fee_Model
|
||
*/
|
||
protected $fee_model;
|
||
|
||
/**
|
||
* @var Device_Model
|
||
*/
|
||
protected $device_model;
|
||
|
||
/**
|
||
* Creates service.
|
||
*
|
||
* @param \ServiceFactory $factory
|
||
*/
|
||
public function __construct(\ServiceFactory $factory)
|
||
/**
|
||
* Creates service.
|
||
*
|
||
* @param \ServiceFactory $factory
|
||
*/
|
||
public function __construct(\ServiceFactory $factory)
|
||
{
|
||
parent::__construct($factory);
|
||
parent::__construct($factory);
|
||
$this->transfer_model = new Transfer_Model;
|
||
$this->fee_model = new Fee_Model;
|
||
$this->device_model = new Device_Model;
|
||
... | ... | |
* @author Michal Kliment, Ondrej Fibich
|
||
* @param object $account
|
||
* @param int $shortened_on_year year to shortened expiration date from
|
||
* (10 years from now by default)
|
||
* (10 years from now by default)
|
||
* @return ExpirationCalcResult
|
||
*/
|
||
public function get_expiration_info($account, $shortened_on_year = NULL)
|
||
... | ... | |
$balance = $account->balance;
|
||
|
||
$last_deduct_date = date_parse(
|
||
date::get_closses_deduct_date_to(
|
||
$this->transfer_model->get_last_transfer_datetime_of_account($account->id)
|
||
)
|
||
date::get_closses_deduct_date_to(
|
||
$this->transfer_model->get_last_transfer_datetime_of_account($account->id)
|
||
)
|
||
);
|
||
|
||
// date
|
||
... | ... | |
$entrance_date = date_parse($entrance_date_str);
|
||
|
||
// finds debt payment rate of entrance fee
|
||
$debt_payment_rate = ($account->member->debt_payment_rate > 0)
|
||
? $account->member->debt_payment_rate : $account->member->entrance_fee;
|
||
$debt_payment_rate = ($account->member->debt_payment_rate > 0) ? $account->member->debt_payment_rate : $account->member->entrance_fee;
|
||
|
||
// finds all debt payments of entrance fee
|
||
self::find_debt_payments(
|
||
$payments, $entrance_date['month'], $entrance_date['year'],
|
||
$account->member->entrance_fee, $debt_payment_rate
|
||
$payments, $entrance_date['month'], $entrance_date['year'], $account->member->entrance_fee, $debt_payment_rate
|
||
);
|
||
|
||
// finds all member's devices with debt payments
|
||
... | ... | |
|
||
// finds all debt payments of this device
|
||
self::find_debt_payments(
|
||
$payments, $buy_date['month'], $buy_date['year'],
|
||
$device->price, $device->payment_rate
|
||
$payments, $buy_date['month'], $buy_date['year'], $device->price, $device->payment_rate
|
||
);
|
||
}
|
||
|
||
... | ... | |
* @param float $payment_rate
|
||
*/
|
||
protected static function find_debt_payments(
|
||
&$payments, $month, $year, $payment_left, $payment_rate)
|
||
&$payments, $month, $year, $payment_left, $payment_rate)
|
||
{
|
||
while ($payment_left > 0)
|
||
{
|
||
... | ... | |
* @var string
|
||
*/
|
||
public $expiration_date;
|
||
|
||
|
||
/**
|
||
* Flag whether the expiration was too long and was shortened.
|
||
*
|
tests/AbstractItCase.php | ||
---|---|---|
*/
|
||
abstract class AbstractItCase extends PHPUnit_Framework_TestCase
|
||
{
|
||
/**
|
||
* @var Database
|
||
*/
|
||
protected static $connection;
|
||
|
||
/**
|
||
* @var ServiceFactory
|
||
*/
|
||
protected static $services;
|
||
/**
|
||
* @var Database
|
||
*/
|
||
protected static $connection;
|
||
|
||
/**
|
||
* Reset URL setting to given values.
|
||
*
|
||
* @param string $domain installed domain name
|
||
* @param string $path installed directory sub-path
|
||
*/
|
||
private static function reset_url_settings($domain, $path)
|
||
{
|
||
// set base domain
|
||
Settings::set('domain', $domain);
|
||
// set subdirectory
|
||
Settings::set('suffix', $path);
|
||
}
|
||
|
||
/**
|
||
* Reset URL setting to values in test configuration.
|
||
*/
|
||
protected static function reset_url_settings_to_current()
|
||
{
|
||
$domain = TestConfig::get('url.domain', 'localhost');
|
||
$path = TestConfig::get('url.path', '/freenetis/');
|
||
self::reset_url_settings($domain, $path);
|
||
}
|
||
/**
|
||
* @var ServiceFactory
|
||
*/
|
||
protected static $services;
|
||
|
||
/**
|
||
* Overridden setup before class in order to init/update databse schema
|
||
* and setup provided service factory and DB connection.
|
||
*/
|
||
public static function setUpBeforeClass()
|
||
{
|
||
parent::setUpBeforeClass();
|
||
// service factory
|
||
self::$services = new ServiceFactory();
|
||
// init DB schema if not already
|
||
$lck_file = server::base_dir() . '/upload/mutex';
|
||
self::$services->injectCoreDatabaseInit()->make($lck_file, function ()
|
||
{
|
||
self::reset_url_settings_to_current();
|
||
});
|
||
/**
|
||
* Reset URL setting to given values.
|
||
*
|
||
* @param string $domain installed domain name
|
||
* @param string $path installed directory sub-path
|
||
*/
|
||
private static function reset_url_settings($domain, $path)
|
||
{
|
||
// set base domain
|
||
Settings::set('domain', $domain);
|
||
// set subdirectory
|
||
Settings::set('suffix', $path);
|
||
}
|
||
|
||
/**
|
||
* Reset URL setting to values in test configuration.
|
||
*/
|
||
protected static function reset_url_settings_to_current()
|
||
{
|
||
$domain = TestConfig::get('url.domain', 'localhost');
|
||
$path = TestConfig::get('url.path', '/freenetis/');
|
||
self::reset_url_settings($domain, $path);
|
||
}
|
||
|
||
/**
|
||
* Overridden setup before class in order to init/update databse schema
|
||
* and setup provided service factory and DB connection.
|
||
*/
|
||
public static function setUpBeforeClass()
|
||
{
|
||
parent::setUpBeforeClass();
|
||
// service factory
|
||
self::$services = new ServiceFactory();
|
||
// init DB schema if not already
|
||
$lck_file = server::base_dir() . '/upload/mutex';
|
||
self::$services->injectCoreDatabaseInit()->make($lck_file, function ()
|
||
{
|
||
self::reset_url_settings_to_current();
|
||
});
|
||
unlink($lck_file);
|
||
// get DB connection
|
||
self::$connection = Database::instance();
|
||
}
|
||
// get DB connection
|
||
self::$connection = Database::instance();
|
||
}
|
||
|
||
}
|
tests/application/controllers/api_endpoints/AbstractEndPointTestCase.php | ||
---|---|---|
<?php
|
||
|
||
/*
|
||
* This file is part of open source system FreenetIS
|
||
* and it is release under GPLv3 licence.
|
||
... | ... | |
*/
|
||
abstract class AbstractEndPointTestCase extends AbstractItCase
|
||
{
|
||
/**
|
||
* API user account username.
|
||
*/
|
||
const API_USERNAME = 'test_NL';
|
||
|
||
/**
|
||
* API user account password.
|
||
*/
|
||
const API_PASSWORD = '12345678901234567890123456789012';
|
||
|
||
/**
|
||
* Base path to FreenetIS API.
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $base_path;
|
||
|
||
/**
|
||
* API account that is used for logging to API.
|
||
*
|
||
* @var Api_account_Model
|
||
*/
|
||
protected $api_account;
|
||
|
||
/**
|
||
* Defines authentication type that is used for connecting to API.
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $auth_method;
|
||
|
||
/**
|
||
* API user account username.
|
||
*/
|
||
const API_USERNAME = 'test_NL';
|
||
|
||
/**
|
||
* API user account password.
|
||
*/
|
||
const API_PASSWORD = '12345678901234567890123456789012';
|
||
|
||
/**
|
||
* Base path to FreenetIS API.
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $base_path;
|
||
|
||
/**
|
||
* API account that is used for logging to API.
|
||
*
|
||
* @var Api_account_Model
|
||
*/
|
||
protected $api_account;
|
||
|
||
/**
|
||
* Defines authentication type that is used for connecting to API.
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $auth_method;
|
||
|
||
/**
|
||
* Holds state of settings "api_enabled" during test.
|
||
... | ... | |
/**
|
||
* Enable API and save old state before tests.
|
||
*/
|
||
public static function setUpBeforeClass() {
|
||
public static function setUpBeforeClass()
|
||
{
|
||
parent::setUpBeforeClass();
|
||
self::$old_api_enabled = module::e('api');
|
||
Settings::set('api_enabled', TRUE);
|
||
... | ... | |
/**
|
||
* Restore old API state after all tests done.
|
||
*/
|
||
public static function tearDownAfterClass() {
|
||
public static function tearDownAfterClass()
|
||
{
|
||
parent::tearDownAfterClass();
|
||
Settings::set('api_enabled', self::$old_api_enabled);
|
||
}
|
||
|
||
/**
|
||
* Prepare base path and add API account.
|
||
*/
|
||
protected function setUp()
|
||
{
|
||
$this->base_path = Settings::get('protocol') . '://'
|
||
. Settings::get('domain') . Settings::get('suffix') . 'cs'
|
||
. Api_Controller::API_BASE_PATH;
|
||
|
||
$this->api_account = new Api_account_Model();
|
||
$this->api_account->allowed_paths = '/**'; // allow all
|
||
$this->api_account->enabled = TRUE;
|
||
$this->api_account->readonly = FALSE;
|
||
$this->api_account->username = self::API_USERNAME;
|
||
$this->api_account->token = self::API_PASSWORD;
|
||
$this->api_account->save_throwable();
|
||
|
||
$this->auth_method = 'authenticateWith'
|
||
. ucfirst(Settings::get('api_auth_type'));
|
||
}
|
||
|
||
/**
|
||
* Remove API account.
|
||
*/
|
||
protected function tearDown()
|
||
{
|
||
$this->api_account->delete();
|
||
}
|
||
|
||
/**
|
||
* Gets resource on given path, with given parameters, than test the response
|
||
* code and return the response.
|
||
*
|
||
* @param string $path path relative to base path
|
||
* @params array $params optional request parameters
|
||
* @param integer $expected_code optional expected HTTP response code
|
||
* @return \Httpful\Response
|
||
*/
|
||
protected function request_get($path = '', $params = array(),
|
||
$expected_code = 0)
|
||
{
|
||
if (!empty($params))
|
||
{
|
||
$params_str_array = array();
|
||
foreach ($params as $name => $value)
|
||
{
|
||
$params_str_array[] = urlencode($name) . '=' . urlencode($value);
|
||
}
|
||
$path .= '?' . implode('&', $params_str_array);
|
||
}
|
||
|
||
$rsp = Request::get($this->base_path . $path)
|
||
->{$this->auth_method}(self::API_USERNAME, self::API_PASSWORD)
|
||
->send();
|
||
|
||
$this->assertNotNull($rsp);
|
||
|
||
if ($expected_code > 0)
|
||
{
|
||
$this->assertEquals($expected_code, $rsp->code, 'GET failed expected');
|
||
}
|
||
|
||
return $rsp;
|
||
}
|
||
/**
|
||
* Prepare base path and add API account.
|
||
*/
|
||
protected function setUp()
|
||
{
|
||
$this->base_path = Settings::get('protocol') . '://'
|
||
. Settings::get('domain') . Settings::get('suffix') . 'cs'
|
||
. Api_Controller::API_BASE_PATH;
|
||
|
||
$this->api_account = new Api_account_Model();
|
||
$this->api_account->allowed_paths = '/**'; // allow all
|
||
$this->api_account->enabled = TRUE;
|
||
$this->api_account->readonly = FALSE;
|
||
$this->api_account->username = self::API_USERNAME;
|
||
$this->api_account->token = self::API_PASSWORD;
|
||
$this->api_account->save_throwable();
|
||
|
||
$this->auth_method = 'authenticateWith'
|
||
. ucfirst(Settings::get('api_auth_type'));
|
||
}
|
||
|
||
/**
|
||
* Remove API account.
|
||
*/
|
||
protected function tearDown()
|
||
{
|
||
$this->api_account->delete();
|
||
}
|
||
|
||
/**
|
||
* Gets resource on given path, with given parameters, than test the response
|
||
* code and return the response.
|
||
*
|
||
* @param string $path path relative to base path
|
||
* @params array $params optional request parameters
|
||
* @param integer $expected_code optional expected HTTP response code
|
||
* @return \Httpful\Response
|
||
*/
|
||
protected function request_get($path = '', $params = array(), $expected_code = 0)
|
||
{
|
||
if (!empty($params))
|
||
{
|
||
$params_str_array = array();
|
||
foreach ($params as $name => $value)
|
||
{
|
||
$params_str_array[] = urlencode($name) . '=' . urlencode($value);
|
||
}
|
||
$path .= '?' . implode('&', $params_str_array);
|
||
}
|
||
|
||
$rsp = Request::get($this->base_path . $path)
|
||
->{$this->auth_method}(self::API_USERNAME, self::API_PASSWORD)
|
||
->send();
|
||
|
||
$this->assertNotNull($rsp);
|
||
|
||
if ($expected_code > 0)
|
||
{
|
||
$this->assertEquals($expected_code, $rsp->code, 'GET failed expected');
|
||
}
|
||
|
||
return $rsp;
|
||
}
|
||
|
||
}
|
tests/application/services/member/ExpirationCalcServiceTest.php | ||
---|---|---|
class ExpirationCalcServiceTest extends AbstractItCase
|
||
{
|
||
|
||
/**
|
||
* @var ExpirationCalcService
|
||
*/
|
||
private $object;
|
||
/**
|
||
* @var ExpirationCalcService
|
||
*/
|
||
private $object;
|
||
|
||
/**
|
||
* Hold deduct day during test for recover.
|
||
... | ... | |
private $old_deduct_day;
|
||
|
||
protected function setUp()
|
||
{
|
||
$this->object = new ConfigurableTestExpirationCalcService(self::$services);
|
||
{
|
||
$this->object = new ConfigurableTestExpirationCalcService(self::$services);
|
||
$this->old_deduct_day = Settings::get('deduct_day');
|
||
}
|
||
}
|
||
|
||
protected function tearDown()
|
||
{
|
||
... | ... | |
*/
|
||
class ConfigurableTestExpirationCalcService extends ExpirationCalcService
|
||
{
|
||
|
||
public function set_transfer_model($transfer_model)
|
||
{
|
||
$this->transfer_model = $transfer_model;
|
||
... | ... | |
{
|
||
$this->device_model = $device_model;
|
||
}
|
||
|
||
}
|
||
|
||
class TestStaticTransferModel
|
||
{
|
||
|
||
private $account_id;
|
||
private $last_transfer_date;
|
||
|
||
... | ... | |
{
|
||
return $this->account_id == $account_id ? $this->last_transfer_date : NULL;
|
||
}
|
||
|
||
}
|
||
|
||
class TestStaticFeeModel
|
||
{
|
||
|
||
private $member_id;
|
||
private $fee;
|
||
|
||
... | ... | |
{
|
||
return ($this->member_id == $member_id) ? $this->fee : 0;
|
||
}
|
||
|
||
}
|
Také k dispozici: Unified diff
Format fixes.