<?php
/**
 * PhalApi_App\Common\Filter_Test
 *
 * 针对 ../src/app/Common/Filter.php App\Common\Filter 类的PHPUnit单元测试
 *
 * @author: dogstar 20191231
 */

namespace tests\App\Common;
use App\Common\Filter;

class PhpUnderControl_AppCommonFilter_Test extends \PHPUnit\Framework\TestCase
{
    public $appCommonFilter;

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

        $this->appCommonFilter = new \App\Common\Filter();

        \PhalApi\DI()->debug = false;
    }

    protected function tearDown()
    {
        \PhalApi\DI()->debug = true;

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

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


    /**
     * @group testCheck
     * @expectedException PhalApi\Exception\BadRequestException
     */ 
    public function testCheckMissAT()
    {
        $rs = $this->appCommonFilter->check();
    }

    public function testCheckOK()
    {
        $time = time();
        $config = \PhalApi\DI()->config;

        $token = array(
            "iss" => "phalapi_pro",        // 该JWT的签发者
            "aud" => "app",        // 接收该JWT的一方
            "sub" => 'test',                    // 该JWT所面向的用户
            "uid" => 1,
            "iat" => $time,                         // 在什么时候签发的
            "exp" => $time + $config->get('app.jwt.exp', 86400),        // 什么时候过期，这里是一个Unix时间戳

        );

        $key = $config->get('app.jwt.key');
        $jwt = \lmxdawn\jwt\JWT::encode($token, $key, 'HS256');

        $data = array(
            'access_token' => $jwt,
            's' => 'App.HelloWorld.Say',
        );

        \PhalApi\DI()->request = new \PhalApi\Request($data);

        $this->appCommonFilter->check();

        $this->assertEquals('test', \PhalApi\DI()->context->getAppKey());
        $this->assertEquals(1, \PhalApi\DI()->context->getUid());
    }

    /**
     * @group testCheck
     * @expectedException PhalApi\Exception\BadRequestException
     */
    public function testCheckWrongAT()
    {
        $data = array(
            'access_token' => 'ghjkghjkghj',
            's' => 'App.HelloWorld.Say',
        );

        \PhalApi\DI()->request = new \PhalApi\Request($data);

        $this->appCommonFilter->check();
    }

    /**
     * @group testCheck
     * @expectedException PhalApi\Exception\BadRequestException
     */
    public function testCheckExpireOut()
    {
        $time = time() - 86400; // 过期
        $config = \PhalApi\DI()->config;

        $token = array(
            "iss" => "phalapi_pro",        // 该JWT的签发者
            "aud" => "app",        // 接收该JWT的一方
            "sub" => 'test',                    // 该JWT所面向的用户
            "uid" => 1,
            "iat" => $time,                         // 在什么时候签发的
            "exp" => $time,
        );

        $key = $config->get('app.jwt.key');
        $jwt = \lmxdawn\jwt\JWT::encode($token, $key, 'HS256');

        $data = array(
            'access_token' => $jwt,
            's' => 'App.HelloWorld.Say',
        );

        \PhalApi\DI()->request = new \PhalApi\Request($data);

        $this->appCommonFilter->check();
    }

    /**
     * @group testCheck
     * @expectedException PhalApi\Exception\BadRequestException
     */
    public function testCheckAdmin()
    {
        $time = time();
        $config = \PhalApi\DI()->config;

        $token = array(
            "iss" => "phalapi_pro",        // 该JWT的签发者
            "aud" => "app",        // 接收该JWT的一方
            "sub" => 'test',                    // 该JWT所面向的用户
            "uid" => 1,
            "iat" => $time,                         // 在什么时候签发的
            "exp" => $time + $config->get('app.jwt.exp', 86400),        // 什么时候过期，这里是一个Unix时间戳

        );

        $key = $config->get('app.jwt.key');
        $jwt = \lmxdawn\jwt\JWT::encode($token, $key, 'HS256');

        $data = array(
            'access_token' => $jwt,
            's' => 'Admin.HelloWorld.Say', // 管理员接口
        );

        \PhalApi\DI()->request = new \PhalApi\Request($data);

        $this->appCommonFilter->check();
    }

    /**
     * @group testCheck
     * @ expectedException PhalApi\Exception\BadRequestException
     */
    public function testCheckNoAppRights()
    {
        $domain = new \Base\Domain\Rights();
        $domain->removeRights('test', 'App.HelloWorld.SayNo', 1);

        $time = time();
        $config = \PhalApi\DI()->config;

        $token = array(
            "iss" => "phalapi_pro",        // 该JWT的签发者
            "aud" => "app",        // 接收该JWT的一方
            "sub" => 'test',                    // 该JWT所面向的用户
            "uid" => 1,
            "iat" => $time,                         // 在什么时候签发的
            "exp" => $time + 100,
        );

        $key = $config->get('app.jwt.key');
        $jwt = \lmxdawn\jwt\JWT::encode($token, $key, 'HS256');

        $data = array(
            'access_token' => $jwt,
            's' => 'App.HelloWorld.SayNo', // 此接口无权限
        );

        \PhalApi\DI()->request = new \PhalApi\Request($data);

        $this->appCommonFilter->check();
    }

    public function testCheckYesAppRights()
    {
        $domain = new \Base\Domain\Rights();
        $domain->assignRights('test', 'App.HelloWorld.SayNo', 1);

        $time = time();
        $config = \PhalApi\DI()->config;

        $token = array(
            "iss" => "phalapi_pro",        // 该JWT的签发者
            "aud" => "app",        // 接收该JWT的一方
            "sub" => 'test',                    // 该JWT所面向的用户
            "uid" => 1,
            "iat" => $time,                         // 在什么时候签发的
            "exp" => $time + 100,
        );

        $key = $config->get('app.jwt.key');
        $jwt = \lmxdawn\jwt\JWT::encode($token, $key, 'HS256');

        $data = array(
            'access_token' => $jwt,
            's' => 'App.HelloWorld.SayNo', // 此接口重新又有权限
        );

        \PhalApi\DI()->request = new \PhalApi\Request($data);

        $this->appCommonFilter->check();

        $this->assertTrue(true);
    }
}
