<?php


namespace Tests\Inventory\Http\InventoryController;


use App\Authority;
use App\Commodity;
use App\InventoryAccount;
use App\InventoryAccountMission;
use App\Owner;
use App\Role;
use App\Services\InventoryAccountService;
use App\Services\UnitService;
use App\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Tests\TestCase;

class InventoryControllerEnterStockInventoryMissionTestTest extends TestCase
{
    public $response=null;
    public $role;
    public $user;
    public $inventory;
    public $inventoryMissions;
    function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        if(!$this->user){
            $this->user = factory(User::class)->create();
            $this->role= Role::firstOrCreate([
                'name'=>'testRole',
            ]);
            $this->assertNotEmpty($this->role->id);
            $authority= Authority::where('name','库存管理-盘点')->first();
            DB::table('authority_role')->insert(['id_authority'=>$authority->id,'id_role'=>$this->role->id]);
            DB::table('user_role')->insert(['id_user'=>$this->user->id,'id_role'=>$this->role->id]);
            $this->inventory=factory(InventoryAccount::class)->create();
            $total=$this->inventory['total'];
            for ($i=0;$i<$total;$i++){
                $this->inventoryMissions=InventoryAccountMission::create([
                    'inventory_account_id'=>$this->inventory['id'],
                    'location'=>Str::random(5),
                    'commodity_id'=>256226,
                    'produced_at'=>null,
                    'valid_at'=>null,
                    'stored_at'=>null,
                    'batch_number'=>'',
                    'erp_type_position'=>'',
                    'quality'=>'',
                    'stored_amount'=>50,
                    'valid_amount'=>'',
                    'verified_amount'=>'',
                    're_checked_amount'=>'',
                    'difference_amount'=>'',
                    'occupied_amount'=>'',
                    'checked'=>'否',
                    'returned'=>'无',
                ]);
            }
        }
        $this->response=$this->actingAs($this->user)->json('get','http://bswas/inventory/stockInventory/enterStockInventory/'.$this->inventory['id']);
    }

    function testEnterStockInventoryNotHavingException(){
        $this->response->assertDontSee('Exception');
    }

    function testStockInventoryViewIsRight(){
        $this->response->assertViewIs('inventory.stockInventory.inventoryMission');
    }
    function testStockInventoryViewHasInventoryAndInventoryMissions(){
        //$inventory=Inventory::with('owner')->find($this->inventory['id']);
        //$inventoryMissions=InventoryMission::with(['commodity'])->where('inventory_id',$this->inventory['id'])->orderBy('difference_amount','desc')->get();
        $this->response->assertViewHas('inventoryAccountMissions');
        $this->response->assertViewHas('inventoryAccount');
    }
    function tearDown(): void
    {
        DB::table('user_role')->where('id_role',$this->role['id'])->delete();
        DB::table('authority_role')->where('id_role',$this->role['id'])->delete();
        User::where('id',$this->user['id'])->delete();
        Role::where('id',$this->role['id'])->delete();
        InventoryAccountMission::where('inventory_account_id',$this->inventory['id'])->delete();
        InventoryAccount::where('id',$this->inventory['id'])->forceDelete();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}

