<?php

namespace Tests\Unit;

use App\Authority;
use App\WaybillPriceModel;
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 WaybillPriceModelTest 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 testAddWaybillPriceModel(){
        $waybillPriceModel=new WaybillPriceModel([
            'logistic_id'=>1,
            'province_id'=>1,
            'city_id'=>1,
            'unit_id'=>1,
            'range_min'=>0,
            'range_max'=>5000,
            'unit_price'=>1,
            'initial_weight'=>10
        ]);
        $result=$waybillPriceModel->save();
        $this->assertTrue($result);
        $this->assertNotEmpty($waybillPriceModel);
        return $waybillPriceModel;
    }

    /**
     * @depends testAddWaybillPriceModel
     * @depends testUser
     * @depends testUserMark
     */
    public function testIndex(WaybillPriceModel $waybillPriceModel,User $user,User $userMark){
        $response=$this->actingAs($user)->get('maintenance/waybillPriceModel');
        $response->assertStatus(200)->assertSee('5000');
        $responseMark=$this->actingAs($userMark)->get('maintenance/waybillPriceModel');
        $responseMark->assertStatus(302)->assertRedirect('/');
        $waybillPriceModel=WaybillPriceModel::paginate(50);
        $this->assertNotEmpty($waybillPriceModel);
    }

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


    /**
     * @depends testUser
     * @depends testUserMark
     */
    public function testStore(User $user,User $userMark){
        $response=$this->actingAs($user)->post('maintenance/waybillPriceModel',['WaybillPriceModel'=>[
            'logistic_id'=>12,
            'province_id'=>4,
            'city_id'=>1,
            'unit_id'=>1,
            'range_min'=>0,
            'range_max'=>1000,
            'unit_price'=>1,
            'base_fee'=>null,
            'initial_weight'=>10
        ]]);
        $response->assertStatus(302)->assertRedirect('maintenance/waybillPriceModel')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->post('maintenance/waybillPriceModel',['WaybillPriceModel'=>[
            'logistic_id'=>12,
            'province_id'=>4,
            'city_id'=>3,
            'unit_id'=>3,
            'range_min'=>0,
            'range_max'=>1000,
            'unit_price'=>1,
            'base_fee'=>null,
            'initial_weight'=>10
        ]]);
        $responseMark->assertStatus(302)->assertRedirect('/');
        $waybillPriceModel=WaybillPriceModel::whereRaw('logistic_id = ? and province_id = ? and city_id=? and unit_id=? and range_min=? and range_max=? and unit_price=? and initial_weight=?',[
            12,4,1,1,0.00,1000.00,0.01,10.00
        ])->first();
        $result=$waybillPriceModel->delete();
        $this->assertTrue($result);

    }

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

    /**
     * @depends testAddWaybillPriceModel
     * @depends testUser
     * @depends testUserMark
     */
    public function testUpdate(WaybillPriceModel $waybillPriceModel,User $user,User $userMark){
        $response=$this->actingAs($user)->put('maintenance/waybillPriceModel/'.$waybillPriceModel->id,['WaybillPriceModel'=>[
            'logistic_id'=>2,
            'province_id'=>2,
            'city_id'=>11,
            'unit_id'=>1,
            'range_min'=>500,
            'range_max'=>8000,
            'unit_price'=>2,
            'base_fee'=>null,
            'initial_weight'=>5
        ]]);
        $response->assertStatus(302)->assertRedirect('maintenance/waybillPriceModel')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->put('maintenance/waybillPriceModel/'.$waybillPriceModel->id,['WaybillPriceModel'=>[
            'logistic_id'=>2,
            'province_id'=>2,
            'city_id'=>11,
            'unit_id'=>2,
            'range_min'=>500,
            'range_max'=>8000,
            'unit_price'=>2,
            'base_fee'=>null,
            'initial_weight'=>5
        ]]);
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

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

}
