<?php

namespace Tests\Unit;

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

class PackageTest 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 testAddPackage(){
        $package=Package::create([
            'logistic_number'=>'test214',
            ',type'=>'普通波次',
            'weight'=>20,
            'length'=>38,
            'width'=>30,
            'height'=>25,
            'owner_id'=>'1',
            'paper_box_id'=>'1',
            'measuring_machine_id'=>'1',
            'recipient'=>'Test浏览量',
            'status'=>'无',
        ]);
        $this->assertNotEmpty($package);
        return $package;
    }

    /**
     * @depends testUser
     * @depends testUserMark
     * @depends testAddPackage
     */
    public function testIndex(User $user,User $userMark,Package $package){
        $response=$this->actingAs($user)->get('package');
        $response->assertOk()->assertSee('Test浏览量');
        $response=$this->actingAs($userMark)->get('package');
        $response->assertStatus(302)->assertRedirect('/');
    }

    /**
     * @depends testUser
     * @depends testUserMark
     * @depends testAddPackage
     */
    public function testDestroy(User $user,User $userMark,Package $package){
        $resultUser=$user->delete();
        $resultUserMark=$userMark->delete();
        $resultPackage=$package->delete();
        $this->assertTrue($resultUser);
        $this->assertTrue($resultUserMark);
        $this->assertTrue($resultPackage);
    }

}
