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

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

class PhpUnderControl_AppApiConfig_Test extends \PHPUnit\Framework\TestCase
{
    public $appApiConfig;

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

        $this->appApiConfig = new \Admin\Api\Config();
    }

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

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


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

    /**
     * @group testAddConfig
     */ 
    public function testAddConfig()
    {
        //Step 1. 构建请求URL
        $url = 'service=Admin.Config.AddConfig&config_name=phpunit_test&config_value=2019&username=dogstar&sign=B83F9015965B68185E97792EC40FC55D&app_key=CEE4B8A091578B252AC4C92FB4E893C3';

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

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertEquals(true, $rs['is_add']);
    }

    /**
     * @group testGetConfig
     */ 
    public function testGetConfig()
    {
        //Step 1. 构建请求URL
        $url = 'service=Admin.Config.GetConfig&config_name=phpunit_test&config_value=2019&username=dogstar&sign=B83F9015965B68185E97792EC40FC55D&app_key=CEE4B8A091578B252AC4C92FB4E893C3';

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

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertEquals('2019', $rs['config_value']);
    }

    public function testGetConfigNotExists()
    {
        //Step 1. 构建请求URL
        $url = 'service=Admin.Config.GetConfig&config_name=phpunit_test_404&config_value=2019&username=dogstar&sign=B83F9015965B68185E97792EC40FC55D&app_key=CEE4B8A091578B252AC4C92FB4E893C3';

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

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertSame(null, $rs['config_value']);
    }

    /**
     * @group testEditConfig
     */ 
    public function testEditConfig()
    {
        //Step 1. 构建请求URL
        $url = 'service=Admin.Config.EditConfig&config_name=phpunit_test&config_value=2020&username=dogstar&sign=B83F9015965B68185E97792EC40FC55D&app_key=CEE4B8A091578B252AC4C92FB4E893C3';

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

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertEquals(true, $rs['is_edit']);

        $url = 'service=Admin.Config.GetConfig&config_name=phpunit_test&config_value=2019&username=dogstar&sign=B83F9015965B68185E97792EC40FC55D&app_key=CEE4B8A091578B252AC4C92FB4E893C3';
        $rs = TestRunner::go($url);
        $this->assertEquals('2020', $rs['config_value']);
    }

    /**
     * @group testRemoveConfig
     */ 
    public function testRemoveConfig()
    {
        $url = 'service=Admin.Config.RemoveConfig&config_name=phpunit_test&config_value=2019&username=dogstar&sign=B83F9015965B68185E97792EC40FC55D&app_key=CEE4B8A091578B252AC4C92FB4E893C3';
        $rs = TestRunner::go($url);
        $this->assertEquals(true, $rs['is_remove']);
    }

}
