<?php
/**
 * PhalApi_Base\Domain\Rights_Test
 *
 * 针对 ../src/app/Domain/Rights.php Base\Domain\Rights 类的PHPUnit单元测试
 *
 * @author: dogstar 20200408
 */

namespace tests\Base\Domain;
use Base\Domain\Rights;

class PhpUnderControl_AppDomainRights_Test extends \PHPUnit\Framework\TestCase
{
    public $appDomainRights;

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

        $this->appDomainRights = new \Base\Domain\Rights();
    }

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

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


    /**
     * @group testCheckAppRights
     */ 
    public function testCheckAppRights()
    {
        $appKey = 'xxxx';
        $service = 'App.HH.YY';

        $rs = $this->appDomainRights->checkAppRights($appKey, $service);

        $this->assertTrue($rs);
    }

    // 整个流程的测试，从创建开发者账号、创建应用，到分配接口权限，检测权限，步步为营
    public function testCheckAppRightsAllInOne()
    {
        $appKey = 'rights_app_key';
        $service = 'App.AAA.BBBRights';

        // 往回走，最终是true，测试配置
        $rs = $this->appDomainRights->checkAppRights($appKey, $service);
        $this->assertTrue($rs);

        // 账号
        $userDomain = new \Base\Domain\User();
        $uid = $userDomain->register('rights_user', '123456', '', '', '', 0, '', 100);
        // 应用
        $appDomain = new \Base\Domain\Apps();
        $appDomain->addApp('test', $appKey, 'hjkghjhj', 0, $uid, '', 1);

        // 账号类型权限-开
        $this->appDomainRights->assignRightsForDevTypeOrNot($service, 100);
        $rs = $this->appDomainRights->checkAppRights($appKey, $service);
        $this->assertTrue($rs);
        // 账号类型权限-关
        $this->appDomainRights->assignRightsForDevTypeOrNot($service, 100);
        $rs = $this->appDomainRights->checkAppRights($appKey, $service);
        $this->assertFalse($rs);

        // 开发者账号权限-开
        $this->appDomainRights->createNewRightsRule($service, 0, $uid, '', true);
        $rs = $this->appDomainRights->checkAppRights($appKey, $service);
        $this->assertTrue($rs);
        // 开发者-关

        // 最后，app_key-关
        $this->appDomainRights->createNewRightsRule($service, 0, 0, $appKey, false);
        $rs = $this->appDomainRights->checkAppRights($appKey, $service);
        $this->assertFalse($rs);
    }

    /**
     * @group testAssignRights
     */ 
    //public function testAssignRights()
    //{
    //    $appKey = '';
    //    $service = '';

    //    $rs = $this->appDomainRights->assignRights($appKey, $service);
    //}

    ///**
    // * @group testRemoveRights
    // */ 
    //public function testRemoveRights()
    //{
    //    $appKey = '';
    //    $service = '';
    //    $adminUid = null;

    //    $rs = $this->appDomainRights->removeRights($appKey, $service, $adminUid);
    //}

    /**
     * @group testGetRightsForApp
     */ 
    //public function testGetRightsForApp()
    //{
    //    $appKey = 'xxxx';

    //    $rs = $this->appDomainRights->getRightsForApp($appKey);
    //}

    /**
     * @group testListAllRightsForDeveloperType
     */ 
    public function testListAllRightsForDeveloperType()
    {
        $rs = $this->appDomainRights->listAllRightsForDeveloperType();

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

    /**
     * @group testGetRightsForDevApp
     */ 
    public function testGetRightsForDevApp()
    {
        $uid = '1';
        $appKey = 'xxx';

        $rs = $this->appDomainRights->getRightsForDevApp($uid, $appKey);

        $this->assertTrue(is_array($rs));
    }

    /**
     * @group testAssignRightsForDevTypeOrNot
     */ 
    public function testAssignRightsForDevTypeOrNot()
    {
        $rightsService = 'App.KK.LL';
        $memberType = 100;

        $rs = $this->appDomainRights->assignRightsForDevTypeOrNot($rightsService, $memberType);

        $this->assertTrue(true);
    }

    /**
     * @group testSearchRightsList
     */ 
    public function testSearchRightsList()
    {
        $rightsService = 'App';
        $memberType = 100;
        $userId = 1;
        $appKey = 'abc';
        $page = 1;
        $perpage = 5;

        $rs = $this->appDomainRights->searchRightsList($rightsService, $memberType, $userId, $appKey, $page, $perpage);

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

    /**
     * @group testAssignRightsOrNot
     */ 
    public function testAssignRightsOrNot()
    {
        $id = '1';

        $rs = $this->appDomainRights->assignRightsOrNot($id);

         $this->assertTrue(true);
    }

    /**
     * @group testDelelteRightsRule
     */ 
    public function testDelelteRightsRule()
    {
        $id = '9999';

        $rs = $this->appDomainRights->delelteRightsRule($id);

        $this->assertTrue(true);
    }

    /**
     * @group testCreateNewRightsRule
     */ 
    public function testCreateNewRightsRule()
    {
        $rightsService = 'App.xxxxx.yyyyyy';
        $memberType = 100;
        $userId = 0;
        $appKey = '';
        $isAllow = true;

        $rs = $this->appDomainRights->createNewRightsRule($rightsService, $memberType, $userId, $appKey, $isAllow);
    }

}
