<?php

namespace Tests\Unit;

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

class UnitTest 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]);
        $this->assertNotEmpty($user->id);
        return $user;
    }

    public function testAddUnit(){
        $unit=new Unit([
            'name'=>'cm',
        ]);
        $result=$unit->save();
        $this->assertTrue($result);
        $this->assertNotEmpty($unit);
        return $unit;
    }

    /**
     * @depends testAddUnit
     * @depends testUser
     * @depends testUserMark
     */
    public function testIndex(Unit $unit,User $user,User $userMark){
        $response=$this->actingAs($user)->get('maintenance/unit');
        $response->assertOk()->assertSee('cm');
        $responseMark=$this->actingAs($userMark)->get('maintenance/unit');
        $responseMark->assertStatus(302)->assertRedirect('/');
        $unit=Unit::paginate(10);
        $this->assertNotEmpty($unit);
}

    /**
     * @depends testUser
     * @depends testUserMark
     */
    public function testCreate(User $user,User $userMark){
        $response=$this->actingAs($user)->get('maintenance/unit/create');
        $response->assertOk()->assertSee('单位名称');
        $responseMark=$this->actingAs($userMark)->get('maintenance/unit/create');
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

    /**
     * @depends testUser
     * @depends testUserMark
     */
    public function testStore(User $user,User $userMark){
        $response=$this->actingAs($user)->post('maintenance/unit',['Unit'=>[
            'name'=>'g',
        ]]);
        $response->assertStatus(302)->assertRedirect('maintenance/unit')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->post('maintenance/unit',['Unit'=>[
            'name'=>'g',
        ]]);
        $responseMark->assertStatus(302)->assertRedirect('/');
        $unit=Unit::where('name','=','g')->first();
        $result=$unit->delete();
        $this->assertTrue($result);
    }

    /**
     * @depends testAddUnit
     * @depends testUser
     * @depends testUserMark
     */
    public function testEdit(Unit $unit,User $user,User $userMark){
        $response=$this->actingAs($user)->get("maintenance/unit/$unit->id/edit");
        $response->assertOk()->assertSee('cm');
        $responseMark=$this->actingAs($userMark)->get("maintenance/unit/$unit->id/edit");
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

    /**
     * @depends testAddUnit
     * @depends testUser
     * @depends testUserMark
     */
    public function testUpdate(Unit $unit,User $user,User $userMark){
        $response=$this->actingAs($user)->put("maintenance/unit/$unit->id",['Unit'=>[
            'name'=>'千克'
        ]]);
        $response->assertStatus(302)->assertRedirect('maintenance/unit')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->put("maintenance/unit/$unit->id",['Unit'=>[
            'name'=>'千克'
        ]]);
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

    /**
     * @depends testAddUnit
     * @depends testUser
     * @depends testUserMark
     * @depends testRole
     */
    public function testDestroy(Unit $unit,User $user,User $userMark,Role $role){
        $response=$this->actingAs($user)->json('delete','maintenance/unit/'.$unit->id,['id'=>$unit->id]);
        $response->assertOk()->assertJson(['success'=>true]);
        $responseMark=$this->actingAs($userMark)->json('delete','maintenance/unit/'.$unit->id,['id'=>$unit->id]);
        $responseMark->assertStatus(302)->assertRedirect('/');
        $result=$user->delete();
        $this->assertTrue($result);
        $this->assertNotEmpty($userMark->id);
        $resultUserMark=$userMark->delete();
        $this->assertTrue($resultUserMark);
        $resultRole=$role->delete();
        $this->assertTrue($resultRole);
        DB::table('authority_role')->where('id_role','=',$role->id)->delete();
        DB::table('user_role')->where('id_role','=',$role->id)->delete();
    }
}
