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

namespace tests\Admin\Api;
use Admin\Api\Apps;
use Base\Domain\Apps as AppsDomain;
use PhalApi\Helper\TestRunner;

class PhpUnderControl_AdminApiApps_Test extends \PHPUnit\Framework\TestCase
{
    public $adminApiApps;

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

        $this->adminApiApps = new \Admin\Api\Apps();
    }

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

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


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

    /**
     * @group testGetAppList
     */ 
    public function testGetAppList()
    {
        $domain = new AppsDomain();
        $domain->addApp('appName_testGetAppList', 'appKey_testGetAppList', 'appSecret_123');

        $url = 's=Admin.Apps.GetAppList';
        $rs = TestRunner::go($url);

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

    /**
     * @group testGetAppSecret
     */ 
    public function testGetAppSecret()
    {
        $domain = new AppsDomain();
        $id = $domain->addApp('appName_testGetAppSecret', 'testGetAppSecret', '123');

        $url = 's=Admin.Apps.GetAppSecret&id='.$id;
        $rs = TestRunner::go($url);

        $this->assertEquals('123', $rs['app_secret']);
    }

    /**
     * @group testAlterAppStat
     */ 
    public function testAlterAppStat()
    {
        $domain = new AppsDomain();
        $id = $domain->addApp('appName_testAlterAppStat', 'testAlterAppStat', '888');

        $url = 's=Admin.Apps.AlterAppStat&type=0&id=' . $id;
        $rs = TestRunner::go($url);

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

        $url = 's=Admin.Apps.AlterAppStat&type=1&id=' . $id;
        $rs = TestRunner::go($url);

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

    /**
     * @group testAddApp
     */ 
    public function testAddApp()
    {
        $url = 's=Admin.Apps.AddApp&app_key=testAddApp&app_secret=xxx&appName=testAddApp';
        $rs = TestRunner::go($url);

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

    /**
     * @group testCreateAppSecret
     */
    public function testCreateAppSecret()
    {
        $url = 's=Admin.Apps.CreateAppSecret';
        $rs = TestRunner::go($url);

        $this->assertNotEmpty($rs['appSecret']);
        $this->assertTrue(is_string($rs['appSecret']));
        $this->assertEquals(32, strlen($rs['appSecret']));
    }

    /**
     * @group testEditApp
     */
    public function testEditApp(){
        $domain = new AppsDomain();
        $id = $domain->addApp('phpunit', 'phpunit', 'phpunit');

        $url = "s=Admin.Apps.EditApp&id={$id}&app_name=testEditApp&app_secret=testEditApp";
        $rs = TestRunner::go($url);
        $this->assertTrue($rs['is_update']);

        $rs = $domain->getAppById($id);
        $this->assertEquals('testEditApp', $rs['app_name']);
        $this->assertEquals('testEditApp', $rs['app_secret']);

    }

}
