<?php


namespace Tests\Services\WarehouseService;


use App\OracleBasCustomer;
use App\Services\OracleBasCustomerService;
use App\Services\WarehouseService;
use App\Warehouse;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class GetWareHouseByCodesTest extends TestCase
{
    /** @var WarehouseService $service */
    private $service;
    private $data = [];

    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->service = app('WarehouseService');
        $warehouse1 = factory(Warehouse::class)->create();
        $warehouse2 = factory(Warehouse::class)->create();
        $basCustomer1 = factory(OracleBasCustomer::class)->make(['customerid'=>$warehouse1->code,'descr_c'=>$warehouse1->name,'customer_type'=>'OW']);
        $basCustomer2 = factory(OracleBasCustomer::class)->make(['customerid'=>$warehouse2->code,'descr_c'=>$warehouse2->name,'customer_type'=>'OW']);
        $basCustomers = collect([$basCustomer1,$basCustomer2]);
        $this->data['warehouses'] = collect([$warehouse1,$warehouse2]);
        $this->data['basCustomers'] = $basCustomers;
        $this->data['codes']  = [$warehouse1->code,$warehouse2->code];
        $this->mock(OracleBasCustomerService::class,function($mock)use($basCustomers){
            foreach ( $basCustomers as $basCustomer) {
                $mock->shouldReceive('first')->with(['CustomerId'=>$basCustomer['customerid'],'Customer_Type'=>'WH'])->andReturn($basCustomer);
            }
        });
    }

    /**
     * @test
     */
    public function getWareHouseCodes()
    {
        $wareHouses = $this->service->getWareHouseByCodes($this->data['codes']);
        foreach ($this->data['basCustomers'] as $datum) {
            $wareHouse = $wareHouses->where('code',$datum['customerid'])->first();
            $this->assertNotNull($wareHouse);
            $this->assertEquals($wareHouse->name,$datum['descr_c']);
            $this->assertEquals($wareHouse->code,$datum['customerid']);
        }
    }

    public function tearDown(): void
    {
        foreach ($this->data['warehouses'] as $datum) {
            $datum->delete();
            cache()->forget("WareHouse_{$datum['code']}");
        }
        parent::tearDown(); // TODO: Change the autogenerated stub
    }


}
