<?php
/**
 * PhalApi_Mall\Domain\Order_Test
 *
 * 针对 ../src/mall/Domain/Order.php Mall\Domain\Order 类的PHPUnit单元测试
 *
 * @author: dogstar 20200924
 */

namespace tests\Mall\Domain;
use Mall\Domain\Order;

class PhpUnderControl_MallDomainOrder_Test extends \PHPUnit\Framework\TestCase
{
    public $mallDomainOrder;

    protected function setUp()
    {
        parent::setUp();

        $this->mallDomainOrder = new \Mall\Domain\Order();
    }

    protected function tearDown()
    {
        // 输出本次单元测试所执行的SQL语句
        // var_dump(\PhalApi\DI()->tracer->getSqls());

        // 输出本次单元测试所涉及的追踪埋点
        // var_dump(\PhalApi\DI()->tracer->getStack());
    }


    /**
     * @group testGetOrderInfo
     */ 
    public function testGetOrderInfo()
    {
        $sql = "insert into `pp_mall_order` ( `is_deliver`, `username`, `pay_time`, `product_type`, `product_key`, `member_id`, `order_status`, `pay_type`, `third_payment_id`, `product_expire_time`, `order_id`, `id`, `product_id`, `add_time`, `num`, `product_expire_time_desc`, `note`, `product_amount`, `price`, `order_name`) values ( '0', 'admin', '2020-09-21 21:58:55', 'api_buy', 'App.BarCode.Gen', '1', '0', 'cash', '', '86400', '2020098816064072753', '140', '4', '2020-09-15 16:06:40', '4', '1个月', 'test', '9', '2.00', '条形码流量套餐');";
        \PhalApi\DI()->notorm->demo->executeSql($sql);

        $order_id = '2020098816064072753';

        $rs = $this->mallDomainOrder->getOrderInfo($order_id);

        $this->assertNotEmpty($rs);
    }

    /**
     * @group testCheckOrderPay
     */ 
    public function testCheckOrderPay()
    {
        $order_id = '2020098816064072753';

        $rs = $this->mallDomainOrder->checkOrderPay($order_id);

        $this->assertFalse($rs);
    }

    /**
     * @group testCreateOrder
     */ 
    public function testCreateOrder()
    {
        $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 ( '600', '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);

        $product_id = '600';
        $num = '2';
        $note = 'testtest';

        $rs = $this->mallDomainOrder->createOrder($product_id, $num, $note);

        $this->assertGreaterThan(0, $rs);
    }

    /**
     * @group testUpdatePayType
     */ 
    public function testUpdatePayType()
    {
        $sql = "insert into `pp_mall_order` ( `is_deliver`, `username`, `pay_time`, `product_type`, `product_key`, `member_id`, `order_status`, `pay_type`, `third_payment_id`, `product_expire_time`, `order_id`, `id`, `product_id`, `add_time`, `num`, `product_expire_time_desc`, `note`, `product_amount`, `price`, `order_name`) values ( '0', 'admin', '2020-09-21 21:58:55', 'api_buy', 'App.BarCode.Gen', '1', '0', 'paypal', '', '86400', '2020091516064072153', '15', '4', '2020-09-15 16:06:40', '4', '1个月', 'test', '9', '2.00', '条形码流量套餐');";
        \PhalApi\DI()->notorm->demo->executeSql($sql);
        $id = '15';
        $pay_type = 'cash';

        $rs = $this->mallDomainOrder->updatePayType($id, $pay_type);

        $this->assertEquals(1, $rs);
    }

    /**
     * @group testGetMyOrderList
     */ 
    public function testGetMyOrderList()
    {
        $page = '1';
        $perpage = '2';
        $uid = 1;

        $rs = $this->mallDomainOrder->getMyOrderList($page, $perpage, $uid);

        $this->assertNotEmpty($rs['items']);
    }

    /**
     * @group testUpdateOrder
     */ 
    public function testUpdateOrder()
    {
        $sql = "insert into `pp_mall_order` ( `is_deliver`, `username`, `pay_time`, `product_type`, `product_key`, `member_id`, `order_status`, `pay_type`, `third_payment_id`, `product_expire_time`, `order_id`, `id`, `product_id`, `add_time`, `num`, `product_expire_time_desc`, `note`, `product_amount`, `price`, `order_name`) values ( '0', 'admin', '2020-09-21 21:58:55', 'api_buy', 'App.BarCode.Gen', '1', '0', 'paypal', '', '86400', '2020091516064072888', '1600', '4', '2020-09-15 16:06:40', '4', '1个月', 'test', '9', '2.00', '条形码流量套餐');";
        \PhalApi\DI()->notorm->demo->executeSql($sql);

        $id = '1600';
        $order = array('order_status' => 10);
        $op = '';

        $rs = $this->mallDomainOrder->updateOrder($id, $order, $op);

        // 工人审核，发流量
        $order = array('order_status' => 20);
        $op = 'send';

        $rs = $this->mallDomainOrder->updateOrder($id, $order, $op);

        $count = \PhalApi\DI()->notorm->mall_flow->where('order_id', '2020091516064072888')->where('flow_status', 10)->count();
        $this->assertGreaterThan(0, $count);

        // 退款，回收流量
        $order = array('order_status' => 30);
        $op = 'back';

        $rs = $this->mallDomainOrder->updateOrder($id, $order, $op);

        $count = \PhalApi\DI()->notorm->mall_flow->where('order_id', '2020091516064072888')->where('flow_status', 0)->count();
        $this->assertGreaterThan(0, $count);
    }

    /**
     * @group testGetOrder
     */ 
    public function testGetOrder()
    {
        $id = '140';

        $rs = $this->mallDomainOrder->getOrder($id);

        $this->assertNotEmpty($rs);
    }

    /**
     * @group testDeleteOrder
     */ 
    public function testDeleteOrder()
    {
        $id = '400';

        $rs = $this->mallDomainOrder->deleteOrder($id);
    }

    /**
     * @group testGetOrderList
     */ 
    public function testGetOrderList()
    {
        $page = '1';
        $perpage = '10';

        $rs = $this->mallDomainOrder->getOrderList($page, $perpage);

        $this->assertNotEmpty($rs['items']);
    }

    /**
     * @group testGetOrderStatusList
     */ 
    public function testGetOrderStatusList()
    {
        $isMap = true;

        $rs = $this->mallDomainOrder->getOrderStatusList($isMap);

        $this->assertNotEmpty($rs);
    }

}
