<?php

namespace Tests\Unit;

use App\Authority;
use App\Carrier;
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 CarrierTest 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 testAddCarrier(){

        $carrier=new Carrier([
            'name'=>'货拉拉',
            'mobile'=>'18601677143',
            'remark'=>'测试用例'
        ]);
        $result=$carrier->save();
        $this->assertTrue($result);
        $this->assertNotEmpty($carrier);
        return $carrier;
    }

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

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

    /**
     * @depends  testUser
     * @depends  testUserMark
     */
    public function testStore(User $user,User $userMark){
        $response=$this->actingAs($user)->post('maintenance/carrier',['Carrier'=>[
            'name'=>'德邦',
            'mobile'=>'18452365478',
            'remark'=>'测试'
        ]]);
        $response->assertStatus(302)->assertRedirect('maintenance/carrier')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->post('maintenance/carrier',['Carrier'=>[
            'name'=>'德邦',
            'mobile'=>'18452365478',
            'remark'=>'测试'
        ]]);
        $responseMark->assertStatus(302)->assertRedirect('/');
        $carrier=Carrier::where('name','=','德邦')->first();
        $result=$carrier->delete();
        $this->assertTrue($result);
    }

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

    /**
     * @depends  testUser
     * @depends  testAddCarrier
     * @depends  testUserMark
     */
    public function testUpdate(User $user,Carrier $carrier,User $userMark){
        $response=$this->actingAs($user)->put("maintenance/carrier/$carrier->id",['Carrier'=>[
            'name'=>'辉腾',
            'mobile'=>'021587468547',
            'remark'=>'测试更改'
        ]]);
        $response->assertStatus(302)->assertRedirect('maintenance/carrier')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->put("maintenance/carrier/$carrier->id",['Carrier'=>[
            'name'=>'辉腾',
            'mobile'=>'021587468547',
            'remark'=>'测试更改'
        ]]);
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

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

}
