<?php

namespace Tests\Unit;

use App\Authority;
use App\PaperBox;
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 PaperBoxTest 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 testAddPaperBox(){
        $paperBox=PaperBox::create([
            'model'=>'test',
            'length'=>50,
            'height'=>40,
            'width'=>30,
        ]);
        $this->assertNotEmpty($paperBox);
        return $paperBox;
    }

    /**
     * @depends testUser
     * @depends testAddPaperBox
     * @depends testUserMark
     */
    public function testIndex(User $user,PaperBox $paperBox,User $userMark){
        $model=$this->actingAs($user)->get('maintenance/paperBox/index/model');
        $owner=$this->actingAs($user)->get('maintenance/paperBox/index/owner');
        $model->assertOk()->assertSee('test');
        $owner->assertOk();
        $modelFalse=$this->actingAs($userMark)->get('maintenance/paperBox/index/model');
        $ownerFalse=$this->actingAs($userMark)->get('maintenance/paperBox/index/owner');
        $modelFalse->assertStatus(302)->assertRedirect('/');
        $ownerFalse->assertStatus(302)->assertRedirect('/');
    }

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

    /**
     * @depends testUser
     * @depends testUserMark
     */
    public function testStore(User $user,User $userMark){
        $response=$this->actingAs($user)->post('maintenance/paperBox',[
            'model'=>'测试纸箱',
            'length'=>2,
            'width'=>10,
            'height'=>80,
        ]);
        $response->assertStatus(302)->assertRedirect('maintenance/paperBox/index/model')->assertSessionHas('successTip');
        $paperBox=PaperBox::where('model','测试纸箱')->first();
        $this->assertEquals(80,$paperBox->length);
        $this->assertEquals(10,$paperBox->width);
        $this->assertEquals(2,$paperBox->height);
        $result=$paperBox->delete();
        $this->assertTrue($result);
        $responseFalse=$this->actingAs($userMark)->post('maintenance/paperBox',[
            'model'=>'测试纸箱',
            'length'=>2,
            'width'=>10,
            'height'=>80,
        ]);
        $responseFalse->assertStatus(302)->assertRedirect('/');
    }

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

    /**
     * @depends testUser
     * @depends testAddPaperBox
     * @depends testUserMark
     */
    public function testUpdate(User $user,PaperBox $paperBox,User $userMark){
        $response=$this->actingAs($user)->put("maintenance/paperBox/$paperBox->id",[
            'model'=>'test','length'=>'50','width'=>'20','height'=>'40'
        ]);
        $response->assertStatus(302)->assertRedirect('maintenance/paperBox/index/model')->assertSessionHas('successTip');
        $paperSelect=PaperBox::find($paperBox->id);
        $this->assertEquals(50,$paperSelect->length);
        $this->assertEquals(40,$paperSelect->width);
        $this->assertEquals(20,$paperSelect->height);
        $responseMark=$this->actingAs($userMark)->put("maintenance/paperBox/$paperBox->id",[
            'model'=>'test','length'=>'50','width'=>'20','height'=>'40'
        ]);
        $responseMark->assertStatus(302)->assertRedirect('/');
    }

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