<?php

namespace Tests\Unit;

use App\Authority;
use App\Role;
use App\User;
use App\WaybillFinancialExcepted;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class WaybillFinancialExceptedTest 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 testAdd(){
        $waybillFinancialExcepted=new WaybillFinancialExcepted([
              'waybill_id'=>1,
             'json_content'=>"{'waybill_number':'BSZX5416854985'}"
        ]);
        $result=$waybillFinancialExcepted->save();
        $this->assertTrue($result);
        return $waybillFinancialExcepted;
    }

    /**
     * @depends testAdd
     * @depends testUser
     * @depends testUserMark
     */
    public function testIndex(WaybillFinancialExcepted $waybillFinancialExcepted, User $user,User $userMark){
        $response=$this->actingAs($user)->get('waybill/waybillFinancialExcepted');
        $response->assertOk()->assertSee('1');
        $responseMark=$this->actingAs($userMark)->get('waybill/waybillFinancialExcepted');
        $responseMark->assertStatus(302)->assertRedirect('/');
        $waybillFinancialExcepted=WaybillFinancialExcepted::paginate(10);
        $this->assertNotEmpty($waybillFinancialExcepted);
    }

    /**
     * @depends testAdd
     * @depends testUser
     * @depends testUserMark
     * @depends testRole
     */
    public function testDestroy(WaybillFinancialExcepted $waybillFinancialExcepted,User $user,User $userMark,Role $role){
        $waybillFinancialExcepted->delete();
        $result=WaybillFinancialExcepted::find($waybillFinancialExcepted->id);
        $this->assertEmpty($result);
        $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();
    }
}
