<?php

namespace Tests\Unit;

use App\Authority;
use App\City;
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 CityTest 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;
    }

    public function testAddCity(){
        $city=new City([
            'name'=>'测试',
            'province_id'=>1,
        ]);
        $result=$city->save();
        $this->assertTrue($result);
        $this->assertNotEmpty($city);
        return $city;
    }

    /**
     * @depends testAddCity
     * @depends testUser
     * @depends testUserMark
     */
    public function testIndex(City $city,User $user,User $userMark){
        $response=$this->actingAs($user)->get('maintenance/city');
        $response->assertOk();
        $responseMark=$this->actingAs($userMark)->get('maintenance/city');
        $responseMark->assertStatus(302)->assertRedirect('/');
        $city=City::paginate(50);
        $this->assertNotEmpty($city);
    }

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

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

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

    /**
     * @depends testAddCity
     * @depends testUser
     * @depends testUserMark
     */
    public function testUpdate(City $city,User $user,User $userMark){
        $response=$this->actingAs($user)->put("maintenance/city/$city->id",['City'=>[
            'name'=>'测试Update',
            'province_id'=>1,
        ]]);
        $response->assertStatus(302)->assertRedirect('maintenance/city')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->put("maintenance/city/$city->id",['City'=>[
            'name'=>'测试Update',
            'province_id'=>1,
        ]]);
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

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