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

namespace tests\App\Api;
use App\Api\Auth;
use PhalApi\Helper\TestRunner;

class PhpUnderControl_AppApiAuth_Test extends \PHPUnit\Framework\TestCase
{
    public $appApiAuth;

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

        $this->appApiAuth = new \App\Api\Auth();
    }

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

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


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

    /**
     * @group testApplyToken
     * @expectedException PhalApi\Exception\BadRequestException
     */ 
    public function testApplyTokenFailNotVerify()
    {
        // 创建app
        $domain = new \Base\Domain\Apps();
        $domain->addApp('test', 'test_not_verify', '123456');

        //Step 1. 构建请求URL
        $url = 'service=App.Auth.ApplyToken&app_key=test_not_verify&app_secret=123456&config_name=phpunit_test&config_value=2020&username=dogstar&sign=B83F9015965B68185E97792EC40FC55D';

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

    /**
     * @group testApplyToken
     */ 
    public function testApplyToken()
    {
        // 创建app
        $domain = new \Base\Domain\Apps();
        $domain->addApp('test', 'test', '123456', 0, 0, '', 1);

        //Step 1. 构建请求URL
        $url = 'service=App.Auth.ApplyToken&app_key=test&app_secret=123456&config_name=phpunit_test&config_value=2020&username=dogstar&sign=B83F9015965B68185E97792EC40FC55D';

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

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

        return $rs['access_token'];
    }

    /**
     * @depends testApplyToken
     */
    public function testRefreshAccessTokenForApp($at) {
        //Step 1. 构建请求URL
        $url = 'service=App.Auth.RefreshAccessToken&access_token='.$at;

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

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

    /**
     * @expectedException \PhalApi\Exception\BadRequestException
     */
    public function testApplyTokenWrongKey()
    {
        //Step 1. 构建请求URL
        $url = 'service=App.Auth.ApplyToken&app_key=test_404&app_secret=123456&config_name=phpunit_test&config_value=2020&username=dogstar&sign=B83F9015965B68185E97792EC40FC55D';

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

    /**
     * @ expectedException \PhalApi\Exception\BadRequestException
     */
    public function testUserLogin()
    {
        //Step 1. 构建请求URL
        $url = 'service=App.User.Register&username=test_auth&password=123456';

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

        //Step 1. 构建请求URL
        $url = 'service=App.Auth.UserLogin&username=test_auth&password=123456&app_key=test';

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

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

        return $rs['access_token'];
    }

    public function testAppUserLogin() {
        //Step 1. 构建请求URL
        $url = 'service=App.Auth.AppUserLogin&username=test_auth&password=123456&app_key=test&app_secret=123456';

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

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


    /**
     * @depends testUserLogin
     */
    //public function testRefreshAccessTokenForUser($at) {
    //    //Step 1. 构建请求URL
    //    $url = 'service=App.Auth.RefreshAccessToken&access_token='.$at;

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

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