<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/7/14
 * Time: 下午4:22
 */

define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));

require_once(BASE_ROOT_PATH . '/fooder.php');
require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');
require_once(BASE_ROOT_PATH . '/helper/message/publisher.php');


class TestFriends extends PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass()
    {
        Base::run_util();
    }

    public function testAddFollow()
    {
        $result = search\relation_client::instance()->add_follow(array('user_id' => 36490,'friends'=>[1,2,3,4,5]));
        $result = search\relation_client::instance()->add_follow(array('user_id' => 36490,'friends'=>[3,4,5,6,7]));
    }

    public function testDelFollow()
    {
        $result = search\relation_client::instance()->del_follow(array('user_id' => 36490,'friends'=>[1]));
        $result = search\relation_client::instance()->del_follow(array('user_id' => 36490,'friends'=>[3,4]));
    }

    public function testFetchFollow()
    {
        for ($i =0 ; $i < 10000; ++$i) {
            $result = search\relation_client::instance()->fetch_follow(array('user_id' => 36490));
        }
    }

    public function testAddSpecial()
    {
        $result = search\relation_client::instance()->add_special(array('user_id' => 36490,'specials'=>array(['type' => 0,'spid' => 7])));
        $result = search\relation_client::instance()->add_special(array('user_id' => 36490,'specials'=>array(['type' => 0,'spid' => 6])));
        $result = search\relation_client::instance()->add_special(array('user_id' => 36490,'specials'=>array(['type' => 0,'spid' => 9])));
        $result = search\relation_client::instance()->add_special(array('user_id' => 36490,'specials'=>array(['type' => 0,'spid' => 17])));
        $result = search\relation_client::instance()->add_special(array('user_id' => 36490,'specials'=>array(['type' => 0,'spid' => 16])));
        $result = search\relation_client::instance()->add_special(array('user_id' => 36490,'specials'=>array(['type' => 0,'spid' => 19])));
    }

    public function testDelSpecial()
    {
        $result = search\relation_client::instance()->add_special(array('user_id' => 36490,'specials'=>array(['type' => 0,'spid' => 9])));
        $result = search\relation_client::instance()->del_special(array('user_id' => 36490,'specials'=>array(['type' => 0,'spid' => 9])));
    }

    public function testFetchPub()
    {
        $result = search\relation_client::instance()->fetch_pub_special(['special_id' => -1,'count'=> 1000,'type' => 1]);


        for ($i =0 ; $i < 10000; ++$i)
        {
            $result = search\relation_client::instance()->fetch_pub_special(['special_id' => 11,'count'=> 10,'type' => 1]);
            $result = search\relation_client::instance()->fetch_pub_special(['special_id' => 11,'count'=> 10,'type' => 0]);
            $result = search\relation_client::instance()->fetch_pub_special(['special_id' => 9,'count'=> 10,'type' => 0]);
            $result = search\relation_client::instance()->fetch_pub_special(['special_id' => 9,'count'=> 10,'type' => 1]);
        }
    }
    //publisher
    public function testAddFriend()
    {
        $publisher = new message\publisher();
        $publisher->add_follow(36490,[1,2,3,57]);
    }
    public function testDelFriend()
    {
        $publisher = new message\publisher();
        $publisher->del_follow(36490,[1,2,3,57]);
    }
    public function testAddSpecials()
    {
        $publisher = new message\publisher();
        $publisher->add_special(36490,array(['type' => 1, 'spid' =>1],['type' => 1,'spid' => 2],['type' => 1,'spid' => 3]));
    }
    public function testDelSpecials()
    {
        $publisher = new message\publisher();
        $publisher->del_special(36490,array(['type' => 1, 'spid' =>1],['type' => 1,'spid' => 2],['type' => 1,'spid' => 3]));
    }
    public function testAddInviter()
    {
//        search\relation_client::instance()->add_inviter(['user_id' => 39638,'inviter_id' => 39623]);
//        $result = search\relation_client::instance()->fetch_inviters(['user_id' => 39633]);
        $publisher = new message\publisher();
        $publisher->add_inviter(100,39623);
        $publisher->add_inviter(39634,39623);
        $publisher->add_inviter(39635,39623);
        $publisher->add_inviter(39636,39623);
        $publisher->add_inviter(39637,39623);
        $publisher->add_inviter(39639,39623);
        $result = search\relation_client::instance()->fetch_invitees(['user_id' => 39623]);

    }

    public function testFetchInviters()
    {
        $mod_member = Model('member');
        $items = $mod_member->where(['inviter_id' => ['gt',0]])->field('*')->limit(false)->select();

        foreach ($items as $item)
        {
            $user = intval($item['member_id']);
            $inviters = search\relation_client::instance()->fetch_inviters(['user_id' => $user]);
            $invitees = search\relation_client::instance()->fetch_invitees(['user_id' => $user]);
            echo implode(",",$inviters);
            echo "\n";
            echo implode("_",$invitees);
            echo "\n";
        }
    }

    public function testFollows()
    {
        $iRelation = new \relation\mem_relation(39623);
        $subscriber = $iRelation->subscriber();
        $follower = $iRelation->follower();

        $inviters = search\relation_client::instance()->fetch_follow(['user_id' => 39623]);
        $specials = search\relation_client::instance()->fetch_pri_special(['user_id' => 39623,'special_id' => -1,'count'=> 100,'type' => 1]);
        $specials_follow = search\relation_client::instance()->fetch_self_special(['user_id' => 39646,'special_id' => -1,'count'=> 100,'type' => 1]);
        $inviters = search\relation_client::instance()->fetch_follow(['user_id' => 39646]);
    }
}
