<?php
/**
 * PhalApi_App\Api\User_Test
 *
 * 针对 ../src/app/Api/User.php App\Api\User 类的PHPUnit单元测试
 *
 * @author: dogstar 20191219
 */

namespace tests\Platform\Api;
use Platform\Api\User;
use PhalApi\Helper\TestRunner;

class PhpUnderControl_PlatformApiUser_Test extends \PHPUnit\Framework\TestCase
{
    public $appApiUser;

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

        $this->appApiUser = new \Platform\Api\User();
    }

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

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


    /**
     * @group testGetRules
     */ 
    public function testGetRules()
    {
        $rs = $this->appApiUser->getRules();
        $this->assertTrue(is_array($rs));
    }

    /**
     * @group testRegister
     */ 
    public function testRegister()
    {
        //Step 1. 构建请求URL
        $url = 'service=Platform.User.Register&username=test_dev&password=123456&member_type=100';

        //Step 2. 执行请求	
        $rs = TestRunner::go($url);

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertTrue($rs['is_register']);
        $this->assertGreaterThan(0, $rs['uid']);
    }

    /**
     * @group testRegister
     * @expectedException PhalApi\Exception\BadRequestException
     */ 
    public function testRegisterFailType()
    {
        //Step 1. 构建请求URL
        $url = 'service=Platform.User.Register&username=test_dev&password=123456&member_type=98';

        //Step 2. 执行请求	
        $rs = TestRunner::go($url);
    }

    /**
     * @group testAlterPass
     */ 
    public function testAlterPass()
    {
        // \PhalApi\DI()->context->setUid(1);
        
        //Step 1. 构建请求URL
        $url = 'service=Platform.User.AlterPass&username=test_dev&password=123456&new_password=654321&_uid=1&access_token=123';

        //Step 2. 执行请求	
        $rs = TestRunner::go($url);

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertTrue($rs['is_alter']);

        //Step 1. 构建请求URL
        $url = 'service=Platform.User.AlterPass&username=test_dev&password=654321&new_password=123456&_uid=1&access_token=123';

        //Step 2. 执行请求	
        $rs = TestRunner::go($url);

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertTrue($rs['is_alter']);
    }

    /**
     * @group testProfile
     */ 
    public function testUserProfile()
    {
        //Step 1. 构建请求URL
        $url = 'service=Platform.User.UserProfile&username=test_dev&access_token=123';

        //Step 2. 执行请求	
        $rs = TestRunner::go($url);

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertNotEmpty($rs['profile']);
        $this->assertNotEmpty($rs['profile']['id']);
    }

    /**
     * @group testUpdateProfile
     */ 
    public function testUpdateProfile()
    {
        // \PhalApi\DI()->context->setUid(1);
        
        //Step 1. 构建请求URL
        $url = 'service=Platform.User.UpdateProfile&access_token=123&username=test_dev&_uid=1&mobile='.time();

        //Step 2. 执行请求	
        $rs = TestRunner::go($url);

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertTrue($rs['is_update']);
    }

}
