<?php

namespace Tests\Unit;

use App\Authority;
use App\CarType;
use App\Role;
use App\User;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class CarTypeTest 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 testAddCarType(){
        $carType=new CarType([
            'name'=>'欧马可',
            'model'=>'s3',
            'length'=>5.6,
            'load'=>10,
            'remark'=>'测试车'
        ]);
        $result=$carType->save();
        $this->assertTrue($result);
        $this->assertNotEmpty($carType);
        return $carType;
    }



    /**
     * @depends testAddCarType
     * @depends testUser
     * @depends testUserMark
     */
    public function testIndex(CarType $carType,User $user,User $userMark){
        $response=$this->actingAs($user)->get('maintenance/carType');
        $response->assertStatus(200)->assertSee('欧马可');
        $responseMark=$this->actingAs($userMark)->get('maintenance/carType');
        $responseMark->assertStatus(302)->assertRedirect('/');
        $carType=CarType::paginate(10);
        $this->assertNotEmpty($carType);
    }

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

    /**
     * @depends testUser
     * @depends  testUserMark
     */
    public function testStore(User $user,User $userMark){
        $response=$this->actingAs($user)->post('maintenance/carType',['CarType'=>[
            'name'=>'吉安',
            'model'=>'35',
            'length'=>3.5,
            'load'=>8,
            'remark'=>'测试添加'
        ]]);
        $response->assertStatus(302)->assertRedirect('maintenance/carType')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->post('maintenance/carType',['CarType'=>[
            'name'=>'吉安',
            'model'=>'35',
            'length'=>3.5,
            'load'=>8,
            'remark'=>'测试添加'
        ]]);
        $responseMark->assertStatus(302)->assertRedirect('/');
        $carType=CarType::where('name','=','吉安')->first();
        $result=$carType->delete();
        $this->assertTrue($result);
    }

    /**
     * @depends testAddCarType
     * @depends testUser
     * @depends  testUserMark
     */
    public function testEdit(CarType $carType,User $user,User $userMark){
        $response=$this->actingAs($user)->get("maintenance/carType/$carType->id/edit");
        $response->assertOk()->assertSee('欧马可');
        $responseMark=$this->actingAs($userMark)->get("maintenance/carType/$carType->id/edit");
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

    /**
     * @depends testAddCarType
     * @depends testUser
     * @depends  testUserMark
     */
    public function testUpdate(CarType $carType,User $user,User $userMark){
        $response=$this->actingAs($user)->put("maintenance/carType/$carType->id",['CarType'=>[
            'name'=>'东风',
            'model'=>'q7',
            'length'=>6.7,
            'load'=>18,
            'remark'=>'测试添加'
        ]]);
        $response->assertStatus(302)->assertRedirect('maintenance/carType')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->put("maintenance/carType/$carType->id",['CarType'=>[
            'name'=>'东风',
            'model'=>'q7',
            'length'=>6.7,
            'load'=>18,
            'remark'=>'测试添加'
        ]]);
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

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