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

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

class PhpUnderControl_AdminApiUser_Test extends \PHPUnit\Framework\TestCase
{
    public $adminApiUser;

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

        $this->adminApiUser = new \Admin\Api\User();
    }

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

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


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

    /**
     * @group testGetAllMembers
     */ 
    public function testGetAllMembers()
    {
        $url = 's=Admin.User.AddMember&username=testGetAllMembers&password=123456&member_type=0';
        $rs = TestRunner::go($url);

        $url = 's=Admin.User.GetAllMembers';

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

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

    /**
     * @group testDelMember
     */ 
    public function testDelMemberOK()
    {
        $url = 's=Admin.User.AddMember&username=testDelMemberOK&password=123456&member_type=0';
        $rs = TestRunner::go($url);

        $url = 's=Admin.User.DelMember&id=2';

        $rs = TestRunner::go($url);
        $this->assertTrue($rs['is_success']);
    }

    /**
     * @group testDelMember
     */ 
    public function testDelMember()
    {
        $url = 's=Admin.User.DelMember&id=100';

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

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

    public function testGetMember()
    {
        $url = 's=Admin.User.GetMember&id=1';

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

        $this->assertEquals(1, $rs['member']['id']);
    }

    /**
     * @depends testDelMember
     */
    public function testGetMemberNo()
    {
        $url = 's=Admin.User.GetMember&id=2';

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

        $this->assertEmpty($rs['member']);
    }

    public function testEditMember() {
        $url = 's=Admin.User.AddMember&username=testEditMember&password=123456&member_type=0';
        $rs = TestRunner::go($url);

        $uid = $rs['uid'];
        
        // 修改信息和密码
        $url = 's=Admin.User.EditMember&sex=1&email=xx@api.com&password=654321&id=' . $uid;
        $rs = TestRunner::go($url);
        $this->assertTrue($rs['is_update']);
        $this->assertTrue($rs['is_alter_password']);
    }

    /**
     * @expectedException PhalApi\Exception\BadRequestException
     */
    public function testEditAdminMember() {
        $url = 's=Admin.User.AddMember&username=testEditAdminMember&password=123456&member_type=200';
        $rs = TestRunner::go($url);

        $uid = $rs['uid'];
        
        // 修改信息和密码
        $url = 's=Admin.User.EditMember&sex=1&email=xx@api.com&password=654321&id=' . $uid;
        $rs = TestRunner::go($url);
    }

    /**
     * @group testAddMember
     */ 
    public function testAddMember()
    {
        $url = 's=Admin.User.AddMember&username=testAddMember&password=123456&member_type=0';

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

        $this->assertTrue($rs['is_add']);
        $this->assertGreaterThan(0, $rs['uid']);
    }

    /**
     * @group testGetDataFlow
     */ 
    public function testGetDataFlow()
    {
        $url = 's=Admin.User.GetDataFlow';

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

        $this->assertArrayHasKey('list', $rs);
    }

    /**
     * @group testGetLoginStatistics
     */ 
    public function testGetLoginStatistics()
    {
        $url = 's=Admin.User.GetLoginStatistics';

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

        $this->assertArrayHasKey('list', $rs);

    }

    /**
     * @group testAlterMemberStatus
     */ 
    public function testAlterMemberStatus()
    {
        $url = 's=Admin.User.AddMember&username=testAlterMemberStatus&password=123456&member_type=0';
        $rs = TestRunner::go($url);
        $uid = $rs['uid'];

        $url = 's=Admin.User.AlterMemberStatus&type=0&id=' . $uid;

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

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

    }

    public function testGetLastestLoginRecord()
    {
        $url = 's=Admin.User.GetLastestLoginRecord';

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

        $this->assertArrayHasKey('records', $rs);

    }

}
