<?php


namespace Tests\Services\CacheShelfService;


use App\MaterialBox;
use App\Services\CacheService;
use App\Services\CacheShelfService;
use App\Services\StationTaskMaterialBoxService;
use App\Station;
use App\StationTask;
use App\StationTaskMaterialBox;
use App\StationType;
use Tests\TestCase;

class PutStationTaskMaterialBoxProcessTest extends TestCase
{
    /** @var CacheShelfService $cacheShelfService */
    private $cacheShelfService;
    /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
    private $stationTaskMaterialBoxService;
    private $data = [];

    protected function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->cacheShelfService = app(CacheShelfService::class);
        $this->stationTaskMaterialBoxService = app(StationTaskMaterialBoxService::class);
        $stationType = StationType::query()->firstOrCreate(['name' => '缓存架']);
        $this->data['materialBox'] = factory(MaterialBox::class)->create();

        $this->data['parentStation'] = factory(Station::class)->create([ 'station_type_id' => $stationType['id']]);
        $this->data['station'] = factory(Station::class)->create([ 'station_type_id' => $stationType['id'], 'parent_id' => $this->data['parentStation']['id']]);
        $this->data['stationTask'] = factory(StationTask::class)->create(['station_id' => $this->data['station']['id'], 'status' => '处理中']);
        $this->data['takeStationTaskMaterialBox'] = $this->stationTaskMaterialBoxService->createByStationAndMaterialBox($this->data['station'], $this->data['materialBox']);
        $this->data['putStationTaskMaterialBox'] = $this->stationTaskMaterialBoxService->createByStationAndMaterialBox($this->data['station'], $this->data['materialBox']);
    }

    public function testPutStationTaskMaterialBoxProcess()
    {
        $this->data['takeStationTaskMaterialBox']->update(['status' => '处理中']);
        $this->cacheShelfService->putStationTaskMaterialBoxProcess($this->data['putStationTaskMaterialBox']);
        $task = StationTaskMaterialBox::query()->where('id', $this->data['takeStationTaskMaterialBox']['id'])->first();
        $this->assertTrue($task ? true : false);
        $this->assertEquals( '完成',$task['status']);
    }

    public function testPutStationTaskMaterialBoxProcessT()
    {
        $this->cacheShelfService->putStationTaskMaterialBoxProcess($this->data['putStationTaskMaterialBox']);
        $task = StationTaskMaterialBox::query()->where('id', $this->data['takeStationTaskMaterialBox']['id'])->first();
        $this->assertTrue($task ? true : false);
        $this->assertNotEquals('完成',$task['status']);
    }

    protected function tearDown(): void
    {
        if ($this->data['materialBox']) Station::query()->where('id', $this->data['materialBox']['id'])->delete();
        if ($this->data['parentStation']) Station::query()->where('id', $this->data['parentStation']['id'])->delete();
        if ($this->data['station']) Station::query()->where('id', $this->data['station']['id'])->delete();
        if ($this->data['stationTask']) StationTask::query()->where('id', $this->data['stationTask']['id'])->delete();
        if ($this->data['takeStationTaskMaterialBox']) StationTaskMaterialBox::query()->where('id', $this->data['takeStationTaskMaterialBox']['id'])->delete();
        if ($this->data['putStationTaskMaterialBox']) StationTaskMaterialBox::query()->where('id', $this->data['putStationTaskMaterialBox']['id'])->delete();
        parent::tearDown();
    }
}

