<?php
/**
 * PhalApi_Admin\Api\Rights_Test
 *
 * 针对 ./src/admin/Api/Rights.php Admin\Api\Rights 类的PHPUnit单元测试
 *
 * @author: dogstar 20200113
 */

namespace tests\Admin\Api;
use Admin\Api\Rights;
use PhalApi\Helper\TestRunner;

class PhpUnderControl_AdminApiRights_Test extends \PHPUnit\Framework\TestCase
{
    public $adminApiRights;

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

        $this->adminApiRights = new \Admin\Api\Rights();
    }

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

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


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

    /**
     * @group testGetAllAppApis
     */ 
    public function testGetAllAppApis()
    {

        $url = "s=Admin.Rights.GetAllAppApis";

        @$rs = TestRunner::go($url);

        $this->assertNotEmpty($rs);

        $this->assertGreaterThanOrEqual(0, $rs['total']);

    }

    /**
     * @group testAssignRights
     */ 
    public function testAssignRights()
    {
        $url = "s=Admin.Rights.AssignRights&appKey=123&assign_service=App.Site.Index";

        $rs = TestRunner::go($url);

        $this->assertEquals($rs, array('service' => array("App.Site.Index")));

    }

    /**
     * @group testRemoveRights
     */ 
    public function testRemoveRights()
    {

        $domain = new \Base\Domain\Rights();
        $domain->assignRights('123', 'App.Site.Index');

        $url = "s=Admin.Rights.RemoveRights&remove_service=App.Site.Index";

        $rs = TestRunner::go($url);

        $this->assertEquals($rs, array('service' => array("App.Site.Index")));

    }

    public function testaddRightsService()
    {
        $url = "s=Admin.Rights.addRightsService&service_desc=xxx&rights_service=Mine.Site.Index";

        $rs = TestRunner::go($url);

        $this->assertTrue($rs['is_add']);

        $rs = TestRunner::go($url);

        $this->assertFalse($rs['is_add']);

    }

    public function testRemoveRightsService()
    {
        $url = "s=Admin.Rights.RemoveRightsService&service_desc=xxx&rights_service=Mine.Site.Index";

        $rs = TestRunner::go($url);

        $this->assertTrue($rs['is_remove']);

    }

    public function testListAllRightsService()
    {
        $url = "s=Admin.Rights.addRightsService&service_desc=xxx&rights_service=Mine.Site.Index222";
        $rs = TestRunner::go($url);
        $url = "s=Admin.Rights.addRightsService&service_desc=xxx&rights_service=Mine.Site.Index333";
        $rs = TestRunner::go($url);

        $url = "s=Admin.Rights.ListAllRightsService";

        $rs = TestRunner::go($url);
        // var_dump($rs);

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

    }

    public function testassignRightsForDevTypeOrNot() {
        $url = "s=Admin.Rights.assignRightsForDevTypeOrNot&rights_service=Mine.Site.Index222&member_type=100";

        $rs = TestRunner::go($url);
        // var_dump($rs);

        $this->assertTrue($rs['is_allow']);

        $url = "s=Admin.Rights.assignRightsForDevTypeOrNot&rights_service=Mine.Site.Index222&member_type=100";

        $rs = TestRunner::go($url);
        // var_dump($rs);

        $this->assertFalse($rs['is_allow']);
    }

    public function testListAllRightsForDeveloperType()
    {
        $url = "s=Admin.Rights.ListAllRightsForDeveloperType";

        $rs = TestRunner::go($url);
        // var_dump($rs);

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

    }

    public function testsearchRightsList()
    {
        $url = "s=Admin.Rights.searchRightsList&member_type=100&user_id=9";

        $rs = TestRunner::go($url);
        // var_dump($rs);

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


        $url = "s=Admin.Rights.searchRightsList";

        $rs = TestRunner::go($url);
        // var_dump($rs);

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

    }

    public function testassignRightsOrNot()
    {
        $url = "s=Admin.Rights.assignRightsOrNot&id=1";

        $rs = TestRunner::go($url);

        $this->assertTrue(true);
    }

    public function testcreateNewRightsRuleRepeat() {
        $url = "s=Admin.Rights.createNewRightsRule&rights_service=App.xx.yy&member_type=100&is_allow=1";
        $rs = TestRunner::go($url);
        $this->assertTrue($rs['is_add']);
    }

    /**
     * @expectedException PhalApi\Exception\BadRequestException
     */
    public function testcreateNewRightsRule() {
        $url = "s=Admin.Rights.createNewRightsRule&rights_service=App.xx.yy&member_type=100&is_allow=1";

        $rs = TestRunner::go($url);
    }

    /**
     * @expectedException PhalApi\Exception\BadRequestException
     */
    public function testgetDevAppApis()
    {
        $url = "s=Admin.Rights.getDevAppApis&app_key=123";

        $rs = TestRunner::go($url);

        $this->assertTrue(true);
    }

}
