<?php

namespace Tests\Unit;

use App\Authority;
use App\Role;
use App\User;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class MeasureMonitorTest extends TestCase
{
    public function testUserMark(){
        $userMark=factory(User::class)->create();
        $this->assertNotEmpty($userMark->id);
        return $userMark;
    }

    public function testRole(){
        $role=Role::create([
            'name'=>'测试admin'
        ]);
        $this->assertNotEmpty($role->id);
        $authorities= Authority::get();
        foreach ($authorities as $authority){
            DB::table('authority_role')->insert(['id_authority'=>$authority->id,'id_role'=>$role->id]);
        }
        return $role;
    }

    /**
     * @depends testRole
     */
    public function testUser(Role $role){
        $user=factory(User::class)->create();
        DB::table('user_role')->insert(['id_user'=>$user->id,'id_role'=>$role->id]);
        $user->touchToken();
        $this->assertNotEmpty($user->id);
        return $user;
    }

    /**
     * @depends testUserMark
     * @depends testUser
     */
    public function testIndex(User $userMark,User $user){
        $response=$this->actingAs($user)->get('package/measureMonitor');
        $response->assertOk();
        $response=$this->actingAs($userMark)->get('package/measureMonitor');
        $response->assertStatus(302)->assertRedirect('/');
    }
}
