211 lines
5.9 KiB
PHP
211 lines
5.9 KiB
PHP
<?php
|
||
/**
|
||
* PhalApi_Mall\Domain\Flow_Test
|
||
*
|
||
* 针对 ../src/mall/Domain/Flow.php Mall\Domain\Flow 类的PHPUnit单元测试
|
||
*
|
||
* @author: dogstar 20200915
|
||
*/
|
||
|
||
namespace tests\Mall\Domain;
|
||
use Mall\Domain\Flow;
|
||
|
||
class PhpUnderControl_MallDomainFlow_Test extends \PHPUnit\Framework\TestCase
|
||
{
|
||
public $mallDomainFlow;
|
||
|
||
protected function setUp()
|
||
{
|
||
parent::setUp();
|
||
|
||
$this->mallDomainFlow = new \Mall\Domain\Flow();
|
||
}
|
||
|
||
protected function tearDown()
|
||
{
|
||
// 输出本次单元测试所执行的SQL语句
|
||
// var_dump(\PhalApi\DI()->tracer->getSqls());
|
||
|
||
// 输出本次单元测试所涉及的追踪埋点
|
||
// var_dump(\PhalApi\DI()->tracer->getStack());
|
||
}
|
||
|
||
|
||
/**
|
||
* @group testAddNewFlow
|
||
*/
|
||
public function testAddNewFlow()
|
||
{
|
||
$flow = array(
|
||
'username' => 'rights_user',
|
||
'product_key' => 'App.Config.GetConfig',
|
||
'product_name' => 'test',
|
||
'total_amount' => 10,
|
||
'expire_time' => time() + 86400,
|
||
);
|
||
|
||
$rs = $this->mallDomainFlow->addNewFlow($flow);
|
||
}
|
||
|
||
public function testAddNewFlowExpireOut()
|
||
{
|
||
$flow = array(
|
||
'username' => 'rights_user',
|
||
'product_key' => 'App.Config.GetConfig',
|
||
'product_name' => 'test',
|
||
'total_amount' => 10,
|
||
'expire_time' => time() - 86400, // 注意这里
|
||
);
|
||
|
||
$rs = $this->mallDomainFlow->addNewFlow($flow);
|
||
}
|
||
|
||
/**
|
||
* @group testUpdateFlow
|
||
*/
|
||
public function testUpdateFlow()
|
||
{
|
||
$id = 1;
|
||
$flow = array('total_amount' => 200);
|
||
|
||
$rs = $this->mallDomainFlow->updateFlow($id, $flow);
|
||
}
|
||
|
||
/**
|
||
* @group testGetFlow
|
||
*/
|
||
public function testGetFlow()
|
||
{
|
||
$id = 1;
|
||
|
||
$rs = $this->mallDomainFlow->getFlow($id);
|
||
|
||
$this->assertNotEmpty($rs);
|
||
$this->assertEquals(200, $rs['total_amount']);
|
||
}
|
||
|
||
/**
|
||
* @group testDeleteFlow
|
||
*/
|
||
public function testDeleteFlow()
|
||
{
|
||
$id = 404;
|
||
|
||
$rs = $this->mallDomainFlow->deleteFlow($id);
|
||
}
|
||
|
||
/**
|
||
* @group testGetFlowList
|
||
*/
|
||
public function testGetFlowList()
|
||
{
|
||
$page = 1;
|
||
$perpage = 2;
|
||
|
||
$rs = $this->mallDomainFlow->getFlowList($page, $perpage);
|
||
|
||
$this->assertNotEmpty($rs);
|
||
}
|
||
|
||
/**
|
||
* @group testGetFlowStatusList
|
||
*/
|
||
public function testGetFlowStatusList()
|
||
{
|
||
$isMap = true;
|
||
|
||
$rs = $this->mallDomainFlow->getFlowStatusList($isMap);
|
||
|
||
$this->assertNotEmpty($rs);
|
||
}
|
||
|
||
/**
|
||
* @group testCutdown
|
||
* 什么套餐都没有时,不需要扣费
|
||
*/
|
||
public function testCutdown()
|
||
{
|
||
$service = 'App.Config.GetConfig';
|
||
$appKey = 'test_flow';
|
||
|
||
$rs = $this->mallDomainFlow->cutdown($service, $appKey);
|
||
|
||
$this->assertEquals(0, $rs);
|
||
}
|
||
|
||
// 有付费套餐时,扣费失败
|
||
public function testCutdownFail()
|
||
{
|
||
$sql = "insert into `pp_mall_product` ( `id`, `add_time`, `product_type`, `product_name`, `product_key_lower`, `amount`, `expire_time_desc`, `price`, `product_key`, `product_desc`, `original_price`, `product_status`, `expire_time`) values ( '6', '2020-09-15 10:42:19', 'api_buy', '配置接口流量套餐', 'app.config.getconfig', '220', '', '2.00', 'App.Config.GetConfig', null, '0.00', '1', '2020');";
|
||
\PhalApi\DI()->notorm->demo->executeSql($sql);
|
||
|
||
$service = 'App.Config.GetConfig';
|
||
$appKey = 'test_flow';
|
||
|
||
$rs = $this->mallDomainFlow->cutdown($service, $appKey);
|
||
|
||
$this->assertEquals(-1, $rs);
|
||
}
|
||
|
||
// 有付费套餐时,增加了免费套餐,因此通过
|
||
public function testCutdownFree()
|
||
{
|
||
$sql = "insert into `pp_mall_product` ( `id`, `add_time`, `product_type`, `product_name`, `product_key_lower`, `amount`, `expire_time_desc`, `price`, `product_key`, `product_desc`, `original_price`, `product_status`, `expire_time`) values ( '7', '2020-09-15 10:42:24', 'api_free', '配置接口流量套餐', 'app.config.getconfig', '2', '', '0.00', 'App.Config.GetConfig', null, '0.00', '1', '2020');";
|
||
\PhalApi\DI()->notorm->demo->executeSql($sql);
|
||
|
||
$service = 'App.Config.GetConfig';
|
||
$appKey = 'test_flow';
|
||
|
||
// 准备一个新的app
|
||
$domain = new \Base\Domain\Apps();
|
||
$domain->addApp('测试流量', $appKey, '123', 0, 10, '', 1);
|
||
|
||
$rs = $this->mallDomainFlow->cutdown($service, $appKey);
|
||
$this->assertGreaterThan(0, $rs);
|
||
|
||
// 第2次免费
|
||
$rs = $this->mallDomainFlow->cutdown($service, $appKey);
|
||
$this->assertGreaterThan(0, $rs);
|
||
|
||
// 失败了
|
||
$rs = $this->mallDomainFlow->cutdown($service, $appKey);
|
||
$this->assertEquals(-1, $rs);
|
||
}
|
||
|
||
public function testCutdownBuy()
|
||
{
|
||
// 添加流量
|
||
$flow = array(
|
||
'username' => 'test_dev',
|
||
'product_key' => 'App.Config.GetConfig',
|
||
'product_name' => 'test_flow',
|
||
'total_amount' => 3,
|
||
'expire_time' => time() + 86400,
|
||
);
|
||
|
||
$rs = $this->mallDomainFlow->addNewFlow($flow);
|
||
|
||
$service = 'App.Config.GetConfig';
|
||
$appKey = 'test_flow';
|
||
|
||
$rs = $this->mallDomainFlow->cutdown($service, $appKey);
|
||
$this->assertGreaterThan(0, $rs);
|
||
|
||
$rs3 = $this->mallDomainFlow->cutdown($service, $appKey);
|
||
$this->assertGreaterThan(0, $rs3);
|
||
|
||
$flow3 = $this->mallDomainFlow->getFlow($rs3);
|
||
$this->assertEquals(\Mall\Model\Mall\Flow::FLOW_STATUS_USING, $flow3['flow_status']);
|
||
|
||
$rs4 = $this->mallDomainFlow->cutdown($service, $appKey);
|
||
$this->assertGreaterThan(0, $rs4);
|
||
|
||
$flow4 = $this->mallDomainFlow->getFlow($rs4);
|
||
$this->assertEquals(\Mall\Model\Mall\Flow::FLOW_STATUS_OUT, $flow4['flow_status']);
|
||
|
||
// 第4次,用光
|
||
$rs = $this->mallDomainFlow->cutdown($service, $appKey);
|
||
$this->assertEquals(-1, $rs);
|
||
}
|
||
}
|