<?php

namespace Tests\Unit;

use App\Authority;
use App\MeasuringMachine;
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 MeasuringMachineTest 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 testAddMeasuringMachine(){
        $measuringMachine=MeasuringMachine::create([
            'name'=>'tests1',
            'code'=>'testK8',
        ]);
        $this->assertNotEmpty($measuringMachine);
        return $measuringMachine;
    }


    /**
     * @depends testUser
     * @depends testAddMeasuringMachine
     * @depends testUserMark
     */
    public function testIndex(User $user,MeasuringMachine $measuringMachine,User $userMark){
        $response=$this->actingAs($user)->get('maintenance/measuringMachine');
        $response->assertOk()->assertSee('tests1');
        $responseMark=$this->actingAs($userMark)->get('maintenance/measuringMachine');
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

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

    /**
     * @depends testUser
     * @depends testUserMark
     */
    public function testStore(User $user,User $userMark){
        $response=$this->actingAs($user)->post('maintenance/measuringMachine',[
            'name'=>'testx15',
            'code'=>'testx15',
        ]);
        $response->assertStatus(302)->assertRedirect('maintenance/measuringMachine')->assertSessionHas('successTip');
        $measuringMachine=MeasuringMachine::where('code','testx15')->first();
        $result=$measuringMachine->delete();
        $this->assertTrue($result);
        $responseFalse=$this->actingAs($userMark)->post('maintenance/measuringMachine',[
            'name'=>'testx15',
            'code'=>'testx15',
        ]);
        $responseFalse->assertStatus(302)->assertRedirect('/');
    }

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

    /**
     * @depends testUser
     * @depends testAddMeasuringMachine
     * @depends testUserMark
     */
    public function testUpdate(User $user,MeasuringMachine $measuringMachine,User $userMark){
        $response=$this->actingAs($user)->put("maintenance/measuringMachine/$measuringMachine->id",[
            'name'=>'testUpdate',
            'code'=>'test',
        ]);
        $response->assertStatus(302)->assertRedirect('maintenance/measuringMachine')->assertSessionHas('successTip');
        $responseMark=$this->actingAs($userMark)->put("maintenance/measuringMachine/$measuringMachine->id",[
            'name'=>'testUpdate',
            'code'=>'test',
        ]);
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

    /**
     * @depends testUser
     * @depends testAddMeasuringMachine
     * @depends testUserMark
     */
    public function testDestroy(User $user,MeasuringMachine $measuringMachine,User $userMark){
        $response=$this->actingAs($user)->delete("maintenance/measuringMachine/$measuringMachine->id");
        $response->assertOk()->assertJson(['success'=>true]);
        $response=$this->actingAs($userMark)->delete("maintenance/measuringMachine/$measuringMachine->id");
        $response->assertStatus(302)->assertRedirect('/');
        $resultUser=$user->delete();
        $resultUserMark=$userMark->delete();
        $this->assertTrue($resultUser);
        $this->assertTrue($resultUserMark);
    }
}
