<?php

namespace Tests\Unit;

use App\Authority;
use App\Province;
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 ProvinceTest 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 testAddProvince(){
        $province=new Province([
            'name'=>'汉城',
        ]);
        $result=$province->save();
        $this->assertTrue($result);
        $this->assertNotEmpty($province->id);
        return $province;
    }

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

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

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

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

    /**
     * @depends testAddProvince
     * @depends testUser
     * @depends testUserMark
     */
    public function testUpdate(Province $province,User $user,User $userMark){
        $response=$this->actingAs($user)->put("maintenance/province/$province->id",['Province'=>[
            'name'=>'圣都'
        ]]);
        $response->assertStatus(302)->assertRedirect('maintenance/province')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->put("maintenance/province/$province->id",['Province'=>[
            'name'=>'圣都'
        ]]);
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

    /**
     * @depends testAddProvince
     * @depends testUser
     * @depends testUserMark
     * @depends testRole
     */
    public function testDestroy(Province $province,User $user,User $userMark,Role $role){
        $response=$this->actingAs($user)->json('delete','maintenance/province/'.$province->id,['id'=>$province->id]);
        $response->assertOk()->assertJson(['success'=>true]);
        $responseMark=$this->actingAs($userMark)->json('delete','maintenance/province/'.$province->id,['id'=>$province->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();
    }
}
